github.com/jimmyx0x/go-ethereum@v1.10.28/core/state/statedb.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 state provides a caching layer atop the Ethereum state trie. 18 package state 19 20 import ( 21 "errors" 22 "fmt" 23 "math/big" 24 "sort" 25 "time" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core/rawdb" 29 "github.com/ethereum/go-ethereum/core/state/snapshot" 30 "github.com/ethereum/go-ethereum/core/types" 31 "github.com/ethereum/go-ethereum/crypto" 32 "github.com/ethereum/go-ethereum/log" 33 "github.com/ethereum/go-ethereum/metrics" 34 "github.com/ethereum/go-ethereum/params" 35 "github.com/ethereum/go-ethereum/rlp" 36 "github.com/ethereum/go-ethereum/trie" 37 ) 38 39 type revision struct { 40 id int 41 journalIndex int 42 } 43 44 var ( 45 // emptyRoot is the known root hash of an empty trie. 46 emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 47 ) 48 49 type proofList [][]byte 50 51 func (n *proofList) Put(key []byte, value []byte) error { 52 *n = append(*n, value) 53 return nil 54 } 55 56 func (n *proofList) Delete(key []byte) error { 57 panic("not supported") 58 } 59 60 // StateDB structs within the ethereum protocol are used to store anything 61 // within the merkle trie. StateDBs take care of caching and storing 62 // nested states. It's the general query interface to retrieve: 63 // * Contracts 64 // * Accounts 65 type StateDB struct { 66 db Database 67 prefetcher *triePrefetcher 68 trie Trie 69 hasher crypto.KeccakState 70 71 // originalRoot is the pre-state root, before any changes were made. 72 // It will be updated when the Commit is called. 73 originalRoot common.Hash 74 75 snaps *snapshot.Tree 76 snap snapshot.Snapshot 77 snapAccounts map[common.Hash][]byte 78 snapStorage map[common.Hash]map[common.Hash][]byte 79 80 // This map holds 'live' objects, which will get modified while processing a state transition. 81 stateObjects map[common.Address]*stateObject 82 stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie 83 stateObjectsDirty map[common.Address]struct{} // State objects modified in the current execution 84 stateObjectsDestruct map[common.Address]struct{} // State objects destructed in the block 85 86 // DB error. 87 // State objects are used by the consensus core and VM which are 88 // unable to deal with database-level errors. Any error that occurs 89 // during a database read is memoized here and will eventually be returned 90 // by StateDB.Commit. 91 dbErr error 92 93 // The refund counter, also used by state transitioning. 94 refund uint64 95 96 thash common.Hash 97 txIndex int 98 logs map[common.Hash][]*types.Log 99 logSize uint 100 101 preimages map[common.Hash][]byte 102 103 // Per-transaction access list 104 accessList *accessList 105 106 // Transient storage 107 transientStorage transientStorage 108 109 // Journal of state modifications. This is the backbone of 110 // Snapshot and RevertToSnapshot. 111 journal *journal 112 validRevisions []revision 113 nextRevisionId int 114 115 // Measurements gathered during execution for debugging purposes 116 AccountReads time.Duration 117 AccountHashes time.Duration 118 AccountUpdates time.Duration 119 AccountCommits time.Duration 120 StorageReads time.Duration 121 StorageHashes time.Duration 122 StorageUpdates time.Duration 123 StorageCommits time.Duration 124 SnapshotAccountReads time.Duration 125 SnapshotStorageReads time.Duration 126 SnapshotCommits time.Duration 127 TrieDBCommits time.Duration 128 129 AccountUpdated int 130 StorageUpdated int 131 AccountDeleted int 132 StorageDeleted int 133 } 134 135 // New creates a new state from a given trie. 136 func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) { 137 tr, err := db.OpenTrie(root) 138 if err != nil { 139 return nil, err 140 } 141 sdb := &StateDB{ 142 db: db, 143 trie: tr, 144 originalRoot: root, 145 snaps: snaps, 146 stateObjects: make(map[common.Address]*stateObject), 147 stateObjectsPending: make(map[common.Address]struct{}), 148 stateObjectsDirty: make(map[common.Address]struct{}), 149 stateObjectsDestruct: make(map[common.Address]struct{}), 150 logs: make(map[common.Hash][]*types.Log), 151 preimages: make(map[common.Hash][]byte), 152 journal: newJournal(), 153 accessList: newAccessList(), 154 transientStorage: newTransientStorage(), 155 hasher: crypto.NewKeccakState(), 156 } 157 if sdb.snaps != nil { 158 if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil { 159 sdb.snapAccounts = make(map[common.Hash][]byte) 160 sdb.snapStorage = make(map[common.Hash]map[common.Hash][]byte) 161 } 162 } 163 return sdb, nil 164 } 165 166 // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the 167 // state trie concurrently while the state is mutated so that when we reach the 168 // commit phase, most of the needed data is already hot. 169 func (s *StateDB) StartPrefetcher(namespace string) { 170 if s.prefetcher != nil { 171 s.prefetcher.close() 172 s.prefetcher = nil 173 } 174 if s.snap != nil { 175 s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace) 176 } 177 } 178 179 // StopPrefetcher terminates a running prefetcher and reports any leftover stats 180 // from the gathered metrics. 181 func (s *StateDB) StopPrefetcher() { 182 if s.prefetcher != nil { 183 s.prefetcher.close() 184 s.prefetcher = nil 185 } 186 } 187 188 // setError remembers the first non-nil error it is called with. 189 func (s *StateDB) setError(err error) { 190 if s.dbErr == nil { 191 s.dbErr = err 192 } 193 } 194 195 func (s *StateDB) Error() error { 196 return s.dbErr 197 } 198 199 func (s *StateDB) AddLog(log *types.Log) { 200 s.journal.append(addLogChange{txhash: s.thash}) 201 202 log.TxHash = s.thash 203 log.TxIndex = uint(s.txIndex) 204 log.Index = s.logSize 205 s.logs[s.thash] = append(s.logs[s.thash], log) 206 s.logSize++ 207 } 208 209 // GetLogs returns the logs matching the specified transaction hash, and annotates 210 // them with the given blockNumber and blockHash. 211 func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log { 212 logs := s.logs[hash] 213 for _, l := range logs { 214 l.BlockNumber = blockNumber 215 l.BlockHash = blockHash 216 } 217 return logs 218 } 219 220 func (s *StateDB) Logs() []*types.Log { 221 var logs []*types.Log 222 for _, lgs := range s.logs { 223 logs = append(logs, lgs...) 224 } 225 return logs 226 } 227 228 // AddPreimage records a SHA3 preimage seen by the VM. 229 func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) { 230 if _, ok := s.preimages[hash]; !ok { 231 s.journal.append(addPreimageChange{hash: hash}) 232 pi := make([]byte, len(preimage)) 233 copy(pi, preimage) 234 s.preimages[hash] = pi 235 } 236 } 237 238 // Preimages returns a list of SHA3 preimages that have been submitted. 239 func (s *StateDB) Preimages() map[common.Hash][]byte { 240 return s.preimages 241 } 242 243 // AddRefund adds gas to the refund counter 244 func (s *StateDB) AddRefund(gas uint64) { 245 s.journal.append(refundChange{prev: s.refund}) 246 s.refund += gas 247 } 248 249 // SubRefund removes gas from the refund counter. 250 // This method will panic if the refund counter goes below zero 251 func (s *StateDB) SubRefund(gas uint64) { 252 s.journal.append(refundChange{prev: s.refund}) 253 if gas > s.refund { 254 panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund)) 255 } 256 s.refund -= gas 257 } 258 259 // Exist reports whether the given account address exists in the state. 260 // Notably this also returns true for suicided accounts. 261 func (s *StateDB) Exist(addr common.Address) bool { 262 return s.getStateObject(addr) != nil 263 } 264 265 // Empty returns whether the state object is either non-existent 266 // or empty according to the EIP161 specification (balance = nonce = code = 0) 267 func (s *StateDB) Empty(addr common.Address) bool { 268 so := s.getStateObject(addr) 269 return so == nil || so.empty() 270 } 271 272 // GetBalance retrieves the balance from the given address or 0 if object not found 273 func (s *StateDB) GetBalance(addr common.Address) *big.Int { 274 stateObject := s.getStateObject(addr) 275 if stateObject != nil { 276 return stateObject.Balance() 277 } 278 return common.Big0 279 } 280 281 func (s *StateDB) GetNonce(addr common.Address) uint64 { 282 stateObject := s.getStateObject(addr) 283 if stateObject != nil { 284 return stateObject.Nonce() 285 } 286 287 return 0 288 } 289 290 // TxIndex returns the current transaction index set by Prepare. 291 func (s *StateDB) TxIndex() int { 292 return s.txIndex 293 } 294 295 func (s *StateDB) GetCode(addr common.Address) []byte { 296 stateObject := s.getStateObject(addr) 297 if stateObject != nil { 298 return stateObject.Code(s.db) 299 } 300 return nil 301 } 302 303 func (s *StateDB) GetCodeSize(addr common.Address) int { 304 stateObject := s.getStateObject(addr) 305 if stateObject != nil { 306 return stateObject.CodeSize(s.db) 307 } 308 return 0 309 } 310 311 func (s *StateDB) GetCodeHash(addr common.Address) common.Hash { 312 stateObject := s.getStateObject(addr) 313 if stateObject == nil { 314 return common.Hash{} 315 } 316 return common.BytesToHash(stateObject.CodeHash()) 317 } 318 319 // GetState retrieves a value from the given account's storage trie. 320 func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash { 321 stateObject := s.getStateObject(addr) 322 if stateObject != nil { 323 return stateObject.GetState(s.db, hash) 324 } 325 return common.Hash{} 326 } 327 328 // GetProof returns the Merkle proof for a given account. 329 func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) { 330 return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes())) 331 } 332 333 // GetProofByHash returns the Merkle proof for a given account. 334 func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) { 335 var proof proofList 336 err := s.trie.Prove(addrHash[:], 0, &proof) 337 return proof, err 338 } 339 340 // GetStorageProof returns the Merkle proof for given storage slot. 341 func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) { 342 trie, err := s.StorageTrie(a) 343 if err != nil { 344 return nil, err 345 } 346 if trie == nil { 347 return nil, errors.New("storage trie for requested address does not exist") 348 } 349 var proof proofList 350 err = trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) 351 if err != nil { 352 return nil, err 353 } 354 return proof, nil 355 } 356 357 // GetCommittedState retrieves a value from the given account's committed storage trie. 358 func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash { 359 stateObject := s.getStateObject(addr) 360 if stateObject != nil { 361 return stateObject.GetCommittedState(s.db, hash) 362 } 363 return common.Hash{} 364 } 365 366 // Database retrieves the low level database supporting the lower level trie ops. 367 func (s *StateDB) Database() Database { 368 return s.db 369 } 370 371 // StorageTrie returns the storage trie of an account. The return value is a copy 372 // and is nil for non-existent accounts. An error will be returned if storage trie 373 // is existent but can't be loaded correctly. 374 func (s *StateDB) StorageTrie(addr common.Address) (Trie, error) { 375 stateObject := s.getStateObject(addr) 376 if stateObject == nil { 377 return nil, nil 378 } 379 cpy := stateObject.deepCopy(s) 380 if _, err := cpy.updateTrie(s.db); err != nil { 381 return nil, err 382 } 383 return cpy.getTrie(s.db) 384 } 385 386 func (s *StateDB) HasSuicided(addr common.Address) bool { 387 stateObject := s.getStateObject(addr) 388 if stateObject != nil { 389 return stateObject.suicided 390 } 391 return false 392 } 393 394 /* 395 * SETTERS 396 */ 397 398 // AddBalance adds amount to the account associated with addr. 399 func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { 400 stateObject := s.GetOrNewStateObject(addr) 401 if stateObject != nil { 402 stateObject.AddBalance(amount) 403 } 404 } 405 406 // SubBalance subtracts amount from the account associated with addr. 407 func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { 408 stateObject := s.GetOrNewStateObject(addr) 409 if stateObject != nil { 410 stateObject.SubBalance(amount) 411 } 412 } 413 414 func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) { 415 stateObject := s.GetOrNewStateObject(addr) 416 if stateObject != nil { 417 stateObject.SetBalance(amount) 418 } 419 } 420 421 func (s *StateDB) SetNonce(addr common.Address, nonce uint64) { 422 stateObject := s.GetOrNewStateObject(addr) 423 if stateObject != nil { 424 stateObject.SetNonce(nonce) 425 } 426 } 427 428 func (s *StateDB) SetCode(addr common.Address, code []byte) { 429 stateObject := s.GetOrNewStateObject(addr) 430 if stateObject != nil { 431 stateObject.SetCode(crypto.Keccak256Hash(code), code) 432 } 433 } 434 435 func (s *StateDB) SetState(addr common.Address, key, value common.Hash) { 436 stateObject := s.GetOrNewStateObject(addr) 437 if stateObject != nil { 438 stateObject.SetState(s.db, key, value) 439 } 440 } 441 442 // SetStorage replaces the entire storage for the specified account with given 443 // storage. This function should only be used for debugging. 444 func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) { 445 stateObject := s.GetOrNewStateObject(addr) 446 if stateObject != nil { 447 stateObject.SetStorage(storage) 448 } 449 } 450 451 // Suicide marks the given account as suicided. 452 // This clears the account balance. 453 // 454 // The account's state object is still available until the state is committed, 455 // getStateObject will return a non-nil account after Suicide. 456 func (s *StateDB) Suicide(addr common.Address) bool { 457 stateObject := s.getStateObject(addr) 458 if stateObject == nil { 459 return false 460 } 461 s.journal.append(suicideChange{ 462 account: &addr, 463 prev: stateObject.suicided, 464 prevbalance: new(big.Int).Set(stateObject.Balance()), 465 }) 466 stateObject.markSuicided() 467 stateObject.data.Balance = new(big.Int) 468 469 return true 470 } 471 472 // SetTransientState sets transient storage for a given account. It 473 // adds the change to the journal so that it can be rolled back 474 // to its previous value if there is a revert. 475 func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash) { 476 prev := s.GetTransientState(addr, key) 477 if prev == value { 478 return 479 } 480 481 s.journal.append(transientStorageChange{ 482 account: &addr, 483 key: key, 484 prevalue: prev, 485 }) 486 487 s.setTransientState(addr, key, value) 488 } 489 490 // setTransientState is a lower level setter for transient storage. It 491 // is called during a revert to prevent modifications to the journal. 492 func (s *StateDB) setTransientState(addr common.Address, key, value common.Hash) { 493 s.transientStorage.Set(addr, key, value) 494 } 495 496 // GetTransientState gets transient storage for a given account. 497 func (s *StateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash { 498 return s.transientStorage.Get(addr, key) 499 } 500 501 // 502 // Setting, updating & deleting state object methods. 503 // 504 505 // updateStateObject writes the given object to the trie. 506 func (s *StateDB) updateStateObject(obj *stateObject) { 507 // Track the amount of time wasted on updating the account from the trie 508 if metrics.EnabledExpensive { 509 defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now()) 510 } 511 // Encode the account and update the account trie 512 addr := obj.Address() 513 if err := s.trie.TryUpdateAccount(addr, &obj.data); err != nil { 514 s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) 515 } 516 517 // If state snapshotting is active, cache the data til commit. Note, this 518 // update mechanism is not symmetric to the deletion, because whereas it is 519 // enough to track account updates at commit time, deletions need tracking 520 // at transaction boundary level to ensure we capture state clearing. 521 if s.snap != nil { 522 s.snapAccounts[obj.addrHash] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash) 523 } 524 } 525 526 // deleteStateObject removes the given object from the state trie. 527 func (s *StateDB) deleteStateObject(obj *stateObject) { 528 // Track the amount of time wasted on deleting the account from the trie 529 if metrics.EnabledExpensive { 530 defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now()) 531 } 532 // Delete the account from the trie 533 addr := obj.Address() 534 if err := s.trie.TryDeleteAccount(addr); err != nil { 535 s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err)) 536 } 537 } 538 539 // getStateObject retrieves a state object given by the address, returning nil if 540 // the object is not found or was deleted in this execution context. If you need 541 // to differentiate between non-existent/just-deleted, use getDeletedStateObject. 542 func (s *StateDB) getStateObject(addr common.Address) *stateObject { 543 if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted { 544 return obj 545 } 546 return nil 547 } 548 549 // getDeletedStateObject is similar to getStateObject, but instead of returning 550 // nil for a deleted state object, it returns the actual object with the deleted 551 // flag set. This is needed by the state journal to revert to the correct s- 552 // destructed object instead of wiping all knowledge about the state object. 553 func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { 554 // Prefer live objects if any is available 555 if obj := s.stateObjects[addr]; obj != nil { 556 return obj 557 } 558 // If no live objects are available, attempt to use snapshots 559 var data *types.StateAccount 560 if s.snap != nil { 561 start := time.Now() 562 acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())) 563 if metrics.EnabledExpensive { 564 s.SnapshotAccountReads += time.Since(start) 565 } 566 if err == nil { 567 if acc == nil { 568 return nil 569 } 570 data = &types.StateAccount{ 571 Nonce: acc.Nonce, 572 Balance: acc.Balance, 573 CodeHash: acc.CodeHash, 574 Root: common.BytesToHash(acc.Root), 575 } 576 if len(data.CodeHash) == 0 { 577 data.CodeHash = emptyCodeHash 578 } 579 if data.Root == (common.Hash{}) { 580 data.Root = emptyRoot 581 } 582 } 583 } 584 // If snapshot unavailable or reading from it failed, load from the database 585 if data == nil { 586 start := time.Now() 587 var err error 588 data, err = s.trie.TryGetAccount(addr) 589 if metrics.EnabledExpensive { 590 s.AccountReads += time.Since(start) 591 } 592 if err != nil { 593 s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %w", addr.Bytes(), err)) 594 return nil 595 } 596 if data == nil { 597 return nil 598 } 599 } 600 // Insert into the live set 601 obj := newObject(s, addr, *data) 602 s.setStateObject(obj) 603 return obj 604 } 605 606 func (s *StateDB) setStateObject(object *stateObject) { 607 s.stateObjects[object.Address()] = object 608 } 609 610 // GetOrNewStateObject retrieves a state object or create a new state object if nil. 611 func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject { 612 stateObject := s.getStateObject(addr) 613 if stateObject == nil { 614 stateObject, _ = s.createObject(addr) 615 } 616 return stateObject 617 } 618 619 // createObject creates a new state object. If there is an existing account with 620 // the given address, it is overwritten and returned as the second return value. 621 func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) { 622 prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that! 623 624 var prevdestruct bool 625 if prev != nil { 626 _, prevdestruct = s.stateObjectsDestruct[prev.address] 627 if !prevdestruct { 628 s.stateObjectsDestruct[prev.address] = struct{}{} 629 } 630 } 631 newobj = newObject(s, addr, types.StateAccount{}) 632 if prev == nil { 633 s.journal.append(createObjectChange{account: &addr}) 634 } else { 635 s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct}) 636 } 637 s.setStateObject(newobj) 638 if prev != nil && !prev.deleted { 639 return newobj, prev 640 } 641 return newobj, nil 642 } 643 644 // CreateAccount explicitly creates a state object. If a state object with the address 645 // already exists the balance is carried over to the new account. 646 // 647 // CreateAccount is called during the EVM CREATE operation. The situation might arise that 648 // a contract does the following: 649 // 650 // 1. sends funds to sha(account ++ (nonce + 1)) 651 // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1) 652 // 653 // Carrying over the balance ensures that Ether doesn't disappear. 654 func (s *StateDB) CreateAccount(addr common.Address) { 655 newObj, prev := s.createObject(addr) 656 if prev != nil { 657 newObj.setBalance(prev.data.Balance) 658 } 659 } 660 661 func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error { 662 so := db.getStateObject(addr) 663 if so == nil { 664 return nil 665 } 666 tr, err := so.getTrie(db.db) 667 if err != nil { 668 return err 669 } 670 it := trie.NewIterator(tr.NodeIterator(nil)) 671 672 for it.Next() { 673 key := common.BytesToHash(db.trie.GetKey(it.Key)) 674 if value, dirty := so.dirtyStorage[key]; dirty { 675 if !cb(key, value) { 676 return nil 677 } 678 continue 679 } 680 681 if len(it.Value) > 0 { 682 _, content, _, err := rlp.Split(it.Value) 683 if err != nil { 684 return err 685 } 686 if !cb(key, common.BytesToHash(content)) { 687 return nil 688 } 689 } 690 } 691 return nil 692 } 693 694 // Copy creates a deep, independent copy of the state. 695 // Snapshots of the copied state cannot be applied to the copy. 696 func (s *StateDB) Copy() *StateDB { 697 // Copy all the basic fields, initialize the memory ones 698 state := &StateDB{ 699 db: s.db, 700 trie: s.db.CopyTrie(s.trie), 701 originalRoot: s.originalRoot, 702 stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)), 703 stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), 704 stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)), 705 stateObjectsDestruct: make(map[common.Address]struct{}, len(s.stateObjectsDestruct)), 706 refund: s.refund, 707 logs: make(map[common.Hash][]*types.Log, len(s.logs)), 708 logSize: s.logSize, 709 preimages: make(map[common.Hash][]byte, len(s.preimages)), 710 journal: newJournal(), 711 hasher: crypto.NewKeccakState(), 712 } 713 // Copy the dirty states, logs, and preimages 714 for addr := range s.journal.dirties { 715 // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527), 716 // and in the Finalise-method, there is a case where an object is in the journal but not 717 // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for 718 // nil 719 if object, exist := s.stateObjects[addr]; exist { 720 // Even though the original object is dirty, we are not copying the journal, 721 // so we need to make sure that any side-effect the journal would have caused 722 // during a commit (or similar op) is already applied to the copy. 723 state.stateObjects[addr] = object.deepCopy(state) 724 725 state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits 726 state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits 727 } 728 } 729 // Above, we don't copy the actual journal. This means that if the copy 730 // is copied, the loop above will be a no-op, since the copy's journal 731 // is empty. Thus, here we iterate over stateObjects, to enable copies 732 // of copies. 733 for addr := range s.stateObjectsPending { 734 if _, exist := state.stateObjects[addr]; !exist { 735 state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state) 736 } 737 state.stateObjectsPending[addr] = struct{}{} 738 } 739 for addr := range s.stateObjectsDirty { 740 if _, exist := state.stateObjects[addr]; !exist { 741 state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state) 742 } 743 state.stateObjectsDirty[addr] = struct{}{} 744 } 745 // Deep copy the destruction flag. 746 for addr := range s.stateObjectsDestruct { 747 state.stateObjectsDestruct[addr] = struct{}{} 748 } 749 for hash, logs := range s.logs { 750 cpy := make([]*types.Log, len(logs)) 751 for i, l := range logs { 752 cpy[i] = new(types.Log) 753 *cpy[i] = *l 754 } 755 state.logs[hash] = cpy 756 } 757 for hash, preimage := range s.preimages { 758 state.preimages[hash] = preimage 759 } 760 // Do we need to copy the access list and transient storage? 761 // In practice: No. At the start of a transaction, these two lists are empty. 762 // In practice, we only ever copy state _between_ transactions/blocks, never 763 // in the middle of a transaction. However, it doesn't cost us much to copy 764 // empty lists, so we do it anyway to not blow up if we ever decide copy them 765 // in the middle of a transaction. 766 state.accessList = s.accessList.Copy() 767 state.transientStorage = s.transientStorage.Copy() 768 769 // If there's a prefetcher running, make an inactive copy of it that can 770 // only access data but does not actively preload (since the user will not 771 // know that they need to explicitly terminate an active copy). 772 if s.prefetcher != nil { 773 state.prefetcher = s.prefetcher.copy() 774 } 775 if s.snaps != nil { 776 // In order for the miner to be able to use and make additions 777 // to the snapshot tree, we need to copy that as well. 778 // Otherwise, any block mined by ourselves will cause gaps in the tree, 779 // and force the miner to operate trie-backed only 780 state.snaps = s.snaps 781 state.snap = s.snap 782 783 // deep copy needed 784 state.snapAccounts = make(map[common.Hash][]byte) 785 for k, v := range s.snapAccounts { 786 state.snapAccounts[k] = v 787 } 788 state.snapStorage = make(map[common.Hash]map[common.Hash][]byte) 789 for k, v := range s.snapStorage { 790 temp := make(map[common.Hash][]byte) 791 for kk, vv := range v { 792 temp[kk] = vv 793 } 794 state.snapStorage[k] = temp 795 } 796 } 797 return state 798 } 799 800 // Snapshot returns an identifier for the current revision of the state. 801 func (s *StateDB) Snapshot() int { 802 id := s.nextRevisionId 803 s.nextRevisionId++ 804 s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()}) 805 return id 806 } 807 808 // RevertToSnapshot reverts all state changes made since the given revision. 809 func (s *StateDB) RevertToSnapshot(revid int) { 810 // Find the snapshot in the stack of valid snapshots. 811 idx := sort.Search(len(s.validRevisions), func(i int) bool { 812 return s.validRevisions[i].id >= revid 813 }) 814 if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid { 815 panic(fmt.Errorf("revision id %v cannot be reverted", revid)) 816 } 817 snapshot := s.validRevisions[idx].journalIndex 818 819 // Replay the journal to undo changes and remove invalidated snapshots 820 s.journal.revert(s, snapshot) 821 s.validRevisions = s.validRevisions[:idx] 822 } 823 824 // GetRefund returns the current value of the refund counter. 825 func (s *StateDB) GetRefund() uint64 { 826 return s.refund 827 } 828 829 // Finalise finalises the state by removing the destructed objects and clears 830 // the journal as well as the refunds. Finalise, however, will not push any updates 831 // into the tries just yet. Only IntermediateRoot or Commit will do that. 832 func (s *StateDB) Finalise(deleteEmptyObjects bool) { 833 addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties)) 834 for addr := range s.journal.dirties { 835 obj, exist := s.stateObjects[addr] 836 if !exist { 837 // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2 838 // That tx goes out of gas, and although the notion of 'touched' does not exist there, the 839 // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake, 840 // it will persist in the journal even though the journal is reverted. In this special circumstance, 841 // it may exist in `s.journal.dirties` but not in `s.stateObjects`. 842 // Thus, we can safely ignore it here 843 continue 844 } 845 if obj.suicided || (deleteEmptyObjects && obj.empty()) { 846 obj.deleted = true 847 848 // We need to maintain account deletions explicitly (will remain 849 // set indefinitely). 850 s.stateObjectsDestruct[obj.address] = struct{}{} 851 852 // If state snapshotting is active, also mark the destruction there. 853 // Note, we can't do this only at the end of a block because multiple 854 // transactions within the same block might self destruct and then 855 // resurrect an account; but the snapshotter needs both events. 856 if s.snap != nil { 857 delete(s.snapAccounts, obj.addrHash) // Clear out any previously updated account data (may be recreated via a resurrect) 858 delete(s.snapStorage, obj.addrHash) // Clear out any previously updated storage data (may be recreated via a resurrect) 859 } 860 } else { 861 obj.finalise(true) // Prefetch slots in the background 862 } 863 s.stateObjectsPending[addr] = struct{}{} 864 s.stateObjectsDirty[addr] = struct{}{} 865 866 // At this point, also ship the address off to the precacher. The precacher 867 // will start loading tries, and when the change is eventually committed, 868 // the commit-phase will be a lot faster 869 addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure 870 } 871 if s.prefetcher != nil && len(addressesToPrefetch) > 0 { 872 s.prefetcher.prefetch(common.Hash{}, s.originalRoot, addressesToPrefetch) 873 } 874 // Invalidate journal because reverting across transactions is not allowed. 875 s.clearJournalAndRefund() 876 } 877 878 // IntermediateRoot computes the current root hash of the state trie. 879 // It is called in between transactions to get the root hash that 880 // goes into transaction receipts. 881 func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { 882 // Finalise all the dirty storage states and write them into the tries 883 s.Finalise(deleteEmptyObjects) 884 885 // If there was a trie prefetcher operating, it gets aborted and irrevocably 886 // modified after we start retrieving tries. Remove it from the statedb after 887 // this round of use. 888 // 889 // This is weird pre-byzantium since the first tx runs with a prefetcher and 890 // the remainder without, but pre-byzantium even the initial prefetcher is 891 // useless, so no sleep lost. 892 prefetcher := s.prefetcher 893 if s.prefetcher != nil { 894 defer func() { 895 s.prefetcher.close() 896 s.prefetcher = nil 897 }() 898 } 899 // Although naively it makes sense to retrieve the account trie and then do 900 // the contract storage and account updates sequentially, that short circuits 901 // the account prefetcher. Instead, let's process all the storage updates 902 // first, giving the account prefetches just a few more milliseconds of time 903 // to pull useful data from disk. 904 for addr := range s.stateObjectsPending { 905 if obj := s.stateObjects[addr]; !obj.deleted { 906 obj.updateRoot(s.db) 907 } 908 } 909 // Now we're about to start to write changes to the trie. The trie is so far 910 // _untouched_. We can check with the prefetcher, if it can give us a trie 911 // which has the same root, but also has some content loaded into it. 912 if prefetcher != nil { 913 if trie := prefetcher.trie(common.Hash{}, s.originalRoot); trie != nil { 914 s.trie = trie 915 } 916 } 917 usedAddrs := make([][]byte, 0, len(s.stateObjectsPending)) 918 for addr := range s.stateObjectsPending { 919 if obj := s.stateObjects[addr]; obj.deleted { 920 s.deleteStateObject(obj) 921 s.AccountDeleted += 1 922 } else { 923 s.updateStateObject(obj) 924 s.AccountUpdated += 1 925 } 926 usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure 927 } 928 if prefetcher != nil { 929 prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs) 930 } 931 if len(s.stateObjectsPending) > 0 { 932 s.stateObjectsPending = make(map[common.Address]struct{}) 933 } 934 // Track the amount of time wasted on hashing the account trie 935 if metrics.EnabledExpensive { 936 defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now()) 937 } 938 return s.trie.Hash() 939 } 940 941 // SetTxContext sets the current transaction hash and index which are 942 // used when the EVM emits new state logs. It should be invoked before 943 // transaction execution. 944 func (s *StateDB) SetTxContext(thash common.Hash, ti int) { 945 s.thash = thash 946 s.txIndex = ti 947 } 948 949 func (s *StateDB) clearJournalAndRefund() { 950 if len(s.journal.entries) > 0 { 951 s.journal = newJournal() 952 s.refund = 0 953 } 954 s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entries 955 } 956 957 // Commit writes the state to the underlying in-memory trie database. 958 func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { 959 if s.dbErr != nil { 960 return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr) 961 } 962 // Finalize any pending changes and merge everything into the tries 963 s.IntermediateRoot(deleteEmptyObjects) 964 965 // Commit objects to the trie, measuring the elapsed time 966 var ( 967 accountTrieNodesUpdated int 968 accountTrieNodesDeleted int 969 storageTrieNodesUpdated int 970 storageTrieNodesDeleted int 971 nodes = trie.NewMergedNodeSet() 972 ) 973 codeWriter := s.db.DiskDB().NewBatch() 974 for addr := range s.stateObjectsDirty { 975 if obj := s.stateObjects[addr]; !obj.deleted { 976 // Write any contract code associated with the state object 977 if obj.code != nil && obj.dirtyCode { 978 rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code) 979 obj.dirtyCode = false 980 } 981 // Write any storage changes in the state object to its storage trie 982 set, err := obj.commitTrie(s.db) 983 if err != nil { 984 return common.Hash{}, err 985 } 986 // Merge the dirty nodes of storage trie into global set 987 if set != nil { 988 if err := nodes.Merge(set); err != nil { 989 return common.Hash{}, err 990 } 991 updates, deleted := set.Size() 992 storageTrieNodesUpdated += updates 993 storageTrieNodesDeleted += deleted 994 } 995 } 996 // If the contract is destructed, the storage is still left in the 997 // database as dangling data. Theoretically it's should be wiped from 998 // database as well, but in hash-based-scheme it's extremely hard to 999 // determine that if the trie nodes are also referenced by other storage, 1000 // and in path-based-scheme some technical challenges are still unsolved. 1001 // Although it won't affect the correctness but please fix it TODO(rjl493456442). 1002 } 1003 if len(s.stateObjectsDirty) > 0 { 1004 s.stateObjectsDirty = make(map[common.Address]struct{}) 1005 } 1006 if codeWriter.ValueSize() > 0 { 1007 if err := codeWriter.Write(); err != nil { 1008 log.Crit("Failed to commit dirty codes", "error", err) 1009 } 1010 } 1011 // Write the account trie changes, measuring the amount of wasted time 1012 var start time.Time 1013 if metrics.EnabledExpensive { 1014 start = time.Now() 1015 } 1016 root, set, err := s.trie.Commit(true) 1017 if err != nil { 1018 return common.Hash{}, err 1019 } 1020 // Merge the dirty nodes of account trie into global set 1021 if set != nil { 1022 if err := nodes.Merge(set); err != nil { 1023 return common.Hash{}, err 1024 } 1025 accountTrieNodesUpdated, accountTrieNodesDeleted = set.Size() 1026 } 1027 if metrics.EnabledExpensive { 1028 s.AccountCommits += time.Since(start) 1029 1030 accountUpdatedMeter.Mark(int64(s.AccountUpdated)) 1031 storageUpdatedMeter.Mark(int64(s.StorageUpdated)) 1032 accountDeletedMeter.Mark(int64(s.AccountDeleted)) 1033 storageDeletedMeter.Mark(int64(s.StorageDeleted)) 1034 accountTrieUpdatedMeter.Mark(int64(accountTrieNodesUpdated)) 1035 accountTrieDeletedMeter.Mark(int64(accountTrieNodesDeleted)) 1036 storageTriesUpdatedMeter.Mark(int64(storageTrieNodesUpdated)) 1037 storageTriesDeletedMeter.Mark(int64(storageTrieNodesDeleted)) 1038 s.AccountUpdated, s.AccountDeleted = 0, 0 1039 s.StorageUpdated, s.StorageDeleted = 0, 0 1040 } 1041 // If snapshotting is enabled, update the snapshot tree with this new version 1042 if s.snap != nil { 1043 start := time.Now() 1044 // Only update if there's a state transition (skip empty Clique blocks) 1045 if parent := s.snap.Root(); parent != root { 1046 if err := s.snaps.Update(root, parent, s.convertAccountSet(s.stateObjectsDestruct), s.snapAccounts, s.snapStorage); err != nil { 1047 log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err) 1048 } 1049 // Keep 128 diff layers in the memory, persistent layer is 129th. 1050 // - head layer is paired with HEAD state 1051 // - head-1 layer is paired with HEAD-1 state 1052 // - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state 1053 if err := s.snaps.Cap(root, 128); err != nil { 1054 log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err) 1055 } 1056 } 1057 if metrics.EnabledExpensive { 1058 s.SnapshotCommits += time.Since(start) 1059 } 1060 s.snap, s.snapAccounts, s.snapStorage = nil, nil, nil 1061 } 1062 if len(s.stateObjectsDestruct) > 0 { 1063 s.stateObjectsDestruct = make(map[common.Address]struct{}) 1064 } 1065 if root == (common.Hash{}) { 1066 root = emptyRoot 1067 } 1068 origin := s.originalRoot 1069 if origin == (common.Hash{}) { 1070 origin = emptyRoot 1071 } 1072 if root != origin { 1073 start := time.Now() 1074 if err := s.db.TrieDB().Update(nodes); err != nil { 1075 return common.Hash{}, err 1076 } 1077 s.originalRoot = root 1078 if metrics.EnabledExpensive { 1079 s.TrieDBCommits += time.Since(start) 1080 } 1081 } 1082 return root, nil 1083 } 1084 1085 // Prepare handles the preparatory steps for executing a state transition with. 1086 // This method must be invoked before state transition. 1087 // 1088 // Berlin fork: 1089 // - Add sender to access list (2929) 1090 // - Add destination to access list (2929) 1091 // - Add precompiles to access list (2929) 1092 // - Add the contents of the optional tx access list (2930) 1093 // 1094 // Potential EIPs: 1095 // - Reset access list (Berlin) 1096 // - Add coinbase to access list (EIP-3651) 1097 // - Reset transient storage (EIP-1153) 1098 func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) { 1099 if rules.IsBerlin { 1100 // Clear out any leftover from previous executions 1101 al := newAccessList() 1102 s.accessList = al 1103 1104 al.AddAddress(sender) 1105 if dst != nil { 1106 al.AddAddress(*dst) 1107 // If it's a create-tx, the destination will be added inside evm.create 1108 } 1109 for _, addr := range precompiles { 1110 al.AddAddress(addr) 1111 } 1112 for _, el := range list { 1113 al.AddAddress(el.Address) 1114 for _, key := range el.StorageKeys { 1115 al.AddSlot(el.Address, key) 1116 } 1117 } 1118 if rules.IsShanghai { // EIP-3651: warm coinbase 1119 al.AddAddress(coinbase) 1120 } 1121 } 1122 // Reset transient storage at the beginning of transaction execution 1123 s.transientStorage = newTransientStorage() 1124 } 1125 1126 // AddAddressToAccessList adds the given address to the access list 1127 func (s *StateDB) AddAddressToAccessList(addr common.Address) { 1128 if s.accessList.AddAddress(addr) { 1129 s.journal.append(accessListAddAccountChange{&addr}) 1130 } 1131 } 1132 1133 // AddSlotToAccessList adds the given (address, slot)-tuple to the access list 1134 func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) { 1135 addrMod, slotMod := s.accessList.AddSlot(addr, slot) 1136 if addrMod { 1137 // In practice, this should not happen, since there is no way to enter the 1138 // scope of 'address' without having the 'address' become already added 1139 // to the access list (via call-variant, create, etc). 1140 // Better safe than sorry, though 1141 s.journal.append(accessListAddAccountChange{&addr}) 1142 } 1143 if slotMod { 1144 s.journal.append(accessListAddSlotChange{ 1145 address: &addr, 1146 slot: &slot, 1147 }) 1148 } 1149 } 1150 1151 // AddressInAccessList returns true if the given address is in the access list. 1152 func (s *StateDB) AddressInAccessList(addr common.Address) bool { 1153 return s.accessList.ContainsAddress(addr) 1154 } 1155 1156 // SlotInAccessList returns true if the given (address, slot)-tuple is in the access list. 1157 func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) { 1158 return s.accessList.Contains(addr, slot) 1159 } 1160 1161 // convertAccountSet converts a provided account set from address keyed to hash keyed. 1162 func (s *StateDB) convertAccountSet(set map[common.Address]struct{}) map[common.Hash]struct{} { 1163 ret := make(map[common.Hash]struct{}) 1164 for addr := range set { 1165 obj, exist := s.stateObjects[addr] 1166 if !exist { 1167 ret[crypto.Keccak256Hash(addr[:])] = struct{}{} 1168 } else { 1169 ret[obj.addrHash] = struct{}{} 1170 } 1171 } 1172 return ret 1173 }