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