mpt: reduce max key length

The constant used is actually a nibbled-path restriction.
Clarify this and make them private.
This commit is contained in:
Evgeniy Stratonikov 2021-06-03 15:44:21 +03:00
parent ebff8be20a
commit 5b1f6207de
4 changed files with 12 additions and 6 deletions

View file

@ -11,8 +11,14 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
)
// MaxKeyLength is the max length of the extension node key.
const MaxKeyLength = (storage.MaxStorageKeyLen + 4) * 2
const (
// maxPathLength is the max length of the extension node key.
maxPathLength = (storage.MaxStorageKeyLen + 4) * 2
// MaxKeyLength is the max length of the key to put in trie
// before transforming to nibbles.
MaxKeyLength = maxPathLength / 2
)
// ExtensionNode represents MPT's extension node.
type ExtensionNode struct {
@ -48,7 +54,7 @@ func (e *ExtensionNode) Bytes() []byte {
// DecodeBinary implements io.Serializable.
func (e *ExtensionNode) DecodeBinary(r *io.BinReader) {
sz := r.ReadVarUint()
if sz > MaxKeyLength {
if sz > maxPathLength {
r.Err = fmt.Errorf("extension node key is too big: %d", sz)
return
}

View file

@ -96,7 +96,7 @@ func (n *NodeObject) UnmarshalJSON(data []byte) error {
key, err := unmarshalHex(keyRaw)
if err != nil {
return err
} else if len(key) > MaxKeyLength {
} else if len(key) > maxPathLength {
return errors.New("extension key is too big")
}

View file

@ -61,7 +61,7 @@ func TestNode_Serializable(t *testing.T) {
t.Run("WithType", getTestFuncEncode(true, &NodeObject{e}, new(NodeObject)))
})
t.Run("BigKey", getTestFuncEncode(false,
NewExtensionNode(random.Bytes(MaxKeyLength+1), NewLeafNode(random.Bytes(10))), new(ExtensionNode)))
NewExtensionNode(random.Bytes(maxPathLength+1), NewLeafNode(random.Bytes(10))), new(ExtensionNode)))
})
t.Run("Branch", func(t *testing.T) {

View file

@ -161,7 +161,7 @@ func TestTrie_PutInvalid(t *testing.T) {
key, value := []byte("key"), []byte("value")
// big key
require.Error(t, tr.Put(make([]byte, MaxKeyLength+1), value))
require.Error(t, tr.Put(make([]byte, maxPathLength+1), value))
// big value
require.Error(t, tr.Put(key, make([]byte, MaxValueLength+1)))