Skip to content

Commit 1fa857a

Browse files
committed
Refactor BlockHash wrapper based on kernelApi_66
1 parent 7987947 commit 1fa857a

File tree

7 files changed

+69
-54
lines changed

7 files changed

+69
-54
lines changed

kernel/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewBlock(rawBlock []byte) (*Block, error) {
3737
}
3838

3939
func (b *Block) Hash() *BlockHash {
40-
return newBlockHash(C.btck_block_get_hash((*C.btck_Block)(b.ptr)))
40+
return newBlockHash(C.btck_block_get_hash((*C.btck_Block)(b.ptr)), true)
4141
}
4242

4343
// Bytes returns the consensus serialized block

kernel/block_hash.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,32 @@ func (blockHashCFuncs) destroy(ptr unsafe.Pointer) {
1414
C.btck_block_hash_destroy((*C.btck_BlockHash)(ptr))
1515
}
1616

17+
func (blockHashCFuncs) copy(ptr unsafe.Pointer) unsafe.Pointer {
18+
return unsafe.Pointer(C.btck_block_hash_copy((*C.btck_BlockHash)(ptr)))
19+
}
20+
1721
type BlockHash struct {
18-
*uniqueHandle
22+
*handle
23+
}
24+
25+
func newBlockHash(ptr *C.btck_BlockHash, fromOwned bool) *BlockHash {
26+
h := newHandle(unsafe.Pointer(ptr), blockHashCFuncs{}, fromOwned)
27+
return &BlockHash{handle: h}
1928
}
2029

21-
func newBlockHash(ptr *C.btck_BlockHash) *BlockHash {
22-
h := newUniqueHandle(unsafe.Pointer(ptr), blockHashCFuncs{})
23-
return &BlockHash{uniqueHandle: h}
30+
// NewBlockHash creates a new BlockHash from raw 32-byte hash data
31+
func NewBlockHash(hashBytes [32]byte) *BlockHash {
32+
ptr := C.btck_block_hash_create((*C.uchar)(unsafe.Pointer(&hashBytes[0])))
33+
return newBlockHash(ptr, true)
2434
}
2535

2636
// Bytes returns the raw hash bytes
27-
func (bh *BlockHash) Bytes() []byte {
28-
// BlockHash is a 32-byte array in the C struct
29-
return C.GoBytes(unsafe.Pointer(&(*C.btck_BlockHash)(bh.ptr).hash[0]), 32)
37+
func (bh *BlockHash) Bytes() [32]byte {
38+
var output [32]C.uchar
39+
C.btck_block_hash_to_bytes((*C.btck_BlockHash)(bh.ptr), &output[0])
40+
return *(*[32]byte)(unsafe.Pointer(&output[0]))
41+
}
42+
43+
func (bh *BlockHash) Copy() *BlockHash {
44+
return newBlockHash((*C.btck_BlockHash)(bh.ptr), false)
3045
}

kernel/block_hash_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package kernel
2+
3+
import (
4+
"encoding/hex"
5+
"testing"
6+
)
7+
8+
func TestBlockHash(t *testing.T) {
9+
genesisHex := "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"
10+
genesisBytes, err := hex.DecodeString(genesisHex)
11+
if err != nil {
12+
t.Fatalf("Failed to decode genesis hex: %v", err)
13+
}
14+
15+
block, err := NewBlock(genesisBytes)
16+
if err != nil {
17+
t.Fatalf("NewBlock() error = %v", err)
18+
}
19+
defer block.Destroy()
20+
21+
hash := block.Hash()
22+
defer hash.Destroy()
23+
24+
hashBytes := hash.Bytes()
25+
26+
newHash := NewBlockHash(hashBytes)
27+
defer newHash.Destroy()
28+
29+
newHashBytes := newHash.Bytes()
30+
31+
if hashBytes != newHashBytes {
32+
t.Errorf("Hash bytes differ: %x != %x", hashBytes, newHashBytes)
33+
}
34+
35+
copiedHash := hash.Copy()
36+
defer copiedHash.Destroy()
37+
38+
copiedHashBytes := copiedHash.Bytes()
39+
40+
if hashBytes != copiedHashBytes {
41+
t.Errorf("Copied hash bytes differ: %x != %x", hashBytes, copiedHashBytes)
42+
}
43+
}

kernel/block_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ func TestBlockFromRaw(t *testing.T) {
3232
hash := block.Hash()
3333
defer hash.Destroy()
3434

35-
hashBytes := hash.Bytes()
36-
if len(hashBytes) != 32 {
37-
t.Errorf("Expected hash length 32, got %d", len(hashBytes))
38-
}
39-
4035
// Expected genesis block hash (reversed byte order for display)
4136
expectedHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
42-
actualHashHex := hex.EncodeToString(reverseBytes(hashBytes))
37+
hashBytes := hash.Bytes()
38+
actualHashHex := hex.EncodeToString(reverseBytes(hashBytes[:]))
4339
if actualHashHex != expectedHash {
4440
t.Logf("Actual hash: %s", actualHashHex)
4541
t.Logf("Expected hash: %s", expectedHash)

kernel/block_tree_entry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (bi *BlockTreeEntry) Height() int32 {
1515

1616
func (bi *BlockTreeEntry) Hash() *BlockHash {
1717
ptr := C.btck_block_tree_entry_get_block_hash(bi.ptr)
18-
return newBlockHash(check(ptr))
18+
return newBlockHash(check(ptr), true)
1919
}
2020

2121
func (bi *BlockTreeEntry) Previous() *BlockTreeEntry {

kernel/chain_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,17 @@ func TestChain(t *testing.T) {
1616

1717
// Test GetGenesis
1818
genesis := chain.GetGenesis()
19-
2019
height := genesis.Height()
2120
if height != 0 {
2221
t.Errorf("Expected genesis height 0, got %d", height)
2322
}
2423

25-
genesisHash := genesis.Hash()
26-
defer genesisHash.Destroy()
27-
28-
hashBytes := genesisHash.Bytes()
29-
if len(hashBytes) != 32 {
30-
t.Errorf("Expected hash length 32, got %d", len(hashBytes))
31-
}
32-
3324
// Test GetTip
3425
tip := chain.GetTip()
35-
3626
tipHeight := tip.Height()
3727
if tipHeight <= 0 {
3828
t.Errorf("Expected tip height > 0, got %d", tipHeight)
3929
}
40-
41-
tipHash := tip.Hash()
42-
defer tipHash.Destroy()
43-
44-
tipHashBytes := tipHash.Bytes()
45-
if len(tipHashBytes) != 32 {
46-
t.Errorf("Expected hash length 32, got %d", len(tipHashBytes))
47-
}
48-
4930
if tip.Height() != suite.ImportedBlocksCount {
5031
t.Errorf("Expected tip height %d, got %d", suite.ImportedBlocksCount, tip.Height())
5132
}

kernel/chainstate_manager_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func (s *ChainstateManagerTestSuite) TestReadBlock(t *testing.T) {
6969

7070
// Test reading genesis block
7171
genesis := chain.GetGenesis()
72-
7372
genesisBlock, err := s.Manager.ReadBlock(genesis)
7473
if err != nil {
7574
t.Fatalf("ChainstateManager.ReadBlock() for genesis error = %v", err)
@@ -79,18 +78,8 @@ func (s *ChainstateManagerTestSuite) TestReadBlock(t *testing.T) {
7978
}
8079
defer genesisBlock.Destroy()
8180

82-
// Verify genesis block has expected properties
83-
genesisHash := genesisBlock.Hash()
84-
defer genesisHash.Destroy()
85-
86-
hashBytes := genesisHash.Bytes()
87-
if len(hashBytes) != 32 {
88-
t.Errorf("Expected hash length 32, got %d", len(hashBytes))
89-
}
90-
9181
// Test reading tip block
9282
tip := chain.GetTip()
93-
9483
tipBlock, err := s.Manager.ReadBlock(tip)
9584
if err != nil {
9685
t.Fatalf("ChainstateManager.ReadBlock() for tip error = %v", err)
@@ -99,15 +88,6 @@ func (s *ChainstateManagerTestSuite) TestReadBlock(t *testing.T) {
9988
t.Fatal("Read tip block is nil")
10089
}
10190
defer tipBlock.Destroy()
102-
103-
// Verify tip block properties
104-
tipHash := tipBlock.Hash()
105-
defer tipHash.Destroy()
106-
107-
tipHashBytes := tipHash.Bytes()
108-
if len(tipHashBytes) != 32 {
109-
t.Errorf("Expected tip hash length 32, got %d", len(tipHashBytes))
110-
}
11191
}
11292

11393
func (s *ChainstateManagerTestSuite) TestGetBlockTreeEntryByHash(t *testing.T) {

0 commit comments

Comments
 (0)