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

fix(trie): empty value encoding for leaves #2958

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/trie/node/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func decodeLeaf(reader io.Reader, partialKeyLength uint16) (node *Node, err erro
return nil, fmt.Errorf("%w: %s", ErrDecodeValue, err)
}

// The scale decoding above can encounter either io.EOF,
// leaving the value byte slice as `[]byte(nil)`, or
// `[]byte{0}` which decodes to `[]byte{}`.
// This implementation forces empty node values to `[]byte(nil)`.
if len(value) > 0 {
node.SubValue = value
}
Expand Down
10 changes: 5 additions & 5 deletions internal/trie/node/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func (n *Node) Encode(buffer Buffer) (err error) {
return fmt.Errorf("cannot write LE key to buffer: %w", err)
}

kind := n.Kind()
nodeIsBranch := kind == Branch
nodeIsBranch := n.Kind() == Branch
if nodeIsBranch {
childrenBitmap := common.Uint16ToBytes(n.ChildrenBitmap())
_, err = buffer.Write(childrenBitmap)
Expand All @@ -37,9 +36,10 @@ func (n *Node) Encode(buffer Buffer) (err error) {
}
}

// Only encode node value if the node is a leaf or
// the node is a branch with a non empty value.
if !nodeIsBranch || (nodeIsBranch && n.SubValue != nil) {
// Only encode node value if it's not empty.
// See https://spec.polkadot.network/#defn-node-subvalue
// See https://github.com/paritytech/substrate/blob/a7ba55d3c54b9957c242f659e02f5b5a0f47b3ff/primitives/trie/src/node_codec.rs#L123
if n.SubValue != nil {
encoder := scale.NewEncoder(buffer)
err = encoder.Encode(n.SubValue)
if err != nil {
Expand Down
21 changes: 19 additions & 2 deletions internal/trie/node/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package node

import (
"bytes"
"errors"
"testing"

"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -102,8 +104,6 @@ func Test_Node_Encode(t *testing.T) {
writes: []writeCall{
{written: []byte{leafVariant.bits | 3}}, // partial key length 3
{written: []byte{0x01, 0x23}}, // partial key
{written: []byte{0}}, // node value encoded length
{written: nil}, // node value
},
expectedEncoding: []byte{1, 2, 3},
},
Expand Down Expand Up @@ -310,3 +310,20 @@ func Test_Node_Encode(t *testing.T) {
})
}
}

func Test_scaleEncodeEmptyByteSlices(t *testing.T) {
t.Parallel()

buffer := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buffer)
err := encoder.Encode([]byte(nil))
require.NoError(t, err)
assert.Equal(t, []byte{0}, buffer.Bytes())

buffer.Reset()

encoder = scale.NewEncoder(buffer)
err = encoder.Encode([]byte{})
require.NoError(t, err)
assert.Equal(t, []byte{0}, buffer.Bytes())
}