Skip to content

Commit 19b82ce

Browse files
authored
refactor: move default precompiles out of evmd config (#635)
* move default static precompiles creator out of app.go and set as defaults * move defaults to precompile folder and use existing builder pattern * undo interface changes on tests * undo unnecessary changes * fix tests * lints * add sum docs
1 parent b63a681 commit 19b82ce

File tree

4 files changed

+59
-29
lines changed

4 files changed

+59
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
- [\#598](https://github.com/cosmos/evm/pull/598) Reduce number of times CreateQueryContext in mempool.
3232
- [\#606](https://github.com/cosmos/evm/pull/606) Regenerate mock file for bank keeper related test.
3333
- [\#624](https://github.com/cosmos/evm/pull/624) Cleanup unnecessary `fix-revert-gas-refund-height`.
34+
- [\#635](https://github.com/cosmos/evm/pull/635) Move DefaultStaticPrecompiles to /evm and allow projects to set it by default alongside the keeper.
35+
3436

3537
### FEATURES
3638

docs/migrations/v0.4.0_to_v0.5.0_UNRELEASED.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,39 @@ mempoolConfig.CosmosPoolConfig = &cosmosCfg
8080
mempoolConfig.BroadCastTxFn = func(txs []*ethtypes.Transaction) error { return nil }
8181
```
8282

83+
### Default Precompiles
84+
85+
Default precompiles have been moved to `/evm/precompiles/types/defaults.go` and the function name was
86+
changed to `DefaultStaticPrecompiles`. The function signature has also changed, and now takes pointers
87+
as inputs for the `Erc20Keeper` and `TransferKeeper`. Finally, the `WithStaticPrecompiles` builder
88+
function can now happen *alongside the keeper instantiation*, and not after. The new wiring is shown below:
89+
90+
```go
91+
app.EVMKeeper = evmkeeper.NewKeeper(
92+
appCodec, keys[evmtypes.StoreKey], tkeys[evmtypes.TransientKey], keys,
93+
authtypes.NewModuleAddress(govtypes.ModuleName),
94+
app.AccountKeeper,
95+
app.PreciseBankKeeper,
96+
app.StakingKeeper,
97+
app.FeeMarketKeeper,
98+
&app.ConsensusParamsKeeper,
99+
&app.Erc20Keeper,
100+
tracer,
101+
).WithStaticPrecompiles(
102+
precompiletypes.DefaultStaticPrecompiles(
103+
*app.StakingKeeper,
104+
app.DistrKeeper,
105+
app.BankKeeper,
106+
&app.Erc20Keeper, // UPDATED
107+
&app.TransferKeeper, // UPDATED
108+
app.IBCKeeper.ChannelKeeper,
109+
app.GovKeeper,
110+
app.SlashingKeeper,
111+
appCodec,
112+
),
113+
)
114+
```
115+
83116
---
84117

85118
## 3) Build & quick tests

evmd/app.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
precompiletypes "github.com/cosmos/evm/precompiles/types"
78
"io"
89

910
"os"
@@ -486,6 +487,18 @@ func NewExampleApp(
486487
&app.ConsensusParamsKeeper,
487488
&app.Erc20Keeper,
488489
tracer,
490+
).WithStaticPrecompiles(
491+
precompiletypes.DefaultStaticPrecompiles(
492+
*app.StakingKeeper,
493+
app.DistrKeeper,
494+
app.BankKeeper,
495+
&app.Erc20Keeper,
496+
&app.TransferKeeper,
497+
app.IBCKeeper.ChannelKeeper,
498+
app.GovKeeper,
499+
app.SlashingKeeper,
500+
appCodec,
501+
),
489502
)
490503

491504
app.Erc20Keeper = erc20keeper.NewKeeper(
@@ -561,23 +574,6 @@ func NewExampleApp(
561574
// Override the ICS20 app module
562575
transferModule := transfer.NewAppModule(app.TransferKeeper)
563576

564-
// NOTE: we are adding all available Cosmos EVM EVM extensions.
565-
// Not all of them need to be enabled, which can be configured on a per-chain basis.
566-
app.EVMKeeper.WithStaticPrecompiles(
567-
NewAvailableStaticPrecompiles(
568-
*app.StakingKeeper,
569-
app.DistrKeeper,
570-
app.PreciseBankKeeper,
571-
app.Erc20Keeper,
572-
app.TransferKeeper,
573-
app.IBCKeeper.ChannelKeeper,
574-
app.EVMKeeper,
575-
app.GovKeeper,
576-
app.SlashingKeeper,
577-
app.AppCodec(),
578-
),
579-
)
580-
581577
/**** Module Options ****/
582578

583579
// NOTE: Any module instantiated in the module manager that is later modified
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
package evmd
1+
package types
22

33
import (
44
"fmt"
55
"maps"
66

7-
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
8-
sdk "github.com/cosmos/cosmos-sdk/types"
97
"github.com/ethereum/go-ethereum/common"
108
"github.com/ethereum/go-ethereum/core/vm"
119

@@ -20,11 +18,13 @@ import (
2018
stakingprecompile "github.com/cosmos/evm/precompiles/staking"
2119
erc20Keeper "github.com/cosmos/evm/x/erc20/keeper"
2220
transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper"
23-
evmkeeper "github.com/cosmos/evm/x/vm/keeper"
2421
channelkeeper "github.com/cosmos/ibc-go/v10/modules/core/04-channel/keeper"
2522

2623
"cosmossdk.io/core/address"
24+
2725
"github.com/cosmos/cosmos-sdk/codec"
26+
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
27+
sdktypes "github.com/cosmos/cosmos-sdk/types"
2828
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
2929
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
3030
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
@@ -42,9 +42,9 @@ type Optionals struct {
4242

4343
func defaultOptionals() Optionals {
4444
return Optionals{
45-
AddressCodec: addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
46-
ValidatorAddrCodec: addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
47-
ConsensusAddrCodec: addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
45+
AddressCodec: addresscodec.NewBech32Codec(sdktypes.GetConfig().GetBech32AccountAddrPrefix()),
46+
ValidatorAddrCodec: addresscodec.NewBech32Codec(sdktypes.GetConfig().GetBech32ValidatorAddrPrefix()),
47+
ConsensusAddrCodec: addresscodec.NewBech32Codec(sdktypes.GetConfig().GetBech32ConsensusAddrPrefix()),
4848
}
4949
}
5050

@@ -70,17 +70,16 @@ func WithConsensusAddrCodec(codec address.Codec) Option {
7070

7171
const bech32PrecompileBaseGas = 6_000
7272

73-
// NewAvailableStaticPrecompiles returns the list of all available static precompiled contracts from Cosmos EVM.
73+
// DefaultStaticPrecompiles returns the list of all available static precompiled contracts from Cosmos EVM.
7474
//
7575
// NOTE: this should only be used during initialization of the Keeper.
76-
func NewAvailableStaticPrecompiles(
76+
func DefaultStaticPrecompiles(
7777
stakingKeeper stakingkeeper.Keeper,
7878
distributionKeeper distributionkeeper.Keeper,
7979
bankKeeper cmn.BankKeeper,
80-
erc20Keeper erc20Keeper.Keeper,
81-
transferKeeper transferkeeper.Keeper,
80+
erc20Keeper *erc20Keeper.Keeper,
81+
transferKeeper *transferkeeper.Keeper,
8282
channelKeeper *channelkeeper.Keeper,
83-
evmKeeper *evmkeeper.Keeper,
8483
govKeeper govkeeper.Keeper,
8584
slashingKeeper slashingkeeper.Keeper,
8685
codec codec.Codec,

0 commit comments

Comments
 (0)