Skip to content

Commit 8c02536

Browse files
committed
Add BlockTreeEntry.Equals() method
Expose `btck_block_tree_entry_equals` from the C API as `BlockTreeEntry.Equals()` to enable comparison of block tree entries. Add unit test.
1 parent 579271e commit 8c02536

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

kernel/block_tree_entry.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ func (bi *BlockTreeEntry) Previous() *BlockTreeEntry {
4040
prevIndex := &BlockTreeEntry{ptr: ptr}
4141
return prevIndex
4242
}
43+
44+
// Equals compares two block tree entries for equality.
45+
// Returns true if both entries point to the same block in the tree.
46+
func (bi *BlockTreeEntry) Equals(other *BlockTreeEntry) bool {
47+
if other == nil {
48+
return false
49+
}
50+
return C.btck_block_tree_entry_equals(bi.ptr, other.ptr) != 0
51+
}

kernel/block_tree_entry_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,35 @@ func TestBlockTreeEntryGetPrevious(t *testing.T) {
3636
t.Error("Genesis block should not have a previous block")
3737
}
3838
}
39+
40+
func TestBlockTreeEntryEquals(t *testing.T) {
41+
suite := ChainstateManagerTestSuite{
42+
MaxBlockHeightToImport: 2,
43+
}
44+
suite.Setup(t)
45+
46+
chain := suite.Manager.GetActiveChain()
47+
48+
// Same entry should equal itself
49+
entry1 := chain.GetByHeight(1)
50+
if !entry1.Equals(entry1) {
51+
t.Error("Entry should equal itself")
52+
}
53+
54+
// Different retrievals of same height should be equal
55+
entry1Again := chain.GetByHeight(1)
56+
if !entry1.Equals(entry1Again) {
57+
t.Error("Same height entries should be equal")
58+
}
59+
60+
// Different heights should not be equal
61+
entry0 := chain.GetByHeight(0)
62+
if entry1.Equals(entry0) {
63+
t.Error("Different height entries should not be equal")
64+
}
65+
66+
// Nil comparison should return false
67+
if entry1.Equals(nil) {
68+
t.Error("Entry should not equal nil")
69+
}
70+
}

0 commit comments

Comments
 (0)