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