github.com/haliliceylan/bsc@v1.1.10-0.20220501224556-eb78d644ebcb/core/state/statedb.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package state provides a caching layer atop the Ethereum state trie. 18 package state 19 20 import ( 21 "errors" 22 "fmt" 23 "math/big" 24 "runtime" 25 "sort" 26 "sync" 27 "time" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/common/gopool" 31 "github.com/ethereum/go-ethereum/core/rawdb" 32 "github.com/ethereum/go-ethereum/core/state/snapshot" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/crypto" 35 "github.com/ethereum/go-ethereum/ethdb" 36 "github.com/ethereum/go-ethereum/log" 37 "github.com/ethereum/go-ethereum/metrics" 38 "github.com/ethereum/go-ethereum/rlp" 39 "github.com/ethereum/go-ethereum/trie" 40 ) 41 42 const defaultNumOfSlots = 100 43 44 type revision struct { 45 id int 46 journalIndex int 47 } 48 49 var ( 50 // emptyRoot is the known root hash of an empty trie. 51 emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 52 53 // dummyRoot is the dummy account root before corrected in pipecommit sync mode, 54 // the value is 542e5fc2709de84248e9bce43a9c0c8943a608029001360f8ab55bf113b23d28 55 dummyRoot = crypto.Keccak256Hash([]byte("dummy_account_root")) 56 57 emptyAddr = crypto.Keccak256Hash(common.Address{}.Bytes()) 58 ) 59 60 type proofList [][]byte 61 62 func (n *proofList) Put(key []byte, value []byte) error { 63 *n = append(*n, value) 64 return nil 65 } 66 67 func (n *proofList) Delete(key []byte) error { 68 panic("not supported") 69 } 70 71 // StateDB structs within the ethereum protocol are used to store anything 72 // within the merkle trie. StateDBs take care of caching and storing 73 // nested states. It's the general query interface to retrieve: 74 // * Contracts 75 // * Accounts 76 type StateDB struct { 77 db Database 78 prefetcherLock sync.Mutex 79 prefetcher *triePrefetcher 80 originalRoot common.Hash // The pre-state root, before any changes were made 81 expectedRoot common.Hash // The state root in the block header 82 stateRoot common.Hash // The calculation result of IntermediateRoot 83 84 trie Trie 85 hasher crypto.KeccakState 86 diffLayer *types.DiffLayer 87 diffTries map[common.Address]Trie 88 diffCode map[common.Hash][]byte 89 lightProcessed bool 90 fullProcessed bool 91 pipeCommit bool 92 93 snapMux sync.Mutex 94 snaps *snapshot.Tree 95 snap snapshot.Snapshot 96 snapDestructs map[common.Address]struct{} 97 snapAccounts map[common.Address][]byte 98 snapStorage map[common.Address]map[string][]byte 99 100 // This map holds 'live' objects, which will get modified while processing a state transition. 101 stateObjects map[common.Address]*StateObject 102 stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie 103 stateObjectsDirty map[common.Address]struct{} // State objects modified in the current execution 104 105 storagePool *StoragePool // sharedPool to store L1 originStorage of stateObjects 106 writeOnSharedStorage bool // Write to the shared origin storage of a stateObject while reading from the underlying storage layer. 107 // DB error. 108 // State objects are used by the consensus core and VM which are 109 // unable to deal with database-level errors. Any error that occurs 110 // during a database read is memoized here and will eventually be returned 111 // by StateDB.Commit. 112 dbErr error 113 114 // The refund counter, also used by state transitioning. 115 refund uint64 116 117 thash, bhash common.Hash 118 txIndex int 119 logs map[common.Hash][]*types.Log 120 logSize uint 121 122 preimages map[common.Hash][]byte 123 124 // Per-transaction access list 125 accessList *accessList 126 127 // Journal of state modifications. This is the backbone of 128 // Snapshot and RevertToSnapshot. 129 journal *journal 130 validRevisions []revision 131 nextRevisionId int 132 133 // Measurements gathered during execution for debugging purposes 134 MetricsMux sync.Mutex 135 AccountReads time.Duration 136 AccountHashes time.Duration 137 AccountUpdates time.Duration 138 AccountCommits time.Duration 139 StorageReads time.Duration 140 StorageHashes time.Duration 141 StorageUpdates time.Duration 142 StorageCommits time.Duration 143 SnapshotAccountReads time.Duration 144 SnapshotStorageReads time.Duration 145 SnapshotCommits time.Duration 146 } 147 148 // New creates a new state from a given trie. 149 func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) { 150 return newStateDB(root, db, snaps) 151 } 152 153 // NewWithSharedPool creates a new state with sharedStorge on layer 1.5 154 func NewWithSharedPool(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) { 155 statedb, err := newStateDB(root, db, snaps) 156 if err != nil { 157 return nil, err 158 } 159 statedb.storagePool = NewStoragePool() 160 return statedb, nil 161 } 162 163 func newStateDB(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) { 164 sdb := &StateDB{ 165 db: db, 166 originalRoot: root, 167 snaps: snaps, 168 stateObjects: make(map[common.Address]*StateObject, defaultNumOfSlots), 169 stateObjectsPending: make(map[common.Address]struct{}, defaultNumOfSlots), 170 stateObjectsDirty: make(map[common.Address]struct{}, defaultNumOfSlots), 171 logs: make(map[common.Hash][]*types.Log, defaultNumOfSlots), 172 preimages: make(map[common.Hash][]byte), 173 journal: newJournal(), 174 hasher: crypto.NewKeccakState(), 175 } 176 if sdb.snaps != nil { 177 if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil { 178 sdb.snapDestructs = make(map[common.Address]struct{}) 179 sdb.snapAccounts = make(map[common.Address][]byte) 180 sdb.snapStorage = make(map[common.Address]map[string][]byte) 181 } 182 } 183 184 snapVerified := sdb.snap != nil && sdb.snap.Verified() 185 tr, err := db.OpenTrie(root) 186 // return error when 1. failed to open trie and 2. the snap is nil or the snap is not nil and done verification 187 if err != nil && (sdb.snap == nil || snapVerified) { 188 return nil, err 189 } 190 sdb.trie = tr 191 return sdb, nil 192 } 193 194 func (s *StateDB) EnableWriteOnSharedStorage() { 195 s.writeOnSharedStorage = true 196 } 197 198 // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the 199 // state trie concurrently while the state is mutated so that when we reach the 200 // commit phase, most of the needed data is already hot. 201 func (s *StateDB) StartPrefetcher(namespace string) { 202 s.prefetcherLock.Lock() 203 defer s.prefetcherLock.Unlock() 204 if s.prefetcher != nil { 205 s.prefetcher.close() 206 s.prefetcher = nil 207 } 208 if s.snap != nil { 209 s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace) 210 } 211 } 212 213 // StopPrefetcher terminates a running prefetcher and reports any leftover stats 214 // from the gathered metrics. 215 func (s *StateDB) StopPrefetcher() { 216 s.prefetcherLock.Lock() 217 defer s.prefetcherLock.Unlock() 218 if s.prefetcher != nil { 219 s.prefetcher.close() 220 s.prefetcher = nil 221 } 222 } 223 224 // Mark that the block is processed by diff layer 225 func (s *StateDB) SetExpectedStateRoot(root common.Hash) { 226 s.expectedRoot = root 227 } 228 229 // Mark that the block is processed by diff layer 230 func (s *StateDB) MarkLightProcessed() { 231 s.lightProcessed = true 232 } 233 234 // Enable the pipeline commit function of statedb 235 func (s *StateDB) EnablePipeCommit() { 236 if s.snap != nil && s.snaps.Layers() > 1 { 237 s.pipeCommit = true 238 } 239 } 240 241 // IsPipeCommit checks whether pipecommit is enabled on the statedb or not 242 func (s *StateDB) IsPipeCommit() bool { 243 return s.pipeCommit 244 } 245 246 // Mark that the block is full processed 247 func (s *StateDB) MarkFullProcessed() { 248 s.fullProcessed = true 249 } 250 251 func (s *StateDB) IsLightProcessed() bool { 252 return s.lightProcessed 253 } 254 255 // setError remembers the first non-nil error it is called with. 256 func (s *StateDB) setError(err error) { 257 if s.dbErr == nil { 258 s.dbErr = err 259 } 260 } 261 262 func (s *StateDB) Error() error { 263 return s.dbErr 264 } 265 266 // Not thread safe 267 func (s *StateDB) Trie() (Trie, error) { 268 if s.trie == nil { 269 err := s.WaitPipeVerification() 270 if err != nil { 271 return nil, err 272 } 273 tr, err := s.db.OpenTrie(s.originalRoot) 274 if err != nil { 275 return nil, err 276 } 277 s.trie = tr 278 } 279 return s.trie, nil 280 } 281 282 func (s *StateDB) SetDiff(diffLayer *types.DiffLayer, diffTries map[common.Address]Trie, diffCode map[common.Hash][]byte) { 283 s.diffLayer, s.diffTries, s.diffCode = diffLayer, diffTries, diffCode 284 } 285 286 func (s *StateDB) SetSnapData(snapDestructs map[common.Address]struct{}, snapAccounts map[common.Address][]byte, 287 snapStorage map[common.Address]map[string][]byte) { 288 s.snapDestructs, s.snapAccounts, s.snapStorage = snapDestructs, snapAccounts, snapStorage 289 } 290 291 func (s *StateDB) AddLog(log *types.Log) { 292 s.journal.append(addLogChange{txhash: s.thash}) 293 294 log.TxHash = s.thash 295 log.BlockHash = s.bhash 296 log.TxIndex = uint(s.txIndex) 297 log.Index = s.logSize 298 s.logs[s.thash] = append(s.logs[s.thash], log) 299 s.logSize++ 300 } 301 302 func (s *StateDB) GetLogs(hash common.Hash) []*types.Log { 303 return s.logs[hash] 304 } 305 306 func (s *StateDB) Logs() []*types.Log { 307 var logs []*types.Log 308 for _, lgs := range s.logs { 309 logs = append(logs, lgs...) 310 } 311 return logs 312 } 313 314 // AddPreimage records a SHA3 preimage seen by the VM. 315 func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) { 316 if _, ok := s.preimages[hash]; !ok { 317 s.journal.append(addPreimageChange{hash: hash}) 318 pi := make([]byte, len(preimage)) 319 copy(pi, preimage) 320 s.preimages[hash] = pi 321 } 322 } 323 324 // Preimages returns a list of SHA3 preimages that have been submitted. 325 func (s *StateDB) Preimages() map[common.Hash][]byte { 326 return s.preimages 327 } 328 329 // AddRefund adds gas to the refund counter 330 func (s *StateDB) AddRefund(gas uint64) { 331 s.journal.append(refundChange{prev: s.refund}) 332 s.refund += gas 333 } 334 335 // SubRefund removes gas from the refund counter. 336 // This method will panic if the refund counter goes below zero 337 func (s *StateDB) SubRefund(gas uint64) { 338 s.journal.append(refundChange{prev: s.refund}) 339 if gas > s.refund { 340 panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund)) 341 } 342 s.refund -= gas 343 } 344 345 // Exist reports whether the given account address exists in the state. 346 // Notably this also returns true for suicided accounts. 347 func (s *StateDB) Exist(addr common.Address) bool { 348 return s.getStateObject(addr) != nil 349 } 350 351 // Empty returns whether the state object is either non-existent 352 // or empty according to the EIP161 specification (balance = nonce = code = 0) 353 func (s *StateDB) Empty(addr common.Address) bool { 354 so := s.getStateObject(addr) 355 return so == nil || so.empty() 356 } 357 358 // GetBalance retrieves the balance from the given address or 0 if object not found 359 func (s *StateDB) GetBalance(addr common.Address) *big.Int { 360 stateObject := s.getStateObject(addr) 361 if stateObject != nil { 362 return stateObject.Balance() 363 } 364 return common.Big0 365 } 366 367 func (s *StateDB) GetNonce(addr common.Address) uint64 { 368 stateObject := s.getStateObject(addr) 369 if stateObject != nil { 370 return stateObject.Nonce() 371 } 372 373 return 0 374 } 375 376 // TxIndex returns the current transaction index set by Prepare. 377 func (s *StateDB) TxIndex() int { 378 return s.txIndex 379 } 380 381 // BlockHash returns the current block hash set by Prepare. 382 func (s *StateDB) BlockHash() common.Hash { 383 return s.bhash 384 } 385 386 func (s *StateDB) GetCode(addr common.Address) []byte { 387 stateObject := s.getStateObject(addr) 388 if stateObject != nil { 389 return stateObject.Code(s.db) 390 } 391 return nil 392 } 393 394 func (s *StateDB) GetCodeSize(addr common.Address) int { 395 stateObject := s.getStateObject(addr) 396 if stateObject != nil { 397 return stateObject.CodeSize(s.db) 398 } 399 return 0 400 } 401 402 func (s *StateDB) GetCodeHash(addr common.Address) common.Hash { 403 stateObject := s.getStateObject(addr) 404 if stateObject == nil { 405 return common.Hash{} 406 } 407 return common.BytesToHash(stateObject.CodeHash()) 408 } 409 410 // GetState retrieves a value from the given account's storage trie. 411 func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash { 412 stateObject := s.getStateObject(addr) 413 if stateObject != nil { 414 return stateObject.GetState(s.db, hash) 415 } 416 return common.Hash{} 417 } 418 419 // GetProof returns the Merkle proof for a given account. 420 func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) { 421 return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes())) 422 } 423 424 // GetProofByHash returns the Merkle proof for a given account. 425 func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) { 426 var proof proofList 427 if _, err := s.Trie(); err != nil { 428 return nil, err 429 } 430 err := s.trie.Prove(addrHash[:], 0, &proof) 431 return proof, err 432 } 433 434 // GetStorageProof returns the Merkle proof for given storage slot. 435 func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) { 436 var proof proofList 437 trie := s.StorageTrie(a) 438 if trie == nil { 439 return proof, errors.New("storage trie for requested address does not exist") 440 } 441 err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) 442 return proof, err 443 } 444 445 // GetStorageProofByHash returns the Merkle proof for given storage slot. 446 func (s *StateDB) GetStorageProofByHash(a common.Address, key common.Hash) ([][]byte, error) { 447 var proof proofList 448 trie := s.StorageTrie(a) 449 if trie == nil { 450 return proof, errors.New("storage trie for requested address does not exist") 451 } 452 err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) 453 return proof, err 454 } 455 456 // GetCommittedState retrieves a value from the given account's committed storage trie. 457 func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash { 458 stateObject := s.getStateObject(addr) 459 if stateObject != nil { 460 return stateObject.GetCommittedState(s.db, hash) 461 } 462 return common.Hash{} 463 } 464 465 // Database retrieves the low level database supporting the lower level trie ops. 466 func (s *StateDB) Database() Database { 467 return s.db 468 } 469 470 // StorageTrie returns the storage trie of an account. 471 // The return value is a copy and is nil for non-existent accounts. 472 func (s *StateDB) StorageTrie(addr common.Address) Trie { 473 stateObject := s.getStateObject(addr) 474 if stateObject == nil { 475 return nil 476 } 477 cpy := stateObject.deepCopy(s) 478 cpy.updateTrie(s.db) 479 return cpy.getTrie(s.db) 480 } 481 482 func (s *StateDB) HasSuicided(addr common.Address) bool { 483 stateObject := s.getStateObject(addr) 484 if stateObject != nil { 485 return stateObject.suicided 486 } 487 return false 488 } 489 490 /* 491 * SETTERS 492 */ 493 494 // AddBalance adds amount to the account associated with addr. 495 func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { 496 stateObject := s.GetOrNewStateObject(addr) 497 if stateObject != nil { 498 stateObject.AddBalance(amount) 499 } 500 } 501 502 // SubBalance subtracts amount from the account associated with addr. 503 func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { 504 stateObject := s.GetOrNewStateObject(addr) 505 if stateObject != nil { 506 stateObject.SubBalance(amount) 507 } 508 } 509 510 func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) { 511 stateObject := s.GetOrNewStateObject(addr) 512 if stateObject != nil { 513 stateObject.SetBalance(amount) 514 } 515 } 516 517 func (s *StateDB) SetNonce(addr common.Address, nonce uint64) { 518 stateObject := s.GetOrNewStateObject(addr) 519 if stateObject != nil { 520 stateObject.SetNonce(nonce) 521 } 522 } 523 524 func (s *StateDB) SetCode(addr common.Address, code []byte) { 525 stateObject := s.GetOrNewStateObject(addr) 526 if stateObject != nil { 527 stateObject.SetCode(crypto.Keccak256Hash(code), code) 528 } 529 } 530 531 func (s *StateDB) SetState(addr common.Address, key, value common.Hash) { 532 stateObject := s.GetOrNewStateObject(addr) 533 if stateObject != nil { 534 stateObject.SetState(s.db, key, value) 535 } 536 } 537 538 // SetStorage replaces the entire storage for the specified account with given 539 // storage. This function should only be used for debugging. 540 func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) { 541 stateObject := s.GetOrNewStateObject(addr) 542 if stateObject != nil { 543 stateObject.SetStorage(storage) 544 } 545 } 546 547 // Suicide marks the given account as suicided. 548 // This clears the account balance. 549 // 550 // The account's state object is still available until the state is committed, 551 // getStateObject will return a non-nil account after Suicide. 552 func (s *StateDB) Suicide(addr common.Address) bool { 553 stateObject := s.getStateObject(addr) 554 if stateObject == nil { 555 return false 556 } 557 s.journal.append(suicideChange{ 558 account: &addr, 559 prev: stateObject.suicided, 560 prevbalance: new(big.Int).Set(stateObject.Balance()), 561 }) 562 stateObject.markSuicided() 563 stateObject.data.Balance = new(big.Int) 564 565 return true 566 } 567 568 // 569 // Setting, updating & deleting state object methods. 570 // 571 572 // updateStateObject writes the given object to the trie. 573 func (s *StateDB) updateStateObject(obj *StateObject) { 574 // Track the amount of time wasted on updating the account from the trie 575 if metrics.EnabledExpensive { 576 defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now()) 577 } 578 // Encode the account and update the account trie 579 addr := obj.Address() 580 data := obj.encodeData 581 var err error 582 if data == nil { 583 data, err = rlp.EncodeToBytes(obj) 584 if err != nil { 585 panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) 586 } 587 } 588 if err = s.trie.TryUpdate(addr[:], data); err != nil { 589 s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) 590 } 591 } 592 593 // deleteStateObject removes the given object from the state trie. 594 func (s *StateDB) deleteStateObject(obj *StateObject) { 595 // Track the amount of time wasted on deleting the account from the trie 596 if metrics.EnabledExpensive { 597 defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now()) 598 } 599 // Delete the account from the trie 600 addr := obj.Address() 601 if err := s.trie.TryDelete(addr[:]); err != nil { 602 s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err)) 603 } 604 } 605 606 // getStateObject retrieves a state object given by the address, returning nil if 607 // the object is not found or was deleted in this execution context. If you need 608 // to differentiate between non-existent/just-deleted, use getDeletedStateObject. 609 func (s *StateDB) getStateObject(addr common.Address) *StateObject { 610 if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted { 611 return obj 612 } 613 return nil 614 } 615 616 // getDeletedStateObject is similar to getStateObject, but instead of returning 617 // nil for a deleted state object, it returns the actual object with the deleted 618 // flag set. This is needed by the state journal to revert to the correct s- 619 // destructed object instead of wiping all knowledge about the state object. 620 func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject { 621 // Prefer live objects if any is available 622 if obj := s.stateObjects[addr]; obj != nil { 623 return obj 624 } 625 // If no live objects are available, attempt to use snapshots 626 var ( 627 data *Account 628 err error 629 ) 630 if s.snap != nil { 631 if metrics.EnabledExpensive { 632 defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now()) 633 } 634 var acc *snapshot.Account 635 if acc, err = s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())); err == nil { 636 if acc == nil { 637 return nil 638 } 639 data = &Account{ 640 Nonce: acc.Nonce, 641 Balance: acc.Balance, 642 CodeHash: acc.CodeHash, 643 Root: common.BytesToHash(acc.Root), 644 } 645 if len(data.CodeHash) == 0 { 646 data.CodeHash = emptyCodeHash 647 } 648 if data.Root == (common.Hash{}) { 649 data.Root = emptyRoot 650 } 651 } 652 } 653 // If snapshot unavailable or reading from it failed, load from the database 654 if s.snap == nil || err != nil { 655 if s.trie == nil { 656 tr, err := s.db.OpenTrie(s.originalRoot) 657 if err != nil { 658 s.setError(fmt.Errorf("failed to open trie tree")) 659 return nil 660 } 661 s.trie = tr 662 } 663 if metrics.EnabledExpensive { 664 defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now()) 665 } 666 enc, err := s.trie.TryGet(addr.Bytes()) 667 if err != nil { 668 s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err)) 669 return nil 670 } 671 if len(enc) == 0 { 672 return nil 673 } 674 data = new(Account) 675 if err := rlp.DecodeBytes(enc, data); err != nil { 676 log.Error("Failed to decode state object", "addr", addr, "err", err) 677 return nil 678 } 679 } 680 // Insert into the live set 681 obj := newObject(s, addr, *data) 682 s.SetStateObject(obj) 683 return obj 684 } 685 686 func (s *StateDB) SetStateObject(object *StateObject) { 687 s.stateObjects[object.Address()] = object 688 } 689 690 // GetOrNewStateObject retrieves a state object or create a new state object if nil. 691 func (s *StateDB) GetOrNewStateObject(addr common.Address) *StateObject { 692 stateObject := s.getStateObject(addr) 693 if stateObject == nil { 694 stateObject, _ = s.createObject(addr) 695 } 696 return stateObject 697 } 698 699 // createObject creates a new state object. If there is an existing account with 700 // the given address, it is overwritten and returned as the second return value. 701 func (s *StateDB) createObject(addr common.Address) (newobj, prev *StateObject) { 702 prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that! 703 704 var prevdestruct bool 705 if s.snap != nil && prev != nil { 706 _, prevdestruct = s.snapDestructs[prev.address] 707 if !prevdestruct { 708 s.snapDestructs[prev.address] = struct{}{} 709 } 710 } 711 newobj = newObject(s, addr, Account{}) 712 newobj.setNonce(0) // sets the object to dirty 713 if prev == nil { 714 s.journal.append(createObjectChange{account: &addr}) 715 } else { 716 s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct}) 717 } 718 s.SetStateObject(newobj) 719 if prev != nil && !prev.deleted { 720 return newobj, prev 721 } 722 return newobj, nil 723 } 724 725 // CreateAccount explicitly creates a state object. If a state object with the address 726 // already exists the balance is carried over to the new account. 727 // 728 // CreateAccount is called during the EVM CREATE operation. The situation might arise that 729 // a contract does the following: 730 // 731 // 1. sends funds to sha(account ++ (nonce + 1)) 732 // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1) 733 // 734 // Carrying over the balance ensures that Ether doesn't disappear. 735 func (s *StateDB) CreateAccount(addr common.Address) { 736 newObj, prev := s.createObject(addr) 737 if prev != nil { 738 newObj.setBalance(prev.data.Balance) 739 } 740 } 741 742 func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error { 743 so := db.getStateObject(addr) 744 if so == nil { 745 return nil 746 } 747 it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) 748 749 for it.Next() { 750 key := common.BytesToHash(db.trie.GetKey(it.Key)) 751 if value, dirty := so.dirtyStorage[key]; dirty { 752 if !cb(key, value) { 753 return nil 754 } 755 continue 756 } 757 758 if len(it.Value) > 0 { 759 _, content, _, err := rlp.Split(it.Value) 760 if err != nil { 761 return err 762 } 763 if !cb(key, common.BytesToHash(content)) { 764 return nil 765 } 766 } 767 } 768 return nil 769 } 770 771 // Copy creates a deep, independent copy of the state. 772 // Snapshots of the copied state cannot be applied to the copy. 773 func (s *StateDB) Copy() *StateDB { 774 // Copy all the basic fields, initialize the memory ones 775 state := &StateDB{ 776 db: s.db, 777 trie: s.db.CopyTrie(s.trie), 778 stateObjects: make(map[common.Address]*StateObject, len(s.journal.dirties)), 779 stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), 780 stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)), 781 storagePool: s.storagePool, 782 refund: s.refund, 783 logs: make(map[common.Hash][]*types.Log, len(s.logs)), 784 logSize: s.logSize, 785 preimages: make(map[common.Hash][]byte, len(s.preimages)), 786 journal: newJournal(), 787 hasher: crypto.NewKeccakState(), 788 } 789 // Copy the dirty states, logs, and preimages 790 for addr := range s.journal.dirties { 791 // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527), 792 // and in the Finalise-method, there is a case where an object is in the journal but not 793 // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for 794 // nil 795 if object, exist := s.stateObjects[addr]; exist { 796 // Even though the original object is dirty, we are not copying the journal, 797 // so we need to make sure that anyside effect the journal would have caused 798 // during a commit (or similar op) is already applied to the copy. 799 state.stateObjects[addr] = object.deepCopy(state) 800 801 state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits 802 state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits 803 } 804 } 805 // Above, we don't copy the actual journal. This means that if the copy is copied, the 806 // loop above will be a no-op, since the copy's journal is empty. 807 // Thus, here we iterate over stateObjects, to enable copies of copies 808 for addr := range s.stateObjectsPending { 809 if _, exist := state.stateObjects[addr]; !exist { 810 state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state) 811 } 812 state.stateObjectsPending[addr] = struct{}{} 813 } 814 for addr := range s.stateObjectsDirty { 815 if _, exist := state.stateObjects[addr]; !exist { 816 state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state) 817 } 818 state.stateObjectsDirty[addr] = struct{}{} 819 } 820 for hash, logs := range s.logs { 821 cpy := make([]*types.Log, len(logs)) 822 for i, l := range logs { 823 cpy[i] = new(types.Log) 824 *cpy[i] = *l 825 } 826 state.logs[hash] = cpy 827 } 828 for hash, preimage := range s.preimages { 829 state.preimages[hash] = preimage 830 } 831 // Do we need to copy the access list? In practice: No. At the start of a 832 // transaction, the access list is empty. In practice, we only ever copy state 833 // _between_ transactions/blocks, never in the middle of a transaction. 834 // However, it doesn't cost us much to copy an empty list, so we do it anyway 835 // to not blow up if we ever decide copy it in the middle of a transaction 836 if s.accessList != nil { 837 state.accessList = s.accessList.Copy() 838 } 839 840 // If there's a prefetcher running, make an inactive copy of it that can 841 // only access data but does not actively preload (since the user will not 842 // know that they need to explicitly terminate an active copy). 843 if s.prefetcher != nil { 844 state.prefetcher = s.prefetcher.copy() 845 } 846 if s.snaps != nil { 847 // In order for the miner to be able to use and make additions 848 // to the snapshot tree, we need to copy that aswell. 849 // Otherwise, any block mined by ourselves will cause gaps in the tree, 850 // and force the miner to operate trie-backed only 851 state.snaps = s.snaps 852 state.snap = s.snap 853 // deep copy needed 854 state.snapDestructs = make(map[common.Address]struct{}) 855 for k, v := range s.snapDestructs { 856 state.snapDestructs[k] = v 857 } 858 state.snapAccounts = make(map[common.Address][]byte) 859 for k, v := range s.snapAccounts { 860 state.snapAccounts[k] = v 861 } 862 state.snapStorage = make(map[common.Address]map[string][]byte) 863 for k, v := range s.snapStorage { 864 temp := make(map[string][]byte) 865 for kk, vv := range v { 866 temp[kk] = vv 867 } 868 state.snapStorage[k] = temp 869 } 870 } 871 return state 872 } 873 874 // Snapshot returns an identifier for the current revision of the state. 875 func (s *StateDB) Snapshot() int { 876 id := s.nextRevisionId 877 s.nextRevisionId++ 878 s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()}) 879 return id 880 } 881 882 // RevertToSnapshot reverts all state changes made since the given revision. 883 func (s *StateDB) RevertToSnapshot(revid int) { 884 // Find the snapshot in the stack of valid snapshots. 885 idx := sort.Search(len(s.validRevisions), func(i int) bool { 886 return s.validRevisions[i].id >= revid 887 }) 888 if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid { 889 panic(fmt.Errorf("revision id %v cannot be reverted", revid)) 890 } 891 snapshot := s.validRevisions[idx].journalIndex 892 893 // Replay the journal to undo changes and remove invalidated snapshots 894 s.journal.revert(s, snapshot) 895 s.validRevisions = s.validRevisions[:idx] 896 } 897 898 // GetRefund returns the current value of the refund counter. 899 func (s *StateDB) GetRefund() uint64 { 900 return s.refund 901 } 902 903 // GetRefund returns the current value of the refund counter. 904 func (s *StateDB) WaitPipeVerification() error { 905 // We need wait for the parent trie to commit 906 if s.snap != nil { 907 if valid := s.snap.WaitAndGetVerifyRes(); !valid { 908 return fmt.Errorf("verification on parent snap failed") 909 } 910 } 911 return nil 912 } 913 914 // Finalise finalises the state by removing the s destructed objects and clears 915 // the journal as well as the refunds. Finalise, however, will not push any updates 916 // into the tries just yet. Only IntermediateRoot or Commit will do that. 917 func (s *StateDB) Finalise(deleteEmptyObjects bool) { 918 addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties)) 919 for addr := range s.journal.dirties { 920 obj, exist := s.stateObjects[addr] 921 if !exist { 922 // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2 923 // That tx goes out of gas, and although the notion of 'touched' does not exist there, the 924 // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake, 925 // it will persist in the journal even though the journal is reverted. In this special circumstance, 926 // it may exist in `s.journal.dirties` but not in `s.stateObjects`. 927 // Thus, we can safely ignore it here 928 continue 929 } 930 if obj.suicided || (deleteEmptyObjects && obj.empty()) { 931 obj.deleted = true 932 933 // If state snapshotting is active, also mark the destruction there. 934 // Note, we can't do this only at the end of a block because multiple 935 // transactions within the same block might self destruct and then 936 // ressurrect an account; but the snapshotter needs both events. 937 if s.snap != nil { 938 s.snapDestructs[obj.address] = struct{}{} // We need to maintain account deletions explicitly (will remain set indefinitely) 939 delete(s.snapAccounts, obj.address) // Clear out any previously updated account data (may be recreated via a ressurrect) 940 delete(s.snapStorage, obj.address) // Clear out any previously updated storage data (may be recreated via a ressurrect) 941 } 942 } else { 943 obj.finalise(true) // Prefetch slots in the background 944 } 945 if _, exist := s.stateObjectsPending[addr]; !exist { 946 s.stateObjectsPending[addr] = struct{}{} 947 } 948 if _, exist := s.stateObjectsDirty[addr]; !exist { 949 s.stateObjectsDirty[addr] = struct{}{} 950 // At this point, also ship the address off to the precacher. The precacher 951 // will start loading tries, and when the change is eventually committed, 952 // the commit-phase will be a lot faster 953 addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure 954 } 955 } 956 if s.prefetcher != nil && len(addressesToPrefetch) > 0 { 957 s.prefetcher.prefetch(s.originalRoot, addressesToPrefetch, emptyAddr) 958 } 959 // Invalidate journal because reverting across transactions is not allowed. 960 s.clearJournalAndRefund() 961 } 962 963 // IntermediateRoot computes the current root hash of the state trie. 964 // It is called in between transactions to get the root hash that 965 // goes into transaction receipts. 966 func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { 967 if s.lightProcessed { 968 s.StopPrefetcher() 969 return s.trie.Hash() 970 } 971 // Finalise all the dirty storage states and write them into the tries 972 s.Finalise(deleteEmptyObjects) 973 s.AccountsIntermediateRoot() 974 return s.StateIntermediateRoot() 975 } 976 977 //CorrectAccountsRoot will fix account roots in pipecommit mode 978 func (s *StateDB) CorrectAccountsRoot() { 979 if accounts, err := s.snap.Accounts(); err == nil && accounts != nil { 980 for _, obj := range s.stateObjects { 981 if !obj.deleted && !obj.rootCorrected && obj.data.Root == dummyRoot { 982 if account, exist := accounts[crypto.Keccak256Hash(obj.address[:])]; exist && len(account.Root) != 0 { 983 obj.data.Root = common.BytesToHash(account.Root) 984 } 985 } 986 } 987 } 988 } 989 990 //PopulateSnapAccountAndStorage tries to populate required accounts and storages for pipecommit 991 func (s *StateDB) PopulateSnapAccountAndStorage() { 992 for addr := range s.stateObjectsPending { 993 if obj := s.stateObjects[addr]; !obj.deleted { 994 if s.snap != nil && !obj.deleted { 995 root := obj.data.Root 996 storageChanged := s.populateSnapStorage(obj) 997 if storageChanged { 998 root = dummyRoot 999 } 1000 s.snapAccounts[obj.address] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, root, obj.data.CodeHash) 1001 } 1002 } 1003 } 1004 } 1005 1006 //populateSnapStorage tries to populate required storages for pipecommit, and returns a flag to indicate whether the storage root changed or not 1007 func (s *StateDB) populateSnapStorage(obj *StateObject) bool { 1008 for key, value := range obj.dirtyStorage { 1009 obj.pendingStorage[key] = value 1010 } 1011 if len(obj.pendingStorage) == 0 { 1012 return false 1013 } 1014 var storage map[string][]byte 1015 for key, value := range obj.pendingStorage { 1016 var v []byte 1017 if (value != common.Hash{}) { 1018 // Encoding []byte cannot fail, ok to ignore the error. 1019 v, _ = rlp.EncodeToBytes(common.TrimLeftZeroes(value[:])) 1020 } 1021 // If state snapshotting is active, cache the data til commit 1022 if obj.db.snap != nil { 1023 if storage == nil { 1024 // Retrieve the old storage map, if available, create a new one otherwise 1025 if storage = obj.db.snapStorage[obj.address]; storage == nil { 1026 storage = make(map[string][]byte) 1027 obj.db.snapStorage[obj.address] = storage 1028 } 1029 } 1030 storage[string(key[:])] = v // v will be nil if value is 0x00 1031 } 1032 } 1033 return true 1034 } 1035 1036 func (s *StateDB) AccountsIntermediateRoot() { 1037 tasks := make(chan func()) 1038 finishCh := make(chan struct{}) 1039 defer close(finishCh) 1040 wg := sync.WaitGroup{} 1041 for i := 0; i < runtime.NumCPU(); i++ { 1042 go func() { 1043 for { 1044 select { 1045 case task := <-tasks: 1046 task() 1047 case <-finishCh: 1048 return 1049 } 1050 } 1051 }() 1052 } 1053 1054 // Although naively it makes sense to retrieve the account trie and then do 1055 // the contract storage and account updates sequentially, that short circuits 1056 // the account prefetcher. Instead, let's process all the storage updates 1057 // first, giving the account prefeches just a few more milliseconds of time 1058 // to pull useful data from disk. 1059 for addr := range s.stateObjectsPending { 1060 if obj := s.stateObjects[addr]; !obj.deleted { 1061 wg.Add(1) 1062 tasks <- func() { 1063 obj.updateRoot(s.db) 1064 1065 // If state snapshotting is active, cache the data til commit. Note, this 1066 // update mechanism is not symmetric to the deletion, because whereas it is 1067 // enough to track account updates at commit time, deletions need tracking 1068 // at transaction boundary level to ensure we capture state clearing. 1069 if s.snap != nil && !obj.deleted { 1070 s.snapMux.Lock() 1071 // It is possible to add unnecessary change, but it is fine. 1072 s.snapAccounts[obj.address] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash) 1073 s.snapMux.Unlock() 1074 } 1075 data, err := rlp.EncodeToBytes(obj) 1076 if err != nil { 1077 panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) 1078 } 1079 obj.encodeData = data 1080 wg.Done() 1081 } 1082 } 1083 } 1084 wg.Wait() 1085 } 1086 1087 func (s *StateDB) StateIntermediateRoot() common.Hash { 1088 // If there was a trie prefetcher operating, it gets aborted and irrevocably 1089 // modified after we start retrieving tries. Remove it from the statedb after 1090 // this round of use. 1091 // 1092 // This is weird pre-byzantium since the first tx runs with a prefetcher and 1093 // the remainder without, but pre-byzantium even the initial prefetcher is 1094 // useless, so no sleep lost. 1095 prefetcher := s.prefetcher 1096 defer func() { 1097 s.prefetcherLock.Lock() 1098 if s.prefetcher != nil { 1099 s.prefetcher.close() 1100 s.prefetcher = nil 1101 } 1102 // try not use defer inside defer 1103 s.prefetcherLock.Unlock() 1104 }() 1105 1106 // Now we're about to start to write changes to the trie. The trie is so far 1107 // _untouched_. We can check with the prefetcher, if it can give us a trie 1108 // which has the same root, but also has some content loaded into it. 1109 if prefetcher != nil { 1110 if trie := prefetcher.trie(s.originalRoot); trie != nil { 1111 s.trie = trie 1112 } 1113 } 1114 if s.trie == nil { 1115 tr, err := s.db.OpenTrie(s.originalRoot) 1116 if err != nil { 1117 panic(fmt.Sprintf("Failed to open trie tree %s", s.originalRoot)) 1118 } 1119 s.trie = tr 1120 } 1121 1122 usedAddrs := make([][]byte, 0, len(s.stateObjectsPending)) 1123 for addr := range s.stateObjectsPending { 1124 if obj := s.stateObjects[addr]; obj.deleted { 1125 s.deleteStateObject(obj) 1126 } else { 1127 s.updateStateObject(obj) 1128 } 1129 usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure 1130 } 1131 if prefetcher != nil { 1132 prefetcher.used(s.originalRoot, usedAddrs) 1133 } 1134 1135 if len(s.stateObjectsPending) > 0 { 1136 s.stateObjectsPending = make(map[common.Address]struct{}) 1137 } 1138 // Track the amount of time wasted on hashing the account trie 1139 if metrics.EnabledExpensive { 1140 defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now()) 1141 } 1142 root := s.trie.Hash() 1143 return root 1144 } 1145 1146 // Prepare sets the current transaction hash and index and block hash which is 1147 // used when the EVM emits new state logs. 1148 func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) { 1149 s.thash = thash 1150 s.bhash = bhash 1151 s.txIndex = ti 1152 s.accessList = nil 1153 } 1154 1155 func (s *StateDB) clearJournalAndRefund() { 1156 if len(s.journal.entries) > 0 { 1157 s.journal = newJournal() 1158 s.refund = 0 1159 } 1160 s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entires 1161 } 1162 1163 func (s *StateDB) LightCommit() (common.Hash, *types.DiffLayer, error) { 1164 codeWriter := s.db.TrieDB().DiskDB().NewBatch() 1165 1166 // light process already verified it, expectedRoot is trustworthy. 1167 root := s.expectedRoot 1168 1169 commitFuncs := []func() error{ 1170 func() error { 1171 for codeHash, code := range s.diffCode { 1172 rawdb.WriteCode(codeWriter, codeHash, code) 1173 if codeWriter.ValueSize() >= ethdb.IdealBatchSize { 1174 if err := codeWriter.Write(); err != nil { 1175 return err 1176 } 1177 codeWriter.Reset() 1178 } 1179 } 1180 if codeWriter.ValueSize() > 0 { 1181 if err := codeWriter.Write(); err != nil { 1182 return err 1183 } 1184 } 1185 return nil 1186 }, 1187 func() error { 1188 tasks := make(chan func()) 1189 taskResults := make(chan error, len(s.diffTries)) 1190 tasksNum := 0 1191 finishCh := make(chan struct{}) 1192 defer close(finishCh) 1193 threads := gopool.Threads(len(s.diffTries)) 1194 1195 for i := 0; i < threads; i++ { 1196 go func() { 1197 for { 1198 select { 1199 case task := <-tasks: 1200 task() 1201 case <-finishCh: 1202 return 1203 } 1204 } 1205 }() 1206 } 1207 1208 for account, diff := range s.diffTries { 1209 tmpAccount := account 1210 tmpDiff := diff 1211 tasks <- func() { 1212 root, err := tmpDiff.Commit(nil) 1213 if err != nil { 1214 taskResults <- err 1215 return 1216 } 1217 s.db.CacheStorage(crypto.Keccak256Hash(tmpAccount[:]), root, tmpDiff) 1218 taskResults <- nil 1219 } 1220 tasksNum++ 1221 } 1222 1223 for i := 0; i < tasksNum; i++ { 1224 err := <-taskResults 1225 if err != nil { 1226 return err 1227 } 1228 } 1229 1230 // commit account trie 1231 var account Account 1232 root, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error { 1233 if err := rlp.DecodeBytes(leaf, &account); err != nil { 1234 return nil 1235 } 1236 if account.Root != emptyRoot { 1237 s.db.TrieDB().Reference(account.Root, parent) 1238 } 1239 return nil 1240 }) 1241 if err != nil { 1242 return err 1243 } 1244 if root != emptyRoot { 1245 s.db.CacheAccount(root, s.trie) 1246 } 1247 return nil 1248 }, 1249 func() error { 1250 if s.snap != nil { 1251 if metrics.EnabledExpensive { 1252 defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now()) 1253 } 1254 // Only update if there's a state transition (skip empty Clique blocks) 1255 if parent := s.snap.Root(); parent != root { 1256 // for light commit, always do sync commit 1257 if err := s.snaps.Update(root, parent, s.snapDestructs, s.snapAccounts, s.snapStorage, nil); err != nil { 1258 log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err) 1259 } 1260 // Keep n diff layers in the memory 1261 // - head layer is paired with HEAD state 1262 // - head-1 layer is paired with HEAD-1 state 1263 // - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state 1264 if err := s.snaps.Cap(root, s.snaps.CapLimit()); err != nil { 1265 log.Warn("Failed to cap snapshot tree", "root", root, "layers", s.snaps.CapLimit(), "err", err) 1266 } 1267 } 1268 } 1269 return nil 1270 }, 1271 } 1272 commitRes := make(chan error, len(commitFuncs)) 1273 for _, f := range commitFuncs { 1274 tmpFunc := f 1275 go func() { 1276 commitRes <- tmpFunc() 1277 }() 1278 } 1279 for i := 0; i < len(commitFuncs); i++ { 1280 r := <-commitRes 1281 if r != nil { 1282 return common.Hash{}, nil, r 1283 } 1284 } 1285 s.snap, s.snapDestructs, s.snapAccounts, s.snapStorage = nil, nil, nil, nil 1286 s.diffTries, s.diffCode = nil, nil 1287 return root, s.diffLayer, nil 1288 } 1289 1290 // Commit writes the state to the underlying in-memory trie database. 1291 func (s *StateDB) Commit(failPostCommitFunc func(), postCommitFuncs ...func() error) (common.Hash, *types.DiffLayer, error) { 1292 if s.dbErr != nil { 1293 return common.Hash{}, nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr) 1294 } 1295 // Finalize any pending changes and merge everything into the tries 1296 if s.lightProcessed { 1297 root, diff, err := s.LightCommit() 1298 if err != nil { 1299 return root, diff, err 1300 } 1301 for _, postFunc := range postCommitFuncs { 1302 err = postFunc() 1303 if err != nil { 1304 return root, diff, err 1305 } 1306 } 1307 return root, diff, nil 1308 } 1309 var diffLayer *types.DiffLayer 1310 var verified chan struct{} 1311 var snapUpdated chan struct{} 1312 1313 if s.snap != nil { 1314 diffLayer = &types.DiffLayer{} 1315 } 1316 if s.pipeCommit { 1317 // async commit the MPT 1318 verified = make(chan struct{}) 1319 snapUpdated = make(chan struct{}) 1320 } 1321 1322 commmitTrie := func() error { 1323 commitErr := func() error { 1324 if s.pipeCommit { 1325 <-snapUpdated 1326 // Due to state verification pipeline, the accounts roots are not updated, leading to the data in the difflayer is not correct, capture the correct data here 1327 s.AccountsIntermediateRoot() 1328 if parent := s.snap.Root(); parent != s.expectedRoot { 1329 accountData := make(map[common.Hash][]byte) 1330 for k, v := range s.snapAccounts { 1331 accountData[crypto.Keccak256Hash(k[:])] = v 1332 } 1333 s.snaps.Snapshot(s.expectedRoot).CorrectAccounts(accountData) 1334 } 1335 } 1336 1337 if s.stateRoot = s.StateIntermediateRoot(); s.fullProcessed && s.expectedRoot != s.stateRoot { 1338 log.Error("Invalid merkle root", "remote", s.expectedRoot, "local", s.stateRoot) 1339 return fmt.Errorf("invalid merkle root (remote: %x local: %x)", s.expectedRoot, s.stateRoot) 1340 } 1341 1342 tasks := make(chan func()) 1343 taskResults := make(chan error, len(s.stateObjectsDirty)) 1344 tasksNum := 0 1345 finishCh := make(chan struct{}) 1346 1347 threads := gopool.Threads(len(s.stateObjectsDirty)) 1348 wg := sync.WaitGroup{} 1349 for i := 0; i < threads; i++ { 1350 wg.Add(1) 1351 go func() { 1352 defer wg.Done() 1353 for { 1354 select { 1355 case task := <-tasks: 1356 task() 1357 case <-finishCh: 1358 return 1359 } 1360 } 1361 }() 1362 } 1363 1364 for addr := range s.stateObjectsDirty { 1365 if obj := s.stateObjects[addr]; !obj.deleted { 1366 // Write any contract code associated with the state object 1367 tasks <- func() { 1368 // Write any storage changes in the state object to its storage trie 1369 err := obj.CommitTrie(s.db) 1370 taskResults <- err 1371 } 1372 tasksNum++ 1373 } 1374 } 1375 1376 for i := 0; i < tasksNum; i++ { 1377 err := <-taskResults 1378 if err != nil { 1379 close(finishCh) 1380 return err 1381 } 1382 } 1383 close(finishCh) 1384 1385 // The onleaf func is called _serially_, so we can reuse the same account 1386 // for unmarshalling every time. 1387 var account Account 1388 root, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error { 1389 if err := rlp.DecodeBytes(leaf, &account); err != nil { 1390 return nil 1391 } 1392 if account.Root != emptyRoot { 1393 s.db.TrieDB().Reference(account.Root, parent) 1394 } 1395 return nil 1396 }) 1397 if err != nil { 1398 return err 1399 } 1400 if root != emptyRoot { 1401 s.db.CacheAccount(root, s.trie) 1402 } 1403 for _, postFunc := range postCommitFuncs { 1404 err = postFunc() 1405 if err != nil { 1406 return err 1407 } 1408 } 1409 wg.Wait() 1410 return nil 1411 }() 1412 1413 if s.pipeCommit { 1414 if commitErr == nil { 1415 s.snaps.Snapshot(s.stateRoot).MarkValid() 1416 close(verified) 1417 } else { 1418 // The blockchain will do the further rewind if write block not finish yet 1419 close(verified) 1420 if failPostCommitFunc != nil { 1421 failPostCommitFunc() 1422 } 1423 log.Error("state verification failed", "err", commitErr) 1424 } 1425 } 1426 return commitErr 1427 } 1428 1429 commitFuncs := []func() error{ 1430 func() error { 1431 codeWriter := s.db.TrieDB().DiskDB().NewBatch() 1432 for addr := range s.stateObjectsDirty { 1433 if obj := s.stateObjects[addr]; !obj.deleted { 1434 if obj.code != nil && obj.dirtyCode { 1435 rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code) 1436 obj.dirtyCode = false 1437 if s.snap != nil { 1438 diffLayer.Codes = append(diffLayer.Codes, types.DiffCode{ 1439 Hash: common.BytesToHash(obj.CodeHash()), 1440 Code: obj.code, 1441 }) 1442 } 1443 if codeWriter.ValueSize() > ethdb.IdealBatchSize { 1444 if err := codeWriter.Write(); err != nil { 1445 return err 1446 } 1447 codeWriter.Reset() 1448 } 1449 } 1450 } 1451 } 1452 if codeWriter.ValueSize() > 0 { 1453 if err := codeWriter.Write(); err != nil { 1454 log.Crit("Failed to commit dirty codes", "error", err) 1455 return err 1456 } 1457 } 1458 return nil 1459 }, 1460 func() error { 1461 // If snapshotting is enabled, update the snapshot tree with this new version 1462 if s.snap != nil { 1463 if metrics.EnabledExpensive { 1464 defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now()) 1465 } 1466 if s.pipeCommit { 1467 defer close(snapUpdated) 1468 } 1469 diffLayer.Destructs, diffLayer.Accounts, diffLayer.Storages = s.SnapToDiffLayer() 1470 // Only update if there's a state transition (skip empty Clique blocks) 1471 if parent := s.snap.Root(); parent != s.expectedRoot { 1472 err := s.snaps.Update(s.expectedRoot, parent, s.snapDestructs, s.snapAccounts, s.snapStorage, verified) 1473 1474 if err != nil { 1475 log.Warn("Failed to update snapshot tree", "from", parent, "to", s.expectedRoot, "err", err) 1476 } 1477 1478 // Keep n diff layers in the memory 1479 // - head layer is paired with HEAD state 1480 // - head-1 layer is paired with HEAD-1 state 1481 // - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state 1482 go func() { 1483 if err := s.snaps.Cap(s.expectedRoot, s.snaps.CapLimit()); err != nil { 1484 log.Warn("Failed to cap snapshot tree", "root", s.expectedRoot, "layers", s.snaps.CapLimit(), "err", err) 1485 } 1486 }() 1487 } 1488 } 1489 return nil 1490 }, 1491 } 1492 if s.pipeCommit { 1493 go commmitTrie() 1494 } else { 1495 commitFuncs = append(commitFuncs, commmitTrie) 1496 } 1497 commitRes := make(chan error, len(commitFuncs)) 1498 for _, f := range commitFuncs { 1499 tmpFunc := f 1500 go func() { 1501 commitRes <- tmpFunc() 1502 }() 1503 } 1504 for i := 0; i < len(commitFuncs); i++ { 1505 r := <-commitRes 1506 if r != nil { 1507 return common.Hash{}, nil, r 1508 } 1509 } 1510 root := s.stateRoot 1511 if s.pipeCommit { 1512 root = s.expectedRoot 1513 } 1514 1515 return root, diffLayer, nil 1516 } 1517 1518 func (s *StateDB) DiffLayerToSnap(diffLayer *types.DiffLayer) (map[common.Address]struct{}, map[common.Address][]byte, map[common.Address]map[string][]byte, error) { 1519 snapDestructs := make(map[common.Address]struct{}) 1520 snapAccounts := make(map[common.Address][]byte) 1521 snapStorage := make(map[common.Address]map[string][]byte) 1522 1523 for _, des := range diffLayer.Destructs { 1524 snapDestructs[des] = struct{}{} 1525 } 1526 for _, account := range diffLayer.Accounts { 1527 snapAccounts[account.Account] = account.Blob 1528 } 1529 for _, storage := range diffLayer.Storages { 1530 // should never happen 1531 if len(storage.Keys) != len(storage.Vals) { 1532 return nil, nil, nil, errors.New("invalid diffLayer: length of keys and values mismatch") 1533 } 1534 snapStorage[storage.Account] = make(map[string][]byte, len(storage.Keys)) 1535 n := len(storage.Keys) 1536 for i := 0; i < n; i++ { 1537 snapStorage[storage.Account][storage.Keys[i]] = storage.Vals[i] 1538 } 1539 } 1540 return snapDestructs, snapAccounts, snapStorage, nil 1541 } 1542 1543 func (s *StateDB) SnapToDiffLayer() ([]common.Address, []types.DiffAccount, []types.DiffStorage) { 1544 destructs := make([]common.Address, 0, len(s.snapDestructs)) 1545 for account := range s.snapDestructs { 1546 destructs = append(destructs, account) 1547 } 1548 accounts := make([]types.DiffAccount, 0, len(s.snapAccounts)) 1549 for accountHash, account := range s.snapAccounts { 1550 accounts = append(accounts, types.DiffAccount{ 1551 Account: accountHash, 1552 Blob: account, 1553 }) 1554 } 1555 storages := make([]types.DiffStorage, 0, len(s.snapStorage)) 1556 for accountHash, storage := range s.snapStorage { 1557 keys := make([]string, 0, len(storage)) 1558 values := make([][]byte, 0, len(storage)) 1559 for k, v := range storage { 1560 keys = append(keys, k) 1561 values = append(values, v) 1562 } 1563 storages = append(storages, types.DiffStorage{ 1564 Account: accountHash, 1565 Keys: keys, 1566 Vals: values, 1567 }) 1568 } 1569 return destructs, accounts, storages 1570 } 1571 1572 // PrepareAccessList handles the preparatory steps for executing a state transition with 1573 // regards to both EIP-2929 and EIP-2930: 1574 // 1575 // - Add sender to access list (2929) 1576 // - Add destination to access list (2929) 1577 // - Add precompiles to access list (2929) 1578 // - Add the contents of the optional tx access list (2930) 1579 // 1580 // This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number. 1581 func (s *StateDB) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) { 1582 s.AddAddressToAccessList(sender) 1583 if dst != nil { 1584 s.AddAddressToAccessList(*dst) 1585 // If it's a create-tx, the destination will be added inside evm.create 1586 } 1587 for _, addr := range precompiles { 1588 s.AddAddressToAccessList(addr) 1589 } 1590 for _, el := range list { 1591 s.AddAddressToAccessList(el.Address) 1592 for _, key := range el.StorageKeys { 1593 s.AddSlotToAccessList(el.Address, key) 1594 } 1595 } 1596 } 1597 1598 // AddAddressToAccessList adds the given address to the access list 1599 func (s *StateDB) AddAddressToAccessList(addr common.Address) { 1600 if s.accessList == nil { 1601 s.accessList = newAccessList() 1602 } 1603 if s.accessList.AddAddress(addr) { 1604 s.journal.append(accessListAddAccountChange{&addr}) 1605 } 1606 } 1607 1608 // AddSlotToAccessList adds the given (address, slot)-tuple to the access list 1609 func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) { 1610 if s.accessList == nil { 1611 s.accessList = newAccessList() 1612 } 1613 addrMod, slotMod := s.accessList.AddSlot(addr, slot) 1614 if addrMod { 1615 // In practice, this should not happen, since there is no way to enter the 1616 // scope of 'address' without having the 'address' become already added 1617 // to the access list (via call-variant, create, etc). 1618 // Better safe than sorry, though 1619 s.journal.append(accessListAddAccountChange{&addr}) 1620 } 1621 if slotMod { 1622 s.journal.append(accessListAddSlotChange{ 1623 address: &addr, 1624 slot: &slot, 1625 }) 1626 } 1627 } 1628 1629 // AddressInAccessList returns true if the given address is in the access list. 1630 func (s *StateDB) AddressInAccessList(addr common.Address) bool { 1631 if s.accessList == nil { 1632 return false 1633 } 1634 return s.accessList.ContainsAddress(addr) 1635 } 1636 1637 // SlotInAccessList returns true if the given (address, slot)-tuple is in the access list. 1638 func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) { 1639 if s.accessList == nil { 1640 return false, false 1641 } 1642 return s.accessList.Contains(addr, slot) 1643 } 1644 1645 func (s *StateDB) GetDirtyAccounts() []common.Address { 1646 accounts := make([]common.Address, 0, len(s.stateObjectsDirty)) 1647 for account := range s.stateObjectsDirty { 1648 accounts = append(accounts, account) 1649 } 1650 return accounts 1651 } 1652 1653 func (s *StateDB) GetStorage(address common.Address) *sync.Map { 1654 return s.storagePool.getStorage(address) 1655 }