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