github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/trie/trie.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package trie implements Merkle Patricia Tries. 18 package trie 19 20 import ( 21 "bytes" 22 "fmt" 23 "sync" 24 25 "github.com/zhiqiangxu/go-ethereum/common" 26 "github.com/zhiqiangxu/go-ethereum/crypto" 27 "github.com/zhiqiangxu/go-ethereum/log" 28 ) 29 30 var ( 31 // emptyRoot is the known root hash of an empty trie. 32 emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 33 34 // emptyState is the known hash of an empty state trie entry. 35 emptyState = crypto.Keccak256Hash(nil) 36 ) 37 38 // LeafCallback is a callback type invoked when a trie operation reaches a leaf 39 // node. It's used by state sync and commit to allow handling external references 40 // between account and storage tries. 41 type LeafCallback func(leaf []byte, parent common.Hash) error 42 43 // Trie is a Merkle Patricia Trie. 44 // The zero value is an empty trie with no database. 45 // Use New to create a trie that sits on top of a database. 46 // 47 // Trie is not safe for concurrent use. 48 type Trie struct { 49 db *Database 50 root node 51 // Keep track of the number leafs which have been inserted since the last 52 // hashing operation. This number will not directly map to the number of 53 // actually unhashed nodes 54 unhashed int 55 } 56 57 // newFlag returns the cache flag value for a newly created node. 58 func (t *Trie) newFlag() nodeFlag { 59 return nodeFlag{dirty: true} 60 } 61 62 // New creates a trie with an existing root node from db. 63 // 64 // If root is the zero hash or the sha3 hash of an empty string, the 65 // trie is initially empty and does not require a database. Otherwise, 66 // New will panic if db is nil and returns a MissingNodeError if root does 67 // not exist in the database. Accessing the trie loads nodes from db on demand. 68 func New(root common.Hash, db *Database) (*Trie, error) { 69 if db == nil { 70 panic("trie.New called without a database") 71 } 72 trie := &Trie{ 73 db: db, 74 } 75 if root != (common.Hash{}) && root != emptyRoot { 76 rootnode, err := trie.resolveHash(root[:], nil) 77 if err != nil { 78 return nil, err 79 } 80 trie.root = rootnode 81 } 82 return trie, nil 83 } 84 85 // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at 86 // the key after the given start key. 87 func (t *Trie) NodeIterator(start []byte) NodeIterator { 88 return newNodeIterator(t, start) 89 } 90 91 // Get returns the value for key stored in the trie. 92 // The value bytes must not be modified by the caller. 93 func (t *Trie) Get(key []byte) []byte { 94 res, err := t.TryGet(key) 95 if err != nil { 96 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 97 } 98 return res 99 } 100 101 // TryGet returns the value for key stored in the trie. 102 // The value bytes must not be modified by the caller. 103 // If a node was not found in the database, a MissingNodeError is returned. 104 func (t *Trie) TryGet(key []byte) ([]byte, error) { 105 key = keybytesToHex(key) 106 value, newroot, didResolve, err := t.tryGet(t.root, key, 0) 107 if err == nil && didResolve { 108 t.root = newroot 109 } 110 return value, err 111 } 112 113 func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) { 114 switch n := (origNode).(type) { 115 case nil: 116 return nil, nil, false, nil 117 case valueNode: 118 return n, n, false, nil 119 case *shortNode: 120 if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) { 121 // key not found in trie 122 return nil, n, false, nil 123 } 124 value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key)) 125 if err == nil && didResolve { 126 n = n.copy() 127 n.Val = newnode 128 } 129 return value, n, didResolve, err 130 case *fullNode: 131 value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1) 132 if err == nil && didResolve { 133 n = n.copy() 134 n.Children[key[pos]] = newnode 135 } 136 return value, n, didResolve, err 137 case hashNode: 138 child, err := t.resolveHash(n, key[:pos]) 139 if err != nil { 140 return nil, n, true, err 141 } 142 value, newnode, _, err := t.tryGet(child, key, pos) 143 return value, newnode, true, err 144 default: 145 panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode)) 146 } 147 } 148 149 // Update associates key with value in the trie. Subsequent calls to 150 // Get will return value. If value has length zero, any existing value 151 // is deleted from the trie and calls to Get will return nil. 152 // 153 // The value bytes must not be modified by the caller while they are 154 // stored in the trie. 155 func (t *Trie) Update(key, value []byte) { 156 if err := t.TryUpdate(key, value); err != nil { 157 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 158 } 159 } 160 161 // TryUpdate associates key with value in the trie. Subsequent calls to 162 // Get will return value. If value has length zero, any existing value 163 // is deleted from the trie and calls to Get will return nil. 164 // 165 // The value bytes must not be modified by the caller while they are 166 // stored in the trie. 167 // 168 // If a node was not found in the database, a MissingNodeError is returned. 169 func (t *Trie) TryUpdate(key, value []byte) error { 170 t.unhashed++ 171 k := keybytesToHex(key) 172 if len(value) != 0 { 173 _, n, err := t.insert(t.root, nil, k, valueNode(value)) 174 if err != nil { 175 return err 176 } 177 t.root = n 178 } else { 179 _, n, err := t.delete(t.root, nil, k) 180 if err != nil { 181 return err 182 } 183 t.root = n 184 } 185 return nil 186 } 187 188 func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { 189 if len(key) == 0 { 190 if v, ok := n.(valueNode); ok { 191 return !bytes.Equal(v, value.(valueNode)), value, nil 192 } 193 return true, value, nil 194 } 195 switch n := n.(type) { 196 case *shortNode: 197 matchlen := prefixLen(key, n.Key) 198 // If the whole key matches, keep this short node as is 199 // and only update the value. 200 if matchlen == len(n.Key) { 201 dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value) 202 if !dirty || err != nil { 203 return false, n, err 204 } 205 return true, &shortNode{n.Key, nn, t.newFlag()}, nil 206 } 207 // Otherwise branch out at the index where they differ. 208 branch := &fullNode{flags: t.newFlag()} 209 var err error 210 _, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val) 211 if err != nil { 212 return false, nil, err 213 } 214 _, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value) 215 if err != nil { 216 return false, nil, err 217 } 218 // Replace this shortNode with the branch if it occurs at index 0. 219 if matchlen == 0 { 220 return true, branch, nil 221 } 222 // Otherwise, replace it with a short node leading up to the branch. 223 return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil 224 225 case *fullNode: 226 dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value) 227 if !dirty || err != nil { 228 return false, n, err 229 } 230 n = n.copy() 231 n.flags = t.newFlag() 232 n.Children[key[0]] = nn 233 return true, n, nil 234 235 case nil: 236 return true, &shortNode{key, value, t.newFlag()}, nil 237 238 case hashNode: 239 // We've hit a part of the trie that isn't loaded yet. Load 240 // the node and insert into it. This leaves all child nodes on 241 // the path to the value in the trie. 242 rn, err := t.resolveHash(n, prefix) 243 if err != nil { 244 return false, nil, err 245 } 246 dirty, nn, err := t.insert(rn, prefix, key, value) 247 if !dirty || err != nil { 248 return false, rn, err 249 } 250 return true, nn, nil 251 252 default: 253 panic(fmt.Sprintf("%T: invalid node: %v", n, n)) 254 } 255 } 256 257 // Delete removes any existing value for key from the trie. 258 func (t *Trie) Delete(key []byte) { 259 if err := t.TryDelete(key); err != nil { 260 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 261 } 262 } 263 264 // TryDelete removes any existing value for key from the trie. 265 // If a node was not found in the database, a MissingNodeError is returned. 266 func (t *Trie) TryDelete(key []byte) error { 267 t.unhashed++ 268 k := keybytesToHex(key) 269 _, n, err := t.delete(t.root, nil, k) 270 if err != nil { 271 return err 272 } 273 t.root = n 274 return nil 275 } 276 277 // delete returns the new root of the trie with key deleted. 278 // It reduces the trie to minimal form by simplifying 279 // nodes on the way up after deleting recursively. 280 func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { 281 switch n := n.(type) { 282 case *shortNode: 283 matchlen := prefixLen(key, n.Key) 284 if matchlen < len(n.Key) { 285 return false, n, nil // don't replace n on mismatch 286 } 287 if matchlen == len(key) { 288 return true, nil, nil // remove n entirely for whole matches 289 } 290 // The key is longer than n.Key. Remove the remaining suffix 291 // from the subtrie. Child can never be nil here since the 292 // subtrie must contain at least two other values with keys 293 // longer than n.Key. 294 dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):]) 295 if !dirty || err != nil { 296 return false, n, err 297 } 298 switch child := child.(type) { 299 case *shortNode: 300 // Deleting from the subtrie reduced it to another 301 // short node. Merge the nodes to avoid creating a 302 // shortNode{..., shortNode{...}}. Use concat (which 303 // always creates a new slice) instead of append to 304 // avoid modifying n.Key since it might be shared with 305 // other nodes. 306 return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil 307 default: 308 return true, &shortNode{n.Key, child, t.newFlag()}, nil 309 } 310 311 case *fullNode: 312 dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:]) 313 if !dirty || err != nil { 314 return false, n, err 315 } 316 n = n.copy() 317 n.flags = t.newFlag() 318 n.Children[key[0]] = nn 319 320 // Check how many non-nil entries are left after deleting and 321 // reduce the full node to a short node if only one entry is 322 // left. Since n must've contained at least two children 323 // before deletion (otherwise it would not be a full node) n 324 // can never be reduced to nil. 325 // 326 // When the loop is done, pos contains the index of the single 327 // value that is left in n or -2 if n contains at least two 328 // values. 329 pos := -1 330 for i, cld := range &n.Children { 331 if cld != nil { 332 if pos == -1 { 333 pos = i 334 } else { 335 pos = -2 336 break 337 } 338 } 339 } 340 if pos >= 0 { 341 if pos != 16 { 342 // If the remaining entry is a short node, it replaces 343 // n and its key gets the missing nibble tacked to the 344 // front. This avoids creating an invalid 345 // shortNode{..., shortNode{...}}. Since the entry 346 // might not be loaded yet, resolve it just for this 347 // check. 348 cnode, err := t.resolve(n.Children[pos], prefix) 349 if err != nil { 350 return false, nil, err 351 } 352 if cnode, ok := cnode.(*shortNode); ok { 353 k := append([]byte{byte(pos)}, cnode.Key...) 354 return true, &shortNode{k, cnode.Val, t.newFlag()}, nil 355 } 356 } 357 // Otherwise, n is replaced by a one-nibble short node 358 // containing the child. 359 return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil 360 } 361 // n still contains at least two values and cannot be reduced. 362 return true, n, nil 363 364 case valueNode: 365 return true, nil, nil 366 367 case nil: 368 return false, nil, nil 369 370 case hashNode: 371 // We've hit a part of the trie that isn't loaded yet. Load 372 // the node and delete from it. This leaves all child nodes on 373 // the path to the value in the trie. 374 rn, err := t.resolveHash(n, prefix) 375 if err != nil { 376 return false, nil, err 377 } 378 dirty, nn, err := t.delete(rn, prefix, key) 379 if !dirty || err != nil { 380 return false, rn, err 381 } 382 return true, nn, nil 383 384 default: 385 panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key)) 386 } 387 } 388 389 func concat(s1 []byte, s2 ...byte) []byte { 390 r := make([]byte, len(s1)+len(s2)) 391 copy(r, s1) 392 copy(r[len(s1):], s2) 393 return r 394 } 395 396 func (t *Trie) resolve(n node, prefix []byte) (node, error) { 397 if n, ok := n.(hashNode); ok { 398 return t.resolveHash(n, prefix) 399 } 400 return n, nil 401 } 402 403 func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { 404 hash := common.BytesToHash(n) 405 if node := t.db.node(hash); node != nil { 406 return node, nil 407 } 408 return nil, &MissingNodeError{NodeHash: hash, Path: prefix} 409 } 410 411 // Hash returns the root hash of the trie. It does not write to the 412 // database and can be used even if the trie doesn't have one. 413 func (t *Trie) Hash() common.Hash { 414 hash, cached, _ := t.hashRoot(nil) 415 t.root = cached 416 return common.BytesToHash(hash.(hashNode)) 417 } 418 419 // Commit writes all nodes to the trie's memory database, tracking the internal 420 // and external (for account tries) references. 421 func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) { 422 if t.db == nil { 423 panic("commit called on trie with nil database") 424 } 425 if t.root == nil { 426 return emptyRoot, nil 427 } 428 rootHash := t.Hash() 429 h := newCommitter() 430 defer returnCommitterToPool(h) 431 // Do a quick check if we really need to commit, before we spin 432 // up goroutines. This can happen e.g. if we load a trie for reading storage 433 // values, but don't write to it. 434 if !h.commitNeeded(t.root) { 435 return rootHash, nil 436 } 437 var wg sync.WaitGroup 438 if onleaf != nil { 439 h.onleaf = onleaf 440 h.leafCh = make(chan *leaf, leafChanSize) 441 wg.Add(1) 442 go func() { 443 defer wg.Done() 444 h.commitLoop(t.db) 445 }() 446 } 447 var newRoot hashNode 448 newRoot, err = h.Commit(t.root, t.db) 449 if onleaf != nil { 450 // The leafch is created in newCommitter if there was an onleaf callback 451 // provided. The commitLoop only _reads_ from it, and the commit 452 // operation was the sole writer. Therefore, it's safe to close this 453 // channel here. 454 close(h.leafCh) 455 wg.Wait() 456 } 457 if err != nil { 458 return common.Hash{}, err 459 } 460 t.root = newRoot 461 return rootHash, nil 462 } 463 464 // hashRoot calculates the root hash of the given trie 465 func (t *Trie) hashRoot(db *Database) (node, node, error) { 466 if t.root == nil { 467 return hashNode(emptyRoot.Bytes()), nil, nil 468 } 469 // If the number of changes is below 100, we let one thread handle it 470 h := newHasher(t.unhashed >= 100) 471 defer returnHasherToPool(h) 472 hashed, cached := h.hash(t.root, true) 473 t.unhashed = 0 474 return hashed, cached, nil 475 }