diff --git a/core/state/state_object.go b/core/state/state_object.go index 133bfdc33316..c813a6ce4b45 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -107,7 +107,7 @@ func newObject(db *StateDB, address common.Address, data types.StateAccount) *st data.CodeHash = emptyCodeHash } if data.Root == (common.Hash{}) { - data.Root = emptyRoot + data.Root = db.db.TrieDB().EmptyRoot() } return &stateObject{ db: db, @@ -151,7 +151,7 @@ func (s *stateObject) getTrie(db Database) Trie { if s.trie == nil { // Try fetching from prefetcher first // We don't prefetch empty tries - if s.data.Root != emptyRoot && s.db.prefetcher != nil { + if s.data.Root != s.db.db.TrieDB().EmptyRoot() && s.db.prefetcher != nil { // When the miner is creating the pending state, there is no // prefetcher s.trie = s.db.prefetcher.trie(s.data.Root) @@ -331,7 +331,7 @@ func (s *stateObject) finalise(prefetch bool) { slotsToPrefetch = append(slotsToPrefetch, common.CopyBytes(key[:])) // Copy needed for closure } } - if s.db.prefetcher != nil && prefetch && len(slotsToPrefetch) > 0 && s.data.Root != emptyRoot { + if s.db.prefetcher != nil && prefetch && len(slotsToPrefetch) > 0 && s.data.Root != s.db.db.TrieDB().EmptyRoot() { s.db.prefetcher.prefetch(s.data.Root, slotsToPrefetch) } if len(s.dirtyStorage) > 0 { diff --git a/core/state/statedb.go b/core/state/statedb.go index bb7a9ce81426..29f673115813 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -40,11 +40,6 @@ type revision struct { journalIndex int } -var ( - // emptyRoot is the known root hash of an empty trie. - emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") -) - type proofList [][]byte func (n *proofList) Put(key []byte, value []byte) error { @@ -562,7 +557,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { data.CodeHash = emptyCodeHash } if data.Root == (common.Hash{}) { - data.Root = emptyRoot + data.Root = s.db.TrieDB().EmptyRoot() } } } @@ -980,7 +975,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { if err := rlp.DecodeBytes(leaf, &account); err != nil { return nil } - if account.Root != emptyRoot { + if account.Root != s.db.TrieDB().EmptyRoot() { s.db.TrieDB().Reference(account.Root, parent) } return nil diff --git a/miner/worker.go b/miner/worker.go index 65286bbe48c9..06420d27d591 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -803,38 +803,43 @@ func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Addres w.current.txs = append(w.current.txs, tx) w.current.receipts = append(w.current.receipts, receipt) - var storage *types.StorageRes - if w.config.SMTTrace { - proofFrom, proofTo := tracer.BaseProofs() - proofFromEnc := make([]hexutil.Bytes, len(proofFrom)) - for i := range proofFrom { - proofFromEnc[i] = proofFrom[i] - } - proofToEnc := make([]hexutil.Bytes, len(proofTo)) - for i := range proofTo { - proofToEnc[i] = proofTo[i] - } - accs := tracer.UpdatedAccounts() - accsEnc := make(map[string]hexutil.Bytes) - for addr, data := range accs { - accsEnc[addr.String()] = data.MarshalBytes() - } - finalRoot := w.current.state.GetRootHash() - var createdAcc []byte - if acc := tracer.CreatedAccount(); acc != nil { - createdAcc = acc.MarshalBytes() - } + proofFrom, proofTo := tracer.BaseProofs() + proofFromEnc := make([]hexutil.Bytes, len(proofFrom)) + for i := range proofFrom { + proofFromEnc[i] = proofFrom[i] + } + proofToEnc := make([]hexutil.Bytes, len(proofTo)) + for i := range proofTo { + proofToEnc[i] = proofTo[i] + } - storage = &types.StorageRes{ - RootBefore: tracer.StateRootBefore(), - ToAddress: tracer.ToAddress(), - RootAfter: &finalRoot, - AccountCreated: createdAcc, - ProofFrom: proofFromEnc, - ProofTo: proofToEnc, - AccountsAfter: accsEnc, - } + var finalRoot common.Hash + if len(receipt.PostState) == 0 { + finalRoot = w.current.state.IntermediateRoot(w.chainConfig.IsEIP158(w.current.header.Number)) + } else { + finalRoot = common.BytesToHash(receipt.PostState) + } + + accs := tracer.UpdatedAccounts() + accsEnc := make(map[string]hexutil.Bytes) + for addr, data := range accs { + accsEnc[addr.String()] = data.MarshalBytes() + } + + var createdAcc []byte + if acc := tracer.CreatedAccount(); acc != nil { + createdAcc = acc.MarshalBytes() + } + + storage := &types.StorageRes{ + RootBefore: tracer.StateRootBefore(), + ToAddress: tracer.ToAddress(), + RootAfter: &finalRoot, + AccountCreated: createdAcc, + ProofFrom: proofFromEnc, + ProofTo: proofToEnc, + AccountsAfter: accsEnc, } w.current.executionResults = append(w.current.executionResults, &types.ExecutionResult{ diff --git a/trie/database.go b/trie/database.go index c0db04342e1f..769acd82ed33 100644 --- a/trie/database.go +++ b/trie/database.go @@ -911,3 +911,13 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st } } } + +// EmptyRoot indicate what root is for an empty trie, it depends on its underlying implement (zktrie or common trie) +func (db *Database) EmptyRoot() common.Hash { + + if db.Zktrie { + return common.Hash{} + } else { + return emptyRoot + } +}