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