Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix zktrie branch when zktrie mode #93

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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
Expand Down
65 changes: 35 additions & 30 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 10 additions & 0 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}