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