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