// NVM-L0 区块定义 use crate::types::{Address, Hash}; use crate::transaction::Transaction; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct BlockHeader { pub number: u64, pub timestamp: u64, pub parent_hash: Hash, pub state_root: Hash, pub tx_root: Hash, pub miner: Address, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Block { pub header: BlockHeader, pub transactions: Vec, pub hash: Hash, } impl Block { pub fn new(header: BlockHeader, transactions: Vec) -> Self { let mut block = Self { header, transactions, hash: Hash::zero() }; block.hash = block.calculate_hash(); block } fn calculate_hash(&self) -> Hash { let mut data = Vec::new(); data.extend_from_slice(&self.header.number.to_le_bytes()); data.extend_from_slice(&self.header.timestamp.to_le_bytes()); data.extend_from_slice(self.header.parent_hash.as_bytes()); Hash::sha3_384(&data) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_block() { let header = BlockHeader { number: 1, timestamp: 1000, parent_hash: Hash::zero(), state_root: Hash::zero(), tx_root: Hash::zero(), miner: Address::zero(), }; let block = Block::new(header, Vec::new()); assert_eq!(block.header.number, 1); } }