Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions kernel/block_tree_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func (bi *BlockTreeEntry) Equals(other *BlockTreeEntry) bool {
return C.btck_block_tree_entry_equals(bi.ptr, other.ptr) != 0
}

// Ancestor returns the ancestor block tree entry at the given height.
//
// Returns nil if height is negative or greater than this entry's height.
func (bi *BlockTreeEntry) Ancestor(height int32) *BlockTreeEntry {
if height < 0 || height > bi.Height() {
return nil
}
ptr := C.btck_block_tree_entry_get_ancestor(bi.ptr, C.int32_t(height))
return &BlockTreeEntry{ptr: check(ptr)}
}

// GetHeader returns the block header associated with this block tree entry.
func (bi *BlockTreeEntry) GetHeader() *BlockHeader {
return newBlockHeader(C.btck_block_tree_entry_get_block_header(bi.ptr), true)
Expand Down
23 changes: 23 additions & 0 deletions kernel/block_tree_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ func TestBlockTreeEntry(t *testing.T) {
}
})

t.Run("Ancestor", func(t *testing.T) {
entry2 := chain.GetByHeight(2)
if entry2 == nil {
t.Fatal("Entry at height 2 is nil")
}

for _, height := range []int32{0, 1, 2} {
ancestor := entry2.Ancestor(height)
if ancestor == nil {
t.Fatalf("Ancestor(%d) returned nil", height)
}
if got := ancestor.Height(); got != height {
t.Fatalf("Ancestor(%d) height = %d, want %d", height, got, height)
}
}

for _, height := range []int32{-1, 3} {
if ancestor := entry2.Ancestor(height); ancestor != nil {
t.Fatalf("Ancestor(%d) returned height %d, want nil", height, ancestor.Height())
}
}
})

t.Run("GetHeader", func(t *testing.T) {
// Get genesis block entry
genesisEntry := chain.GetByHeight(0)
Expand Down
Loading