use chia_protocol::{Coin, CoinSpend, Program, SpendBundle};
use dig_block::{Bytes32, Cost, L2Block, L2BlockHeader, Signature};
fn minimal_spend_bundle() -> SpendBundle {
let coin = Coin::new(Bytes32::default(), Bytes32::default(), 1);
let coin_spend = CoinSpend::new(coin, Program::from(vec![0x01]), Program::from(vec![0x80]));
SpendBundle::new(vec![coin_spend], Signature::default())
}
fn sample_header() -> L2BlockHeader {
let tag = |b: u8| Bytes32::new([b; 32]);
L2BlockHeader::new(
7,
3,
tag(0x01),
tag(0x02),
tag(0x03),
tag(0x04),
tag(0x05),
tag(0x06),
100,
tag(0x07),
2,
1,
100 as Cost,
0,
0,
0,
0,
tag(0x08),
)
}
#[test]
fn test_l2block_new() {
let header = sample_header();
let sb = minimal_spend_bundle();
let slash = vec![vec![0xde, 0xad]];
let sig = Signature::default();
let block = L2Block::new(header.clone(), vec![sb.clone()], slash.clone(), sig.clone());
assert_eq!(block.header, header);
assert_eq!(block.spend_bundles.len(), 1);
assert_eq!(block.slash_proposal_payloads, slash);
assert_eq!(block.proposer_signature, sig);
}
#[test]
fn test_l2block_hash_delegates() {
let header = sample_header();
let h = header.hash();
let block = L2Block::new(
header,
vec![minimal_spend_bundle()],
vec![vec![1, 2, 3]],
Signature::default(),
);
assert_eq!(block.hash(), h);
assert_eq!(block.hash(), block.header.hash());
}
#[test]
fn test_l2block_height_delegates() {
let header = sample_header();
let expected = header.height;
let block = L2Block::new(header, vec![], vec![], Signature::default());
assert_eq!(block.height(), expected);
}
#[test]
fn test_l2block_epoch_delegates() {
let header = sample_header();
let expected = header.epoch;
let block = L2Block::new(header, vec![], vec![], Signature::default());
assert_eq!(block.epoch(), expected);
}
#[test]
fn test_l2block_empty_spend_bundles() {
let header = sample_header();
let block = L2Block::new(header, vec![], vec![], Signature::default());
assert!(block.spend_bundles.is_empty());
}
#[test]
fn test_l2block_empty_slash_proposals() {
let header = sample_header();
let block = L2Block::new(
header,
vec![minimal_spend_bundle()],
vec![],
Signature::default(),
);
assert!(block.slash_proposal_payloads.is_empty());
}
#[test]
fn test_l2block_serde_roundtrip() {
let block = L2Block::new(
sample_header(),
vec![minimal_spend_bundle()],
vec![vec![0xab]],
Signature::default(),
);
let bytes = bincode::serialize(&block).expect("serialize");
let back: L2Block = bincode::deserialize(&bytes).expect("deserialize");
assert_eq!(back.header, block.header);
assert_eq!(back.spend_bundles.len(), block.spend_bundles.len());
assert_eq!(back.slash_proposal_payloads, block.slash_proposal_payloads);
}