Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for btc block header deserialize #26

Merged
merged 16 commits into from
Jan 15, 2025
17 changes: 14 additions & 3 deletions btclightclient/btc_header_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ package btclightclient
import (
"bytes"
"encoding/hex"
"errors"

"github.com/btcsuite/btcd/wire"
)

// We don't want to make it compilicated yet :D
vuvoth marked this conversation as resolved.
Show resolved Hide resolved
// type BTCHeaderBytes []byte
// const BTCHeaderSize = 80

const BTCHeaderSize = 80

func BlockHeaderFromHex(hexStr string) (wire.BlockHeader, error) {
data, _ := hex.DecodeString(hexStr)
var header wire.BlockHeader

if len(hexStr) != BTCHeaderSize*2 {
return header, errors.New("invalid header size, must have 80 bytes")
vuvoth marked this conversation as resolved.
Show resolved Hide resolved
}

data, err := hex.DecodeString(hexStr)
if err != nil {
return header, err
}

reader := bytes.NewReader(data)
err := header.Deserialize(reader)
err = header.Deserialize(reader)
return header, err
}
49 changes: 49 additions & 0 deletions btclightclient/btc_header_bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package btclightclient

import (
"errors"
"testing"

"gotest.tools/assert"
)

func TestBTCHeaderFromHex(t *testing.T) {
type testCase struct {
name string
headerHex string
expectedBlockHash string
expectedError error
}

run := func(t *testing.T, tc testCase) {
header, err := BlockHeaderFromHex(tc.headerHex)
blockHash := header.BlockHash()
if tc.expectedError != nil {
assert.Error(t, err, tc.expectedError.Error())
} else {
assert.NilError(t, err)
assert.Equal(t, tc.expectedBlockHash, blockHash.String())
}
}

testCases := []testCase{
{
name: "happy test case",
headerHex: "020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061a",
expectedError: nil,
expectedBlockHash: "0000000000000363d7f5f3341fb0b5b69949103e2d681591c9f737e4ea67e2a7",
},
{
name: "invalid header length",
headerHex: "020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061ab",
expectedError: errors.New("invalid header size, must have 80 bytes"),
vuvoth marked this conversation as resolved.
Show resolved Hide resolved
expectedBlockHash: "0000000000000363d7f5f3341fb0b5b69949103e2d681591c9f737e4ea67e2a7",
},
vuvoth marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
run(t, tc)
})
}
}
1 change: 0 additions & 1 deletion btclightclient/btc_light_client_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func (s *MemStore) SetIsNotHead(bh chainhash.Hash) {
delete(s.latestBlockHashOfFork, bh)
}

// TODO: convert to iterator rather than returning a map
func (s *MemStore) LatestBlockHashOfFork() []chainhash.Hash {
hashes := []chainhash.Hash{}
for h := range s.latestBlockHashOfFork {
Expand Down
Loading
Loading