2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +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.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// 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
|
2015-07-22 16:48:40 +00:00
|
|
|
// 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
|
2015-07-22 16:48:40 +00:00
|
|
|
// 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 (
|
2020-02-06 10:53:09 +00:00
|
|
|
"bytes"
|
2015-07-05 23:19:48 +00:00
|
|
|
"io"
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"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 {
|
2019-05-27 13:51:49 +00:00
|
|
|
print(io.Writer)
|
2014-11-18 11:02:13 +00:00
|
|
|
fstring(string) string
|
2020-02-12 11:34:44 +00:00
|
|
|
|
|
|
|
// 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 (
|
2019-05-27 13:51:49 +00:00
|
|
|
// DESCRIBED: docs/programmers_guide/guide.md#hexary-radix-patricia-tree
|
2016-05-19 10:24:14 +00:00
|
|
|
fullNode struct {
|
2020-02-12 11:34:44 +00:00
|
|
|
ref nodeRef
|
2020-01-31 07:31:50 +00:00
|
|
|
Children [17]node // Actual trie node data to encode/decode (needs custom encoder)
|
2016-05-19 10:24:14 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
// DESCRIBED: docs/programmers_guide/guide.md#hexary-radix-patricia-tree
|
|
|
|
duoNode struct {
|
2020-02-12 11:34:44 +00:00
|
|
|
ref nodeRef
|
2019-05-27 13:51:49 +00:00
|
|
|
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 {
|
2020-02-12 11:34:44 +00:00
|
|
|
ref nodeRef
|
|
|
|
Key []byte // HEX encoding
|
|
|
|
Val node
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
hashNode []byte
|
|
|
|
valueNode []byte
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
accountNode struct {
|
|
|
|
accounts.Account
|
|
|
|
storage node
|
2020-02-06 10:53:09 +00:00
|
|
|
rootCorrect bool
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
)
|
2015-02-03 03:58:34 +00:00
|
|
|
|
2018-06-21 09:28:05 +00:00
|
|
|
// nilValueNode is used when collapsing internal trie nodes for hashing, since
|
|
|
|
// unset children need to serialize correctly.
|
|
|
|
var nilValueNode = valueNode(nil)
|
|
|
|
|
2020-02-06 10:53:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:24:14 +00:00
|
|
|
// EncodeRLP encodes a full node into the consensus RLP format.
|
2016-10-14 16:04:33 +00:00
|
|
|
func (n *fullNode) EncodeRLP(w io.Writer) error {
|
2018-06-21 09:28:05 +00:00
|
|
|
var nodes [17]node
|
|
|
|
|
2018-08-07 10:56:40 +00:00
|
|
|
for i, child := range &n.Children {
|
2018-06-21 09:28:05 +00:00
|
|
|
if child != nil {
|
|
|
|
nodes[i] = child
|
|
|
|
} else {
|
|
|
|
nodes[i] = nilValueNode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rlp.Encode(w, nodes)
|
2016-05-19 10:24:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
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)
|
2016-10-14 16:04:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
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
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
m <<= 1
|
2014-11-18 11:02:13 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
return i1, i2
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (n *fullNode) copy() *fullNode {
|
|
|
|
c := *n
|
|
|
|
return &c
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +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
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
return m
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +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
|
|
|
}
|
|
|
|
}
|
2020-02-12 11:34:44 +00:00
|
|
|
if n.ref.len > 0 {
|
|
|
|
copy(c.ref.data[:], n.ref.data[:])
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2020-02-12 11:34:44 +00:00
|
|
|
c.ref.len = n.ref.len
|
2019-05-27 13:51:49 +00:00
|
|
|
return &c
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (n *duoNode) fullCopy() *fullNode {
|
|
|
|
c := fullNode{}
|
|
|
|
i1, i2 := n.childrenIdx()
|
|
|
|
c.Children[i1] = n.child1
|
|
|
|
c.Children[i2] = n.child2
|
2020-02-12 11:34:44 +00:00
|
|
|
if n.ref.len > 0 {
|
|
|
|
copy(c.ref.data[:], n.ref.data[:])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-02-12 11:34:44 +00:00
|
|
|
c.ref.len = n.ref.len
|
2019-05-27 13:51:49 +00:00
|
|
|
return &c
|
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (n *duoNode) copy() *duoNode {
|
|
|
|
c := *n
|
|
|
|
return &c
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +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
|
|
|
|
2020-02-12 11:34:44 +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
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2020-02-12 11:34:44 +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 }
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
// 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("") }
|