mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
1b83dc2476
Mostly it's about Go 1.22+ syntax with ranging over integers, but it also prefers ranging over slices where possible (it makes code a little better to read). Notice that we have a number of dangerous loops where slices are mutated during loop execution, many of these can't be converted since we need proper length evalutation at every iteration. Signed-off-by: Roman Khimov <roman@nspcc.ru>
104 lines
2.2 KiB
Go
104 lines
2.2 KiB
Go
package mpt
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
)
|
|
|
|
const (
|
|
// childrenCount represents the number of children of a branch node.
|
|
childrenCount = 17
|
|
// lastChild is the index of the last child.
|
|
lastChild = childrenCount - 1
|
|
)
|
|
|
|
// BranchNode represents an MPT's branch node.
|
|
type BranchNode struct {
|
|
BaseNode
|
|
Children [childrenCount]Node
|
|
}
|
|
|
|
var _ Node = (*BranchNode)(nil)
|
|
|
|
// NewBranchNode returns a new branch node.
|
|
func NewBranchNode() *BranchNode {
|
|
b := new(BranchNode)
|
|
for i := range childrenCount {
|
|
b.Children[i] = EmptyNode{}
|
|
}
|
|
return b
|
|
}
|
|
|
|
// Type implements the Node interface.
|
|
func (b *BranchNode) Type() NodeType { return BranchT }
|
|
|
|
// Hash implements the BaseNode interface.
|
|
func (b *BranchNode) Hash() util.Uint256 {
|
|
return b.getHash(b)
|
|
}
|
|
|
|
// Bytes implements the BaseNode interface.
|
|
func (b *BranchNode) Bytes() []byte {
|
|
return b.getBytes(b)
|
|
}
|
|
|
|
// Size implements the Node interface.
|
|
func (b *BranchNode) Size() int {
|
|
sz := childrenCount
|
|
for i := range b.Children {
|
|
if !isEmpty(b.Children[i]) {
|
|
sz += util.Uint256Size
|
|
}
|
|
}
|
|
return sz
|
|
}
|
|
|
|
// EncodeBinary implements io.Serializable.
|
|
func (b *BranchNode) EncodeBinary(w *io.BinWriter) {
|
|
for i := range childrenCount {
|
|
encodeBinaryAsChild(b.Children[i], w)
|
|
}
|
|
}
|
|
|
|
// DecodeBinary implements io.Serializable.
|
|
func (b *BranchNode) DecodeBinary(r *io.BinReader) {
|
|
for i := range childrenCount {
|
|
no := new(NodeObject)
|
|
no.DecodeBinary(r)
|
|
b.Children[i] = no.Node
|
|
}
|
|
}
|
|
|
|
// MarshalJSON implements the json.Marshaler.
|
|
func (b *BranchNode) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(b.Children)
|
|
}
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler.
|
|
func (b *BranchNode) UnmarshalJSON(data []byte) error {
|
|
var obj NodeObject
|
|
if err := obj.UnmarshalJSON(data); err != nil {
|
|
return err
|
|
} else if u, ok := obj.Node.(*BranchNode); ok {
|
|
*b = *u
|
|
return nil
|
|
}
|
|
return errors.New("expected branch node")
|
|
}
|
|
|
|
// Clone implements Node interface.
|
|
func (b *BranchNode) Clone() Node {
|
|
res := *b
|
|
return &res
|
|
}
|
|
|
|
// splitPath splits path for a branch node.
|
|
func splitPath(path []byte) (byte, []byte) {
|
|
if len(path) != 0 {
|
|
return path[0], path[1:]
|
|
}
|
|
return lastChild, path
|
|
}
|