use chia_protocol::{Coin, CoinState};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CoinRecord {
pub coin: Coin,
pub confirmed_height: Option<u32>,
pub spent_height: Option<u32>,
pub timestamp: Option<u64>,
pub coinbase: bool,
}
impl CoinRecord {
pub fn is_spent(&self) -> bool {
self.spent_height.is_some()
}
pub fn from_coin_state(state: CoinState) -> Self {
Self {
coin: state.coin,
confirmed_height: state.created_height,
spent_height: state.spent_height,
timestamp: None,
coinbase: false,
}
}
}
impl From<CoinState> for CoinRecord {
fn from(state: CoinState) -> Self {
Self::from_coin_state(state)
}
}
#[cfg(test)]
mod tests {
use super::*;
use chia_protocol::Bytes32;
fn sample_coin() -> Coin {
Coin::new(Bytes32::new([1u8; 32]), Bytes32::new([2u8; 32]), 7)
}
#[test]
fn from_coin_state_maps_created_to_confirmed_and_defaults() {
let state = CoinState {
coin: sample_coin(),
spent_height: Some(200),
created_height: Some(100),
};
let record = CoinRecord::from(state);
assert_eq!(record.coin, sample_coin());
assert_eq!(record.confirmed_height, Some(100));
assert_eq!(record.spent_height, Some(200));
assert_eq!(record.timestamp, None);
assert!(!record.coinbase);
assert!(record.is_spent());
}
#[test]
fn unspent_record_reports_not_spent() {
let state = CoinState {
coin: sample_coin(),
spent_height: None,
created_height: Some(100),
};
let record = CoinRecord::from_coin_state(state);
assert!(!record.is_spent());
}
}