chia_protocol/
coin_record.rs

1use chia_streamable_macro::streamable;
2#[cfg(feature = "py-bindings")]
3use pyo3::pymethods;
4
5use crate::Coin;
6#[cfg(feature = "py-bindings")]
7use crate::{Bytes32, CoinState};
8
9#[streamable]
10#[derive(Copy)]
11pub struct CoinRecord {
12    // These are values that correspond to a CoinName that are used
13    // in keeping track of the unspent database.
14    coin: Coin,
15    confirmed_block_index: u32,
16    spent_block_index: u32,
17    coinbase: bool,
18    timestamp: u64, // Timestamp of the block at height confirmed_block_index
19}
20
21#[cfg(feature = "py-bindings")]
22#[pymethods]
23impl CoinRecord {
24    #[inline]
25    #[getter]
26    pub fn spent(&self) -> bool {
27        self.spent_block_index > 0
28    }
29
30    #[inline]
31    #[getter]
32    pub fn name(&self) -> Bytes32 {
33        self.coin.coin_id()
34    }
35
36    #[getter]
37    pub fn coin_state(&self) -> CoinState {
38        let spent_h = if self.spent_block_index > 0 {
39            Some(self.spent_block_index)
40        } else {
41            None
42        };
43
44        // CoinRecord is sometimes used to represent coins that are *removed* as part of reorgs.
45        // Removed coins don't have a timestamp or confirmed_block_index.
46        let confirmed_height = if self.confirmed_block_index == 0_u32 && self.timestamp == 0_u64 {
47            None
48        } else {
49            Some(self.confirmed_block_index)
50        };
51
52        CoinState::new(self.coin, spent_h, confirmed_height)
53    }
54}