Skip to content

Commit

Permalink
fix(evmutil): create module account on InitGenesis
Browse files Browse the repository at this point in the history
ensures the creation of the x/evmutil module account on init genesis.
  • Loading branch information
pirtleshell committed Jul 21, 2023
1 parent 2bcfe5c commit bcbac78
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
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
3 changes: 3 additions & 0 deletions x/evmutil/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func getCmdConvertEvmERC20ToCoin() *cobra.Command {
}

signer := clientCtx.GetFromAddress()
fmt.Println("signer: ", signer.String())
initiator, err := ParseAddrFromHexOrBech32(signer.String())
if err != nil {
return err
Expand All @@ -119,6 +120,8 @@ func getCmdConvertEvmERC20ToCoin() *cobra.Command {
return err
}

fmt.Printf("%+v\n", msg)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
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

0 comments on commit bcbac78

Please sign in to comment.