@@ -22,113 +22,59 @@ import (
22
22
"strings"
23
23
24
24
"github.com/ethereum/go-ethereum/common"
25
+ "github.com/ethereum/go-ethereum/trie/trienode"
25
26
)
26
27
27
- // memoryNode is all the information we know about a single cached trie node
28
- // in the memory.
29
- type memoryNode struct {
30
- hash common.Hash // Node hash by hashing node blob, empty for deleted nodes
31
- node []byte // Encoded node blob, nil for deleted nodes
32
- }
33
-
34
- // memorySize returns the total memory size used by this node.
35
- // nolint:unused
36
- func (n * memoryNode ) memorySize (pathlen int ) int {
37
- return len (n .node ) + common .HashLength + pathlen
38
- }
39
-
40
- // rlp returns the raw rlp encoded blob of the cached trie node, either directly
41
- // from the cache, or by regenerating it from the collapsed node.
42
- // nolint:unused
43
- func (n * memoryNode ) rlp () []byte {
44
- return n .node
45
- }
46
-
47
- // obj returns the decoded and expanded trie node, either directly from the cache,
48
- // or by regenerating it from the rlp encoded blob.
49
- // nolint:unused
50
- func (n * memoryNode ) obj () node {
51
- return mustDecodeNode (n .hash [:], n .node )
52
- }
53
-
54
- // isDeleted returns the indicator if the node is marked as deleted.
55
- func (n * memoryNode ) isDeleted () bool {
56
- return n .hash == (common.Hash {})
57
- }
58
-
59
- // nodeWithPrev wraps the memoryNode with the previous node value.
60
- // nolint: unused
61
- type nodeWithPrev struct {
62
- * memoryNode
63
- prev []byte // RLP-encoded previous value, nil means it's non-existent
64
- }
65
-
66
- // unwrap returns the internal memoryNode object.
67
- // nolint:unused
68
- func (n * nodeWithPrev ) unwrap () * memoryNode {
69
- return n .memoryNode
70
- }
71
-
72
- // memorySize returns the total memory size used by this node. It overloads
73
- // the function in memoryNode by counting the size of previous value as well.
74
- // nolint: unused
75
- func (n * nodeWithPrev ) memorySize (pathlen int ) int {
76
- return n .memoryNode .memorySize (pathlen ) + len (n .prev )
77
- }
78
-
79
28
// NodeSet contains all dirty nodes collected during the commit operation.
80
29
// Each node is keyed by path. It's not thread-safe to use.
81
30
type NodeSet struct {
82
- owner common.Hash // the identifier of the trie
83
- nodes map [ string ] * memoryNode // the set of dirty nodes(inserted, updated, deleted)
84
- leaves [] * leaf // the list of dirty leaves
85
- updates int // the count of updated and inserted nodes
86
- deletes int // the count of deleted nodes
87
-
88
- // The list of accessed nodes, which records the original node value.
89
- // The origin value is expected to be nil for newly inserted node
90
- // and is expected to be non-nil for other types(updated, deleted) .
91
- accessList map [string ][] byte
31
+ owner common.Hash // the identifier of the trie
32
+ leaves [] * leaf // the list of dirty leaves
33
+ updates int // the count of updated and inserted nodes
34
+ deletes int // the count of deleted nodes
35
+
36
+ // The set of all dirty nodes. Dirty nodes include newly inserted nodes,
37
+ // deleted nodes and updated nodes. The original value of the newly
38
+ // inserted node must be nil, and the original value of the other two
39
+ // types must be non-nil.
40
+ nodes map [string ]* trienode. WithPrev
92
41
}
93
42
94
43
// NewNodeSet initializes an empty node set to be used for tracking dirty nodes
95
44
// from a specific account or storage trie. The owner is zero for the account
96
45
// trie and the owning account address hash for storage tries.
97
- func NewNodeSet (owner common.Hash , accessList map [ string ][] byte ) * NodeSet {
46
+ func NewNodeSet (owner common.Hash ) * NodeSet {
98
47
return & NodeSet {
99
- owner : owner ,
100
- nodes : make (map [string ]* memoryNode ),
101
- accessList : accessList ,
48
+ owner : owner ,
49
+ nodes : make (map [string ]* trienode.WithPrev ),
102
50
}
103
51
}
104
52
105
53
// forEachWithOrder iterates the dirty nodes with the order from bottom to top,
106
54
// right to left, nodes with the longest path will be iterated first.
107
- func (set * NodeSet ) forEachWithOrder (callback func (path string , n * memoryNode )) {
55
+ func (set * NodeSet ) forEachWithOrder (callback func (path string , n * trienode. Node )) {
108
56
var paths sort.StringSlice
109
57
for path := range set .nodes {
110
58
paths = append (paths , path )
111
59
}
112
60
// Bottom-up, longest path first
113
61
sort .Sort (sort .Reverse (paths ))
114
62
for _ , path := range paths {
115
- callback (path , set .nodes [path ])
63
+ callback (path , set .nodes [path ]. Unwrap () )
116
64
}
117
65
}
118
66
119
- // markUpdated marks the node as dirty(newly-inserted or updated).
120
- func (set * NodeSet ) markUpdated (path []byte , node * memoryNode ) {
121
- set .nodes [string (path )] = node
122
- set .updates += 1
123
- }
124
-
125
- // markDeleted marks the node as deleted.
126
- func (set * NodeSet ) markDeleted (path []byte ) {
127
- set .nodes [string (path )] = & memoryNode {}
128
- set .deletes += 1
67
+ // addNode adds the provided dirty node into set.
68
+ func (set * NodeSet ) addNode (path []byte , n * trienode.WithPrev ) {
69
+ if n .IsDeleted () {
70
+ set .deletes += 1
71
+ } else {
72
+ set .updates += 1
73
+ }
74
+ set .nodes [string (path )] = n
129
75
}
130
76
131
- // addLeaf collects the provided leaf node into set.
77
+ // addLeaf adds the provided leaf node into set.
132
78
func (set * NodeSet ) addLeaf (node * leaf ) {
133
79
set .leaves = append (set .leaves , node )
134
80
}
@@ -143,7 +89,7 @@ func (set *NodeSet) Size() (int, int) {
143
89
func (set * NodeSet ) Hashes () []common.Hash {
144
90
var ret []common.Hash
145
91
for _ , node := range set .nodes {
146
- ret = append (ret , node .hash )
92
+ ret = append (ret , node .Hash )
147
93
}
148
94
return ret
149
95
}
@@ -155,18 +101,17 @@ func (set *NodeSet) Summary() string {
155
101
if set .nodes != nil {
156
102
for path , n := range set .nodes {
157
103
// Deletion
158
- if n .isDeleted () {
159
- fmt .Fprintf (out , " [-]: %x prev: %x\n " , path , set . accessList [ path ] )
104
+ if n .IsDeleted () {
105
+ fmt .Fprintf (out , " [-]: %x prev: %x\n " , path , n . Prev )
160
106
continue
161
107
}
162
108
// Insertion
163
- origin , ok := set .accessList [path ]
164
- if ! ok {
165
- fmt .Fprintf (out , " [+]: %x -> %v\n " , path , n .hash )
109
+ if len (n .Prev ) == 0 {
110
+ fmt .Fprintf (out , " [+]: %x -> %v\n " , path , n .Hash )
166
111
continue
167
112
}
168
113
// Update
169
- fmt .Fprintf (out , " [*]: %x -> %v prev: %x\n " , path , n .hash , origin )
114
+ fmt .Fprintf (out , " [*]: %x -> %v prev: %x\n " , path , n .Hash , n . Prev )
170
115
}
171
116
}
172
117
for _ , n := range set .leaves {
0 commit comments