forked from TrueCloudLab/neoneo-go
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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
|
@ -82,3 +84,23 @@ func encodeNodeWithType(n Node, w *io.BinWriter) {
|
|||
w.WriteB(byte(n.Type()))
|
||||
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/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
|
@ -43,21 +42,7 @@ func (n NodeObject) EncodeBinary(w *io.BinWriter) {
|
|||
|
||||
// DecodeBinary implements io.Serializable.
|
||||
func (n *NodeObject) DecodeBinary(r *io.BinReader) {
|
||||
typ := NodeType(r.ReadB())
|
||||
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)
|
||||
n.Node = DecodeNodeWithType(r)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
|
|
Loading…
Reference in a new issue