diff --git a/cosmos/x/evm/keeper/host.go b/cosmos/x/evm/keeper/host.go index a1a171f7d..e99590d69 100644 --- a/cosmos/x/evm/keeper/host.go +++ b/cosmos/x/evm/keeper/host.go @@ -26,7 +26,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkmempool "github.com/cosmos/cosmos-sdk/types/mempool" - "pkg.berachain.dev/polaris/cosmos/x/evm/plugins" "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/block" "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/configuration" "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/engine" @@ -49,7 +48,7 @@ var _ core.PolarisHostChain = (*host)(nil) // It includes core.PolarisHostChain and functions that are called in other packages. type Host interface { core.PolarisHostChain - GetAllPlugins() []plugins.Base + GetAllPlugins() []any Setup( storetypes.StoreKey, storetypes.StoreKey, @@ -103,7 +102,7 @@ func (h *host) Setup( ) { // Setup the state, precompile, historical, and txpool plugins h.sp = state.NewPlugin(ak, storeKey, log.NewFactory(h.pcs().GetPrecompiles())) - h.pp = precompile.NewPlugin(h.pcs().GetPrecompiles(), h.sp) + h.pp = precompile.NewPlugin(h.pcs().GetPrecompiles()) // TODO: re-enable historical plugin using ABCI listener. h.hp = historical.NewPlugin(h.cp, h.bp, nil, storeKey) h.txp.SetNonceRetriever(h.sp) @@ -153,6 +152,6 @@ func (h *host) GetTxPoolPlugin() core.TxPoolPlugin { } // GetAllPlugins returns all the plugins. -func (h *host) GetAllPlugins() []plugins.Base { - return []plugins.Base{h.bp, h.cp, h.gp, h.hp, h.pp, h.sp, h.txp} +func (h *host) GetAllPlugins() []any { + return []any{h.bp, h.cp, h.gp, h.hp, h.pp, h.sp, h.txp} } diff --git a/cosmos/x/evm/plugins/base.go b/cosmos/x/evm/plugins/base.go index a88eadf76..61898231f 100644 --- a/cosmos/x/evm/plugins/base.go +++ b/cosmos/x/evm/plugins/base.go @@ -26,10 +26,6 @@ import ( "pkg.berachain.dev/polaris/eth/core" ) -// Base is the base interface which all x/evm Polaris plugins must implement - -type Base interface{} - // HasGenesis represents the base class that all x/evm Polaris plugins which have // InitGenesis or ExportGenesis methods must implement diff --git a/cosmos/x/evm/plugins/block/plugin.go b/cosmos/x/evm/plugins/block/plugin.go index 415939f1a..7c3dc733a 100644 --- a/cosmos/x/evm/plugins/block/plugin.go +++ b/cosmos/x/evm/plugins/block/plugin.go @@ -35,7 +35,6 @@ import ( ) type Plugin interface { - plugins.Base plugins.HasGenesis core.BlockPlugin diff --git a/cosmos/x/evm/plugins/configuration/plugin.go b/cosmos/x/evm/plugins/configuration/plugin.go index 3110e8f9a..ad44908d7 100644 --- a/cosmos/x/evm/plugins/configuration/plugin.go +++ b/cosmos/x/evm/plugins/configuration/plugin.go @@ -36,7 +36,6 @@ import ( // Plugin is the interface that must be implemented by the plugin. type Plugin interface { - plugins.Base plugins.HasGenesis core.ConfigurationPlugin SetChainConfig(*params.ChainConfig) diff --git a/cosmos/x/evm/plugins/engine/plugin.go b/cosmos/x/evm/plugins/engine/plugin.go index ddfa912d9..0b5afac1e 100644 --- a/cosmos/x/evm/plugins/engine/plugin.go +++ b/cosmos/x/evm/plugins/engine/plugin.go @@ -23,7 +23,6 @@ package engine import ( "github.com/cosmos/cosmos-sdk/client" - "pkg.berachain.dev/polaris/cosmos/x/evm/plugins" "pkg.berachain.dev/polaris/eth/core" ) @@ -32,7 +31,6 @@ var _ Plugin = (*plugin)(nil) // Plugin defines the required functions of the transaction pool plugin. type Plugin interface { - plugins.Base core.EnginePlugin Start(client.Context) } diff --git a/cosmos/x/evm/plugins/gas/plugin.go b/cosmos/x/evm/plugins/gas/plugin.go index dfe453e34..018a0b11d 100644 --- a/cosmos/x/evm/plugins/gas/plugin.go +++ b/cosmos/x/evm/plugins/gas/plugin.go @@ -28,7 +28,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "pkg.berachain.dev/polaris/cosmos/x/evm/plugins" "pkg.berachain.dev/polaris/eth/core" "pkg.berachain.dev/polaris/eth/core/vm" ) @@ -38,7 +37,6 @@ const gasMeterDescriptor = `polaris-gas-plugin` // Plugin is the interface that must be implemented by the plugin. type Plugin interface { - plugins.Base core.GasPlugin } diff --git a/cosmos/x/evm/plugins/historical/plugin.go b/cosmos/x/evm/plugins/historical/plugin.go index eb19e21b6..dd01daab2 100644 --- a/cosmos/x/evm/plugins/historical/plugin.go +++ b/cosmos/x/evm/plugins/historical/plugin.go @@ -33,7 +33,6 @@ import ( // Plugin is the interface that must be implemented by the plugin. type Plugin interface { - plugins.Base core.HistoricalPlugin plugins.HasGenesis } diff --git a/cosmos/x/evm/plugins/precompile/plugin.go b/cosmos/x/evm/plugins/precompile/plugin.go index 8f0aeaed8..de506e76e 100644 --- a/cosmos/x/evm/plugins/precompile/plugin.go +++ b/cosmos/x/evm/plugins/precompile/plugin.go @@ -27,11 +27,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "pkg.berachain.dev/polaris/cosmos/x/evm/plugins" "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/state" "pkg.berachain.dev/polaris/eth/common" "pkg.berachain.dev/polaris/eth/core" ethprecompile "pkg.berachain.dev/polaris/eth/core/precompile" + ethstate "pkg.berachain.dev/polaris/eth/core/state" "pkg.berachain.dev/polaris/eth/core/vm" "pkg.berachain.dev/polaris/eth/params" "pkg.berachain.dev/polaris/lib/registry" @@ -41,13 +41,14 @@ import ( // Plugin is the interface that must be implemented by the plugin. type Plugin interface { - plugins.Base core.PrecompilePlugin +} - KVGasConfig() storetypes.GasConfig - SetKVGasConfig(storetypes.GasConfig) - TransientKVGasConfig() storetypes.GasConfig - SetTransientKVGasConfig(storetypes.GasConfig) +// polarisStateDB is the interface that must be implemented by the state DB. +// The stateDB must allow retrieving the plugin in order to set it's gas config. +type polarisStateDB interface { + // GetPlugin retrieves the underlying state plugin from the StateDB. + GetPlugin() ethstate.Plugin } // plugin runs precompile containers in the Cosmos environment with the context gas configs. @@ -59,18 +60,17 @@ type plugin struct { kvGasConfig storetypes.GasConfig // transientKVGasConfig is the gas config for the transient KV store. transientKVGasConfig storetypes.GasConfig - // sp allows resetting the context for the reentrancy into the EVM. - sp StatePlugin } // NewPlugin creates and returns a plugin with the default KV store gas configs. -func NewPlugin(precompiles []ethprecompile.Registrable, sp StatePlugin) Plugin { +func NewPlugin(precompiles []ethprecompile.Registrable) Plugin { return &plugin{ - Registry: registry.NewMap[common.Address, vm.PrecompileContainer](), - precompiles: precompiles, + Registry: registry.NewMap[common.Address, vm.PrecompileContainer](), + precompiles: precompiles, + // NOTE: these are hardcoded as they are also hardcoded in the sdk. + // This should be updated if it ever changes. kvGasConfig: storetypes.KVGasConfig(), transientKVGasConfig: storetypes.TransientGasConfig(), - sp: sp, } } @@ -92,26 +92,6 @@ func (p *plugin) GetActive(rules *params.Rules) []common.Address { return active } -// KVGasConfig implements Plugin. -func (p *plugin) KVGasConfig() storetypes.GasConfig { - return p.kvGasConfig -} - -// SetKVGasConfig implements Plugin. -func (p *plugin) SetKVGasConfig(kvGasConfig storetypes.GasConfig) { - p.kvGasConfig = kvGasConfig -} - -// TransientKVGasConfig implements Plugin. -func (p *plugin) TransientKVGasConfig() storetypes.GasConfig { - return p.transientKVGasConfig -} - -// SetTransientKVGasConfig implements Plugin. -func (p *plugin) SetTransientKVGasConfig(transientKVGasConfig storetypes.GasConfig) { - p.transientKVGasConfig = transientKVGasConfig -} - // Run runs the a precompile container and returns the remaining gas after execution by injecting // a Cosmos SDK `GasMeter`. This function returns an error if the precompile execution returns an // error or insufficient gas is provided. @@ -187,7 +167,9 @@ func (p *plugin) enableReentrancy(sdb vm.PolarisStateDB) { cem.EndPrecompileExecution() // remove Cosmos gas consumption so gas is consumed only per OPCODE - p.sp.SetGasConfig(storetypes.GasConfig{}, storetypes.GasConfig{}) + utils.MustGetAs[state.Plugin]( + utils.MustGetAs[polarisStateDB](sdb).GetPlugin(), + ).SetGasConfig(storetypes.GasConfig{}, storetypes.GasConfig{}) } // DisableReentrancy sets the state so that execution cannot enter the EVM again. @@ -205,5 +187,7 @@ func (p *plugin) disableReentrancy(sdb vm.PolarisStateDB) { cem.BeginPrecompileExecution(sdb) // restore ctx gas configs for continuing precompile execution - p.sp.SetGasConfig(p.kvGasConfig, p.transientKVGasConfig) + utils.MustGetAs[state.Plugin]( + utils.MustGetAs[polarisStateDB](sdb).GetPlugin(), + ).SetGasConfig(p.kvGasConfig, p.transientKVGasConfig) } diff --git a/cosmos/x/evm/plugins/precompile/plugin_test.go b/cosmos/x/evm/plugins/precompile/plugin_test.go index 61903f9a6..cf453e226 100644 --- a/cosmos/x/evm/plugins/precompile/plugin_test.go +++ b/cosmos/x/evm/plugins/precompile/plugin_test.go @@ -35,6 +35,7 @@ import ( "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/state/events" "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/state/events/mock" "pkg.berachain.dev/polaris/eth/common" + ethstate "pkg.berachain.dev/polaris/eth/core/state" coretypes "pkg.berachain.dev/polaris/eth/core/types" "pkg.berachain.dev/polaris/eth/core/vm" "pkg.berachain.dev/polaris/lib/utils" @@ -53,7 +54,7 @@ var _ = Describe("plugin", func() { ctx = ctx.WithEventManager( events.NewManagerFrom(ctx.EventManager(), mock.NewPrecompileLogFactory()), ) - p = utils.MustGetAs[*plugin](NewPlugin(nil, &mockSP{ctx})) + p = utils.MustGetAs[*plugin](NewPlugin(nil)) e = &mockEVM{nil, ctx, &mockSDB{nil, ctx, 0}} }) @@ -68,20 +69,6 @@ var _ = Describe("plugin", func() { Expect(err).To(MatchError("out of gas")) }) - It("should plug in custom gas configs", func() { - Expect(p.KVGasConfig().DeleteCost).To(Equal(uint64(1000))) - Expect(p.TransientKVGasConfig().DeleteCost).To(Equal(uint64(100))) - - p.SetKVGasConfig(storetypes.GasConfig{ - DeleteCost: 2, - }) - Expect(p.KVGasConfig().DeleteCost).To(Equal(uint64(2))) - p.SetTransientKVGasConfig(storetypes.GasConfig{ - DeleteCost: 3, - }) - Expect(p.TransientKVGasConfig().DeleteCost).To(Equal(uint64(3))) - }) - It("should handle read-only static calls", func() { ms := utils.MustGetAs[tmock.MultiStore](ctx.MultiStore()) cem := utils.MustGetAs[state.ControllableEventManager](ctx.EventManager()) @@ -123,16 +110,6 @@ var ( addr2 = common.BytesToAddress([]byte{2}) ) -// MOCKS BELOW. - -type mockSP struct { - ctx sdk.Context -} - -func (msp *mockSP) SetGasConfig(kvg storetypes.GasConfig, tkvg storetypes.GasConfig) { - msp.ctx = msp.ctx.WithKVGasConfig(kvg).WithTransientKVGasConfig(tkvg) -} - type mockEVM struct { vm.PrecompileEVM ctx sdk.Context @@ -149,6 +126,12 @@ type mockSDB struct { logs int } +func (ms *mockSDB) GetPlugin() ethstate.Plugin { + return state.NewPlugin( + nil, nil, nil, + ) +} + func (ms *mockSDB) GetContext() context.Context { return ms.ctx } diff --git a/cosmos/x/evm/plugins/state/plugin.go b/cosmos/x/evm/plugins/state/plugin.go index af3ac8573..aa298bbc2 100644 --- a/cosmos/x/evm/plugins/state/plugin.go +++ b/cosmos/x/evm/plugins/state/plugin.go @@ -53,7 +53,6 @@ var ( // Plugin is the interface that must be implemented by the plugin. type Plugin interface { - plugins.Base plugins.HasGenesis core.StatePlugin // SetQueryContextFn sets the query context func for the plugin. @@ -544,5 +543,3 @@ func (p *plugin) Clone() ethstate.Plugin { func (p *plugin) SetGasConfig(kvGasConfig, transientKVGasConfig storetypes.GasConfig) { p.ctx = p.ctx.WithKVGasConfig(kvGasConfig).WithTransientKVGasConfig(transientKVGasConfig) } - -// IsPlugin implements plugins.Base. diff --git a/cosmos/x/evm/plugins/txpool/plugin.go b/cosmos/x/evm/plugins/txpool/plugin.go index fd9e9e75b..3661cf794 100644 --- a/cosmos/x/evm/plugins/txpool/plugin.go +++ b/cosmos/x/evm/plugins/txpool/plugin.go @@ -29,7 +29,6 @@ import ( "github.com/ethereum/go-ethereum/event" - "pkg.berachain.dev/polaris/cosmos/x/evm/plugins" mempool "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/txpool/mempool" "pkg.berachain.dev/polaris/eth/core" coretypes "pkg.berachain.dev/polaris/eth/core/types" @@ -41,7 +40,6 @@ var _ Plugin = (*plugin)(nil) // Plugin defines the required functions of the transaction pool plugin. type Plugin interface { - plugins.Base core.TxPoolPlugin SetNonceRetriever(mempool.NonceRetriever) SetClientContext(client.Context) diff --git a/eth/core/precompile/default_plugin.go b/eth/core/precompile/default_plugin.go index 1702d88ff..23925b7d4 100644 --- a/eth/core/precompile/default_plugin.go +++ b/eth/core/precompile/default_plugin.go @@ -45,6 +45,12 @@ func NewDefaultPlugin() Plugin { } } +// Register is a no-op for the default plugin. +func (dp *defaultPlugin) Register(vm.PrecompileContainer) error { + // no-op + return nil +} + // GetPrecompiles implements core.PrecompilePlugin. func (dp *defaultPlugin) GetPrecompiles(rules *params.Rules) []Registrable { return GetDefaultPrecompiles(rules) diff --git a/eth/core/state/statedb.go b/eth/core/state/statedb.go index 0ab9d3077..b6d3b8c81 100644 --- a/eth/core/state/statedb.go +++ b/eth/core/state/statedb.go @@ -81,6 +81,15 @@ func newStateDBWithJournals( } } +// ============================================================================= +// Plugin +// ============================================================================= + +// GetPlugin returns the plugin from statedb. +func (sdb *stateDB) GetPlugin() Plugin { + return sdb.Plugin +} + // ============================================================================= // Snapshot // =============================================================================