Skip to content

Commit 8502bc8

Browse files
committed
Add BlockHashView and make BlockTreeEntry hash method return a view
1 parent 6f7cb49 commit 8502bc8

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

kernel/block_hash.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,38 @@ func (blockHashCFuncs) copy(ptr unsafe.Pointer) unsafe.Pointer {
2121
// BlockHash is a type-safe identifier for a block.
2222
type BlockHash struct {
2323
*handle
24+
blockHashApi
2425
}
2526

2627
func newBlockHash(ptr *C.btck_BlockHash, fromOwned bool) *BlockHash {
2728
h := newHandle(unsafe.Pointer(ptr), blockHashCFuncs{}, fromOwned)
28-
return &BlockHash{handle: h}
29+
return &BlockHash{handle: h, blockHashApi: blockHashApi{(*C.btck_BlockHash)(h.ptr)}}
30+
}
31+
32+
// BlockHashView is a type-safe identifier for a block.
33+
type BlockHashView struct {
34+
blockHashApi
35+
ptr *C.btck_BlockHash
36+
}
37+
38+
func newBlockHashView(ptr *C.btck_BlockHash) *BlockHashView {
39+
return &BlockHashView{
40+
blockHashApi: blockHashApi{ptr},
41+
ptr: ptr,
42+
}
43+
}
44+
45+
type blockHashApi struct {
46+
ptr *C.btck_BlockHash
47+
}
48+
49+
func (bh *blockHashApi) blockHashPtr() *C.btck_BlockHash {
50+
return bh.ptr
51+
}
52+
53+
// BlockHashLike is an interface for types that can provide a block hash pointer.
54+
type BlockHashLike interface {
55+
blockHashPtr() *C.btck_BlockHash
2956
}
3057

3158
// NewBlockHash creates a new BlockHash from a 32-byte hash value.
@@ -38,23 +65,23 @@ func NewBlockHash(hashBytes [32]byte) *BlockHash {
3865
}
3966

4067
// Bytes returns the 32-byte representation of the block hash.
41-
func (bh *BlockHash) Bytes() [32]byte {
68+
func (bh *blockHashApi) Bytes() [32]byte {
4269
var output [32]C.uchar
43-
C.btck_block_hash_to_bytes((*C.btck_BlockHash)(bh.ptr), &output[0])
70+
C.btck_block_hash_to_bytes(bh.ptr, &output[0])
4471
return *(*[32]byte)(unsafe.Pointer(&output[0]))
4572
}
4673

4774
// Copy creates a copy of the block hash.
48-
func (bh *BlockHash) Copy() *BlockHash {
49-
return newBlockHash((*C.btck_BlockHash)(bh.ptr), false)
75+
func (bh *blockHashApi) Copy() *BlockHash {
76+
return newBlockHash(bh.ptr, false)
5077
}
5178

5279
// Equals checks if two block hashes are equal.
5380
//
5481
// Parameters:
55-
// - other: Block hash to compare against
82+
// - other: Block hash to compare against (can be *BlockHash or *BlockHashView)
5683
//
5784
// Returns true if the block hashes are equal.
58-
func (bh *BlockHash) Equals(other *BlockHash) bool {
59-
return C.btck_block_hash_equals((*C.btck_BlockHash)(bh.ptr), (*C.btck_BlockHash)(other.ptr)) != 0
85+
func (bh *blockHashApi) Equals(other BlockHashLike) bool {
86+
return C.btck_block_hash_equals(bh.ptr, other.blockHashPtr()) != 0
6087
}

kernel/block_tree_entry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func (bi *BlockTreeEntry) Height() int32 {
2323
}
2424

2525
// Hash returns the block hash associated with this block tree entry.
26-
func (bi *BlockTreeEntry) Hash() *BlockHash {
26+
func (bi *BlockTreeEntry) Hash() *BlockHashView {
2727
ptr := C.btck_block_tree_entry_get_block_hash(bi.ptr)
28-
return newBlockHash(check(ptr), true)
28+
return newBlockHashView(check(ptr))
2929
}
3030

3131
// Previous returns the previous block tree entry in the chain.

kernel/chainstate_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func (cm *ChainstateManager) GetActiveChain() *Chain {
113113
// Returns nil if no block with this hash is found in the block index. The returned
114114
// BlockTreeEntry is a non-owned pointer valid for the lifetime of this chainstate
115115
// manager.
116-
func (cm *ChainstateManager) GetBlockTreeEntryByHash(blockHash *BlockHash) *BlockTreeEntry {
117-
ptr := C.btck_chainstate_manager_get_block_tree_entry_by_hash((*C.btck_ChainstateManager)(cm.ptr), (*C.btck_BlockHash)(blockHash.ptr))
116+
func (cm *ChainstateManager) GetBlockTreeEntryByHash(blockHash BlockHashLike) *BlockTreeEntry {
117+
ptr := C.btck_chainstate_manager_get_block_tree_entry_by_hash((*C.btck_ChainstateManager)(cm.ptr), blockHash.blockHashPtr())
118118
if ptr == nil {
119119
return nil
120120
}

kernel/chainstate_manager_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ func (s *ChainstateManagerTestSuite) TestGetBlockTreeEntryByHash(t *testing.T) {
9797
genesis := chain.GetGenesis()
9898

9999
genesisHash := genesis.Hash()
100-
defer genesisHash.Destroy()
101100

102101
// Use GetBlockTreeEntryByHash to find genesis
103102
foundGenesisIndex := s.Manager.GetBlockTreeEntryByHash(genesisHash)
@@ -116,7 +115,6 @@ func (s *ChainstateManagerTestSuite) TestGetBlockTreeEntryByHash(t *testing.T) {
116115
tipIndex := chain.GetTip()
117116

118117
tipHash := tipIndex.Hash()
119-
defer tipHash.Destroy()
120118

121119
foundTipIndex := s.Manager.GetBlockTreeEntryByHash(tipHash)
122120
if foundTipIndex == nil {

0 commit comments

Comments
 (0)