Skip to content

Commit 8c76591

Browse files
CopilotSteake
andcommitted
Add comprehensive VRF tests for block production
- Added test_vrf_block_production_and_validation to verify VRF functionality - Added test_vrf_deterministic to verify VRF produces consistent outputs - All 5 blockchain tests passing - Validates that VRF outputs are non-zero and proofs are present - Confirms VRF chaining works correctly across blocks Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
1 parent 3a1c82d commit 8c76591

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

crates/bitcell-node/src/blockchain.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,60 @@ mod tests {
518518
// Test reward becomes 0 after 64 halvings
519519
assert_eq!(Blockchain::calculate_block_reward(HALVING_INTERVAL * 64), 0);
520520
}
521+
522+
#[test]
523+
fn test_vrf_block_production_and_validation() {
524+
let sk = Arc::new(SecretKey::generate());
525+
let metrics = MetricsRegistry::new();
526+
let blockchain = Blockchain::new(sk.clone(), metrics);
527+
528+
// Produce first block
529+
let block1 = blockchain.produce_block(
530+
vec![],
531+
vec![],
532+
sk.public_key(),
533+
).unwrap();
534+
535+
// VRF output should not be all zeros (genesis uses zeros)
536+
assert_ne!(block1.header.vrf_output, [0u8; 32]);
537+
538+
// VRF proof should not be empty
539+
assert!(!block1.header.vrf_proof.is_empty());
540+
541+
// Validate the block (includes VRF verification)
542+
blockchain.validate_block(&block1).expect("Block should be valid");
543+
544+
// Add block to chain
545+
blockchain.add_block(block1).expect("Should add block");
546+
547+
// Produce second block
548+
let block2 = blockchain.produce_block(
549+
vec![],
550+
vec![],
551+
sk.public_key(),
552+
).unwrap();
553+
554+
// VRF outputs should be different (demonstrates randomness)
555+
assert_ne!(block2.header.vrf_output, blockchain.get_block(1).unwrap().header.vrf_output);
556+
557+
// Validate second block
558+
blockchain.validate_block(&block2).expect("Second block should be valid");
559+
}
560+
561+
#[test]
562+
fn test_vrf_deterministic() {
563+
// VRF should be deterministic - same input should produce same output
564+
let sk = Arc::new(SecretKey::generate());
565+
let metrics1 = MetricsRegistry::new();
566+
let metrics2 = MetricsRegistry::new();
567+
568+
let blockchain1 = Blockchain::new(sk.clone(), metrics1);
569+
let blockchain2 = Blockchain::new(sk.clone(), metrics2);
570+
571+
let block1 = blockchain1.produce_block(vec![], vec![], sk.public_key()).unwrap();
572+
let block2 = blockchain2.produce_block(vec![], vec![], sk.public_key()).unwrap();
573+
574+
// Same key, same previous state should produce same VRF output
575+
assert_eq!(block1.header.vrf_output, block2.header.vrf_output);
576+
}
521577
}

0 commit comments

Comments
 (0)