mpt: move empty hash node in a separate type

We use them quite frequently (consider children for a new branch
node) and it is better to get rid of unneeded allocations.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-08-03 17:10:46 +03:00
parent f02d8b4ec4
commit db80ef28df
11 changed files with 124 additions and 99 deletions

View file

@ -54,8 +54,8 @@ func (b *BaseNode) getBytes(n Node) []byte {
// updateHash updates hash field for this BaseNode.
func (b *BaseNode) updateHash(n Node) {
if n.Type() == HashT {
panic("can't update hash for hash node")
if n.Type() == HashT || n.Type() == EmptyT {
panic("can't update hash for empty or hash node")
}
b.hash = hash.DoubleSha256(b.getBytes(n))
b.hashValid = true
@ -86,17 +86,7 @@ func encodeBinaryAsChild(n Node, w *io.BinWriter) {
// encodeNodeWithType encodes node together with it's type.
func encodeNodeWithType(n Node, w *io.BinWriter) {
switch t := n.Type(); t {
case HashT:
hn := n.(*HashNode)
if !hn.hashValid {
w.WriteB(byte(EmptyT))
break
}
fallthrough
default:
w.WriteB(byte(t))
}
w.WriteB(byte(n.Type()))
n.EncodeBinary(w)
}
@ -120,11 +110,7 @@ func DecodeNodeWithType(r *io.BinReader) Node {
case LeafT:
n = new(LeafNode)
case EmptyT:
n = &HashNode{
BaseNode: BaseNode{
hashValid: false,
},
}
n = EmptyNode{}
default:
r.Err = fmt.Errorf("invalid node type: %x", typ)
return nil