Skip to content

Commit

Permalink
fix: ensure post values are added to a VerkleTrie during conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Ballet <[email protected]>
  • Loading branch information
gballet committed Jan 11, 2025
1 parent fbb80c4 commit 600b48c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
// Assemble and return the final block.
block := types.NewBlockWithWithdrawals(header, txs, uncles, receipts, withdrawals, trie.NewStackTrie(nil))
if chain.Config().IsVerkle(header.Number, header.Time) && chain.Config().ProofInBlocks {
err := trie.AddPostValuesToProof(state.GetTrie().(*trie.VerkleTrie), proof)
err := trie.AddPostValuesToProof(getVerkleTrieOrEmpty(state.GetTrie(), state.Database().TrieDB()), proof)
if err != nil {
return nil, fmt.Errorf("error adding post values to proof: %w", err)
}
Expand All @@ -434,18 +434,9 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
return block, nil
}

func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot common.Hash, keys [][]byte) (*verkle.Proof, error) {
var (
proof *verkle.Proof
)

preTrie, err := state.Database().OpenTrie(parentRoot)
if err != nil {
return nil, fmt.Errorf("error opening pre-state tree root: %w", err)
}

func getVerkleTrieOrEmpty(tr state.Trie, triedb *trie.Database) *trie.VerkleTrie {
var vtr *trie.VerkleTrie
switch pre := preTrie.(type) {
switch pre := tr.(type) {
case *trie.VerkleTrie:
vtr = pre
case *trie.TransitionTrie:
Expand All @@ -454,9 +445,23 @@ func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot com
// This should only happen for the first block of the
// conversion, when the previous tree is a merkle tree.
// Logically, the "previous" verkle tree is an empty tree.
vtr = trie.NewVerkleTrie(verkle.New(), state.Database().TrieDB(), utils.NewPointCache(), false)
vtr = trie.NewVerkleTrie(verkle.New(), triedb, utils.NewPointCache(), false)
}
return vtr
}

func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot common.Hash, keys [][]byte) (*verkle.Proof, error) {
var (
proof *verkle.Proof
)

preTrie, err := state.Database().OpenTrie(parentRoot)
if err != nil {
return nil, fmt.Errorf("error opening pre-state tree root: %w", err)
}

if len(keys) > 0 {
vtr := getVerkleTrieOrEmpty(preTrie, state.Database().TrieDB())
proof, err = trie.Proof(vtr, nil, keys, vtr.FlatdbNodeResolver)
if err != nil {
return nil, fmt.Errorf("error generating verkle proof for block %d: %w", header.Number, err)
Expand Down

0 comments on commit 600b48c

Please sign in to comment.