github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/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 "errors" 23 "fmt" 24 "sync" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/crypto" 28 "github.com/ethereum/go-ethereum/log" 29 ) 30 31 var ( 32 // emptyRoot is the known root hash of an empty trie. 33 emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 34 35 // emptyState is the known hash of an empty state trie entry. 36 emptyState = crypto.Keccak256Hash(nil) 37 ) 38 39 // LeafCallback is a callback type invoked when a trie operation reaches a leaf 40 // node. It's used by state sync and commit to allow handling external references 41 // between account and storage tries. 42 type LeafCallback func(path []byte, leaf []byte, parent common.Hash) error 43 44 // Trie is a Merkle Patricia Trie. 45 // The zero value is an empty trie with no database. 46 // Use New to create a trie that sits on top of a database. 47 // 48 // Trie is not safe for concurrent use. 49 type Trie struct { 50 db *Database 51 root node 52 // Keep track of the number leafs which have been inserted since the last 53 // hashing operation. This number will not directly map to the number of 54 // actually unhashed nodes 55 unhashed int 56 } 57 58 // newFlag returns the cache flag value for a newly created node. 59 func (t *Trie) newFlag() nodeFlag { 60 return nodeFlag{dirty: true} 61 } 62 63 // New creates a trie with an existing root node from db. 64 // 65 // If root is the zero hash or the sha3 hash of an empty string, the 66 // trie is initially empty and does not require a database. Otherwise, 67 // New will panic if db is nil and returns a MissingNodeError if root does 68 // not exist in the database. Accessing the trie loads nodes from db on demand. 69 func New(root common.Hash, db *Database) (*Trie, error) { 70 if db == nil { 71 panic("trie.New called without a database") 72 } 73 trie := &Trie{ 74 db: db, 75 } 76 if root != (common.Hash{}) && root != emptyRoot { 77 rootnode, err := trie.resolveHash(root[:], nil) 78 if err != nil { 79 return nil, err 80 } 81 trie.root = rootnode 82 } 83 return trie, nil 84 } 85 86 // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at 87 // the key after the given start key. 88 func (t *Trie) NodeIterator(start []byte) NodeIterator { 89 return newNodeIterator(t, start) 90 } 91 92 // Get returns the value for key stored in the trie. 93 // The value bytes must not be modified by the caller. 94 func (t *Trie) Get(key []byte) []byte { 95 res, err := t.TryGet(key) 96 if err != nil { 97 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 98 } 99 return res 100 } 101 102 // TryGet returns the value for key stored in the trie. 103 // The value bytes must not be modified by the caller. 104 // If a node was not found in the database, a MissingNodeError is returned. 105 func (t *Trie) TryGet(key []byte) ([]byte, error) { 106 value, newroot, didResolve, err := t.tryGet(t.root, keybytesToHex(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 // TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not 150 // possible to use keybyte-encoding as the path might contain odd nibbles. 151 func (t *Trie) TryGetNode(path []byte) ([]byte, int, error) { 152 item, newroot, resolved, err := t.tryGetNode(t.root, compactToHex(path), 0) 153 if err != nil { 154 return nil, resolved, err 155 } 156 if resolved > 0 { 157 t.root = newroot 158 } 159 if item == nil { 160 return nil, resolved, nil 161 } 162 return item, resolved, err 163 } 164 165 func (t *Trie) tryGetNode(origNode node, path []byte, pos int) (item []byte, newnode node, resolved int, err error) { 166 // If we reached the requested path, return the current node 167 if pos >= len(path) { 168 // Although we most probably have the original node expanded, encoding 169 // that into consensus form can be nasty (needs to cascade down) and 170 // time consuming. Instead, just pull the hash up from disk directly. 171 var hash hashNode 172 if node, ok := origNode.(hashNode); ok { 173 hash = node 174 } else { 175 hash, _ = origNode.cache() 176 } 177 if hash == nil { 178 return nil, origNode, 0, errors.New("non-consensus node") 179 } 180 blob, err := t.db.Node(common.BytesToHash(hash)) 181 return blob, origNode, 1, err 182 } 183 // Path still needs to be traversed, descend into children 184 switch n := (origNode).(type) { 185 case nil: 186 // Non-existent path requested, abort 187 return nil, nil, 0, nil 188 189 case valueNode: 190 // Path prematurely ended, abort 191 return nil, nil, 0, nil 192 193 case *shortNode: 194 if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) { 195 // Path branches off from short node 196 return nil, n, 0, nil 197 } 198 item, newnode, resolved, err = t.tryGetNode(n.Val, path, pos+len(n.Key)) 199 if err == nil && resolved > 0 { 200 n = n.copy() 201 n.Val = newnode 202 } 203 return item, n, resolved, err 204 205 case *fullNode: 206 item, newnode, resolved, err = t.tryGetNode(n.Children[path[pos]], path, pos+1) 207 if err == nil && resolved > 0 { 208 n = n.copy() 209 n.Children[path[pos]] = newnode 210 } 211 return item, n, resolved, err 212 213 case hashNode: 214 child, err := t.resolveHash(n, path[:pos]) 215 if err != nil { 216 return nil, n, 1, err 217 } 218 item, newnode, resolved, err := t.tryGetNode(child, path, pos) 219 return item, newnode, resolved + 1, err 220 221 default: 222 panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode)) 223 } 224 } 225 226 // Update associates key with value in the trie. Subsequent calls to 227 // Get will return value. If value has length zero, any existing value 228 // is deleted from the trie and calls to Get will return nil. 229 // 230 // The value bytes must not be modified by the caller while they are 231 // stored in the trie. 232 func (t *Trie) Update(key, value []byte) { 233 if err := t.TryUpdate(key, value); err != nil { 234 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 235 } 236 } 237 238 // TryUpdate associates key with value in the trie. Subsequent calls to 239 // Get will return value. If value has length zero, any existing value 240 // is deleted from the trie and calls to Get will return nil. 241 // 242 // The value bytes must not be modified by the caller while they are 243 // stored in the trie. 244 // 245 // If a node was not found in the database, a MissingNodeError is returned. 246 func (t *Trie) TryUpdate(key, value []byte) error { 247 t.unhashed++ 248 k := keybytesToHex(key) 249 if len(value) != 0 { 250 _, n, err := t.insert(t.root, nil, k, valueNode(value)) 251 if err != nil { 252 return err 253 } 254 t.root = n 255 } else { 256 _, n, err := t.delete(t.root, nil, k) 257 if err != nil { 258 return err 259 } 260 t.root = n 261 } 262 return nil 263 } 264 265 func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { 266 if len(key) == 0 { 267 if v, ok := n.(valueNode); ok { 268 return !bytes.Equal(v, value.(valueNode)), value, nil 269 } 270 return true, value, nil 271 } 272 switch n := n.(type) { 273 case *shortNode: 274 matchlen := prefixLen(key, n.Key) 275 // If the whole key matches, keep this short node as is 276 // and only update the value. 277 if matchlen == len(n.Key) { 278 dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value) 279 if !dirty || err != nil { 280 return false, n, err 281 } 282 return true, &shortNode{n.Key, nn, t.newFlag()}, nil 283 } 284 // Otherwise branch out at the index where they differ. 285 branch := &fullNode{flags: t.newFlag()} 286 var err error 287 _, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val) 288 if err != nil { 289 return false, nil, err 290 } 291 _, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value) 292 if err != nil { 293 return false, nil, err 294 } 295 // Replace this shortNode with the branch if it occurs at index 0. 296 if matchlen == 0 { 297 return true, branch, nil 298 } 299 // Otherwise, replace it with a short node leading up to the branch. 300 return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil 301 302 case *fullNode: 303 dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value) 304 if !dirty || err != nil { 305 return false, n, err 306 } 307 n = n.copy() 308 n.flags = t.newFlag() 309 n.Children[key[0]] = nn 310 return true, n, nil 311 312 case nil: 313 return true, &shortNode{key, value, t.newFlag()}, nil 314 315 case hashNode: 316 // We've hit a part of the trie that isn't loaded yet. Load 317 // the node and insert into it. This leaves all child nodes on 318 // the path to the value in the trie. 319 rn, err := t.resolveHash(n, prefix) 320 if err != nil { 321 return false, nil, err 322 } 323 dirty, nn, err := t.insert(rn, prefix, key, value) 324 if !dirty || err != nil { 325 return false, rn, err 326 } 327 return true, nn, nil 328 329 default: 330 panic(fmt.Sprintf("%T: invalid node: %v", n, n)) 331 } 332 } 333 334 // Delete removes any existing value for key from the trie. 335 func (t *Trie) Delete(key []byte) { 336 if err := t.TryDelete(key); err != nil { 337 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 338 } 339 } 340 341 // TryDelete removes any existing value for key from the trie. 342 // If a node was not found in the database, a MissingNodeError is returned. 343 func (t *Trie) TryDelete(key []byte) error { 344 t.unhashed++ 345 k := keybytesToHex(key) 346 _, n, err := t.delete(t.root, nil, k) 347 if err != nil { 348 return err 349 } 350 t.root = n 351 return nil 352 } 353 354 // delete returns the new root of the trie with key deleted. 355 // It reduces the trie to minimal form by simplifying 356 // nodes on the way up after deleting recursively. 357 func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { 358 switch n := n.(type) { 359 case *shortNode: 360 matchlen := prefixLen(key, n.Key) 361 if matchlen < len(n.Key) { 362 return false, n, nil // don't replace n on mismatch 363 } 364 if matchlen == len(key) { 365 return true, nil, nil // remove n entirely for whole matches 366 } 367 // The key is longer than n.Key. Remove the remaining suffix 368 // from the subtrie. Child can never be nil here since the 369 // subtrie must contain at least two other values with keys 370 // longer than n.Key. 371 dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):]) 372 if !dirty || err != nil { 373 return false, n, err 374 } 375 switch child := child.(type) { 376 case *shortNode: 377 // Deleting from the subtrie reduced it to another 378 // short node. Merge the nodes to avoid creating a 379 // shortNode{..., shortNode{...}}. Use concat (which 380 // always creates a new slice) instead of append to 381 // avoid modifying n.Key since it might be shared with 382 // other nodes. 383 return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil 384 default: 385 return true, &shortNode{n.Key, child, t.newFlag()}, nil 386 } 387 388 case *fullNode: 389 dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:]) 390 if !dirty || err != nil { 391 return false, n, err 392 } 393 n = n.copy() 394 n.flags = t.newFlag() 395 n.Children[key[0]] = nn 396 397 // Check how many non-nil entries are left after deleting and 398 // reduce the full node to a short node if only one entry is 399 // left. Since n must've contained at least two children 400 // before deletion (otherwise it would not be a full node) n 401 // can never be reduced to nil. 402 // 403 // When the loop is done, pos contains the index of the single 404 // value that is left in n or -2 if n contains at least two 405 // values. 406 pos := -1 407 for i, cld := range &n.Children { 408 if cld != nil { 409 if pos == -1 { 410 pos = i 411 } else { 412 pos = -2 413 break 414 } 415 } 416 } 417 if pos >= 0 { 418 if pos != 16 { 419 // If the remaining entry is a short node, it replaces 420 // n and its key gets the missing nibble tacked to the 421 // front. This avoids creating an invalid 422 // shortNode{..., shortNode{...}}. Since the entry 423 // might not be loaded yet, resolve it just for this 424 // check. 425 cnode, err := t.resolve(n.Children[pos], prefix) 426 if err != nil { 427 return false, nil, err 428 } 429 if cnode, ok := cnode.(*shortNode); ok { 430 k := append([]byte{byte(pos)}, cnode.Key...) 431 return true, &shortNode{k, cnode.Val, t.newFlag()}, nil 432 } 433 } 434 // Otherwise, n is replaced by a one-nibble short node 435 // containing the child. 436 return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil 437 } 438 // n still contains at least two values and cannot be reduced. 439 return true, n, nil 440 441 case valueNode: 442 return true, nil, nil 443 444 case nil: 445 return false, nil, nil 446 447 case hashNode: 448 // We've hit a part of the trie that isn't loaded yet. Load 449 // the node and delete from it. This leaves all child nodes on 450 // the path to the value in the trie. 451 rn, err := t.resolveHash(n, prefix) 452 if err != nil { 453 return false, nil, err 454 } 455 dirty, nn, err := t.delete(rn, prefix, key) 456 if !dirty || err != nil { 457 return false, rn, err 458 } 459 return true, nn, nil 460 461 default: 462 panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key)) 463 } 464 } 465 466 func concat(s1 []byte, s2 ...byte) []byte { 467 r := make([]byte, len(s1)+len(s2)) 468 copy(r, s1) 469 copy(r[len(s1):], s2) 470 return r 471 } 472 473 func (t *Trie) resolve(n node, prefix []byte) (node, error) { 474 if n, ok := n.(hashNode); ok { 475 return t.resolveHash(n, prefix) 476 } 477 return n, nil 478 } 479 480 func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { 481 hash := common.BytesToHash(n) 482 if node := t.db.node(hash); node != nil { 483 return node, nil 484 } 485 return nil, &MissingNodeError{NodeHash: hash, Path: prefix} 486 } 487 488 // Hash returns the root hash of the trie. It does not write to the 489 // database and can be used even if the trie doesn't have one. 490 func (t *Trie) Hash() common.Hash { 491 hash, cached, _ := t.hashRoot() 492 t.root = cached 493 return common.BytesToHash(hash.(hashNode)) 494 } 495 496 // Commit writes all nodes to the trie's memory database, tracking the internal 497 // and external (for account tries) references. 498 func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) { 499 if t.db == nil { 500 panic("commit called on trie with nil database") 501 } 502 if t.root == nil { 503 return emptyRoot, nil 504 } 505 // Derive the hash for all dirty nodes first. We hold the assumption 506 // in the following procedure that all nodes are hashed. 507 rootHash := t.Hash() 508 h := newCommitter() 509 defer returnCommitterToPool(h) 510 511 // Do a quick check if we really need to commit, before we spin 512 // up goroutines. This can happen e.g. if we load a trie for reading storage 513 // values, but don't write to it. 514 if _, dirty := t.root.cache(); !dirty { 515 return rootHash, nil 516 } 517 var wg sync.WaitGroup 518 if onleaf != nil { 519 h.onleaf = onleaf 520 h.leafCh = make(chan *leaf, leafChanSize) 521 wg.Add(1) 522 go func() { 523 defer wg.Done() 524 h.commitLoop(t.db) 525 }() 526 } 527 var newRoot hashNode 528 newRoot, err = h.Commit(t.root, t.db) 529 if onleaf != nil { 530 // The leafch is created in newCommitter if there was an onleaf callback 531 // provided. The commitLoop only _reads_ from it, and the commit 532 // operation was the sole writer. Therefore, it's safe to close this 533 // channel here. 534 close(h.leafCh) 535 wg.Wait() 536 } 537 if err != nil { 538 return common.Hash{}, err 539 } 540 t.root = newRoot 541 return rootHash, nil 542 } 543 544 // hashRoot calculates the root hash of the given trie 545 func (t *Trie) hashRoot() (node, node, error) { 546 if t.root == nil { 547 return hashNode(emptyRoot.Bytes()), nil, nil 548 } 549 // If the number of changes is below 100, we let one thread handle it 550 h := newHasher(t.unhashed >= 100) 551 defer returnHasherToPool(h) 552 hashed, cached := h.hash(t.root, true) 553 t.unhashed = 0 554 return hashed, cached, nil 555 } 556 557 // Reset drops the referenced root node and cleans all internal state. 558 func (t *Trie) Reset() { 559 t.root = nil 560 t.unhashed = 0 561 }