mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 13:38:35 +00:00
mpt: export func for decoding node with type
`NodeObject` can contain auxilliary fields and shouldn't be used from outside.
This commit is contained in:
parent
2f824c590a
commit
fd9ff4102a
2 changed files with 23 additions and 16 deletions
|
@ -1,6 +1,8 @@
|
||||||
package mpt
|
package mpt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
@ -82,3 +84,23 @@ func encodeNodeWithType(n Node, w *io.BinWriter) {
|
||||||
w.WriteB(byte(n.Type()))
|
w.WriteB(byte(n.Type()))
|
||||||
n.EncodeBinary(w)
|
n.EncodeBinary(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DecodeNodeWithType decodes node together with it's type.
|
||||||
|
func DecodeNodeWithType(r *io.BinReader) Node {
|
||||||
|
var n Node
|
||||||
|
switch typ := NodeType(r.ReadB()); typ {
|
||||||
|
case BranchT:
|
||||||
|
n = new(BranchNode)
|
||||||
|
case ExtensionT:
|
||||||
|
n = new(ExtensionNode)
|
||||||
|
case HashT:
|
||||||
|
n = new(HashNode)
|
||||||
|
case LeafT:
|
||||||
|
n = new(LeafNode)
|
||||||
|
default:
|
||||||
|
r.Err = fmt.Errorf("invalid node type: %x", typ)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
n.DecodeBinary(r)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
@ -43,21 +42,7 @@ func (n NodeObject) EncodeBinary(w *io.BinWriter) {
|
||||||
|
|
||||||
// DecodeBinary implements io.Serializable.
|
// DecodeBinary implements io.Serializable.
|
||||||
func (n *NodeObject) DecodeBinary(r *io.BinReader) {
|
func (n *NodeObject) DecodeBinary(r *io.BinReader) {
|
||||||
typ := NodeType(r.ReadB())
|
n.Node = DecodeNodeWithType(r)
|
||||||
switch typ {
|
|
||||||
case BranchT:
|
|
||||||
n.Node = new(BranchNode)
|
|
||||||
case ExtensionT:
|
|
||||||
n.Node = new(ExtensionNode)
|
|
||||||
case HashT:
|
|
||||||
n.Node = new(HashNode)
|
|
||||||
case LeafT:
|
|
||||||
n.Node = new(LeafNode)
|
|
||||||
default:
|
|
||||||
r.Err = fmt.Errorf("invalid node type: %x", typ)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
n.Node.DecodeBinary(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements json.Unmarshaler.
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
|
|
Loading…
Reference in a new issue