erigon-pulse/trie/node.go

207 lines
5.0 KiB
Go
Raw Normal View History

2015-07-07 00:54:22 +00:00
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-07 00:54:22 +00:00
//
// The go-ethereum library is free software: you can redistribute it and/or modify
2015-07-07 00:54:22 +00:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
2015-07-07 00:54:22 +00:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 00:54:22 +00:00
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
2015-07-07 00:54:22 +00:00
2015-01-08 10:47:04 +00:00
package trie
2014-11-18 11:02:13 +00:00
2015-07-05 23:19:48 +00:00
import (
"bytes"
2015-07-05 23:19:48 +00:00
"io"
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/rlp"
2015-07-05 23:19:48 +00:00
)
2014-11-18 11:02:13 +00:00
var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "[17]"}
2015-07-05 23:19:48 +00:00
type node interface {
print(io.Writer)
2014-11-18 11:02:13 +00:00
fstring(string) string
// if not empty, returns node's RLP or hash thereof
reference() []byte
2014-11-18 11:02:13 +00:00
}
2015-07-05 23:19:48 +00:00
type (
// DESCRIBED: docs/programmers_guide/guide.md#hexary-radix-patricia-tree
fullNode struct {
ref nodeRef
Children [17]node // Actual trie node data to encode/decode (needs custom encoder)
}
// DESCRIBED: docs/programmers_guide/guide.md#hexary-radix-patricia-tree
duoNode struct {
ref nodeRef
mask uint32 // Bitmask. The set bits indicate the child is not nil
child1 node
child2 node
}
// DESCRIBED: docs/programmers_guide/guide.md#hexary-radix-patricia-tree
2015-07-05 23:19:48 +00:00
shortNode struct {
ref nodeRef
Key []byte // HEX encoding
Val node
2015-07-05 23:19:48 +00:00
}
hashNode []byte
valueNode []byte
accountNode struct {
accounts.Account
storage node
rootCorrect bool
}
2015-07-05 23:19:48 +00:00
)
// nilValueNode is used when collapsing internal trie nodes for hashing, since
// unset children need to serialize correctly.
var nilValueNode = valueNode(nil)
func EncodeAsValue(data []byte) ([]byte, error) {
tmp := new(bytes.Buffer)
if err := rlp.Encode(tmp, valueNode(data)); err != nil {
return nil, err
}
return tmp.Bytes(), nil
}
// EncodeRLP encodes a full node into the consensus RLP format.
func (n *fullNode) EncodeRLP(w io.Writer) error {
var nodes [17]node
for i, child := range &n.Children {
if child != nil {
nodes[i] = child
} else {
nodes[i] = nilValueNode
}
}
return rlp.Encode(w, nodes)
}
func (n *duoNode) EncodeRLP(w io.Writer) error {
var children [17]node
i1, i2 := n.childrenIdx()
children[i1] = n.child1
children[i2] = n.child2
for i := 0; i < 17; i++ {
if i != int(i1) && i != int(i2) {
children[i] = valueNode(nil)
}
}
return rlp.Encode(w, children)
}
func (n *duoNode) childrenIdx() (i1 byte, i2 byte) {
child := 1
var m uint32 = 1
for i := 0; i < 17; i++ {
if (n.mask & m) > 0 {
if child == 1 {
i1 = byte(i)
child = 2
} else if child == 2 {
i2 = byte(i)
break
}
2014-11-18 11:02:13 +00:00
}
m <<= 1
2014-11-18 11:02:13 +00:00
}
return i1, i2
2015-07-05 23:19:48 +00:00
}
func (n *fullNode) copy() *fullNode {
c := *n
return &c
2015-07-05 23:19:48 +00:00
}
func (n *fullNode) mask() uint32 {
var m uint32
for i, child := range n.Children {
if child != nil {
m |= (uint32(1) << uint(i))
}
2015-07-05 23:19:48 +00:00
}
return m
2015-07-05 23:19:48 +00:00
}
func (n *fullNode) duoCopy() *duoNode {
c := duoNode{}
first := true
for i, child := range n.Children {
if child == nil {
continue
}
if first {
first = false
c.mask |= (uint32(1) << uint(i))
c.child1 = child
} else {
c.mask |= (uint32(1) << uint(i))
c.child2 = child
break
2015-07-05 23:19:48 +00:00
}
}
if n.ref.len > 0 {
copy(c.ref.data[:], n.ref.data[:])
2015-07-05 23:19:48 +00:00
}
c.ref.len = n.ref.len
return &c
2015-07-05 23:19:48 +00:00
}
func (n *duoNode) fullCopy() *fullNode {
c := fullNode{}
i1, i2 := n.childrenIdx()
c.Children[i1] = n.child1
c.Children[i2] = n.child2
if n.ref.len > 0 {
copy(c.ref.data[:], n.ref.data[:])
}
c.ref.len = n.ref.len
return &c
}
2015-07-05 23:19:48 +00:00
func (n *duoNode) copy() *duoNode {
c := *n
return &c
2015-07-05 23:19:48 +00:00
}
func (n *shortNode) copy() *shortNode {
c := *n
return &c
2015-07-05 23:19:48 +00:00
}
2014-11-18 11:02:13 +00:00
// nodeRef might contain node's RLP or hash thereof.
// Used instead of []byte in order to reduce GC churn.
type nodeRef struct {
data common.Hash // cached RLP of the node or hash thereof
len byte // length of the data (0 indicates invalid data)
2014-11-18 11:02:13 +00:00
}
func (n hashNode) reference() []byte { return n }
func (n valueNode) reference() []byte { return nil }
func (n *fullNode) reference() []byte { return n.ref.data[0:n.ref.len] }
func (n *duoNode) reference() []byte { return n.ref.data[0:n.ref.len] }
func (n *shortNode) reference() []byte { return n.ref.data[0:n.ref.len] }
func (an *accountNode) reference() []byte { return nil }
// Pretty printing.
func (n fullNode) String() string { return n.fstring("") }
func (n duoNode) String() string { return n.fstring("") }
func (n shortNode) String() string { return n.fstring("") }
func (n hashNode) String() string { return n.fstring("") }
func (n valueNode) String() string { return n.fstring("") }
func (an accountNode) String() string { return an.fstring("") }