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