diff --git a/pkg/core/mpt/base.go b/pkg/core/mpt/base.go index 8762281d6..63e56dc52 100644 --- a/pkg/core/mpt/base.go +++ b/pkg/core/mpt/base.go @@ -23,7 +23,6 @@ type BaseNodeIface interface { Hash() util.Uint256 Type() NodeType Bytes() []byte - EncodeBinaryAsChild(w *io.BinWriter) } type flushedNode interface { @@ -76,6 +75,15 @@ func (b *BaseNode) invalidateCache() { b.hashValid = false } +func encodeBinaryAsChild(n Node, w *io.BinWriter) { + if isEmpty(n) { + w.WriteB(byte(EmptyT)) + return + } + w.WriteB(byte(HashT)) + w.WriteBytes(n.Hash().BytesBE()) +} + // encodeNodeWithType encodes node together with it's type. func encodeNodeWithType(n Node, w *io.BinWriter) { switch t := n.Type(); t { diff --git a/pkg/core/mpt/branch.go b/pkg/core/mpt/branch.go index e01c02620..d66679d8f 100644 --- a/pkg/core/mpt/branch.go +++ b/pkg/core/mpt/branch.go @@ -48,16 +48,10 @@ func (b *BranchNode) Bytes() []byte { // EncodeBinary implements io.Serializable. func (b *BranchNode) EncodeBinary(w *io.BinWriter) { for i := 0; i < childrenCount; i++ { - b.Children[i].EncodeBinaryAsChild(w) + encodeBinaryAsChild(b.Children[i], w) } } -// EncodeBinaryAsChild implements BaseNode interface. -func (b *BranchNode) EncodeBinaryAsChild(w *io.BinWriter) { - n := &NodeObject{Node: NewHashNode(b.Hash())} // with type - n.EncodeBinary(w) -} - // DecodeBinary implements io.Serializable. func (b *BranchNode) DecodeBinary(r *io.BinReader) { for i := 0; i < childrenCount; i++ { diff --git a/pkg/core/mpt/extension.go b/pkg/core/mpt/extension.go index 7cb0bb7f0..1b8047e20 100644 --- a/pkg/core/mpt/extension.go +++ b/pkg/core/mpt/extension.go @@ -69,13 +69,7 @@ func (e *ExtensionNode) DecodeBinary(r *io.BinReader) { // EncodeBinary implements io.Serializable. func (e ExtensionNode) EncodeBinary(w *io.BinWriter) { w.WriteVarBytes(e.key) - e.next.EncodeBinaryAsChild(w) -} - -// EncodeBinaryAsChild implements BaseNode interface. -func (e *ExtensionNode) EncodeBinaryAsChild(w *io.BinWriter) { - n := &NodeObject{Node: NewHashNode(e.Hash())} // with type - n.EncodeBinary(w) + encodeBinaryAsChild(e.next, w) } // MarshalJSON implements json.Marshaler. diff --git a/pkg/core/mpt/hash.go b/pkg/core/mpt/hash.go index ca24dc457..f34b542f2 100644 --- a/pkg/core/mpt/hash.go +++ b/pkg/core/mpt/hash.go @@ -58,12 +58,6 @@ func (h HashNode) EncodeBinary(w *io.BinWriter) { w.WriteBytes(h.hash[:]) } -// EncodeBinaryAsChild implements BaseNode interface. -func (h *HashNode) EncodeBinaryAsChild(w *io.BinWriter) { - no := &NodeObject{Node: h} // with type - no.EncodeBinary(w) -} - // MarshalJSON implements json.Marshaler. func (h *HashNode) MarshalJSON() ([]byte, error) { if !h.hashValid { diff --git a/pkg/core/mpt/leaf.go b/pkg/core/mpt/leaf.go index 49ee55f97..ecb003c23 100644 --- a/pkg/core/mpt/leaf.go +++ b/pkg/core/mpt/leaf.go @@ -56,12 +56,6 @@ func (n LeafNode) EncodeBinary(w *io.BinWriter) { w.WriteVarBytes(n.value) } -// EncodeBinaryAsChild implements BaseNode interface. -func (n *LeafNode) EncodeBinaryAsChild(w *io.BinWriter) { - no := &NodeObject{Node: NewHashNode(n.Hash())} // with type - no.EncodeBinary(w) -} - // MarshalJSON implements json.Marshaler. func (n *LeafNode) MarshalJSON() ([]byte, error) { return []byte(`{"value":"` + hex.EncodeToString(n.value) + `"}`), nil