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
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:55:51 +00:00
|
|
|
// IsEmpty returns true if 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) {
|
2021-03-30 17:35:41 +00:00
|
|
|
if h.hashValid {
|
|
|
|
h.hash.DecodeBinary(r)
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
return
|
|
|
|
}
|
2021-03-30 17:35:41 +00:00
|
|
|
w.WriteBytes(h.hash[:])
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
2020-05-28 05:55:12 +00:00
|
|
|
|
2021-03-31 08:04:42 +00:00
|
|
|
// EncodeBinaryAsChild implements BaseNode interface.
|
|
|
|
func (h *HashNode) EncodeBinaryAsChild(w *io.BinWriter) {
|
|
|
|
no := &NodeObject{Node: h} // with type
|
|
|
|
no.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|