use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockHeader {
pub hash: String,
pub timestamp: u64,
pub height: u32,
#[serde(rename = "chainFrom")]
pub chain_from: u8,
#[serde(rename = "chainTo")]
pub chain_to: u8,
#[serde(rename = "parentHash")]
pub parent_hash: String,
pub nonce: String,
pub version: u8,
#[serde(rename = "depStateHash")]
pub dep_state_hash: String,
#[serde(rename = "txsHash")]
pub txs_hash: String,
pub target: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
pub hash: String,
pub timestamp: u64,
pub height: u32,
#[serde(rename = "chainFrom")]
pub chain_from: u8,
#[serde(rename = "chainTo")]
pub chain_to: u8,
pub transactions: Vec<BlockTransaction>,
#[serde(rename = "parentHash")]
pub parent_hash: String,
pub nonce: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockTransaction {
pub hash: String,
pub inputs: Vec<TransactionInput>,
pub outputs: Vec<TransactionOutput>,
#[serde(rename = "gasAmount")]
pub gas_amount: u32,
#[serde(rename = "gasPrice")]
pub gas_price: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionInput {
#[serde(rename = "outputRef")]
pub output_ref: OutputRef,
#[serde(rename = "unlockScript")]
pub unlock_script: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputRef {
pub key: String,
pub hint: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionOutput {
#[serde(rename = "type")]
pub output_type: String,
pub address: String,
#[serde(rename = "attoAlphAmount")]
pub atto_alph_amount: String,
pub tokens: Option<Vec<TokenOutput>>,
#[serde(rename = "lockTime")]
pub lock_time: Option<u64>,
pub message: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenOutput {
pub id: String,
pub amount: String,
}
impl Block {
pub fn transaction_count(&self) -> usize {
self.transactions.len()
}
pub fn is_from_chain(&self, chain: u8) -> bool {
self.chain_from == chain
}
pub fn is_to_chain(&self, chain: u8) -> bool {
self.chain_to == chain
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_block_transaction_count() {
let block = Block {
hash: "test_hash".to_string(),
timestamp: 1234567890,
height: 100,
chain_from: 0,
chain_to: 1,
transactions: vec![],
parent_hash: "parent_hash".to_string(),
nonce: "nonce".to_string(),
};
assert_eq!(block.transaction_count(), 0);
}
#[test]
fn test_block_chain_check() {
let block = Block {
hash: "test_hash".to_string(),
timestamp: 1234567890,
height: 100,
chain_from: 0,
chain_to: 1,
transactions: vec![],
parent_hash: "parent_hash".to_string(),
nonce: "nonce".to_string(),
};
assert!(block.is_from_chain(0));
assert!(!block.is_from_chain(1));
assert!(block.is_to_chain(1));
assert!(!block.is_to_chain(0));
}
#[test]
fn test_block_serialization() {
let block = Block {
hash: "abc123".to_string(),
timestamp: 1234567890,
height: 100,
chain_from: 0,
chain_to: 1,
transactions: vec![],
parent_hash: "parent".to_string(),
nonce: "nonce_value".to_string(),
};
let json = serde_json::to_string(&block).unwrap();
assert!(json.contains("abc123"));
assert!(json.contains("1234567890"));
}
}