Commit writes the state to the underlying in-memory trie database.
(deleteEmptyObjects bool)
| 580 | |
| 581 | // Commit writes the state to the underlying in-memory trie database. |
| 582 | func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { |
| 583 | defer s.clearJournalAndRefund() |
| 584 | |
| 585 | for addr := range s.journal.dirties { |
| 586 | s.stateObjectsDirty[addr] = struct{}{} |
| 587 | } |
| 588 | // Commit objects to the trie. |
| 589 | for addr, stateObject := range s.stateObjects { |
| 590 | _, isDirty := s.stateObjectsDirty[addr] |
| 591 | switch { |
| 592 | case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()): |
| 593 | // If the object has been removed, don't bother syncing it |
| 594 | // and just mark it for deletion in the trie. |
| 595 | s.deleteStateObject(stateObject) |
| 596 | case isDirty: |
| 597 | // Write any contract code associated with the state object |
| 598 | if stateObject.code != nil && stateObject.dirtyCode { |
| 599 | s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code) |
| 600 | stateObject.dirtyCode = false |
| 601 | } |
| 602 | // Write any storage changes in the state object to its storage trie. |
| 603 | if err := stateObject.CommitTrie(s.db); err != nil { |
| 604 | return common.Hash{}, err |
| 605 | } |
| 606 | // Update the object in the main account trie. |
| 607 | s.updateStateObject(stateObject) |
| 608 | } |
| 609 | delete(s.stateObjectsDirty, addr) |
| 610 | } |
| 611 | // Write trie changes. |
| 612 | root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error { |
| 613 | var account Account |
| 614 | if err := rlp.DecodeBytes(leaf, &account); err != nil { |
| 615 | return nil |
| 616 | } |
| 617 | if account.Root != emptyState { |
| 618 | s.db.TrieDB().Reference(account.Root, parent) |
| 619 | } |
| 620 | code := common.BytesToHash(account.CodeHash) |
| 621 | if code != emptyCode { |
| 622 | s.db.TrieDB().Reference(code, parent) |
| 623 | } |
| 624 | return nil |
| 625 | }) |
| 626 | log.Debug("Trie cache stats after commit", "misses", trie.CacheMisses(), "unloads", trie.CacheUnloads()) |
| 627 | return root, err |
| 628 | } |
nothing calls this directly
no test coverage detected