From 6ee586c52461b224c1d4898d6b4e11662edd112d Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 16:21:53 -0400 Subject: [PATCH 1/8] dont reuse statedb --- eth/core/chain.go | 4 ---- eth/core/chain_writer.go | 19 +++++++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/eth/core/chain.go b/eth/core/chain.go index 53fcf2816..9a77a130c 100644 --- a/eth/core/chain.go +++ b/eth/core/chain.go @@ -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" @@ -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 @@ -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... diff --git a/eth/core/chain_writer.go b/eth/core/chain_writer.go index fd2e196ef..22b8b9471 100644 --- a/eth/core/chain_writer.go +++ b/eth/core/chain_writer.go @@ -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 } @@ -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 } @@ -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() @@ -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. @@ -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 } From 4c1cfd6d7c3cbf2fad50407b754524b54e9d90c6 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 16:27:50 -0400 Subject: [PATCH 2/8] set gas config --- cosmos/x/evm/plugins/state/plugin.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index ab4c9b470..a0a513ae3 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -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 From 5b51d8d9c7b2065cde56f12b1f749bdd9202bf1e Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 16:30:55 -0400 Subject: [PATCH 3/8] cache context query in plugin --- cosmos/x/evm/plugins/state/plugin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index a0a513ae3..28038f942 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -140,9 +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{}) + WithTransientKVGasConfig(storetypes.GasConfig{}).CacheContext() } // Reset sets up the state plugin for execution of a new transaction. It sets up the snapshottable From 869420fc9c8fc1bd8fa0fc65bcd7934c5c2ab5c7 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 16:31:37 -0400 Subject: [PATCH 4/8] fresh event mgr as well --- cosmos/x/evm/plugins/state/plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index 28038f942..02fa0ae22 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -538,7 +538,7 @@ func (p *plugin) StateAtBlockNumber(number uint64) (core.StatePlugin, error) { if p.latestQueryContext.MultiStore() == nil { ctx = p.latestQueryContext.WithEventManager(sdk.NewEventManager()) } else { - ctx, _ = p.latestQueryContext.CacheContext() + ctx, _ = p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext() } } else { // Get the query context at the given height. From 3de07cee40b39f19bffefefc14594284b65e770c Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 17:12:24 -0400 Subject: [PATCH 5/8] bing bong --- eth/core/chain_writer.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/eth/core/chain_writer.go b/eth/core/chain_writer.go index 22b8b9471..e3a576ef8 100644 --- a/eth/core/chain_writer.go +++ b/eth/core/chain_writer.go @@ -72,8 +72,14 @@ func (bc *blockchain) InsertBlockAndSetHead(block *types.Block) error { // ValidateState validates the statedb post block processing. 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 + 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 } // We can just immediately finalize the block. It's okay in this context. From 17dc35cb3a2c9943d009f3b3f5c2e34cb7ab3d78 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 18:07:33 -0400 Subject: [PATCH 6/8] fake finalize block context --- cosmos/miner/miner.go | 10 ++++++++-- eth/core/chain.go | 4 ++++ eth/core/chain_writer.go | 29 ++++++++++------------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cosmos/miner/miner.go b/cosmos/miner/miner.go index 4d5b3daac..a1dfd1037 100644 --- a/cosmos/miner/miner.go +++ b/cosmos/miner/miner.go @@ -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 @@ -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 diff --git a/eth/core/chain.go b/eth/core/chain.go index 9a77a130c..53fcf2816 100644 --- a/eth/core/chain.go +++ b/eth/core/chain.go @@ -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" @@ -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 @@ -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... diff --git a/eth/core/chain_writer.go b/eth/core/chain_writer.go index e3a576ef8..fd2e196ef 100644 --- a/eth/core/chain_writer.go +++ b/eth/core/chain_writer.go @@ -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 } @@ -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 } @@ -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() @@ -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. @@ -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 } From bb47675c783a13df295e095b286f8fe0d486778e Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 18:08:01 -0400 Subject: [PATCH 7/8] ree --- cosmos/x/evm/plugins/state/plugin.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index 02fa0ae22..ab4c9b470 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -140,9 +140,7 @@ 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). - WithKVGasConfig(storetypes.GasConfig{}). - WithTransientKVGasConfig(storetypes.GasConfig{}).CacheContext() + p.latestQueryContext = sdk.UnwrapSDKContext(ctx) } // Reset sets up the state plugin for execution of a new transaction. It sets up the snapshottable @@ -538,7 +536,7 @@ func (p *plugin) StateAtBlockNumber(number uint64) (core.StatePlugin, error) { if p.latestQueryContext.MultiStore() == nil { ctx = p.latestQueryContext.WithEventManager(sdk.NewEventManager()) } else { - ctx, _ = p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext() + ctx, _ = p.latestQueryContext.CacheContext() } } else { // Get the query context at the given height. From 6bf626110a9c013c6aaf8dd329e5e1c7bec6e538 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 2 Nov 2023 18:08:22 -0400 Subject: [PATCH 8/8] ree --- cosmos/miner/miner.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cosmos/miner/miner.go b/cosmos/miner/miner.go index a1dfd1037..21346b4de 100644 --- a/cosmos/miner/miner.go +++ b/cosmos/miner/miner.go @@ -100,6 +100,7 @@ func (m *Miner) PrepareProposal( Misbehavior: req.Misbehavior, Height: req.Height, NextValidatorsHash: req.NextValidatorsHash, + ProposerAddress: req.ProposerAddress, }); err != nil { return nil, err } else if _, err = m.app.BeginBlocker(ctx); err != nil {