Skip to content

Commit 8903f9d

Browse files
authored
refactor: decouple keeper using interface on precompile (cosmos#477)
* refactor: decouple keeper using interface on precompile - Define a set of keeper interfaces in precompiles/common/interfaces.go that specify the exact methods required by the precompiles. - Update all precompile constructors and internal structs to use these new interfaces. - Modifie the precompile assembly in `evmd/precompiles.go` to inject the concrete `MsgServer` and `Querier` implementations, which satisfy the required interfaces. - Removed unnecessary keeper dependencies from the function signatures of several precompiles * docs: add changelog
1 parent f164197 commit 8903f9d

File tree

28 files changed

+200
-163
lines changed

28 files changed

+200
-163
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
### API-BREAKING
6161

62+
- [\#477](https://github.com/cosmos/evm/pull/477) Refactor precompile constructors to accept keeper interfaces instead of concrete implementations, breaking the existing `NewPrecompile` function signatures.
6263
- [\#456](https://github.com/cosmos/evm/pull/456) Remove non–go-ethereum JSON-RPC methods to align with Geth’s surface
6364
- [\#443](https://github.com/cosmos/evm/pull/443) Move `ante` logic from the `evmd` Go package to the `evm` package to
6465
be exported as a library.

evmd/precompiles.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,21 @@ func NewAvailableStaticPrecompiles(
101101
panic(fmt.Errorf("failed to instantiate bech32 precompile: %w", err))
102102
}
103103

104-
stakingPrecompile, err := stakingprecompile.NewPrecompile(stakingKeeper, bankKeeper, options.AddressCodec)
104+
stakingPrecompile, err := stakingprecompile.NewPrecompile(
105+
stakingKeeper,
106+
stakingkeeper.NewMsgServerImpl(&stakingKeeper),
107+
stakingkeeper.NewQuerier(&stakingKeeper),
108+
bankKeeper,
109+
options.AddressCodec,
110+
)
105111
if err != nil {
106112
panic(fmt.Errorf("failed to instantiate staking precompile: %w", err))
107113
}
108114

109115
distributionPrecompile, err := distprecompile.NewPrecompile(
110116
distributionKeeper,
117+
distributionkeeper.NewMsgServerImpl(distributionKeeper),
118+
distributionkeeper.NewQuerier(distributionKeeper),
111119
stakingKeeper,
112120
bankKeeper,
113121
options.AddressCodec,
@@ -121,7 +129,6 @@ func NewAvailableStaticPrecompiles(
121129
stakingKeeper,
122130
transferKeeper,
123131
channelKeeper,
124-
evmKeeper,
125132
)
126133
if err != nil {
127134
panic(fmt.Errorf("failed to instantiate ICS20 precompile: %w", err))
@@ -132,12 +139,24 @@ func NewAvailableStaticPrecompiles(
132139
panic(fmt.Errorf("failed to instantiate bank precompile: %w", err))
133140
}
134141

135-
govPrecompile, err := govprecompile.NewPrecompile(govKeeper, bankKeeper, codec, options.AddressCodec)
142+
govPrecompile, err := govprecompile.NewPrecompile(
143+
govkeeper.NewMsgServerImpl(&govKeeper),
144+
govkeeper.NewQueryServer(&govKeeper),
145+
bankKeeper,
146+
codec,
147+
options.AddressCodec,
148+
)
136149
if err != nil {
137150
panic(fmt.Errorf("failed to instantiate gov precompile: %w", err))
138151
}
139152

140-
slashingPrecompile, err := slashingprecompile.NewPrecompile(slashingKeeper, bankKeeper, options.ValidatorAddrCodec, options.ConsensusAddrCodec)
153+
slashingPrecompile, err := slashingprecompile.NewPrecompile(
154+
slashingKeeper,
155+
slashingkeeper.NewMsgServerImpl(slashingKeeper),
156+
bankKeeper,
157+
options.ValidatorAddrCodec,
158+
options.ConsensusAddrCodec,
159+
)
141160
if err != nil {
142161
panic(fmt.Errorf("failed to instantiate slashing precompile: %w", err))
143162
}

evmd/tests/ibc/ics20_precompile_transfer_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ func (suite *ICS20TransferTestSuite) SetupTest() {
5151
*evmAppA.StakingKeeper,
5252
evmAppA.TransferKeeper,
5353
evmAppA.IBCKeeper.ChannelKeeper,
54-
evmAppA.EVMKeeper,
5554
)
5655
evmAppB := suite.chainB.App.(*evmd.EVMD)
5756
suite.chainBPrecompile, _ = ics20.NewPrecompile(
5857
evmAppB.BankKeeper,
5958
*evmAppB.StakingKeeper,
6059
evmAppB.TransferKeeper,
6160
evmAppB.IBCKeeper.ChannelKeeper,
62-
evmAppB.EVMKeeper,
6361
)
6462
}
6563

evmd/tests/ibc/v2_ics20_precompile_transfer_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ func (suite *ICS20TransferV2TestSuite) SetupTest() {
5252
*evmAppA.StakingKeeper,
5353
evmAppA.TransferKeeper,
5454
evmAppA.IBCKeeper.ChannelKeeper,
55-
evmAppA.EVMKeeper,
5655
)
5756
evmAppB := suite.chainB.App.(*evmd.EVMD)
5857
suite.chainBPrecompile, _ = ics20.NewPrecompile(
5958
evmAppB.BankKeeper,
6059
*evmAppB.StakingKeeper,
6160
evmAppB.TransferKeeper,
6261
evmAppB.IBCKeeper.ChannelKeeper,
63-
evmAppB.EVMKeeper,
6462
)
6563
}
6664

ibc/interfaces.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ibc
2+
3+
import (
4+
cmtbytes "github.com/cometbft/cometbft/libs/bytes"
5+
6+
ibctypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"
7+
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
)
10+
11+
type TransferKeeper interface {
12+
GetDenom(ctx sdk.Context, denomHash cmtbytes.HexBytes) (ibctypes.Denom, bool)
13+
}

ibc/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"strings"
55

66
"github.com/cosmos/evm/utils"
7-
transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper"
87
transfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"
98
channeltypes "github.com/cosmos/ibc-go/v10/modules/core/04-channel/types"
109

@@ -108,7 +107,7 @@ func GetSentCoin(rawDenom, rawAmt string) sdk.Coin {
108107
// GetDenom returns the denomination from the corresponding IBC denomination. If the
109108
// denomination is not an IBC voucher or the trace is not found, it returns an error.
110109
func GetDenom(
111-
transferKeeper transferkeeper.Keeper,
110+
transferKeeper TransferKeeper,
112111
ctx sdk.Context,
113112
voucherDenom string,
114113
) (transfertypes.Denom, error) {

precompiles/bank/bank.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ethereum/go-ethereum/core/vm"
1616

1717
cmn "github.com/cosmos/evm/precompiles/common"
18-
erc20keeper "github.com/cosmos/evm/x/erc20/keeper"
1918
evmtypes "github.com/cosmos/evm/x/vm/types"
2019

2120
storetypes "cosmossdk.io/store/types"
@@ -43,14 +42,14 @@ var f embed.FS
4342
type Precompile struct {
4443
cmn.Precompile
4544
bankKeeper cmn.BankKeeper
46-
erc20Keeper erc20keeper.Keeper
45+
erc20Keeper cmn.ERC20Keeper
4746
}
4847

4948
// NewPrecompile creates a new bank Precompile instance implementing the
5049
// PrecompiledContract interface.
5150
func NewPrecompile(
5251
bankKeeper cmn.BankKeeper,
53-
erc20Keeper erc20keeper.Keeper,
52+
erc20Keeper cmn.ERC20Keeper,
5453
) (*Precompile, error) {
5554
newABI, err := cmn.LoadABI(f, "abi.json")
5655
if err != nil {

precompiles/common/interfaces.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@ package common
33
import (
44
"context"
55

6+
ethcommon "github.com/ethereum/go-ethereum/common"
7+
8+
erc20types "github.com/cosmos/evm/x/erc20/types"
9+
ibctypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"
10+
connectiontypes "github.com/cosmos/ibc-go/v10/modules/core/03-connection/types"
11+
channeltypes "github.com/cosmos/ibc-go/v10/modules/core/04-channel/types"
12+
613
sdk "github.com/cosmos/cosmos-sdk/types"
714
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
15+
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
16+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
817
)
918

1019
type BankKeeper interface {
@@ -18,3 +27,39 @@ type BankKeeper interface {
1827
SpendableCoin(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
1928
BlockedAddr(addr sdk.AccAddress) bool
2029
}
30+
31+
type TransferKeeper interface {
32+
Denom(ctx context.Context, req *ibctypes.QueryDenomRequest) (*ibctypes.QueryDenomResponse, error)
33+
Denoms(ctx context.Context, req *ibctypes.QueryDenomsRequest) (*ibctypes.QueryDenomsResponse, error)
34+
DenomHash(ctx context.Context, req *ibctypes.QueryDenomHashRequest) (*ibctypes.QueryDenomHashResponse, error)
35+
Transfer(ctx context.Context, msg *ibctypes.MsgTransfer) (*ibctypes.MsgTransferResponse, error)
36+
}
37+
38+
type ChannelKeeper interface {
39+
GetChannel(ctx sdk.Context, portID, channelID string) (channeltypes.Channel, bool)
40+
GetConnection(ctx sdk.Context, connectionID string) (connectiontypes.ConnectionEnd, error)
41+
}
42+
43+
type DistributionKeeper interface {
44+
WithdrawDelegationRewards(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Coins, error)
45+
}
46+
47+
type StakingKeeper interface {
48+
BondDenom(ctx context.Context) (string, error)
49+
MaxValidators(ctx context.Context) (uint32, error)
50+
GetDelegatorValidators(ctx context.Context, delegatorAddr sdk.AccAddress, maxRetrieve uint32) (stakingtypes.Validators, error)
51+
GetRedelegation(ctx context.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) (red stakingtypes.Redelegation, err error)
52+
GetValidator(ctx context.Context, addr sdk.ValAddress) (validator stakingtypes.Validator, err error)
53+
}
54+
55+
type SlashingKeeper interface {
56+
Params(ctx context.Context, req *slashingtypes.QueryParamsRequest) (*slashingtypes.QueryParamsResponse, error)
57+
SigningInfo(ctx context.Context, req *slashingtypes.QuerySigningInfoRequest) (*slashingtypes.QuerySigningInfoResponse, error)
58+
SigningInfos(ctx context.Context, req *slashingtypes.QuerySigningInfosRequest) (*slashingtypes.QuerySigningInfosResponse, error)
59+
}
60+
61+
type ERC20Keeper interface {
62+
GetCoinAddress(ctx sdk.Context, denom string) (ethcommon.Address, error)
63+
GetERC20Map(ctx sdk.Context, erc20 ethcommon.Address) []byte
64+
GetTokenPair(ctx sdk.Context, id []byte) (erc20types.TokenPair, bool)
65+
}

precompiles/distribution/distribution.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
"cosmossdk.io/core/address"
1616
storetypes "cosmossdk.io/store/types"
1717

18-
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
19-
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
18+
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
2019
)
2120

2221
var _ vm.PrecompiledContract = &Precompile{}
@@ -29,16 +28,20 @@ var f embed.FS
2928
// Precompile defines the precompiled contract for distribution.
3029
type Precompile struct {
3130
cmn.Precompile
32-
distributionKeeper distributionkeeper.Keeper
33-
stakingKeeper stakingkeeper.Keeper
34-
addrCdc address.Codec
31+
distributionKeeper cmn.DistributionKeeper
32+
distributionMsgServer distributiontypes.MsgServer
33+
distributionQuerier distributiontypes.QueryServer
34+
stakingKeeper cmn.StakingKeeper
35+
addrCdc address.Codec
3536
}
3637

3738
// NewPrecompile creates a new distribution Precompile instance as a
3839
// PrecompiledContract interface.
3940
func NewPrecompile(
40-
distributionKeeper distributionkeeper.Keeper,
41-
stakingKeeper stakingkeeper.Keeper,
41+
distributionKeeper cmn.DistributionKeeper,
42+
distributionMsgServer distributiontypes.MsgServer,
43+
distributionQuerier distributiontypes.QueryServer,
44+
stakingKeeper cmn.StakingKeeper,
4245
bankKeeper cmn.BankKeeper,
4346
addrCdc address.Codec,
4447
) (*Precompile, error) {
@@ -53,9 +56,11 @@ func NewPrecompile(
5356
KvGasConfig: storetypes.KVGasConfig(),
5457
TransientKVGasConfig: storetypes.TransientGasConfig(),
5558
},
56-
stakingKeeper: stakingKeeper,
57-
distributionKeeper: distributionKeeper,
58-
addrCdc: addrCdc,
59+
stakingKeeper: stakingKeeper,
60+
distributionKeeper: distributionKeeper,
61+
distributionMsgServer: distributionMsgServer,
62+
distributionQuerier: distributionQuerier,
63+
addrCdc: addrCdc,
5964
}
6065

6166
// SetAddress defines the address of the distribution compile contract.

precompiles/distribution/query.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
cmn "github.com/cosmos/evm/precompiles/common"
88

99
sdk "github.com/cosmos/cosmos-sdk/types"
10-
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
1110
)
1211

1312
const (
@@ -52,9 +51,7 @@ func (p Precompile) ValidatorDistributionInfo(
5251
return nil, err
5352
}
5453

55-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
56-
57-
res, err := querier.ValidatorDistributionInfo(ctx, req)
54+
res, err := p.distributionQuerier.ValidatorDistributionInfo(ctx, req)
5855
if err != nil {
5956
return nil, err
6057
}
@@ -76,9 +73,7 @@ func (p Precompile) ValidatorOutstandingRewards(
7673
return nil, err
7774
}
7875

79-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
80-
81-
res, err := querier.ValidatorOutstandingRewards(ctx, req)
76+
res, err := p.distributionQuerier.ValidatorOutstandingRewards(ctx, req)
8277
if err != nil {
8378
return nil, err
8479
}
@@ -98,9 +93,7 @@ func (p Precompile) ValidatorCommission(
9893
return nil, err
9994
}
10095

101-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
102-
103-
res, err := querier.ValidatorCommission(ctx, req)
96+
res, err := p.distributionQuerier.ValidatorCommission(ctx, req)
10497
if err != nil {
10598
return nil, err
10699
}
@@ -120,9 +113,7 @@ func (p Precompile) ValidatorSlashes(
120113
return nil, err
121114
}
122115

123-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
124-
125-
res, err := querier.ValidatorSlashes(ctx, req)
116+
res, err := p.distributionQuerier.ValidatorSlashes(ctx, req)
126117
if err != nil {
127118
return nil, err
128119
}
@@ -144,8 +135,7 @@ func (p Precompile) DelegationRewards(
144135
return nil, err
145136
}
146137

147-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
148-
res, err := querier.DelegationRewards(ctx, req)
138+
res, err := p.distributionQuerier.DelegationRewards(ctx, req)
149139
if err != nil {
150140
return nil, err
151141
}
@@ -165,9 +155,7 @@ func (p Precompile) DelegationTotalRewards(
165155
return nil, err
166156
}
167157

168-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
169-
170-
res, err := querier.DelegationTotalRewards(ctx, req)
158+
res, err := p.distributionQuerier.DelegationTotalRewards(ctx, req)
171159
if err != nil {
172160
return nil, err
173161
}
@@ -189,9 +177,7 @@ func (p Precompile) DelegatorValidators(
189177
return nil, err
190178
}
191179

192-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
193-
194-
res, err := querier.DelegatorValidators(ctx, req)
180+
res, err := p.distributionQuerier.DelegatorValidators(ctx, req)
195181
if err != nil {
196182
return nil, err
197183
}
@@ -211,9 +197,7 @@ func (p Precompile) DelegatorWithdrawAddress(
211197
return nil, err
212198
}
213199

214-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
215-
216-
res, err := querier.DelegatorWithdrawAddress(ctx, req)
200+
res, err := p.distributionQuerier.DelegatorWithdrawAddress(ctx, req)
217201
if err != nil {
218202
return nil, err
219203
}
@@ -233,9 +217,7 @@ func (p Precompile) CommunityPool(
233217
return nil, err
234218
}
235219

236-
querier := distributionkeeper.Querier{Keeper: p.distributionKeeper}
237-
238-
res, err := querier.CommunityPool(ctx, req)
220+
res, err := p.distributionQuerier.CommunityPool(ctx, req)
239221
if err != nil {
240222
return nil, err
241223
}

0 commit comments

Comments
 (0)