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

copy the pre-state, use an untouched copy for the proof #72

Merged
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
44 changes: 20 additions & 24 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine}
b.header = makeHeader(chainreader, parent, statedb, b.engine)
preState := statedb.Copy()

// Mutate the state and block according to any hard-fork specs
if daoBlock := config.DAOForkBlock; daoBlock != nil {
Expand Down Expand Up @@ -329,30 +330,25 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
}

// Generate an associated verkle proof
if tr := statedb.GetTrie(); tr.IsVerkle() {
vtr := tr.(*trie.VerkleTrie)
// Generate the proof if we are using a verkle tree
// WORKAROUND: make sure all keys are resolved
// before building the proof. Ultimately, node
// resolution can be done with a prefetcher or
// from GetCommitmentsAlongPath.

keys := statedb.Witness().Keys()
for _, key := range keys {
out, err := vtr.TryGet(key)
if err != nil {
panic(err)
}
if len(out) == 0 {
panic(fmt.Sprintf("%x should be present in the tree", key))
}
}
vtr.Hash()
p, k, err := vtr.ProveAndSerialize(keys, statedb.Witness().KeyVals())
block.SetVerkleProof(p, k)
if err != nil {
panic(err)
}
tr := preState.GetTrie()
if !tr.IsVerkle() {
panic("tree should be verkle")
}

vtr := tr.(*trie.VerkleTrie)
// Make sure all keys are resolved before
// building the proof. Ultimately, node
// resolution can be done with a prefetcher
// or from GetCommitmentsAlongPath.
keys := statedb.Witness().Keys()
for _, key := range keys {
vtr.TryGet(key)
}
Comment on lines +339 to +346
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for doing this explicitly? Are you trying to make sure the commitments are all computed (which appears to already be done from the call to statedb.Commit())?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's necessary to make sure that all required nodes are loaded in memory - this is the pre tree and we're starting off from fresh tree

vtr.Hash()
p, k, err := vtr.ProveAndSerialize(keys, statedb.Witness().KeyVals())
block.SetVerkleProof(p, k)
if err != nil {
panic(err)
}
return block, b.receipts
}
Expand Down
4 changes: 3 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type environment struct {
signer types.Signer

state *state.StateDB // apply state changes here
original *state.StateDB // verkle: keep the orignal data to prove the pre-state
ancestors mapset.Set // ancestor set (used for checking uncle parent validity)
family mapset.Set // family set (used for checking uncle invalidity)
uncles mapset.Set // uncle set
Expand Down Expand Up @@ -692,6 +693,7 @@ func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error {
env := &environment{
signer: types.MakeSigner(w.chainConfig, header.Number),
state: state,
original: state.Copy(),
ancestors: mapset.NewSet(),
family: mapset.NewSet(),
uncles: mapset.NewSet(),
Expand Down Expand Up @@ -1039,7 +1041,7 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st
if err != nil {
return err
}
if tr := s.GetTrie(); tr.IsVerkle() {
if tr := w.current.original.GetTrie(); tr.IsVerkle() {
vtr := tr.(*trie.VerkleTrie)
// Generate the proof if we are using a verkle tree
p, k, err := vtr.ProveAndSerialize(s.Witness().Keys(), s.Witness().KeyVals())
Expand Down