github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/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 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/log" 30 "github.com/ethereum/go-ethereum/rlp" 31 "github.com/ethereum/go-ethereum/trie" 32 ) 33 34 type revision struct { 35 id int 36 journalIndex int 37 } 38 39 var ( 40 // emptyState is the known hash of an empty state trie entry. 41 emptyState = crypto.Keccak256Hash(nil) 42 43 // emptyCode is the known hash of the empty EVM bytecode. 44 emptyCode = crypto.Keccak256Hash(nil) 45 ) 46 47 type proofList [][]byte 48 49 func (n *proofList) Put(key []byte, value []byte) error { 50 *n = append(*n, value) 51 return nil 52 } 53 54 // StateDBs within the ethereum protocol are used to store anything 55 // within the merkle trie. StateDBs take care of caching and storing 56 // nested states. It's the general query interface to retrieve: 57 // * Contracts 58 // * Accounts 59 type StateDB struct { 60 db Database 61 trie Trie 62 63 // This map holds 'live' objects, which will get modified while processing a state transition. 64 stateObjects map[common.Address]*stateObject 65 stateObjectsDirty map[common.Address]struct{} 66 67 // DB error. 68 // State objects are used by the consensus core and VM which are 69 // unable to deal with database-level errors. Any error that occurs 70 // during a database read is memoized here and will eventually be returned 71 // by StateDB.Commit. 72 dbErr error 73 74 // The refund counter, also used by state transitioning. 75 refund uint64 76 77 thash, bhash common.Hash 78 txIndex int 79 logs map[common.Hash][]*types.Log 80 logSize uint 81 82 preimages map[common.Hash][]byte 83 84 // Journal of state modifications. This is the backbone of 85 // Snapshot and RevertToSnapshot. 86 journal *journal 87 validRevisions []revision 88 nextRevisionId int 89 } 90 91 // Create a new state from a given trie. 92 func New(root common.Hash, db Database) (*StateDB, error) { 93 tr, err := db.OpenTrie(root) 94 if err != nil { 95 return nil, err 96 } 97 return &StateDB{ 98 db: db, 99 trie: tr, 100 stateObjects: make(map[common.Address]*stateObject), 101 stateObjectsDirty: make(map[common.Address]struct{}), 102 logs: make(map[common.Hash][]*types.Log), 103 preimages: make(map[common.Hash][]byte), 104 journal: newJournal(), 105 }, nil 106 } 107 108 // setError remembers the first non-nil error it is called with. 109 func (self *StateDB) setError(err error) { 110 if self.dbErr == nil { 111 self.dbErr = err 112 } 113 } 114 115 func (self *StateDB) Error() error { 116 return self.dbErr 117 } 118 119 // Reset clears out all ephemeral state objects from the state db, but keeps 120 // the underlying state trie to avoid reloading data for the next operations. 121 func (self *StateDB) Reset(root common.Hash) error { 122 tr, err := self.db.OpenTrie(root) 123 if err != nil { 124 return err 125 } 126 self.trie = tr 127 self.stateObjects = make(map[common.Address]*stateObject) 128 self.stateObjectsDirty = make(map[common.Address]struct{}) 129 self.thash = common.Hash{} 130 self.bhash = common.Hash{} 131 self.txIndex = 0 132 self.logs = make(map[common.Hash][]*types.Log) 133 self.logSize = 0 134 self.preimages = make(map[common.Hash][]byte) 135 self.clearJournalAndRefund() 136 return nil 137 } 138 139 func (self *StateDB) AddLog(log *types.Log) { 140 self.journal.append(addLogChange{txhash: self.thash}) 141 142 log.TxHash = self.thash 143 log.BlockHash = self.bhash 144 log.TxIndex = uint(self.txIndex) 145 log.Index = self.logSize 146 self.logs[self.thash] = append(self.logs[self.thash], log) 147 self.logSize++ 148 } 149 150 func (self *StateDB) GetLogs(hash common.Hash) []*types.Log { 151 return self.logs[hash] 152 } 153 154 func (self *StateDB) Logs() []*types.Log { 155 var logs []*types.Log 156 for _, lgs := range self.logs { 157 logs = append(logs, lgs...) 158 } 159 return logs 160 } 161 162 // AddPreimage records a SHA3 preimage seen by the VM. 163 func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) { 164 if _, ok := self.preimages[hash]; !ok { 165 self.journal.append(addPreimageChange{hash: hash}) 166 pi := make([]byte, len(preimage)) 167 copy(pi, preimage) 168 self.preimages[hash] = pi 169 } 170 } 171 172 // Preimages returns a list of SHA3 preimages that have been submitted. 173 func (self *StateDB) Preimages() map[common.Hash][]byte { 174 return self.preimages 175 } 176 177 // AddRefund adds gas to the refund counter 178 func (self *StateDB) AddRefund(gas uint64) { 179 self.journal.append(refundChange{prev: self.refund}) 180 self.refund += gas 181 } 182 183 // SubRefund removes gas from the refund counter. 184 // This method will panic if the refund counter goes below zero 185 func (self *StateDB) SubRefund(gas uint64) { 186 self.journal.append(refundChange{prev: self.refund}) 187 if gas > self.refund { 188 panic("Refund counter below zero") 189 } 190 self.refund -= gas 191 } 192 193 // Exist reports whether the given account address exists in the state. 194 // Notably this also returns true for suicided accounts. 195 func (self *StateDB) Exist(addr common.Address) bool { 196 return self.getStateObject(addr) != nil 197 } 198 199 // Empty returns whether the state object is either non-existent 200 // or empty according to the EIP161 specification (balance = nonce = code = 0) 201 func (self *StateDB) Empty(addr common.Address) bool { 202 so := self.getStateObject(addr) 203 return so == nil || so.empty() 204 } 205 206 // Retrieve the balance from the given address or 0 if object not found 207 func (self *StateDB) GetBalance(addr common.Address) *big.Int { 208 stateObject := self.getStateObject(addr) 209 if stateObject != nil { 210 return stateObject.Balance() 211 } 212 return common.Big0 213 } 214 215 func (self *StateDB) GetNonce(addr common.Address) uint64 { 216 stateObject := self.getStateObject(addr) 217 if stateObject != nil { 218 return stateObject.Nonce() 219 } 220 221 return 0 222 } 223 224 func (self *StateDB) GetCode(addr common.Address) []byte { 225 stateObject := self.getStateObject(addr) 226 if stateObject != nil { 227 return stateObject.Code(self.db) 228 } 229 return nil 230 } 231 232 func (self *StateDB) GetCodeSize(addr common.Address) int { 233 stateObject := self.getStateObject(addr) 234 if stateObject == nil { 235 return 0 236 } 237 if stateObject.code != nil { 238 return len(stateObject.code) 239 } 240 size, err := self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash())) 241 if err != nil { 242 self.setError(err) 243 } 244 return size 245 } 246 247 func (self *StateDB) GetCodeHash(addr common.Address) common.Hash { 248 stateObject := self.getStateObject(addr) 249 if stateObject == nil { 250 return common.Hash{} 251 } 252 return common.BytesToHash(stateObject.CodeHash()) 253 } 254 255 // GetState retrieves a value from the given account's storage trie. 256 func (self *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash { 257 stateObject := self.getStateObject(addr) 258 if stateObject != nil { 259 return stateObject.GetState(self.db, hash) 260 } 261 return common.Hash{} 262 } 263 264 // GetProof returns the MerkleProof for a given Account 265 func (self *StateDB) GetProof(a common.Address) ([][]byte, error) { 266 var proof proofList 267 err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) 268 return [][]byte(proof), err 269 } 270 271 // GetProof returns the StorageProof for given key 272 func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) { 273 var proof proofList 274 trie := self.StorageTrie(a) 275 if trie == nil { 276 return proof, errors.New("storage trie for requested address does not exist") 277 } 278 err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof) 279 return [][]byte(proof), err 280 } 281 282 // GetCommittedState retrieves a value from the given account's committed storage trie. 283 func (self *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash { 284 stateObject := self.getStateObject(addr) 285 if stateObject != nil { 286 return stateObject.GetCommittedState(self.db, hash) 287 } 288 return common.Hash{} 289 } 290 291 // Database retrieves the low level database supporting the lower level trie ops. 292 func (self *StateDB) Database() Database { 293 return self.db 294 } 295 296 // StorageTrie returns the storage trie of an account. 297 // The return value is a copy and is nil for non-existent accounts. 298 func (self *StateDB) StorageTrie(addr common.Address) Trie { 299 stateObject := self.getStateObject(addr) 300 if stateObject == nil { 301 return nil 302 } 303 cpy := stateObject.deepCopy(self) 304 return cpy.updateTrie(self.db) 305 } 306 307 func (self *StateDB) HasSuicided(addr common.Address) bool { 308 stateObject := self.getStateObject(addr) 309 if stateObject != nil { 310 return stateObject.suicided 311 } 312 return false 313 } 314 315 // GetStorageRoot returns the root of the storage associated with the given address. 316 func (self *StateDB) GetStorageRoot(addr common.Address) (common.Hash, error) { 317 so := self.getStateObject(addr) 318 if so == nil { 319 return common.Hash{}, fmt.Errorf("can't find state object") 320 } 321 return so.storageRoot(self.db), nil 322 } 323 324 /* 325 * SETTERS 326 */ 327 328 // AddBalance adds amount to the account associated with addr. 329 func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) { 330 stateObject := self.GetOrNewStateObject(addr) 331 if stateObject != nil { 332 stateObject.AddBalance(amount) 333 } 334 } 335 336 // SubBalance subtracts amount from the account associated with addr. 337 func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) { 338 stateObject := self.GetOrNewStateObject(addr) 339 if stateObject != nil { 340 stateObject.SubBalance(amount) 341 } 342 } 343 344 func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) { 345 stateObject := self.GetOrNewStateObject(addr) 346 if stateObject != nil { 347 stateObject.SetBalance(amount) 348 } 349 } 350 351 func (self *StateDB) SetNonce(addr common.Address, nonce uint64) { 352 stateObject := self.GetOrNewStateObject(addr) 353 if stateObject != nil { 354 stateObject.SetNonce(nonce) 355 } 356 } 357 358 func (self *StateDB) SetCode(addr common.Address, code []byte) { 359 stateObject := self.GetOrNewStateObject(addr) 360 if stateObject != nil { 361 stateObject.SetCode(crypto.Keccak256Hash(code), code) 362 } 363 } 364 365 func (self *StateDB) SetState(addr common.Address, key, value common.Hash) { 366 stateObject := self.GetOrNewStateObject(addr) 367 if stateObject != nil { 368 stateObject.SetState(self.db, key, value) 369 } 370 } 371 372 // Suicide marks the given account as suicided. 373 // This clears the account balance. 374 // 375 // The account's state object is still available until the state is committed, 376 // getStateObject will return a non-nil account after Suicide. 377 func (self *StateDB) Suicide(addr common.Address) bool { 378 stateObject := self.getStateObject(addr) 379 if stateObject == nil { 380 return false 381 } 382 self.journal.append(suicideChange{ 383 account: &addr, 384 prev: stateObject.suicided, 385 prevbalance: new(big.Int).Set(stateObject.Balance()), 386 }) 387 stateObject.markSuicided() 388 stateObject.data.Balance = new(big.Int) 389 390 return true 391 } 392 393 // 394 // Setting, updating & deleting state object methods. 395 // 396 397 // updateStateObject writes the given object to the trie. 398 func (self *StateDB) updateStateObject(stateObject *stateObject) { 399 addr := stateObject.Address() 400 data, err := rlp.EncodeToBytes(stateObject) 401 if err != nil { 402 panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) 403 } 404 self.setError(self.trie.TryUpdate(addr[:], data)) 405 } 406 407 // deleteStateObject removes the given object from the state trie. 408 func (self *StateDB) deleteStateObject(stateObject *stateObject) { 409 stateObject.deleted = true 410 addr := stateObject.Address() 411 self.setError(self.trie.TryDelete(addr[:])) 412 } 413 414 // Retrieve a state object given by the address. Returns nil if not found. 415 func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) { 416 // Prefer 'live' objects. 417 if obj := self.stateObjects[addr]; obj != nil { 418 if obj.deleted { 419 return nil 420 } 421 return obj 422 } 423 424 // Load the object from the database. 425 enc, err := self.trie.TryGet(addr[:]) 426 if len(enc) == 0 { 427 self.setError(err) 428 return nil 429 } 430 var data Account 431 if err := rlp.DecodeBytes(enc, &data); err != nil { 432 log.Error("Failed to decode state object", "addr", addr, "err", err) 433 return nil 434 } 435 // Insert into the live set. 436 obj := newObject(self, addr, data) 437 self.setStateObject(obj) 438 return obj 439 } 440 441 func (self *StateDB) setStateObject(object *stateObject) { 442 self.stateObjects[object.Address()] = object 443 } 444 445 // Retrieve a state object or create a new state object if nil. 446 func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject { 447 stateObject := self.getStateObject(addr) 448 if stateObject == nil || stateObject.deleted { 449 stateObject, _ = self.createObject(addr) 450 } 451 return stateObject 452 } 453 454 // createObject creates a new state object. If there is an existing account with 455 // the given address, it is overwritten and returned as the second return value. 456 func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) { 457 prev = self.getStateObject(addr) 458 newobj = newObject(self, addr, Account{}) 459 newobj.setNonce(0) // sets the object to dirty 460 if prev == nil { 461 self.journal.append(createObjectChange{account: &addr}) 462 } else { 463 self.journal.append(resetObjectChange{prev: prev}) 464 } 465 self.setStateObject(newobj) 466 return newobj, prev 467 } 468 469 // CreateAccount explicitly creates a state object. If a state object with the address 470 // already exists the balance is carried over to the new account. 471 // 472 // CreateAccount is called during the EVM CREATE operation. The situation might arise that 473 // a contract does the following: 474 // 475 // 1. sends funds to sha(account ++ (nonce + 1)) 476 // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1) 477 // 478 // Carrying over the balance ensures that Ether doesn't disappear. 479 func (self *StateDB) CreateAccount(addr common.Address) { 480 new, prev := self.createObject(addr) 481 if prev != nil { 482 new.setBalance(prev.data.Balance) 483 } 484 } 485 486 func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) { 487 so := db.getStateObject(addr) 488 if so == nil { 489 return 490 } 491 it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) 492 for it.Next() { 493 key := common.BytesToHash(db.trie.GetKey(it.Key)) 494 if value, dirty := so.dirtyStorage[key]; dirty { 495 cb(key, value) 496 continue 497 } 498 cb(key, common.BytesToHash(it.Value)) 499 } 500 } 501 502 // Copy creates a deep, independent copy of the state. 503 // Snapshots of the copied state cannot be applied to the copy. 504 func (self *StateDB) Copy() *StateDB { 505 // Copy all the basic fields, initialize the memory ones 506 state := &StateDB{ 507 db: self.db, 508 trie: self.db.CopyTrie(self.trie), 509 stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)), 510 stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)), 511 refund: self.refund, 512 logs: make(map[common.Hash][]*types.Log, len(self.logs)), 513 logSize: self.logSize, 514 preimages: make(map[common.Hash][]byte), 515 journal: newJournal(), 516 } 517 // Copy the dirty states, logs, and preimages 518 for addr := range self.journal.dirties { 519 // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527), 520 // and in the Finalise-method, there is a case where an object is in the journal but not 521 // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for 522 // nil 523 if object, exist := self.stateObjects[addr]; exist { 524 state.stateObjects[addr] = object.deepCopy(state) 525 state.stateObjectsDirty[addr] = struct{}{} 526 } 527 } 528 // Above, we don't copy the actual journal. This means that if the copy is copied, the 529 // loop above will be a no-op, since the copy's journal is empty. 530 // Thus, here we iterate over stateObjects, to enable copies of copies 531 for addr := range self.stateObjectsDirty { 532 if _, exist := state.stateObjects[addr]; !exist { 533 state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state) 534 state.stateObjectsDirty[addr] = struct{}{} 535 } 536 } 537 for hash, logs := range self.logs { 538 cpy := make([]*types.Log, len(logs)) 539 for i, l := range logs { 540 cpy[i] = new(types.Log) 541 *cpy[i] = *l 542 } 543 state.logs[hash] = cpy 544 } 545 for hash, preimage := range self.preimages { 546 state.preimages[hash] = preimage 547 } 548 return state 549 } 550 551 // Snapshot returns an identifier for the current revision of the state. 552 func (self *StateDB) Snapshot() int { 553 id := self.nextRevisionId 554 self.nextRevisionId++ 555 self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()}) 556 return id 557 } 558 559 // RevertToSnapshot reverts all state changes made since the given revision. 560 func (self *StateDB) RevertToSnapshot(revid int) { 561 // Find the snapshot in the stack of valid snapshots. 562 idx := sort.Search(len(self.validRevisions), func(i int) bool { 563 return self.validRevisions[i].id >= revid 564 }) 565 if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid { 566 panic(fmt.Errorf("revision id %v cannot be reverted", revid)) 567 } 568 snapshot := self.validRevisions[idx].journalIndex 569 570 // Replay the journal to undo changes and remove invalidated snapshots 571 self.journal.revert(self, snapshot) 572 self.validRevisions = self.validRevisions[:idx] 573 } 574 575 // GetRefund returns the current value of the refund counter. 576 func (self *StateDB) GetRefund() uint64 { 577 return self.refund 578 } 579 580 // Finalise finalises the state by removing the self destructed objects 581 // and clears the journal as well as the refunds. 582 func (s *StateDB) Finalise(deleteEmptyObjects bool) { 583 for addr := range s.journal.dirties { 584 stateObject, exist := s.stateObjects[addr] 585 if !exist { 586 // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2 587 // That tx goes out of gas, and although the notion of 'touched' does not exist there, the 588 // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake, 589 // it will persist in the journal even though the journal is reverted. In this special circumstance, 590 // it may exist in `s.journal.dirties` but not in `s.stateObjects`. 591 // Thus, we can safely ignore it here 592 continue 593 } 594 595 if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { 596 s.deleteStateObject(stateObject) 597 } else { 598 stateObject.updateRoot(s.db) 599 s.updateStateObject(stateObject) 600 } 601 s.stateObjectsDirty[addr] = struct{}{} 602 } 603 // Invalidate journal because reverting across transactions is not allowed. 604 s.clearJournalAndRefund() 605 } 606 607 // IntermediateRoot computes the current root hash of the state trie. 608 // It is called in between transactions to get the root hash that 609 // goes into transaction receipts. 610 func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { 611 s.Finalise(deleteEmptyObjects) 612 return s.trie.Hash() 613 } 614 615 // Prepare sets the current transaction hash and index and block hash which is 616 // used when the EVM emits new state logs. 617 func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) { 618 self.thash = thash 619 self.bhash = bhash 620 self.txIndex = ti 621 } 622 623 func (s *StateDB) clearJournalAndRefund() { 624 s.journal = newJournal() 625 s.validRevisions = s.validRevisions[:0] 626 s.refund = 0 627 } 628 629 // Commit writes the state to the underlying in-memory trie database. 630 func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { 631 defer s.clearJournalAndRefund() 632 633 for addr := range s.journal.dirties { 634 s.stateObjectsDirty[addr] = struct{}{} 635 } 636 // Commit objects to the trie. 637 for addr, stateObject := range s.stateObjects { 638 _, isDirty := s.stateObjectsDirty[addr] 639 switch { 640 case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()): 641 // If the object has been removed, don't bother syncing it 642 // and just mark it for deletion in the trie. 643 s.deleteStateObject(stateObject) 644 case isDirty: 645 // Write any contract code associated with the state object 646 if stateObject.code != nil && stateObject.dirtyCode { 647 s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code) 648 stateObject.dirtyCode = false 649 } 650 // Write any storage changes in the state object to its storage trie. 651 if err := stateObject.CommitTrie(s.db); err != nil { 652 return common.Hash{}, err 653 } 654 // Update the object in the main account trie. 655 s.updateStateObject(stateObject) 656 } 657 delete(s.stateObjectsDirty, addr) 658 } 659 // Write trie changes. 660 root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error { 661 var account Account 662 if err := rlp.DecodeBytes(leaf, &account); err != nil { 663 return nil 664 } 665 if account.Root != emptyState { 666 s.db.TrieDB().Reference(account.Root, parent) 667 } 668 code := common.BytesToHash(account.CodeHash) 669 if code != emptyCode { 670 s.db.TrieDB().Reference(code, parent) 671 } 672 return nil 673 }) 674 log.Debug("Trie cache stats after commit", "misses", trie.CacheMisses(), "unloads", trie.CacheUnloads()) 675 return root, err 676 }