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