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

Commit

Permalink
fake finalize block context
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Nov 2, 2023
1 parent 3de07ce commit 17dc35c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
10 changes: 8 additions & 2 deletions cosmos/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (m *Miner) Init(serializer EnvelopeSerializer) {

// PrepareProposal implements baseapp.PrepareProposal.
func (m *Miner) PrepareProposal(
ctx sdk.Context, _ *abci.RequestPrepareProposal,
ctx sdk.Context, req *abci.RequestPrepareProposal,
) (*abci.ResponsePrepareProposal, error) {
var (
payloadEnvelopeBz []byte
Expand All @@ -94,7 +94,13 @@ func (m *Miner) PrepareProposal(

// We have to run the PreBlocker && BeginBlocker to get the chain into the state
// it'll be in when the EVM transaction actually runs.
if _, err = m.app.PreBlocker(ctx, nil); err != nil {
if _, err = m.app.PreBlocker(ctx, &abci.RequestFinalizeBlock{
Txs: req.Txs,
Time: req.Time,
Misbehavior: req.Misbehavior,
Height: req.Height,
NextValidatorsHash: req.NextValidatorsHash,
}); err != nil {
return nil, err
} else if _, err = m.app.BeginBlocker(ctx); err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions eth/core/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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 @@ -67,6 +68,8 @@ 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 @@ -125,6 +128,7 @@ 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
29 changes: 10 additions & 19 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, state.NewStateDB(bc.sp, bc.pp), true)
_, err := bc.WriteBlockAndSetHead(block, nil, nil, nil, true)
return err
}

Expand All @@ -60,31 +60,22 @@ 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, state, *bc.vmConfig)
receipts, logs, usedGas, err := bc.processor.Process(block, bc.statedb, *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, state, receipts, usedGas); err != nil {
for i := 0; i < 100; i++ {
log.Error(
"invalid state after processing block", "num", block.NumberU64(),
"err", err, "receipts", receipts, "usedGas", usedGas, "logs", logs,
)
}
// TODO: re-enable actually erroring at a later date.
//return err
if err = bc.validator.ValidateState(block, bc.statedb, 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, state, true); err != nil {
block, receipts, logs, nil, true); err != nil {
log.Error("failed to write block", "num", block.NumberU64(), "err", err)
return err
}
Expand All @@ -95,10 +86,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 state.StateDB, emitHeadEvent bool,
_ state.StateDB, emitHeadEvent bool,
) (core.WriteStatus, error) {
// Write the block to the store.
if err := bc.writeBlockWithState(block, receipts, state); err != nil {
if err := bc.writeBlockWithState(block, receipts); err != nil {
return core.NonStatTy, err
}
currentBlock := bc.currentBlock.Load()
Expand Down Expand Up @@ -163,7 +154,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, state state.StateDB,
block *types.Block, receipts []*types.Receipt,
) error {
// In Polaris since we are using single block finality.
// Finalized == Current == Safe. All are the same.
Expand All @@ -182,7 +173,7 @@ func (bc *blockchain) writeBlockWithState(

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

0 comments on commit 17dc35c

Please sign in to comment.