2020-05-22 07:37:07 +00:00
|
|
|
package mpt
|
|
|
|
|
|
|
|
import (
|
2020-05-28 05:55:12 +00:00
|
|
|
"errors"
|
2020-05-22 07:37:07 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HashNode represents MPT's hash node.
|
|
|
|
type HashNode struct {
|
2020-06-02 20:49:46 +00:00
|
|
|
BaseNode
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Node = (*HashNode)(nil)
|
|
|
|
|
|
|
|
// NewHashNode returns hash node with the specified hash.
|
|
|
|
func NewHashNode(h util.Uint256) *HashNode {
|
|
|
|
return &HashNode{
|
2020-06-02 20:49:46 +00:00
|
|
|
BaseNode: BaseNode{
|
|
|
|
hash: h,
|
|
|
|
hashValid: true,
|
|
|
|
},
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements Node interface.
|
|
|
|
func (h *HashNode) Type() NodeType { return HashT }
|
|
|
|
|
|
|
|
// Hash implements Node interface.
|
|
|
|
func (h *HashNode) Hash() util.Uint256 {
|
2020-06-02 20:49:46 +00:00
|
|
|
if !h.hashValid {
|
2020-05-22 07:37:07 +00:00
|
|
|
panic("can't get hash of an empty HashNode")
|
|
|
|
}
|
|
|
|
return h.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true iff h is an empty node i.e. contains no hash.
|
2020-06-02 20:49:46 +00:00
|
|
|
func (h *HashNode) IsEmpty() bool { return !h.hashValid }
|
|
|
|
|
|
|
|
// Bytes returns serialized HashNode.
|
|
|
|
func (h *HashNode) Bytes() []byte {
|
|
|
|
return h.getBytes(h)
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable.
|
|
|
|
func (h *HashNode) DecodeBinary(r *io.BinReader) {
|
|
|
|
sz := r.ReadVarUint()
|
|
|
|
switch sz {
|
|
|
|
case 0:
|
2020-06-02 20:49:46 +00:00
|
|
|
h.hashValid = false
|
2020-05-22 07:37:07 +00:00
|
|
|
case util.Uint256Size:
|
2020-06-02 20:49:46 +00:00
|
|
|
h.hashValid = true
|
2020-05-22 07:37:07 +00:00
|
|
|
r.ReadBytes(h.hash[:])
|
|
|
|
default:
|
|
|
|
r.Err = fmt.Errorf("invalid hash node size: %d", sz)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable.
|
|
|
|
func (h HashNode) EncodeBinary(w *io.BinWriter) {
|
2020-06-02 20:49:46 +00:00
|
|
|
if !h.hashValid {
|
2020-05-22 07:37:07 +00:00
|
|
|
w.WriteVarUint(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteVarBytes(h.hash[:])
|
|
|
|
}
|
2020-05-28 05:55:12 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (h *HashNode) MarshalJSON() ([]byte, error) {
|
2020-06-02 20:49:46 +00:00
|
|
|
if !h.hashValid {
|
2020-05-28 05:55:12 +00:00
|
|
|
return []byte(`{}`), nil
|
|
|
|
}
|
|
|
|
return []byte(`{"hash":"` + h.hash.StringLE() + `"}`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (h *HashNode) UnmarshalJSON(data []byte) error {
|
|
|
|
var obj NodeObject
|
|
|
|
if err := obj.UnmarshalJSON(data); err != nil {
|
|
|
|
return err
|
|
|
|
} else if u, ok := obj.Node.(*HashNode); ok {
|
|
|
|
*h = *u
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("expected hash node")
|
|
|
|
}
|