use crypto::Hash;
use messages::Precommit;
use helpers::{Height, ValidatorId};
pub const SCHEMA_MAJOR_VERSION: u16 = 0;
encoding_struct!(
struct Block {
schema_version: u16,
proposer_id: ValidatorId,
height: Height,
tx_count: u32,
prev_hash: &Hash,
tx_hash: &Hash,
state_hash: &Hash,
}
);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockProof {
pub block: Block,
pub precommits: Vec<Precommit>,
}
#[cfg(test)]
mod tests {
use crypto::hash;
use super::*;
#[test]
fn test_block() {
let proposer_id = ValidatorId(1024);
let txs = [4, 5, 6];
let height = Height(123_345);
let prev_hash = hash(&[1, 2, 3]);
let tx_hash = hash(&txs);
let tx_count = txs.len() as u32;
let state_hash = hash(&[7, 8, 9]);
let block = Block::new(
SCHEMA_MAJOR_VERSION,
proposer_id,
height,
tx_count,
&prev_hash,
&tx_hash,
&state_hash,
);
assert_eq!(block.schema_version(), SCHEMA_MAJOR_VERSION);
assert_eq!(block.proposer_id(), proposer_id);
assert_eq!(block.height(), height);
assert_eq!(block.tx_count(), tx_count);
assert_eq!(block.prev_hash(), &prev_hash);
assert_eq!(block.tx_hash(), &tx_hash);
assert_eq!(block.state_hash(), &state_hash);
let json_str = ::serde_json::to_string(&block).unwrap();
let block1: Block = ::serde_json::from_str(&json_str).unwrap();
assert_eq!(block1, block);
}
}