@@ -22,6 +22,7 @@ import (
22
22
"sync"
23
23
24
24
"github.com/ethereum/go-ethereum/common"
25
+ "github.com/ethereum/go-ethereum/common/hexutil"
25
26
"github.com/ethereum/go-ethereum/common/prque"
26
27
"github.com/ethereum/go-ethereum/core/rawdb"
27
28
"github.com/ethereum/go-ethereum/core/types"
@@ -149,15 +150,42 @@ type CodeSyncResult struct {
149
150
// nodeOp represents an operation upon the trie node. It can either represent a
150
151
// deletion to the specific node or a node write for persisting retrieved node.
151
152
type nodeOp struct {
153
+ del bool // flag if op stands for a delete operation
152
154
owner common.Hash // identifier of the trie (empty for account trie)
153
155
path []byte // path from the root to the specified node.
154
156
blob []byte // the content of the node (nil for deletion)
155
157
hash common.Hash // hash of the node content (empty for node deletion)
156
158
}
157
159
158
160
// isDelete indicates if the operation is a database deletion.
159
- func (op * nodeOp ) isDelete () bool {
160
- return len (op .blob ) == 0
161
+ func (op * nodeOp ) valid () bool {
162
+ if op .del && len (op .blob ) != 0 {
163
+ return false
164
+ }
165
+ if ! op .del && len (op .blob ) == 0 {
166
+ return false
167
+ }
168
+ return true
169
+ }
170
+
171
+ // string returns the node operation in string representation.
172
+ func (op * nodeOp ) string () string {
173
+ var node string
174
+ if op .owner == (common.Hash {}) {
175
+ node = fmt .Sprintf ("node: (%v)" , op .path )
176
+ } else {
177
+ node = fmt .Sprintf ("node: (%x-%v)" , op .owner , op .path )
178
+ }
179
+ var blobHex string
180
+ if len (op .blob ) == 0 {
181
+ blobHex = "nil"
182
+ } else {
183
+ blobHex = hexutil .Encode (op .blob )
184
+ }
185
+ if op .del {
186
+ return fmt .Sprintf ("del %s %s %s" , node , blobHex , op .hash .Hex ())
187
+ }
188
+ return fmt .Sprintf ("write %s %s %s" , node , blobHex , op .hash .Hex ())
161
189
}
162
190
163
191
// syncMemBatch is an in-memory buffer of successfully downloaded but not yet
@@ -220,6 +248,7 @@ func (batch *syncMemBatch) delNode(owner common.Hash, path []byte) {
220
248
batch .size += common .HashLength + uint64 (len (path ))
221
249
}
222
250
batch .nodes = append (batch .nodes , nodeOp {
251
+ del : true ,
223
252
owner : owner ,
224
253
path : path ,
225
254
})
@@ -428,7 +457,10 @@ func (s *Sync) Commit(dbw ethdb.Batch) error {
428
457
storage int
429
458
)
430
459
for _ , op := range s .membatch .nodes {
431
- if op .isDelete () {
460
+ if ! op .valid () {
461
+ return fmt .Errorf ("invalid op, %s" , op .string ())
462
+ }
463
+ if op .del {
432
464
// node deletion is only supported in path mode.
433
465
if op .owner == (common.Hash {}) {
434
466
rawdb .DeleteAccountTrieNode (dbw , op .path )
0 commit comments