github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/core/state/statedb.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-dubxcoin library. 3 // 4 // The go-dubxcoin 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-dubxcoin 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-dubxcoin 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/alexdevranger/node-1.8.27/common" 27 "github.com/alexdevranger/node-1.8.27/core/types" 28 "github.com/alexdevranger/node-1.8.27/crypto" 29 "github.com/alexdevranger/node-1.8.27/log" 30 "github.com/alexdevranger/node-1.8.27/rlp" 31 "github.com/alexdevranger/node-1.8.27/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 /* 316 * SETTERS 317 */ 318 319 // AddBalance adds amount to the account associated with addr. 320 func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) { 321 stateObject := self.GetOrNewStateObject(addr) 322 if stateObject != nil { 323 stateObject.AddBalance(amount) 324 } 325 } 326 327 // SubBalance subtracts amount from the account associated with addr. 328 func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) { 329 stateObject := self.GetOrNewStateObject(addr) 330 if stateObject != nil { 331 stateObject.SubBalance(amount) 332 } 333 } 334 335 func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) { 336 stateObject := self.GetOrNewStateObject(addr) 337 if stateObject != nil { 338 stateObject.SetBalance(amount) 339 } 340 } 341 342 func (self *StateDB) SetNonce(addr common.Address, nonce uint64) { 343 stateObject := self.GetOrNewStateObject(addr) 344 if stateObject != nil { 345 stateObject.SetNonce(nonce) 346 } 347 } 348 349 func (self *StateDB) SetCode(addr common.Address, code []byte) { 350 stateObject := self.GetOrNewStateObject(addr) 351 if stateObject != nil { 352 stateObject.SetCode(crypto.Keccak256Hash(code), code) 353 } 354 } 355 356 func (self *StateDB) SetState(addr common.Address, key, value common.Hash) { 357 stateObject := self.GetOrNewStateObject(addr) 358 if stateObject != nil { 359 stateObject.SetState(self.db, key, value) 360 } 361 } 362 363 // Suicide marks the given account as suicided. 364 // This clears the account balance. 365 // 366 // The account's state object is still available until the state is committed, 367 // getStateObject will return a non-nil account after Suicide. 368 func (self *StateDB) Suicide(addr common.Address) bool { 369 stateObject := self.getStateObject(addr) 370 if stateObject == nil { 371 return false 372 } 373 self.journal.append(suicideChange{ 374 account: &addr, 375 prev: stateObject.suicided, 376 prevbalance: new(big.Int).Set(stateObject.Balance()), 377 }) 378 stateObject.markSuicided() 379 stateObject.data.Balance = new(big.Int) 380 381 return true 382 } 383 384 // 385 // Setting, updating & deleting state object methods. 386 // 387 388 // updateStateObject writes the given object to the trie. 389 func (self *StateDB) updateStateObject(stateObject *stateObject) { 390 addr := stateObject.Address() 391 data, err := rlp.EncodeToBytes(stateObject) 392 if err != nil { 393 panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) 394 } 395 self.setError(self.trie.TryUpdate(addr[:], data)) 396 } 397 398 // deleteStateObject removes the given object from the state trie. 399 func (self *StateDB) deleteStateObject(stateObject *stateObject) { 400 stateObject.deleted = true 401 addr := stateObject.Address() 402 self.setError(self.trie.TryDelete(addr[:])) 403 } 404 405 // Retrieve a state object given by the address. Returns nil if not found. 406 func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) { 407 // Prefer 'live' objects. 408 if obj := self.stateObjects[addr]; obj != nil { 409 if obj.deleted { 410 return nil 411 } 412 return obj 413 } 414 415 // Load the object from the database. 416 enc, err := self.trie.TryGet(addr[:]) 417 if len(enc) == 0 { 418 self.setError(err) 419 return nil 420 } 421 var data Account 422 if err := rlp.DecodeBytes(enc, &data); err != nil { 423 log.Error("Failed to decode state object", "addr", addr, "err", err) 424 return nil 425 } 426 // Insert into the live set. 427 obj := newObject(self, addr, data) 428 self.setStateObject(obj) 429 return obj 430 } 431 432 func (self *StateDB) setStateObject(object *stateObject) { 433 self.stateObjects[object.Address()] = object 434 } 435 436 // Retrieve a state object or create a new state object if nil. 437 func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject { 438 stateObject := self.getStateObject(addr) 439 if stateObject == nil || stateObject.deleted { 440 stateObject, _ = self.createObject(addr) 441 } 442 return stateObject 443 } 444 445 // createObject creates a new state object. If there is an existing account with 446 // the given address, it is overwritten and returned as the second return value. 447 func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) { 448 prev = self.getStateObject(addr) 449 newobj = newObject(self, addr, Account{}) 450 newobj.setNonce(0) // sets the object to dirty 451 if prev == nil { 452 self.journal.append(createObjectChange{account: &addr}) 453 } else { 454 self.journal.append(resetObjectChange{prev: prev}) 455 } 456 self.setStateObject(newobj) 457 return newobj, prev 458 } 459 460 // CreateAccount explicitly creates a state object. If a state object with the address 461 // already exists the balance is carried over to the new account. 462 // 463 // CreateAccount is called during the EVM CREATE operation. The situation might arise that 464 // a contract does the following: 465 // 466 // 1. sends funds to sha(account ++ (nonce + 1)) 467 // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1) 468 // 469 // Carrying over the balance ensures that Ether doesn't disappear. 470 func (self *StateDB) CreateAccount(addr common.Address) { 471 newObj, prev := self.createObject(addr) 472 if prev != nil { 473 newObj.setBalance(prev.data.Balance) 474 } 475 } 476 477 func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) { 478 so := db.getStateObject(addr) 479 if so == nil { 480 return 481 } 482 it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) 483 for it.Next() { 484 key := common.BytesToHash(db.trie.GetKey(it.Key)) 485 if value, dirty := so.dirtyStorage[key]; dirty { 486 cb(key, value) 487 continue 488 } 489 cb(key, common.BytesToHash(it.Value)) 490 } 491 } 492 493 // Copy creates a deep, independent copy of the state. 494 // Snapshots of the copied state cannot be applied to the copy. 495 func (self *StateDB) Copy() *StateDB { 496 // Copy all the basic fields, initialize the memory ones 497 state := &StateDB{ 498 db: self.db, 499 trie: self.db.CopyTrie(self.trie), 500 stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)), 501 stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)), 502 refund: self.refund, 503 logs: make(map[common.Hash][]*types.Log, len(self.logs)), 504 logSize: self.logSize, 505 preimages: make(map[common.Hash][]byte), 506 journal: newJournal(), 507 } 508 // Copy the dirty states, logs, and preimages 509 for addr := range self.journal.dirties { 510 // As documented [here](https://github.com/alexdevranger/node-1.8.27/pull/16485#issuecomment-380438527), 511 // and in the Finalise-method, there is a case where an object is in the journal but not 512 // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for 513 // nil 514 if object, exist := self.stateObjects[addr]; exist { 515 state.stateObjects[addr] = object.deepCopy(state) 516 state.stateObjectsDirty[addr] = struct{}{} 517 } 518 } 519 // Above, we don't copy the actual journal. This means that if the copy is copied, the 520 // loop above will be a no-op, since the copy's journal is empty. 521 // Thus, here we iterate over stateObjects, to enable copies of copies 522 for addr := range self.stateObjectsDirty { 523 if _, exist := state.stateObjects[addr]; !exist { 524 state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state) 525 state.stateObjectsDirty[addr] = struct{}{} 526 } 527 } 528 for hash, logs := range self.logs { 529 cpy := make([]*types.Log, len(logs)) 530 for i, l := range logs { 531 cpy[i] = new(types.Log) 532 *cpy[i] = *l 533 } 534 state.logs[hash] = cpy 535 } 536 for hash, preimage := range self.preimages { 537 state.preimages[hash] = preimage 538 } 539 return state 540 } 541 542 // Snapshot returns an identifier for the current revision of the state. 543 func (self *StateDB) Snapshot() int { 544 id := self.nextRevisionId 545 self.nextRevisionId++ 546 self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()}) 547 return id 548 } 549 550 // RevertToSnapshot reverts all state changes made since the given revision. 551 func (self *StateDB) RevertToSnapshot(revid int) { 552 // Find the snapshot in the stack of valid snapshots. 553 idx := sort.Search(len(self.validRevisions), func(i int) bool { 554 return self.validRevisions[i].id >= revid 555 }) 556 if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid { 557 panic(fmt.Errorf("revision id %v cannot be reverted", revid)) 558 } 559 snapshot := self.validRevisions[idx].journalIndex 560 561 // Replay the journal to undo changes and remove invalidated snapshots 562 self.journal.revert(self, snapshot) 563 self.validRevisions = self.validRevisions[:idx] 564 } 565 566 // GetRefund returns the current value of the refund counter. 567 func (self *StateDB) GetRefund() uint64 { 568 return self.refund 569 } 570 571 // Finalise finalises the state by removing the self destructed objects 572 // and clears the journal as well as the refunds. 573 func (s *StateDB) Finalise(deleteEmptyObjects bool) { 574 for addr := range s.journal.dirties { 575 stateObject, exist := s.stateObjects[addr] 576 if !exist { 577 // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2 578 // That tx goes out of gas, and although the notion of 'touched' does not exist there, the 579 // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake, 580 // it will persist in the journal even though the journal is reverted. In this special circumstance, 581 // it may exist in `s.journal.dirties` but not in `s.stateObjects`. 582 // Thus, we can safely ignore it here 583 continue 584 } 585 586 if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { 587 s.deleteStateObject(stateObject) 588 } else { 589 stateObject.updateRoot(s.db) 590 s.updateStateObject(stateObject) 591 } 592 s.stateObjectsDirty[addr] = struct{}{} 593 } 594 // Invalidate journal because reverting across transactions is not allowed. 595 s.clearJournalAndRefund() 596 } 597 598 // IntermediateRoot computes the current root hash of the state trie. 599 // It is called in between transactions to get the root hash that 600 // goes into transaction receipts. 601 func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { 602 s.Finalise(deleteEmptyObjects) 603 return s.trie.Hash() 604 } 605 606 // Prepare sets the current transaction hash and index and block hash which is 607 // used when the EVM emits new state logs. 608 func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) { 609 self.thash = thash 610 self.bhash = bhash 611 self.txIndex = ti 612 } 613 614 func (s *StateDB) clearJournalAndRefund() { 615 s.journal = newJournal() 616 s.validRevisions = s.validRevisions[:0] 617 s.refund = 0 618 } 619 620 // Commit writes the state to the underlying in-memory trie database. 621 func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { 622 defer s.clearJournalAndRefund() 623 624 for addr := range s.journal.dirties { 625 s.stateObjectsDirty[addr] = struct{}{} 626 } 627 // Commit objects to the trie. 628 for addr, stateObject := range s.stateObjects { 629 _, isDirty := s.stateObjectsDirty[addr] 630 switch { 631 case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()): 632 // If the object has been removed, don't bother syncing it 633 // and just mark it for deletion in the trie. 634 s.deleteStateObject(stateObject) 635 case isDirty: 636 // Write any contract code associated with the state object 637 if stateObject.code != nil && stateObject.dirtyCode { 638 s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code) 639 stateObject.dirtyCode = false 640 } 641 // Write any storage changes in the state object to its storage trie. 642 if err := stateObject.CommitTrie(s.db); err != nil { 643 return common.Hash{}, err 644 } 645 // Update the object in the main account trie. 646 s.updateStateObject(stateObject) 647 } 648 delete(s.stateObjectsDirty, addr) 649 } 650 // Write trie changes. 651 root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error { 652 var account Account 653 if err := rlp.DecodeBytes(leaf, &account); err != nil { 654 return nil 655 } 656 if account.Root != emptyState { 657 s.db.TrieDB().Reference(account.Root, parent) 658 } 659 code := common.BytesToHash(account.CodeHash) 660 if code != emptyCode { 661 s.db.TrieDB().Reference(code, parent) 662 } 663 return nil 664 }) 665 log.Debug("Trie cache stats after commit", "misses", trie.CacheMisses(), "unloads", trie.CacheUnloads()) 666 return root, err 667 }