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