Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

fix(chain): Fake FinalizeBlock in PrepareProposal #1272

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion cosmos/x/evm/plugins/state/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func (p *plugin) SetPrecompileLogFactory(plf events.PrecompileLogFactory) {
// Prepare sets up the context on the state plugin for use in JSON-RPC calls.
// Prepare implements `core.StatePlugin`.
func (p *plugin) Prepare(ctx context.Context) {
p.latestQueryContext = sdk.UnwrapSDKContext(ctx)
p.latestQueryContext = sdk.UnwrapSDKContext(ctx).
WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{})
}

// Reset sets up the state plugin for execution of a new transaction. It sets up the snapshottable
Expand Down
4 changes: 0 additions & 4 deletions eth/core/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

"pkg.berachain.dev/polaris/eth/common"
"pkg.berachain.dev/polaris/eth/consensus"
"pkg.berachain.dev/polaris/eth/core/state"
"pkg.berachain.dev/polaris/eth/core/types"
"pkg.berachain.dev/polaris/eth/core/vm"
"pkg.berachain.dev/polaris/eth/log"
Expand Down Expand Up @@ -68,8 +67,6 @@ type blockchain struct {
processor core.Processor
validator core.Validator

// statedb is the state database that is used to mange state during transactions.
statedb state.StateDB
// vmConfig is the configuration used to create the EVM.
vmConfig *vm.Config

Expand Down Expand Up @@ -128,7 +125,6 @@ func NewChain(
logger: log.Root(),
engine: engine,
}
bc.statedb = state.NewStateDB(bc.sp, bc.pp)
bc.processor = core.NewStateProcessor(bc.config, bc, bc.engine)
bc.validator = core.NewBlockValidator(bc.config, bc, bc.engine)
// TODO: hmm...
Expand Down
19 changes: 11 additions & 8 deletions eth/core/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (bc *blockchain) WriteGenesisBlock(block *types.Block) error {
if block.NumberU64() != 0 {
return errors.New("not the genesis block")
}
_, err := bc.WriteBlockAndSetHead(block, nil, nil, nil, true)
_, err := bc.WriteBlockAndSetHead(block, nil, nil, state.NewStateDB(bc.sp, bc.pp), true)
return err
}

Expand All @@ -60,22 +60,25 @@ func (bc *blockchain) InsertBlockAndSetHead(block *types.Block) error {
}
}

// Create a new statedb to use for this block insertion.
state := state.NewStateDB(bc.sp, bc.pp)

// Process the incoming EVM block.
receipts, logs, usedGas, err := bc.processor.Process(block, bc.statedb, *bc.vmConfig)
receipts, logs, usedGas, err := bc.processor.Process(block, state, *bc.vmConfig)
if err != nil {
log.Error("failed to process block", "num", block.NumberU64(), "err", err)
return err
}

// ValidateState validates the statedb post block processing.
if err = bc.validator.ValidateState(block, bc.statedb, receipts, usedGas); err != nil {
if err = bc.validator.ValidateState(block, state, receipts, usedGas); err != nil {
log.Error("invalid state after processing block", "num", block.NumberU64(), "err", err)
return err
}

// We can just immediately finalize the block. It's okay in this context.
if _, err = bc.WriteBlockAndSetHead(
block, receipts, logs, nil, true); err != nil {
block, receipts, logs, state, true); err != nil {
log.Error("failed to write block", "num", block.NumberU64(), "err", err)
return err
}
Expand All @@ -86,10 +89,10 @@ func (bc *blockchain) InsertBlockAndSetHead(block *types.Block) error {
// WriteBlockAndSetHead sets the head of the blockchain to the given block and finalizes the block.
func (bc *blockchain) WriteBlockAndSetHead(
block *types.Block, receipts []*types.Receipt, logs []*types.Log,
_ state.StateDB, emitHeadEvent bool,
state state.StateDB, emitHeadEvent bool,
) (core.WriteStatus, error) {
// Write the block to the store.
if err := bc.writeBlockWithState(block, receipts); err != nil {
if err := bc.writeBlockWithState(block, receipts, state); err != nil {
return core.NonStatTy, err
}
currentBlock := bc.currentBlock.Load()
Expand Down Expand Up @@ -154,7 +157,7 @@ func (bc *blockchain) WriteBlockAndSetHead(
// writeBlockWithState writes the block along with its state (receipts and logs)
// into the blockchain.
func (bc *blockchain) writeBlockWithState(
block *types.Block, receipts []*types.Receipt,
block *types.Block, receipts []*types.Receipt, state state.StateDB,
) error {
// In Polaris since we are using single block finality.
// Finalized == Current == Safe. All are the same.
Expand All @@ -173,7 +176,7 @@ func (bc *blockchain) writeBlockWithState(

// Commit all cached state changes into underlying memory database.
// In Polaris this is a no-op.
_, err = bc.statedb.Commit(block.NumberU64(), bc.config.IsEIP158(block.Number()))
_, err = state.Commit(block.NumberU64(), bc.config.IsEIP158(block.Number()))
if err != nil {
return err
}
Expand Down
Loading