Skip to content

Commit

Permalink
Unmarshal and Marshal methods for chainhash and btcd headers
Browse files Browse the repository at this point in the history
  • Loading branch information
vitsalis committed Jun 24, 2022
1 parent 35e084b commit 762e8e3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
15 changes: 6 additions & 9 deletions types/btc_header_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type BTCHeaderBytes []byte
type BTCHeadersBytes []BTCHeaderBytes

func (m BTCHeaderBytes) MarshalJSON() ([]byte, error) {
btcdHeader, err := m.ToBtcdHeader()
btcdHeader, err := m.MarshalBlockHeader()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -49,8 +49,8 @@ func (m *BTCHeaderBytes) Unmarshal(data []byte) error {
return nil
}

func (m *BTCHeaderBytes) MarshalHex() (string, error) {
btcdHeader, err := m.ToBtcdHeader()
func (m BTCHeaderBytes) MarshalHex() (string, error) {
btcdHeader, err := m.MarshalBlockHeader()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -85,8 +85,7 @@ func (m *BTCHeaderBytes) Size() int {
return len(bz)
}

// ToBtcdHeader parse header bytes into a BlockHeader instance
func (m BTCHeaderBytes) ToBtcdHeader() (*wire.BlockHeader, error) {
func (m BTCHeaderBytes) MarshalBlockHeader() (*wire.BlockHeader, error) {
// Create an empty header
header := &wire.BlockHeader{}

Expand All @@ -101,11 +100,9 @@ func (m BTCHeaderBytes) ToBtcdHeader() (*wire.BlockHeader, error) {
return header, nil
}

// BtcdHeaderToHeaderBytes gets a BlockHeader instance and returns the header bytes
func BtcdHeaderToHeaderBytes(header *wire.BlockHeader) BTCHeaderBytes {
func (m *BTCHeaderBytes) UnmarshalBlockHeader(header *wire.BlockHeader) {
var buf bytes.Buffer
header.Serialize(&buf)

headerBytes := buf.Bytes()
return headerBytes
*m = buf.Bytes()
}
20 changes: 9 additions & 11 deletions types/btc_header_hash_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type BTCHeaderHashesBytes []BTCHeaderHashBytes

func (m BTCHeaderHashBytes) MarshalJSON() ([]byte, error) {
// Get the chainhash representation
chHash, err := m.ToChainhash()
chHash, err := m.MarshalChainhash()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -48,7 +48,7 @@ func (m *BTCHeaderHashBytes) Unmarshal(bz []byte) error {
}

func (m *BTCHeaderHashBytes) MarshalHex() (string, error) {
chHash, err := m.ToChainhash()
chHash, err := m.MarshalChainhash()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -85,20 +85,18 @@ func (m *BTCHeaderHashBytes) Size() int {
return len(bz)
}

// ToChainhash gets hash bytes in reverse order and returns a Hash instance
func (m BTCHeaderHashBytes) ToChainhash() (*chainhash.Hash, error) {
func (m BTCHeaderHashBytes) MarshalChainhash() (*chainhash.Hash, error) {
return chainhash.NewHash(m)
}

func (m *BTCHeaderHashBytes) UnmarshalChainhash(hash *chainhash.Hash) {
var headerHashBytes BTCHeaderHashBytes
headerHashBytes.Unmarshal(hash[:])
*m = headerHashBytes
}

func (m BTCHeaderHashBytes) reverse() {
for i := 0; i < chainhash.HashSize/2; i++ {
m[i], m[chainhash.HashSize-1-i] = m[chainhash.HashSize-1-i], m[i]
}
}

// ChainhashToHeaderHashBytes gets a Hash instance and returns bytes in reverse order
func ChainhashToHeaderHashBytes(hash *chainhash.Hash) BTCHeaderHashBytes {
var headerHashBytes BTCHeaderHashBytes
headerHashBytes.Unmarshal(hash[:])
return headerHashBytes
}
5 changes: 3 additions & 2 deletions x/btclightclient/keeper/base_btc_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (k Keeper) GetBaseBTCHeader(ctx sdk.Context) types.BaseBTCHeader {
return types.BaseBTCHeader{}
}

headerBytes := bbl.BtcdHeaderToHeaderBytes(baseBtcdHeader)
var headerBytes bbl.BTCHeaderBytes
headerBytes.UnmarshalBlockHeader(baseBtcdHeader)
return types.BaseBTCHeader{Header: &headerBytes, Height: height}
}

Expand All @@ -36,7 +37,7 @@ func (k Keeper) SetBaseBTCHeader(ctx sdk.Context, baseBTCHeader types.BaseBTCHea
panic("A base BTC Header has already been set")
}

btcdHeader, err := baseBTCHeader.Header.ToBtcdHeader()
btcdHeader, err := baseBTCHeader.Header.MarshalBlockHeader()
if err != nil {
panic("Base BTC Header bytes do not correspond to btcd header")
}
Expand Down
2 changes: 1 addition & 1 deletion x/btclightclient/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k Keeper) Contains(ctx context.Context, req *types.QueryContainsRequest) (
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
chHash, err := req.Hash.ToChainhash()
chHash, err := req.Hash.MarshalChainhash()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/btclightclient/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (m msgServer) InsertHeader(ctx context.Context, msg *types.MsgInsertHeader)
// that will get rejected.

// Get Btcd header from bytes
btcdHeader, err := msg.Header.ToBtcdHeader()
btcdHeader, err := msg.Header.MarshalBlockHeader()
if err != nil {
return nil, err
}
Expand Down
17 changes: 10 additions & 7 deletions x/btclightclient/keeper/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (s HeadersState) CreateHeader(header *wire.BlockHeader, height uint64) {
headersKey := types.HeadersObjectKey(height, &headerHash)
heightKey := types.HeadersObjectHeightKey(&headerHash)

headerBytes := bbl.BtcdHeaderToHeaderBytes(header)
var headerBytes bbl.BTCHeaderBytes
headerBytes.UnmarshalBlockHeader(header)
// save concrete object
s.headers.Set(headersKey, headerBytes)
// map header to height
Expand All @@ -53,7 +54,7 @@ func (s HeadersState) GetHeader(height uint64, hash *chainhash.Hash) (*wire.Bloc
var headerBytes bbl.BTCHeaderBytes
headerBytes.Unmarshal(rawBytes)

header, err := headerBytes.ToBtcdHeader()
header, err := headerBytes.MarshalBlockHeader()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,7 +90,7 @@ func (s HeadersState) GetHeadersByHeight(height uint64, f func(*wire.BlockHeader
for ; iter.Valid(); iter.Next() {
var headerBytes bbl.BTCHeaderBytes
headerBytes.Unmarshal(iter.Value())
header, err := headerBytes.ToBtcdHeader()
header, err := headerBytes.MarshalBlockHeader()
if err != nil {
return err
}
Expand All @@ -115,7 +116,7 @@ func (s HeadersState) GetDescendingHeaders() ([]*wire.BlockHeader, error) {
for ; iter.Valid(); iter.Next() {
var headerBytes bbl.BTCHeaderBytes
headerBytes.Unmarshal(iter.Value())
header, err := headerBytes.ToBtcdHeader()
header, err := headerBytes.MarshalBlockHeader()
if err != nil {
return nil, err
}
Expand All @@ -127,7 +128,8 @@ func (s HeadersState) GetDescendingHeaders() ([]*wire.BlockHeader, error) {
// HeaderExists Check whether a hash is maintained in storage
func (s HeadersState) HeaderExists(hash *chainhash.Hash) bool {
store := prefix.NewStore(s.hashToHeight, types.HashToHeightPrefix)
hashBytes := bbl.ChainhashToHeaderHashBytes(hash)
var hashBytes bbl.BTCHeaderHashBytes
hashBytes.UnmarshalChainhash(hash)
return store.Has(hashBytes)
}

Expand Down Expand Up @@ -161,7 +163,8 @@ func (s HeadersState) GetBaseBTCHeader() (*wire.BlockHeader, error) {

// CreateTip sets the provided header as the tip
func (s HeadersState) CreateTip(header *wire.BlockHeader) {
headerBytes := bbl.BtcdHeaderToHeaderBytes(header)
var headerBytes bbl.BTCHeaderBytes
headerBytes.UnmarshalBlockHeader(header)
tipKey := types.TipKey()
s.tip.Set(tipKey, headerBytes)
}
Expand Down Expand Up @@ -196,7 +199,7 @@ func (s HeadersState) GetTip() *wire.BlockHeader {
tipKey := types.TipKey()
var tipBytes bbl.BTCHeaderBytes
tipBytes.Unmarshal(s.tip.Get(tipKey))
tip, err := tipBytes.ToBtcdHeader()
tip, err := tipBytes.MarshalBlockHeader()
if err != nil {
panic("Stored tip is not a valid btcd header")
}
Expand Down
6 changes: 4 additions & 2 deletions x/btclightclient/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ var (
func HeadersObjectKey(height uint64, hash *chainhash.Hash) []byte {
he := sdk.Uint64ToBigEndian(height)

hashBytes := bbl.ChainhashToHeaderHashBytes(hash)
var hashBytes bbl.BTCHeaderHashBytes
hashBytes.UnmarshalChainhash(hash)

heightPrefix := append(HeadersObjectPrefix, he...)
return append(heightPrefix, hashBytes...)
}

func HeadersObjectHeightKey(hash *chainhash.Hash) []byte {
hashBytes := bbl.ChainhashToHeaderHashBytes(hash)
var hashBytes bbl.BTCHeaderHashBytes
hashBytes.UnmarshalChainhash(hash)
return append(HashToHeightPrefix, hashBytes...)
}

Expand Down
2 changes: 1 addition & 1 deletion x/btclightclient/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (msg *MsgInsertHeader) ValidateBasic() error {
return err
}

header, err := msg.Header.ToBtcdHeader()
header, err := msg.Header.MarshalBlockHeader()
if err != nil {
return err
}
Expand Down

0 comments on commit 762e8e3

Please sign in to comment.