2022-08-04 03:03:20 -05:00
|
|
|
// Copyright 2022 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// 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,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
"reflect"
|
2023-02-19 13:10:19 -06:00
|
|
|
"sort"
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
"strings"
|
2022-08-04 03:03:20 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// memoryNode is all the information we know about a single cached trie node
|
|
|
|
// in the memory.
|
|
|
|
type memoryNode struct {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
hash common.Hash // Node hash, computed by hashing rlp value, empty for deleted nodes
|
|
|
|
size uint16 // Byte size of the useful cached data, 0 for deleted nodes
|
|
|
|
node node // Cached collapsed trie node, or raw rlp data, nil for deleted nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// memoryNodeSize is the raw size of a memoryNode data structure without any
|
|
|
|
// node data included. It's an approximate size, but should be a lot better
|
|
|
|
// than not counting them.
|
|
|
|
// nolint:unused
|
|
|
|
var memoryNodeSize = int(reflect.TypeOf(memoryNode{}).Size())
|
|
|
|
|
|
|
|
// memorySize returns the total memory size used by this node.
|
|
|
|
// nolint:unused
|
2023-02-19 13:10:19 -06:00
|
|
|
func (n *memoryNode) memorySize(pathlen int) int {
|
|
|
|
return int(n.size) + memoryNodeSize + pathlen
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// rlp returns the raw rlp encoded blob of the cached trie node, either directly
|
|
|
|
// from the cache, or by regenerating it from the collapsed node.
|
|
|
|
// nolint:unused
|
|
|
|
func (n *memoryNode) rlp() []byte {
|
|
|
|
if node, ok := n.node.(rawNode); ok {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
return nodeToBytes(n.node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// obj returns the decoded and expanded trie node, either directly from the cache,
|
|
|
|
// or by regenerating it from the rlp encoded blob.
|
|
|
|
// nolint:unused
|
|
|
|
func (n *memoryNode) obj() node {
|
|
|
|
if node, ok := n.node.(rawNode); ok {
|
|
|
|
return mustDecodeNode(n.hash[:], node)
|
|
|
|
}
|
|
|
|
return expandNode(n.hash[:], n.node)
|
|
|
|
}
|
|
|
|
|
2023-02-19 13:10:19 -06:00
|
|
|
// isDeleted returns the indicator if the node is marked as deleted.
|
|
|
|
func (n *memoryNode) isDeleted() bool {
|
|
|
|
return n.hash == (common.Hash{})
|
|
|
|
}
|
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
// nodeWithPrev wraps the memoryNode with the previous node value.
|
2023-02-19 13:10:19 -06:00
|
|
|
// nolint: unused
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
type nodeWithPrev struct {
|
|
|
|
*memoryNode
|
|
|
|
prev []byte // RLP-encoded previous value, nil means it's non-existent
|
|
|
|
}
|
|
|
|
|
|
|
|
// unwrap returns the internal memoryNode object.
|
2023-02-19 13:10:19 -06:00
|
|
|
// nolint: unused
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
func (n *nodeWithPrev) unwrap() *memoryNode {
|
|
|
|
return n.memoryNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// memorySize returns the total memory size used by this node. It overloads
|
|
|
|
// the function in memoryNode by counting the size of previous value as well.
|
|
|
|
// nolint: unused
|
2023-02-19 13:10:19 -06:00
|
|
|
func (n *nodeWithPrev) memorySize(pathlen int) int {
|
|
|
|
return n.memoryNode.memorySize(pathlen) + len(n.prev)
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSet contains all dirty nodes collected during the commit operation.
|
|
|
|
// Each node is keyed by path. It's not thread-safe to use.
|
|
|
|
type NodeSet struct {
|
2023-02-19 13:10:19 -06:00
|
|
|
owner common.Hash // the identifier of the trie
|
|
|
|
nodes map[string]*memoryNode // the set of dirty nodes(inserted, updated, deleted)
|
|
|
|
leaves []*leaf // the list of dirty leaves
|
|
|
|
accessList map[string][]byte // The list of accessed nodes, which records the original node value
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeSet initializes an empty node set to be used for tracking dirty nodes
|
2023-02-19 13:10:19 -06:00
|
|
|
// for a specific account or storage trie. The owner is zero for the account trie
|
|
|
|
// and the owning account address hash for storage tries. The provided accessList
|
|
|
|
// represents the original value of accessed nodes, it can be optional but would
|
|
|
|
// be beneficial for speeding up the construction of trie history.
|
|
|
|
func NewNodeSet(owner common.Hash, accessList map[string][]byte) *NodeSet {
|
|
|
|
// Don't panic for lazy users
|
|
|
|
if accessList == nil {
|
|
|
|
accessList = make(map[string][]byte)
|
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
return &NodeSet{
|
2023-02-19 13:10:19 -06:00
|
|
|
owner: owner,
|
|
|
|
nodes: make(map[string]*memoryNode),
|
|
|
|
accessList: accessList,
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 13:10:19 -06:00
|
|
|
// forEachWithOrder iterates the dirty nodes with the specified order.
|
|
|
|
// If topToBottom is true:
|
|
|
|
//
|
|
|
|
// then the order of iteration is top to bottom, left to right.
|
|
|
|
//
|
|
|
|
// If topToBottom is false:
|
|
|
|
//
|
|
|
|
// then the order of iteration is bottom to top, right to left.
|
|
|
|
func (set *NodeSet) forEachWithOrder(topToBottom bool, callback func(path string, n *memoryNode)) {
|
|
|
|
var paths sort.StringSlice
|
|
|
|
for path := range set.nodes {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
if topToBottom {
|
|
|
|
paths.Sort()
|
|
|
|
} else {
|
|
|
|
sort.Sort(sort.Reverse(paths))
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
callback(path, set.nodes[path])
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
// markUpdated marks the node as dirty(newly-inserted or updated) with provided
|
|
|
|
// node path, node object along with its previous value.
|
2023-02-19 13:10:19 -06:00
|
|
|
func (set *NodeSet) markUpdated(path []byte, node *memoryNode) {
|
|
|
|
set.nodes[string(path)] = node
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// markDeleted marks the node as deleted with provided path and previous value.
|
2023-02-19 13:10:19 -06:00
|
|
|
// nolint: unused
|
|
|
|
func (set *NodeSet) markDeleted(path []byte) {
|
|
|
|
set.nodes[string(path)] = &memoryNode{}
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
// addLeaf collects the provided leaf node into set.
|
2022-08-04 03:03:20 -05:00
|
|
|
func (set *NodeSet) addLeaf(node *leaf) {
|
|
|
|
set.leaves = append(set.leaves, node)
|
|
|
|
}
|
|
|
|
|
2023-02-19 13:10:19 -06:00
|
|
|
// Size returns the number of dirty nodes contained in the set.
|
|
|
|
func (set *NodeSet) Size() int {
|
|
|
|
return len(set.nodes)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can
|
|
|
|
// we get rid of it?
|
|
|
|
func (set *NodeSet) Hashes() []common.Hash {
|
|
|
|
var ret []common.Hash
|
2023-02-19 13:10:19 -06:00
|
|
|
for _, node := range set.nodes {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
ret = append(ret, node.hash)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summary returns a string-representation of the NodeSet.
|
|
|
|
func (set *NodeSet) Summary() string {
|
|
|
|
var out = new(strings.Builder)
|
|
|
|
fmt.Fprintf(out, "nodeset owner: %v\n", set.owner)
|
2023-02-19 13:10:19 -06:00
|
|
|
if set.nodes != nil {
|
|
|
|
for path, n := range set.nodes {
|
|
|
|
// Deletion
|
|
|
|
if n.isDeleted() {
|
|
|
|
fmt.Fprintf(out, " [-]: %x\n", path)
|
|
|
|
continue
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
}
|
2023-02-19 13:10:19 -06:00
|
|
|
// Update
|
|
|
|
fmt.Fprintf(out, " [+]: %x -> %v\n", path, n.hash)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 03:01:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, n := range set.leaves {
|
|
|
|
fmt.Fprintf(out, "[leaf]: %v\n", n)
|
|
|
|
}
|
|
|
|
return out.String()
|
2022-08-04 03:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergedNodeSet represents a merged dirty node set for a group of tries.
|
|
|
|
type MergedNodeSet struct {
|
|
|
|
sets map[common.Hash]*NodeSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMergedNodeSet initializes an empty merged set.
|
|
|
|
func NewMergedNodeSet() *MergedNodeSet {
|
|
|
|
return &MergedNodeSet{sets: make(map[common.Hash]*NodeSet)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithNodeSet constructs a merged nodeset with the provided single set.
|
|
|
|
func NewWithNodeSet(set *NodeSet) *MergedNodeSet {
|
|
|
|
merged := NewMergedNodeSet()
|
|
|
|
merged.Merge(set)
|
|
|
|
return merged
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges the provided dirty nodes of a trie into the set. The assumption
|
|
|
|
// is held that no duplicated set belonging to the same trie will be merged twice.
|
|
|
|
func (set *MergedNodeSet) Merge(other *NodeSet) error {
|
|
|
|
_, present := set.sets[other.owner]
|
|
|
|
if present {
|
|
|
|
return fmt.Errorf("duplicate trie for owner %#x", other.owner)
|
|
|
|
}
|
|
|
|
set.sets[other.owner] = other
|
|
|
|
return nil
|
|
|
|
}
|