Skip to content

Commit ac4cfb0

Browse files
committed
f add claude's bestblock test
1 parent 8eea476 commit ac4cfb0

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

lightning/src/chain/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,45 @@ impl ClaimId {
566566
ClaimId(Sha256::from_engine(engine).to_byte_array())
567567
}
568568
}
569+
570+
#[cfg(test)]
571+
mod tests {
572+
use super::*;
573+
use bitcoin::hashes::Hash;
574+
575+
#[test]
576+
fn test_best_block() {
577+
let hash1 = BlockHash::from_slice(&[1; 32]).unwrap();
578+
let mut chain_a = BestBlock::new(hash1, 100);
579+
let mut chain_b = BestBlock::new(hash1, 100);
580+
581+
// Test get_hash_at_height on initial block
582+
assert_eq!(chain_a.get_hash_at_height(100), Some(hash1));
583+
assert_eq!(chain_a.get_hash_at_height(101), None);
584+
assert_eq!(chain_a.get_hash_at_height(99), None);
585+
586+
// Test find_common_ancestor with identical blocks
587+
assert_eq!(chain_a.find_common_ancestor(&chain_b), Some((hash1, 100)));
588+
589+
let hash2 = BlockHash::from_slice(&[2; 32]).unwrap();
590+
chain_a.advance(hash2);
591+
assert_eq!(chain_a.height, 101);
592+
assert_eq!(chain_a.block_hash, hash2);
593+
assert_eq!(chain_a.previous_blocks[0], Some(hash1));
594+
assert_eq!(chain_a.get_hash_at_height(101), Some(hash2));
595+
assert_eq!(chain_a.get_hash_at_height(100), Some(hash1));
596+
597+
// Test find_common_ancestor with different heights
598+
assert_eq!(chain_a.find_common_ancestor(&chain_b), Some((hash1, 100)));
599+
600+
// Test find_common_ancestor with diverged chains but the same height
601+
let hash_b3 = BlockHash::from_slice(&[33; 32]).unwrap();
602+
chain_b.advance(hash_b3);
603+
assert_eq!(chain_a.find_common_ancestor(&chain_b), Some((hash1, 100)));
604+
605+
// Test find_common_ancestor with no common history
606+
let hash_other = BlockHash::from_slice(&[99; 32]).unwrap();
607+
let chain_c = BestBlock::new(hash_other, 200);
608+
assert_eq!(chain_a.find_common_ancestor(&chain_c), None);
609+
}
610+
}

0 commit comments

Comments
 (0)