Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(evmutil): create module account on InitGenesis #1655

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Bug Fixes
- (evmutil) [#1655] Initialize x/evmutil module account in InitGenesis

## [v0.24.0]

### Features
- (evmutil) [#1590] & [#1596] Add allow list param of sdk native denoms that can be transferred to evm
- (evmutil) [#1591] & [#1596] Configure module to support deploying ERC20KavaWrappedCosmosCoin contracts
Expand Down Expand Up @@ -268,6 +273,7 @@ the [changelog](https://github.com/cosmos/cosmos-sdk/blob/v0.38.4/CHANGELOG.md).
- [#257](https://github.com/Kava-Labs/kava/pulls/257) Include scripts to run
large-scale simulations remotely using aws-batch

[#1655]: https://github.com/Kava-Labs/kava/pull/1655
[#1624]: https://github.com/Kava-Labs/kava/pull/1624
[#1622]: https://github.com/Kava-Labs/kava/pull/1622
[#1614]: https://github.com/Kava-Labs/kava/pull/1614
Expand Down Expand Up @@ -307,7 +313,8 @@ the [changelog](https://github.com/cosmos/cosmos-sdk/blob/v0.38.4/CHANGELOG.md).
[#750]: https://github.com/Kava-Labs/kava/pull/750
[#751]: https://github.com/Kava-Labs/kava/pull/751
[#780]: https://github.com/Kava-Labs/kava/pull/780
[unreleased]: https://github.com/Kava-Labs/kava/compare/v0.23.2...HEAD
[unreleased]: https://github.com/Kava-Labs/kava/compare/v0.24.0...HEAD
[v0.24.0]: https://github.com/Kava-Labs/kava/compare/v0.24.0...v0.23.2
[v0.23.2]: https://github.com/Kava-Labs/kava/compare/v0.23.1...v0.23.2
[v0.23.1]: https://github.com/Kava-Labs/kava/compare/v0.23.0...v0.23.1
[v0.23.0]: https://github.com/Kava-Labs/kava/compare/v0.21.1...v0.23.0
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ func NewApp(
hard.NewAppModule(app.hardKeeper, app.accountKeeper, app.bankKeeper, app.pricefeedKeeper),
committee.NewAppModule(app.committeeKeeper, app.accountKeeper),
incentive.NewAppModule(app.incentiveKeeper, app.accountKeeper, app.bankKeeper, app.cdpKeeper),
evmutil.NewAppModule(app.evmutilKeeper, app.bankKeeper),
evmutil.NewAppModule(app.evmutilKeeper, app.bankKeeper, app.accountKeeper),
savings.NewAppModule(app.savingsKeeper, app.accountKeeper, app.bankKeeper),
liquid.NewAppModule(app.liquidKeeper),
earn.NewAppModule(app.earnKeeper, app.accountKeeper, app.bankKeeper),
Expand Down
7 changes: 6 additions & 1 deletion x/evmutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import (
)

// InitGenesis initializes the store state from a genesis state.
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, gs *types.GenesisState) {
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, gs *types.GenesisState, ak types.AccountKeeper) {
if err := gs.Validate(); err != nil {
panic(fmt.Sprintf("failed to validate %s genesis state: %s", types.ModuleName, err))
}

keeper.SetParams(ctx, gs.Params)

// initialize module account
if moduleAcc := ak.GetModuleAccount(ctx, types.ModuleName); moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

for _, account := range gs.Accounts {
keeper.SetAccount(ctx, account)
}
Expand Down
22 changes: 19 additions & 3 deletions x/evmutil/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/kava-labs/kava/x/evmutil"
"github.com/kava-labs/kava/x/evmutil/testutil"
"github.com/kava-labs/kava/x/evmutil/types"
Expand All @@ -28,7 +29,7 @@ func (s *genesisTestSuite) TestInitGenesis_SetAccounts() {
)
accounts := s.Keeper.GetAllAccounts(s.Ctx)
s.Require().Len(accounts, 0)
evmutil.InitGenesis(s.Ctx, s.Keeper, gs)
evmutil.InitGenesis(s.Ctx, s.Keeper, gs, s.AccountKeeper)
accounts = s.Keeper.GetAllAccounts(s.Ctx)
s.Require().Len(accounts, 1)
account := s.Keeper.GetAccount(s.Ctx, s.Addrs[0])
Expand All @@ -47,7 +48,7 @@ func (s *genesisTestSuite) TestInitGenesis_SetParams() {
[]types.Account{},
params,
)
evmutil.InitGenesis(s.Ctx, s.Keeper, gs)
evmutil.InitGenesis(s.Ctx, s.Keeper, gs, s.AccountKeeper)
params = s.Keeper.GetParams(s.Ctx)
s.Require().Len(params.EnabledConversionPairs, 1)
s.Require().Equal(conversionPair, params.EnabledConversionPairs[0])
Expand All @@ -61,10 +62,25 @@ func (s *genesisTestSuite) TestInitGenesis_ValidateFail() {
types.DefaultParams(),
)
s.Require().Panics(func() {
evmutil.InitGenesis(s.Ctx, s.Keeper, gs)
evmutil.InitGenesis(s.Ctx, s.Keeper, gs, s.AccountKeeper)
})
}

func (s *genesisTestSuite) TestInitGenesis_ModuleAccount() {
gs := types.NewGenesisState(
[]types.Account{},
types.DefaultParams(),
)
s.Require().NotPanics(func() {
evmutil.InitGenesis(s.Ctx, s.Keeper, gs, s.AccountKeeper)
})
// check for module account this way b/c GetModuleAccount creates if not existing.
acc := s.AccountKeeper.GetAccount(s.Ctx, s.AccountKeeper.GetModuleAddress(types.ModuleName))
s.Require().NotNil(acc)
_, ok := acc.(authtypes.ModuleAccountI)
s.Require().True(ok)
}

func (s *genesisTestSuite) TestExportGenesis() {
accounts := []types.Account{
{Address: s.Addrs[0], Balance: sdkmath.NewInt(10)},
Expand Down
2 changes: 1 addition & 1 deletion x/evmutil/keeper/conversion_evm_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (k Keeper) LockERC20Tokens(
contractAddr := pair.GetAddress()
initiatorStartBal, err := k.QueryERC20BalanceOf(ctx, contractAddr, initiator)
if err != nil {
return errorsmod.Wrapf(types.ErrEVMCall, "failed to retrieve balance %s", err.Error())
return errorsmod.Wrapf(types.ErrEVMCall, "failed to retrieve balance: %s", err.Error())
}

res, err := k.CallEVM(
Expand Down
2 changes: 1 addition & 1 deletion x/evmutil/keeper/conversion_evm_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (suite *ConversionTestSuite) TestConvertERC20ToCoin_EmptyContract() {
convertAmt,
)
suite.Require().Error(err)
suite.Require().ErrorContains(err, "contract call failed: method 'balanceOf'")
suite.Require().ErrorContains(err, "failed to retrieve balance: failed to unpack method balanceOf")

// bank balance should not change
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, userAddr, pair.Denom)
Expand Down
4 changes: 4 additions & 0 deletions x/evmutil/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ func unpackERC20ResToBigInt(res *evmtypes.MsgEthereumTxResponse, methodName stri
return nil, status.Error(codes.Internal, res.VmError)
}

if len(res.Ret) == 0 {
return nil, fmt.Errorf("failed to unpack method %s: expected response to be big.Int but found nil", methodName)
}

anyOutput, err := types.ERC20MintableBurnableContract.ABI.Unpack(methodName, res.Ret)
if err != nil {
return nil, fmt.Errorf(
Expand Down
10 changes: 6 additions & 4 deletions x/evmutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
type AppModule struct {
AppModuleBasic

keeper keeper.Keeper
bankKeeper types.BankKeeper
keeper keeper.Keeper
accountKeeer types.AccountKeeper
bankKeeper types.BankKeeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(keeper keeper.Keeper, bankKeeper types.BankKeeper) AppModule {
func NewAppModule(keeper keeper.Keeper, bankKeeper types.BankKeeper, accountKeeper types.AccountKeeper) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(),
keeper: keeper,
accountKeeer: accountKeeper,
bankKeeper: bankKeeper,
}
}
Expand Down Expand Up @@ -146,7 +148,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

InitGenesis(ctx, am.keeper, &genState)
InitGenesis(ctx, am.keeper, &genState, am.accountKeeer)
return []abci.ValidatorUpdate{}
}

Expand Down
2 changes: 2 additions & 0 deletions x/evmutil/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

// AccountKeeper defines the expected account keeper interface
type AccountKeeper interface {
GetModuleAccount(ctx sdk.Context, moduleName string) authtypes.ModuleAccountI
GetModuleAddress(moduleName string) sdk.AccAddress
GetSequence(sdk.Context, sdk.AccAddress) (uint64, error)
}
Expand Down