Skip to content

Commit bae60b8

Browse files
CopilotSteake
andcommitted
Improve VRF test comments and add comprehensive chaining test
- Fixed comment on line 554 to clarify VRF chaining vs randomness - Kept test_vrf_deterministic for basic determinism testing - Added new test_vrf_chaining to comprehensively test VRF chaining: - Produces 3 blocks in sequence - Verifies each block has unique VRF output due to chaining - Recreates chain to verify deterministic behavior with chaining - Tests both uniqueness (chaining works) and determinism (same chain = same VRFs) Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
1 parent 988343c commit bae60b8

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

crates/bitcell-node/src/blockchain.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ mod tests {
551551
sk.public_key(),
552552
).unwrap();
553553

554-
// VRF outputs should be different (demonstrates randomness)
554+
// VRF outputs should be different because block2 uses block1's VRF output as input (VRF chaining)
555555
assert_ne!(block2.header.vrf_output, blockchain.get_block(1).unwrap().header.vrf_output);
556556

557557
// Validate second block
@@ -574,4 +574,50 @@ mod tests {
574574
// Same key, same previous state should produce same VRF output
575575
assert_eq!(block1.header.vrf_output, block2.header.vrf_output);
576576
}
577+
578+
#[test]
579+
fn test_vrf_chaining() {
580+
// Test that VRF properly chains - each block's VRF uses previous block's VRF output as input
581+
let sk = Arc::new(SecretKey::generate());
582+
let metrics = MetricsRegistry::new();
583+
let blockchain = Blockchain::new(sk.clone(), metrics);
584+
585+
// Produce first block
586+
let block1 = blockchain.produce_block(vec![], vec![], sk.public_key()).unwrap();
587+
assert_ne!(block1.header.vrf_output, [0u8; 32], "Block 1 VRF should be non-zero");
588+
blockchain.add_block(block1.clone()).unwrap();
589+
590+
// Produce second block
591+
let block2 = blockchain.produce_block(vec![], vec![], sk.public_key()).unwrap();
592+
assert_ne!(block2.header.vrf_output, [0u8; 32], "Block 2 VRF should be non-zero");
593+
assert_ne!(block2.header.vrf_output, block1.header.vrf_output,
594+
"Block 2 VRF should differ from Block 1 due to chaining");
595+
blockchain.add_block(block2.clone()).unwrap();
596+
597+
// Produce third block
598+
let block3 = blockchain.produce_block(vec![], vec![], sk.public_key()).unwrap();
599+
assert_ne!(block3.header.vrf_output, [0u8; 32], "Block 3 VRF should be non-zero");
600+
assert_ne!(block3.header.vrf_output, block1.header.vrf_output,
601+
"Block 3 VRF should differ from Block 1");
602+
assert_ne!(block3.header.vrf_output, block2.header.vrf_output,
603+
"Block 3 VRF should differ from Block 2 due to chaining");
604+
605+
// Verify that recreating the chain produces the same VRF sequence (determinism with chaining)
606+
let metrics2 = MetricsRegistry::new();
607+
let blockchain2 = Blockchain::new(sk.clone(), metrics2);
608+
609+
let block1_v2 = blockchain2.produce_block(vec![], vec![], sk.public_key()).unwrap();
610+
assert_eq!(block1_v2.header.vrf_output, block1.header.vrf_output,
611+
"First block VRF should be deterministic");
612+
blockchain2.add_block(block1_v2).unwrap();
613+
614+
let block2_v2 = blockchain2.produce_block(vec![], vec![], sk.public_key()).unwrap();
615+
assert_eq!(block2_v2.header.vrf_output, block2.header.vrf_output,
616+
"Second block VRF should be deterministic given same chain state");
617+
blockchain2.add_block(block2_v2).unwrap();
618+
619+
let block3_v2 = blockchain2.produce_block(vec![], vec![], sk.public_key()).unwrap();
620+
assert_eq!(block3_v2.header.vrf_output, block3.header.vrf_output,
621+
"Third block VRF should be deterministic given same chain state");
622+
}
577623
}

0 commit comments

Comments
 (0)