use chia_protocol::Bytes32;
use dig_clvm::ValidationConfig;
use dig_block::{BlockError, L2Block, L2BlockHeader, Signature};
fn empty_block() -> L2Block {
let network_id = Bytes32::new([0xaa; 32]);
let l1_hash = Bytes32::new([0xbb; 32]);
let header = L2BlockHeader::genesis(network_id, 1, l1_hash);
L2Block::new(header, Vec::new(), Vec::new(), Signature::default())
}
fn empty_block_with_fees(fees: u64) -> L2Block {
let mut block = empty_block();
block.header.total_fees = fees;
block
}
fn empty_block_with_cost(cost: u64) -> L2Block {
let mut block = empty_block();
block.header.total_cost = cost;
block
}
#[test]
fn empty_block_returns_empty_execution_result() {
let block = empty_block();
let config = ValidationConfig::default();
let genesis_challenge = Bytes32::new([0x42; 32]);
let result = block
.validate_execution(&config, &genesis_challenge)
.expect("empty block validates");
assert!(result.additions.is_empty());
assert!(result.removals.is_empty());
assert!(result.pending_assertions.is_empty());
assert_eq!(result.total_cost, 0);
assert_eq!(result.total_fees, 0);
assert!(result.receipts.is_empty());
}
#[test]
fn signature_matches_normative() {
let _fn_ptr: fn(
&L2Block,
&ValidationConfig,
&Bytes32,
) -> Result<dig_block::ExecutionResult, BlockError> = L2Block::validate_execution;
}
#[test]
fn fee_mismatch_rejected() {
let block = empty_block_with_fees(100);
let config = ValidationConfig::default();
let err = block
.validate_execution(&config, &Bytes32::default())
.expect_err("non-zero header fees on empty body must reject");
match err {
BlockError::FeesMismatch { header, computed } => {
assert_eq!(header, 100);
assert_eq!(computed, 0);
}
other => panic!("expected FeesMismatch, got {:?}", other),
}
}
#[test]
fn cost_mismatch_rejected() {
let block = empty_block_with_cost(999);
let config = ValidationConfig::default();
let err = block
.validate_execution(&config, &Bytes32::default())
.expect_err("non-zero header cost on empty body must reject");
match err {
BlockError::CostMismatch { header, computed } => {
assert_eq!(header, 999);
assert_eq!(computed, 0);
}
other => panic!("expected CostMismatch, got {:?}", other),
}
}
#[test]
fn validation_is_deterministic() {
let block = empty_block();
let config = ValidationConfig::default();
let genesis = Bytes32::new([0x01; 32]);
let r1 = block.validate_execution(&config, &genesis).unwrap();
let r2 = block.validate_execution(&config, &genesis).unwrap();
assert_eq!(r1, r2);
}
#[test]
fn accepts_dig_clvm_validation_config() {
let block = empty_block();
let config = ValidationConfig::default();
assert_eq!(config.max_cost_per_block, dig_clvm::L2_MAX_COST_PER_BLOCK);
let _ = block
.validate_execution(&config, &Bytes32::default())
.unwrap();
}