Skip to content

Commit

Permalink
chore: fix Test_Node_Encode
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Feb 27, 2024
1 parent 15f7305 commit 0162df3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
12 changes: 6 additions & 6 deletions pkg/trie/node/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import (
// of this package, and specified in the Polkadot spec at
// https://spec.polkadot.network/#sect-state-storage
func (n *Node) Encode(buffer Buffer) (err error) {
if n == nil {
_, err = buffer.Write([]byte{emptyVariant.bits})
return err
}

err = encodeHeader(n, n.MustBeHashed, buffer)
if err != nil {
return fmt.Errorf("cannot encode header: %w", err)
}

if n == nil {
// only encode the empty variant byte header
return nil
}

keyLE := codec.NibblesToKeyLE(n.PartialKey)
_, err = buffer.Write(keyLE)
if err != nil {
Expand Down Expand Up @@ -55,7 +55,7 @@ func (n *Node) Encode(buffer Buffer) (err error) {

_, err = buffer.Write(hashedValue.ToBytes())
if err != nil {
return fmt.Errorf("scale encoding storage value: %w", err)
return fmt.Errorf("writing hashed storage value: %w", err)
}
default:
encoder := scale.NewEncoder(buffer)
Expand Down
4 changes: 2 additions & 2 deletions pkg/trie/node/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func Test_Node_Encode(t *testing.T) {
PartialKey: []byte{1, 2, 3},
StorageValue: largeValue,
IsHashedValue: true,
MustBeHashed: false,
MustBeHashed: true,
},
writes: []writeCall{
{
Expand All @@ -151,7 +151,7 @@ func Test_Node_Encode(t *testing.T) {
},
},
wrappedErr: errTest,
errMessage: "encoding hashed storage value: test error",
errMessage: "writing hashed storage value: test error",
},
"branch_header_encoding_error": {
node: &Node{
Expand Down
5 changes: 0 additions & 5 deletions pkg/trie/node/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (

// encodeHeader writes the encoded header for the node.
func encodeHeader(node *Node, isHashedValue bool, writer io.Writer) (err error) {
if node == nil {
_, err = writer.Write([]byte{emptyVariant.bits})
return err
}

partialKeyLength := len(node.PartialKey)
if partialKeyLength > int(maxPartialKeyLength) {
panic(fmt.Sprintf("partial key length is too big: %d", partialKeyLength))
Expand Down

0 comments on commit 0162df3

Please sign in to comment.