diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 036cee8a8416..be9763abb434 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -405,8 +405,16 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea proot common.Hash ) if chain.Config().IsVerkle(header.Number, header.Time) { + parent := chain.GetHeaderByNumber(header.Number.Uint64() - 1) + if parent == nil { + return nil, fmt.Errorf("nil parent header for block %d", header.Number) + } + // Load transition state at beginning of block, because + // OpenTrie needs to know what the conversion status is. + state.Database().LoadTransitionState(parent.Root) + var err error - proot, stateDiff, proof, err = BuildVerkleProof(chain, header, state) + proot, stateDiff, proof, err = BuildVerkleProof(header, state, parent.Root) if err != nil { return nil, fmt.Errorf("error building verkle proof: %w", err) } @@ -420,24 +428,13 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea return block, nil } -func BuildVerkleProof(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) (common.Hash, verkle.StateDiff, *verkle.VerkleProof, error) { +func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot common.Hash) (common.Hash, verkle.StateDiff, *verkle.VerkleProof, error) { var ( proof *verkle.VerkleProof stateDiff verkle.StateDiff ) - // Open the pre-tree to prove the pre-state against - parent := chain.GetHeaderByNumber(header.Number.Uint64() - 1) - if parent == nil { - return common.Hash{}, nil, nil, fmt.Errorf("nil parent header for block %d", header.Number) - } - parentRoot := parent.Root - - // Load transition state at beginning of block, because - // OpenTrie needs to know what the conversion status is. - state.Database().LoadTransitionState(parent.Root) - - preTrie, err := state.Database().OpenTrie(parent.Root) + preTrie, err := state.Database().OpenTrie(parentRoot) if err != nil { return common.Hash{}, nil, nil, fmt.Errorf("error opening pre-state tree root: %w", err) } diff --git a/core/block_validator.go b/core/block_validator.go index b558e7c8f58b..de5a7eaedfdf 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -137,7 +137,11 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD // test filling. When running these tests, geth decides when inserting some blocks that it is a side chain // and the execution witness is lost. This is a workaround to avoid failing the tests until fixed. if v.bc.Config().IsVerkle(header.Number, header.Time) && block.ExecutionWitness() != nil { - proot, stateDiff, proof, err := beacon.BuildVerkleProof(v.bc, header, statedb) + parent := v.bc.GetHeaderByNumber(header.Number.Uint64() - 1) + if parent == nil { + return fmt.Errorf("nil parent header for block %d", header.Number) + } + proot, stateDiff, proof, err := beacon.BuildVerkleProof(header, statedb, parent.Root) if err != nil { return fmt.Errorf("error building verkle proof: %w", err) }