From 5c35bb36b9db5489971cbf3c6a7fcb45fec276cf Mon Sep 17 00:00:00 2001 From: lumtis Date: Fri, 4 Aug 2023 18:29:18 +0200 Subject: [PATCH 1/9] rename observer keeper --- x/fungible/keeper/evm.go | 2 +- x/fungible/keeper/gas_coin_and_pool.go | 2 +- x/fungible/keeper/keeper.go | 34 +++++++++---------- .../msg_server_deploy_fungible_coin_zrc20.go | 2 +- .../keeper/msg_server_remove_foreign_coin.go | 2 +- x/fungible/types/expected_keepers.go | 20 +++++------ 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/x/fungible/keeper/evm.go b/x/fungible/keeper/evm.go index 443880bf4d..ee661f99fd 100644 --- a/x/fungible/keeper/evm.go +++ b/x/fungible/keeper/evm.go @@ -53,7 +53,7 @@ func (k Keeper) DeployZRC20Contract( gasLimit *big.Int, ) (common.Address, error) { chainName := zetacommon.ParseChainName(chainStr) - chain := k.zetaobserverKeeper.GetParams(ctx).GetChainFromChainName(chainName) + chain := k.observerKeeper.GetParams(ctx).GetChainFromChainName(chainName) if chain == nil { return common.Address{}, sdkerrors.Wrapf(zetaObserverTypes.ErrSupportedChains, "chain %s not found", chainStr) } diff --git a/x/fungible/keeper/gas_coin_and_pool.go b/x/fungible/keeper/gas_coin_and_pool.go index 2c629352a7..9d84cf73de 100644 --- a/x/fungible/keeper/gas_coin_and_pool.go +++ b/x/fungible/keeper/gas_coin_and_pool.go @@ -21,7 +21,7 @@ import ( func (k Keeper) setupChainGasCoinAndPool(ctx sdk.Context, c string, gasAssetName string, symbol string, decimals uint8) (ethcommon.Address, error) { name := fmt.Sprintf("%s-%s", gasAssetName, c) chainName := common.ParseChainName(c) - chain := k.zetaobserverKeeper.GetParams(ctx).GetChainFromChainName(chainName) + chain := k.observerKeeper.GetParams(ctx).GetChainFromChainName(chainName) if chain == nil { return ethcommon.Address{}, zetaObserverTypes.ErrSupportedChains } diff --git a/x/fungible/keeper/keeper.go b/x/fungible/keeper/keeper.go index b145802315..a05d3c1b16 100644 --- a/x/fungible/keeper/keeper.go +++ b/x/fungible/keeper/keeper.go @@ -14,14 +14,14 @@ import ( type ( Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey - paramstore paramtypes.Subspace - authKeeper types.AccountKeeper - evmKeeper evmkeeper.Keeper - bankKeeper types.BankKeeper - zetaobserverKeeper types.ZetaObserverKeeper + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + paramstore paramtypes.Subspace + authKeeper types.AccountKeeper + evmKeeper evmkeeper.Keeper + bankKeeper types.BankKeeper + observerKeeper types.ObserverKeeper } ) @@ -33,7 +33,7 @@ func NewKeeper( authKeeper types.AccountKeeper, evmKeeper evmkeeper.Keeper, bankKeeper types.BankKeeper, - zetacobservKeeper types.ZetaObserverKeeper, + observerKeeper types.ObserverKeeper, ) *Keeper { // set KeyTable if it has not already been set if !ps.HasKeyTable() { @@ -42,14 +42,14 @@ func NewKeeper( return &Keeper{ - cdc: cdc, - storeKey: storeKey, - memKey: memKey, - paramstore: ps, - authKeeper: authKeeper, - evmKeeper: evmKeeper, - bankKeeper: bankKeeper, - zetaobserverKeeper: zetacobservKeeper, + cdc: cdc, + storeKey: storeKey, + memKey: memKey, + paramstore: ps, + authKeeper: authKeeper, + evmKeeper: evmKeeper, + bankKeeper: bankKeeper, + observerKeeper: observerKeeper, } } diff --git a/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20.go b/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20.go index 36857c8884..6c8dc0290c 100644 --- a/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20.go +++ b/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20.go @@ -32,7 +32,7 @@ import ( // Only the admin policy account is authorized to broadcast this message. func (k msgServer) DeployFungibleCoinZRC20(goCtx context.Context, msg *types.MsgDeployFungibleCoinZRC20) (*types.MsgDeployFungibleCoinZRC20Response, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if msg.Creator != k.zetaobserverKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { + if msg.Creator != k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Deploy can only be executed by the correct policy account") } if msg.Decimals > 255 { diff --git a/x/fungible/keeper/msg_server_remove_foreign_coin.go b/x/fungible/keeper/msg_server_remove_foreign_coin.go index 995bcb6705..0a9dc3b7b6 100644 --- a/x/fungible/keeper/msg_server_remove_foreign_coin.go +++ b/x/fungible/keeper/msg_server_remove_foreign_coin.go @@ -14,7 +14,7 @@ import ( // Only the admin policy account is authorized to broadcast this message. func (k msgServer) RemoveForeignCoin(goCtx context.Context, msg *types.MsgRemoveForeignCoin) (*types.MsgRemoveForeignCoinResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if msg.Creator != k.zetaobserverKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { + if msg.Creator != k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Removal can only be executed by the correct policy account") } index := msg.Name diff --git a/x/fungible/types/expected_keepers.go b/x/fungible/types/expected_keepers.go index a8dbe958ac..4d55b7d6ca 100644 --- a/x/fungible/types/expected_keepers.go +++ b/x/fungible/types/expected_keepers.go @@ -5,7 +5,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/zeta-chain/zetacore/common" - zetaObserverTypes "github.com/zeta-chain/zetacore/x/observer/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" ) // AccountKeeper defines the expected account keeper used for simulations (noalias) @@ -29,13 +29,13 @@ type BankKeeper interface { GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin } -type ZetaObserverKeeper interface { - SetObserverMapper(ctx sdk.Context, om *zetaObserverTypes.ObserverMapper) - GetObserverMapper(ctx sdk.Context, chain *common.Chain) (val zetaObserverTypes.ObserverMapper, found bool) - GetAllObserverMappers(ctx sdk.Context) (mappers []*zetaObserverTypes.ObserverMapper) - SetBallot(ctx sdk.Context, ballot *zetaObserverTypes.Ballot) - GetBallot(ctx sdk.Context, index string) (val zetaObserverTypes.Ballot, found bool) - GetAllBallots(ctx sdk.Context) (voters []*zetaObserverTypes.Ballot) - GetParams(ctx sdk.Context) (params zetaObserverTypes.Params) - GetCoreParamsByChainID(ctx sdk.Context, chainID int64) (params *zetaObserverTypes.CoreParams, found bool) +type ObserverKeeper interface { + SetObserverMapper(ctx sdk.Context, om *observertypes.ObserverMapper) + GetObserverMapper(ctx sdk.Context, chain *common.Chain) (val observertypes.ObserverMapper, found bool) + GetAllObserverMappers(ctx sdk.Context) (mappers []*observertypes.ObserverMapper) + SetBallot(ctx sdk.Context, ballot *observertypes.Ballot) + GetBallot(ctx sdk.Context, index string) (val observertypes.Ballot, found bool) + GetAllBallots(ctx sdk.Context) (voters []*observertypes.Ballot) + GetParams(ctx sdk.Context) (params observertypes.Params) + GetCoreParamsByChainID(ctx sdk.Context, chainID int64) (params *observertypes.CoreParams, found bool) } From 255859ffea076ca466cc579c1de5ac91a5d8906b Mon Sep 17 00:00:00 2001 From: lumtis Date: Fri, 4 Aug 2023 18:38:38 +0200 Subject: [PATCH 2/9] mock generators --- testutil/keeper/mocks.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 testutil/keeper/mocks.go diff --git a/testutil/keeper/mocks.go b/testutil/keeper/mocks.go new file mode 100644 index 0000000000..1fec174ef3 --- /dev/null +++ b/testutil/keeper/mocks.go @@ -0,0 +1,20 @@ +package keeper + +import ( + fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" +) + +//go:generate mockery --name FungibleAccountKeeper --filename account.go --case underscore --output ./mocks/fungible +type FungibleAccountKeeper interface { + fungibletypes.AccountKeeper +} + +//go:generate mockery --name FungibleBankKeeper --filename fungible.go --case underscore --output ./mocks/fungible +type FungibleBankKeeper interface { + fungibletypes.BankKeeper +} + +//go:generate mockery --name FungibleObserverKeeper --filename observer.go --case underscore --output ./mocks/fungible +type FungibleObserverKeeper interface { + fungibletypes.ObserverKeeper +} From 37de315957ad8cbf3880f04c0035d418fd877447 Mon Sep 17 00:00:00 2001 From: lumtis Date: Fri, 4 Aug 2023 18:38:51 +0200 Subject: [PATCH 3/9] generate mocks --- testutil/keeper/mocks/fungible/account.go | 86 +++++++++++ testutil/keeper/mocks/fungible/fungible.go | 171 +++++++++++++++++++++ testutil/keeper/mocks/fungible/observer.go | 162 +++++++++++++++++++ 3 files changed, 419 insertions(+) create mode 100644 testutil/keeper/mocks/fungible/account.go create mode 100644 testutil/keeper/mocks/fungible/fungible.go create mode 100644 testutil/keeper/mocks/fungible/observer.go diff --git a/testutil/keeper/mocks/fungible/account.go b/testutil/keeper/mocks/fungible/account.go new file mode 100644 index 0000000000..da987d7db2 --- /dev/null +++ b/testutil/keeper/mocks/fungible/account.go @@ -0,0 +1,86 @@ +// Code generated by mockery v2.32.3. DO NOT EDIT. + +package mocks + +import ( + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + + mock "github.com/stretchr/testify/mock" + + types "github.com/cosmos/cosmos-sdk/types" +) + +// FungibleAccountKeeper is an autogenerated mock type for the FungibleAccountKeeper type +type FungibleAccountKeeper struct { + mock.Mock +} + +// GetAccount provides a mock function with given fields: ctx, addr +func (_m *FungibleAccountKeeper) GetAccount(ctx types.Context, addr types.AccAddress) authtypes.AccountI { + ret := _m.Called(ctx, addr) + + var r0 authtypes.AccountI + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress) authtypes.AccountI); ok { + r0 = rf(ctx, addr) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(authtypes.AccountI) + } + } + + return r0 +} + +// GetModuleAccount provides a mock function with given fields: ctx, name +func (_m *FungibleAccountKeeper) GetModuleAccount(ctx types.Context, name string) authtypes.ModuleAccountI { + ret := _m.Called(ctx, name) + + var r0 authtypes.ModuleAccountI + if rf, ok := ret.Get(0).(func(types.Context, string) authtypes.ModuleAccountI); ok { + r0 = rf(ctx, name) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(authtypes.ModuleAccountI) + } + } + + return r0 +} + +// GetSequence provides a mock function with given fields: ctx, addr +func (_m *FungibleAccountKeeper) GetSequence(ctx types.Context, addr types.AccAddress) (uint64, error) { + ret := _m.Called(ctx, addr) + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress) (uint64, error)); ok { + return rf(ctx, addr) + } + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress) uint64); ok { + r0 = rf(ctx, addr) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func(types.Context, types.AccAddress) error); ok { + r1 = rf(ctx, addr) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewFungibleAccountKeeper creates a new instance of FungibleAccountKeeper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewFungibleAccountKeeper(t interface { + mock.TestingT + Cleanup(func()) +}) *FungibleAccountKeeper { + mock := &FungibleAccountKeeper{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/testutil/keeper/mocks/fungible/fungible.go b/testutil/keeper/mocks/fungible/fungible.go new file mode 100644 index 0000000000..dbbb384d3e --- /dev/null +++ b/testutil/keeper/mocks/fungible/fungible.go @@ -0,0 +1,171 @@ +// Code generated by mockery v2.32.3. DO NOT EDIT. + +package mocks + +import ( + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + + mock "github.com/stretchr/testify/mock" + + types "github.com/cosmos/cosmos-sdk/types" +) + +// FungibleBankKeeper is an autogenerated mock type for the FungibleBankKeeper type +type FungibleBankKeeper struct { + mock.Mock +} + +// BlockedAddr provides a mock function with given fields: addr +func (_m *FungibleBankKeeper) BlockedAddr(addr types.AccAddress) bool { + ret := _m.Called(addr) + + var r0 bool + if rf, ok := ret.Get(0).(func(types.AccAddress) bool); ok { + r0 = rf(addr) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// BurnCoins provides a mock function with given fields: ctx, moduleName, amt +func (_m *FungibleBankKeeper) BurnCoins(ctx types.Context, moduleName string, amt types.Coins) error { + ret := _m.Called(ctx, moduleName, amt) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, string, types.Coins) error); ok { + r0 = rf(ctx, moduleName, amt) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// GetBalance provides a mock function with given fields: ctx, addr, denom +func (_m *FungibleBankKeeper) GetBalance(ctx types.Context, addr types.AccAddress, denom string) types.Coin { + ret := _m.Called(ctx, addr, denom) + + var r0 types.Coin + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress, string) types.Coin); ok { + r0 = rf(ctx, addr, denom) + } else { + r0 = ret.Get(0).(types.Coin) + } + + return r0 +} + +// GetDenomMetaData provides a mock function with given fields: ctx, denom +func (_m *FungibleBankKeeper) GetDenomMetaData(ctx types.Context, denom string) (banktypes.Metadata, bool) { + ret := _m.Called(ctx, denom) + + var r0 banktypes.Metadata + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, string) (banktypes.Metadata, bool)); ok { + return rf(ctx, denom) + } + if rf, ok := ret.Get(0).(func(types.Context, string) banktypes.Metadata); ok { + r0 = rf(ctx, denom) + } else { + r0 = ret.Get(0).(banktypes.Metadata) + } + + if rf, ok := ret.Get(1).(func(types.Context, string) bool); ok { + r1 = rf(ctx, denom) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// HasSupply provides a mock function with given fields: ctx, denom +func (_m *FungibleBankKeeper) HasSupply(ctx types.Context, denom string) bool { + ret := _m.Called(ctx, denom) + + var r0 bool + if rf, ok := ret.Get(0).(func(types.Context, string) bool); ok { + r0 = rf(ctx, denom) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsSendEnabledCoin provides a mock function with given fields: ctx, coin +func (_m *FungibleBankKeeper) IsSendEnabledCoin(ctx types.Context, coin types.Coin) bool { + ret := _m.Called(ctx, coin) + + var r0 bool + if rf, ok := ret.Get(0).(func(types.Context, types.Coin) bool); ok { + r0 = rf(ctx, coin) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// MintCoins provides a mock function with given fields: ctx, moduleName, amt +func (_m *FungibleBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { + ret := _m.Called(ctx, moduleName, amt) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, string, types.Coins) error); ok { + r0 = rf(ctx, moduleName, amt) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SendCoinsFromAccountToModule provides a mock function with given fields: ctx, senderAddr, recipientModule, amt +func (_m *FungibleBankKeeper) SendCoinsFromAccountToModule(ctx types.Context, senderAddr types.AccAddress, recipientModule string, amt types.Coins) error { + ret := _m.Called(ctx, senderAddr, recipientModule, amt) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress, string, types.Coins) error); ok { + r0 = rf(ctx, senderAddr, recipientModule, amt) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SendCoinsFromModuleToAccount provides a mock function with given fields: ctx, senderModule, recipientAddr, amt +func (_m *FungibleBankKeeper) SendCoinsFromModuleToAccount(ctx types.Context, senderModule string, recipientAddr types.AccAddress, amt types.Coins) error { + ret := _m.Called(ctx, senderModule, recipientAddr, amt) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, string, types.AccAddress, types.Coins) error); ok { + r0 = rf(ctx, senderModule, recipientAddr, amt) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SetDenomMetaData provides a mock function with given fields: ctx, denomMetaData +func (_m *FungibleBankKeeper) SetDenomMetaData(ctx types.Context, denomMetaData banktypes.Metadata) { + _m.Called(ctx, denomMetaData) +} + +// NewFungibleBankKeeper creates a new instance of FungibleBankKeeper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewFungibleBankKeeper(t interface { + mock.TestingT + Cleanup(func()) +}) *FungibleBankKeeper { + mock := &FungibleBankKeeper{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/testutil/keeper/mocks/fungible/observer.go b/testutil/keeper/mocks/fungible/observer.go new file mode 100644 index 0000000000..7d8248a6ae --- /dev/null +++ b/testutil/keeper/mocks/fungible/observer.go @@ -0,0 +1,162 @@ +// Code generated by mockery v2.32.3. DO NOT EDIT. + +package mocks + +import ( + common "github.com/zeta-chain/zetacore/common" + + mock "github.com/stretchr/testify/mock" + + observertypes "github.com/zeta-chain/zetacore/x/observer/types" + + types "github.com/cosmos/cosmos-sdk/types" +) + +// FungibleObserverKeeper is an autogenerated mock type for the FungibleObserverKeeper type +type FungibleObserverKeeper struct { + mock.Mock +} + +// GetAllBallots provides a mock function with given fields: ctx +func (_m *FungibleObserverKeeper) GetAllBallots(ctx types.Context) []*observertypes.Ballot { + ret := _m.Called(ctx) + + var r0 []*observertypes.Ballot + if rf, ok := ret.Get(0).(func(types.Context) []*observertypes.Ballot); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*observertypes.Ballot) + } + } + + return r0 +} + +// GetAllObserverMappers provides a mock function with given fields: ctx +func (_m *FungibleObserverKeeper) GetAllObserverMappers(ctx types.Context) []*observertypes.ObserverMapper { + ret := _m.Called(ctx) + + var r0 []*observertypes.ObserverMapper + if rf, ok := ret.Get(0).(func(types.Context) []*observertypes.ObserverMapper); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*observertypes.ObserverMapper) + } + } + + return r0 +} + +// GetBallot provides a mock function with given fields: ctx, index +func (_m *FungibleObserverKeeper) GetBallot(ctx types.Context, index string) (observertypes.Ballot, bool) { + ret := _m.Called(ctx, index) + + var r0 observertypes.Ballot + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, string) (observertypes.Ballot, bool)); ok { + return rf(ctx, index) + } + if rf, ok := ret.Get(0).(func(types.Context, string) observertypes.Ballot); ok { + r0 = rf(ctx, index) + } else { + r0 = ret.Get(0).(observertypes.Ballot) + } + + if rf, ok := ret.Get(1).(func(types.Context, string) bool); ok { + r1 = rf(ctx, index) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// GetCoreParamsByChainID provides a mock function with given fields: ctx, chainID +func (_m *FungibleObserverKeeper) GetCoreParamsByChainID(ctx types.Context, chainID int64) (*observertypes.CoreParams, bool) { + ret := _m.Called(ctx, chainID) + + var r0 *observertypes.CoreParams + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, int64) (*observertypes.CoreParams, bool)); ok { + return rf(ctx, chainID) + } + if rf, ok := ret.Get(0).(func(types.Context, int64) *observertypes.CoreParams); ok { + r0 = rf(ctx, chainID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*observertypes.CoreParams) + } + } + + if rf, ok := ret.Get(1).(func(types.Context, int64) bool); ok { + r1 = rf(ctx, chainID) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// GetObserverMapper provides a mock function with given fields: ctx, chain +func (_m *FungibleObserverKeeper) GetObserverMapper(ctx types.Context, chain *common.Chain) (observertypes.ObserverMapper, bool) { + ret := _m.Called(ctx, chain) + + var r0 observertypes.ObserverMapper + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, *common.Chain) (observertypes.ObserverMapper, bool)); ok { + return rf(ctx, chain) + } + if rf, ok := ret.Get(0).(func(types.Context, *common.Chain) observertypes.ObserverMapper); ok { + r0 = rf(ctx, chain) + } else { + r0 = ret.Get(0).(observertypes.ObserverMapper) + } + + if rf, ok := ret.Get(1).(func(types.Context, *common.Chain) bool); ok { + r1 = rf(ctx, chain) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// GetParams provides a mock function with given fields: ctx +func (_m *FungibleObserverKeeper) GetParams(ctx types.Context) observertypes.Params { + ret := _m.Called(ctx) + + var r0 observertypes.Params + if rf, ok := ret.Get(0).(func(types.Context) observertypes.Params); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(observertypes.Params) + } + + return r0 +} + +// SetBallot provides a mock function with given fields: ctx, ballot +func (_m *FungibleObserverKeeper) SetBallot(ctx types.Context, ballot *observertypes.Ballot) { + _m.Called(ctx, ballot) +} + +// SetObserverMapper provides a mock function with given fields: ctx, om +func (_m *FungibleObserverKeeper) SetObserverMapper(ctx types.Context, om *observertypes.ObserverMapper) { + _m.Called(ctx, om) +} + +// NewFungibleObserverKeeper creates a new instance of FungibleObserverKeeper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewFungibleObserverKeeper(t interface { + mock.TestingT + Cleanup(func()) +}) *FungibleObserverKeeper { + mock := &FungibleObserverKeeper{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} From 9b45e7028ab270c08a2660e02bc97adb75b62f06 Mon Sep 17 00:00:00 2001 From: lumtis Date: Fri, 4 Aug 2023 18:39:10 +0200 Subject: [PATCH 4/9] refactor test keeper with mock generator --- testutil/keeper/fungible.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/testutil/keeper/fungible.go b/testutil/keeper/fungible.go index 58669f0433..cbb4ecf9d1 100644 --- a/testutil/keeper/fungible.go +++ b/testutil/keeper/fungible.go @@ -1,23 +1,22 @@ package keeper import ( + "testing" + "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/store" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - authkeeper2 "github.com/cosmos/cosmos-sdk/x/auth/keeper" - bankkeeper2 "github.com/cosmos/cosmos-sdk/x/bank/keeper" typesparams "github.com/cosmos/cosmos-sdk/x/params/types" evmkeeper "github.com/evmos/ethermint/x/evm/keeper" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmdb "github.com/tendermint/tm-db" + fungiblemocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/fungible" "github.com/zeta-chain/zetacore/x/fungible/keeper" "github.com/zeta-chain/zetacore/x/fungible/types" - zetaObserverModuleKeeper "github.com/zeta-chain/zetacore/x/observer/keeper" - "testing" ) func FungibleKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { @@ -40,21 +39,25 @@ func FungibleKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { "FungibleParams", ) - bankkeeper := bankkeeper2.BaseKeeper{} - authkeeper := authkeeper2.AccountKeeper{} + // Initialize mocks from expected keeper interfaces + bankKeeper := fungiblemocks.NewFungibleBankKeeper(t) + authKeeper := fungiblemocks.NewFungibleAccountKeeper(t) + observerKeeper := fungiblemocks.NewFungibleObserverKeeper(t) + + // TODO: Use mock once interface is implemented evmKeeper := evmkeeper.Keeper{} - zetaObserverKeeper := zetaObserverModuleKeeper.Keeper{} - keeper := keeper.NewKeeper( + + k := keeper.NewKeeper( codec.NewProtoCodec(registry), storeKey, memStoreKey, paramsSubspace, - authkeeper, + authKeeper, evmKeeper, - bankkeeper, - zetaObserverKeeper, + bankKeeper, + observerKeeper, ) ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) - return keeper, ctx + return k, ctx } From 840b7a5af10bd6f7a4c95aaec4f8a84fdeb8cf80 Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 8 Aug 2023 11:12:47 +0200 Subject: [PATCH 5/9] add EVM keeper --- testutil/keeper/fungible.go | 7 +- testutil/keeper/mocks.go | 5 + testutil/keeper/mocks/fungible/evm.go | 150 ++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 testutil/keeper/mocks/fungible/evm.go diff --git a/testutil/keeper/fungible.go b/testutil/keeper/fungible.go index acb89661e3..ae9fd02b40 100644 --- a/testutil/keeper/fungible.go +++ b/testutil/keeper/fungible.go @@ -9,7 +9,6 @@ import ( storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" typesparams "github.com/cosmos/cosmos-sdk/x/params/types" - evmkeeper "github.com/evmos/ethermint/x/evm/keeper" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -43,9 +42,7 @@ func FungibleKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { bankKeeper := fungiblemocks.NewFungibleBankKeeper(t) authKeeper := fungiblemocks.NewFungibleAccountKeeper(t) observerKeeper := fungiblemocks.NewFungibleObserverKeeper(t) - - // TODO: Use mock once interface is implemented - evmKeeper := evmkeeper.Keeper{} + evmKeeper := fungiblemocks.NewFungibleEVMKeeper(t) k := keeper.NewKeeper( codec.NewProtoCodec(registry), @@ -53,7 +50,7 @@ func FungibleKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { memStoreKey, paramsSubspace, authKeeper, - &evmKeeper, + evmKeeper, bankKeeper, observerKeeper, ) diff --git a/testutil/keeper/mocks.go b/testutil/keeper/mocks.go index 1fec174ef3..96bdaa71dd 100644 --- a/testutil/keeper/mocks.go +++ b/testutil/keeper/mocks.go @@ -18,3 +18,8 @@ type FungibleBankKeeper interface { type FungibleObserverKeeper interface { fungibletypes.ObserverKeeper } + +//go:generate mockery --name FungibleEVMKeeper --filename evm.go --case underscore --output ./mocks/fungible +type FungibleEVMKeeper interface { + fungibletypes.EVMKeeper +} diff --git a/testutil/keeper/mocks/fungible/evm.go b/testutil/keeper/mocks/fungible/evm.go new file mode 100644 index 0000000000..e05034a958 --- /dev/null +++ b/testutil/keeper/mocks/fungible/evm.go @@ -0,0 +1,150 @@ +// Code generated by mockery v2.32.3. DO NOT EDIT. + +package mocks + +import ( + context "context" + big "math/big" + + core "github.com/ethereum/go-ethereum/core" + + evmtypes "github.com/evmos/ethermint/x/evm/types" + + mock "github.com/stretchr/testify/mock" + + types "github.com/cosmos/cosmos-sdk/types" + + vm "github.com/ethereum/go-ethereum/core/vm" +) + +// FungibleEVMKeeper is an autogenerated mock type for the FungibleEVMKeeper type +type FungibleEVMKeeper struct { + mock.Mock +} + +// ApplyMessage provides a mock function with given fields: ctx, msg, tracer, commit +func (_m *FungibleEVMKeeper) ApplyMessage(ctx types.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*evmtypes.MsgEthereumTxResponse, error) { + ret := _m.Called(ctx, msg, tracer, commit) + + var r0 *evmtypes.MsgEthereumTxResponse + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, core.Message, vm.EVMLogger, bool) (*evmtypes.MsgEthereumTxResponse, error)); ok { + return rf(ctx, msg, tracer, commit) + } + if rf, ok := ret.Get(0).(func(types.Context, core.Message, vm.EVMLogger, bool) *evmtypes.MsgEthereumTxResponse); ok { + r0 = rf(ctx, msg, tracer, commit) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*evmtypes.MsgEthereumTxResponse) + } + } + + if rf, ok := ret.Get(1).(func(types.Context, core.Message, vm.EVMLogger, bool) error); ok { + r1 = rf(ctx, msg, tracer, commit) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// ChainID provides a mock function with given fields: +func (_m *FungibleEVMKeeper) ChainID() *big.Int { + ret := _m.Called() + + var r0 *big.Int + if rf, ok := ret.Get(0).(func() *big.Int); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + return r0 +} + +// EstimateGas provides a mock function with given fields: c, req +func (_m *FungibleEVMKeeper) EstimateGas(c context.Context, req *evmtypes.EthCallRequest) (*evmtypes.EstimateGasResponse, error) { + ret := _m.Called(c, req) + + var r0 *evmtypes.EstimateGasResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *evmtypes.EthCallRequest) (*evmtypes.EstimateGasResponse, error)); ok { + return rf(c, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *evmtypes.EthCallRequest) *evmtypes.EstimateGasResponse); ok { + r0 = rf(c, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*evmtypes.EstimateGasResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *evmtypes.EthCallRequest) error); ok { + r1 = rf(c, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetBlockBloomTransient provides a mock function with given fields: ctx +func (_m *FungibleEVMKeeper) GetBlockBloomTransient(ctx types.Context) *big.Int { + ret := _m.Called(ctx) + + var r0 *big.Int + if rf, ok := ret.Get(0).(func(types.Context) *big.Int); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + return r0 +} + +// GetLogSizeTransient provides a mock function with given fields: ctx +func (_m *FungibleEVMKeeper) GetLogSizeTransient(ctx types.Context) uint64 { + ret := _m.Called(ctx) + + var r0 uint64 + if rf, ok := ret.Get(0).(func(types.Context) uint64); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// SetBlockBloomTransient provides a mock function with given fields: ctx, bloom +func (_m *FungibleEVMKeeper) SetBlockBloomTransient(ctx types.Context, bloom *big.Int) { + _m.Called(ctx, bloom) +} + +// SetLogSizeTransient provides a mock function with given fields: ctx, logSize +func (_m *FungibleEVMKeeper) SetLogSizeTransient(ctx types.Context, logSize uint64) { + _m.Called(ctx, logSize) +} + +// WithChainID provides a mock function with given fields: ctx +func (_m *FungibleEVMKeeper) WithChainID(ctx types.Context) { + _m.Called(ctx) +} + +// NewFungibleEVMKeeper creates a new instance of FungibleEVMKeeper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewFungibleEVMKeeper(t interface { + mock.TestingT + Cleanup(func()) +}) *FungibleEVMKeeper { + mock := &FungibleEVMKeeper{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} From f88bba116d584b56e96d35f4cae154cfb3564611 Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 8 Aug 2023 11:13:23 +0200 Subject: [PATCH 6/9] improve imports --- testutil/keeper/fungible.go | 1 + 1 file changed, 1 insertion(+) diff --git a/testutil/keeper/fungible.go b/testutil/keeper/fungible.go index ae9fd02b40..d7aae64ac2 100644 --- a/testutil/keeper/fungible.go +++ b/testutil/keeper/fungible.go @@ -13,6 +13,7 @@ import ( "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmdb "github.com/tendermint/tm-db" + fungiblemocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/fungible" "github.com/zeta-chain/zetacore/x/fungible/keeper" "github.com/zeta-chain/zetacore/x/fungible/types" From 15d76abb74a249a0a228a8d3e8b58ee474cc309b Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 8 Aug 2023 11:34:49 +0200 Subject: [PATCH 7/9] goimports --- testutil/keeper/fungible.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testutil/keeper/fungible.go b/testutil/keeper/fungible.go index d7aae64ac2..cc7c4d4f4c 100644 --- a/testutil/keeper/fungible.go +++ b/testutil/keeper/fungible.go @@ -13,7 +13,7 @@ import ( "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmdb "github.com/tendermint/tm-db" - + fungiblemocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/fungible" "github.com/zeta-chain/zetacore/x/fungible/keeper" "github.com/zeta-chain/zetacore/x/fungible/types" From 046e36db0103ab41995184109f4834e652400e99 Mon Sep 17 00:00:00 2001 From: lumtis Date: Thu, 10 Aug 2023 21:18:43 +0200 Subject: [PATCH 8/9] fix conflict --- x/fungible/keeper/msg_server_update_system_contract.go | 2 +- x/fungible/keeper/msg_server_update_zrc20_withdraw_fee.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x/fungible/keeper/msg_server_update_system_contract.go b/x/fungible/keeper/msg_server_update_system_contract.go index a586819d2e..d88a52c554 100644 --- a/x/fungible/keeper/msg_server_update_system_contract.go +++ b/x/fungible/keeper/msg_server_update_system_contract.go @@ -16,7 +16,7 @@ import ( func (k Keeper) UpdateSystemContract(goCtx context.Context, msg *types.MsgUpdateSystemContract) (*types.MsgUpdateSystemContractResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if msg.Creator != k.zetaobserverKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { + if msg.Creator != k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Deploy can only be executed by the correct policy account") } newSystemContractAddr := ethcommon.HexToAddress(msg.NewSystemContractAddress) diff --git a/x/fungible/keeper/msg_server_update_zrc20_withdraw_fee.go b/x/fungible/keeper/msg_server_update_zrc20_withdraw_fee.go index 0d85cb917a..8bb7fa4181 100644 --- a/x/fungible/keeper/msg_server_update_zrc20_withdraw_fee.go +++ b/x/fungible/keeper/msg_server_update_zrc20_withdraw_fee.go @@ -14,7 +14,7 @@ import ( func (k Keeper) UpdateZRC20WithdrawFee(goCtx context.Context, msg *types.MsgUpdateZRC20WithdrawFee) (*types.MsgUpdateZRC20WithdrawFeeResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if msg.Creator != k.zetaobserverKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { + if msg.Creator != k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_deploy_fungible_coin) { return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Deploy can only be executed by the correct policy account") } zrc20Addr := ethcommon.HexToAddress(msg.Zrc20Address) From 0752e8fee636a05efddbd6bf76b0b83a2a1e93dc Mon Sep 17 00:00:00 2001 From: lumtis Date: Thu, 10 Aug 2023 21:24:34 +0200 Subject: [PATCH 9/9] fix conflicts 2 --- .../keeper/begin_blocker_deploy_system_contracts_privnet.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/fungible/keeper/begin_blocker_deploy_system_contracts_privnet.go b/x/fungible/keeper/begin_blocker_deploy_system_contracts_privnet.go index 361ea09926..f11aaddf89 100644 --- a/x/fungible/keeper/begin_blocker_deploy_system_contracts_privnet.go +++ b/x/fungible/keeper/begin_blocker_deploy_system_contracts_privnet.go @@ -130,7 +130,7 @@ func (k Keeper) TestUpdateSystemContractAddress(goCtx context.Context) error { if err != nil { return sdkerrors.Wrapf(err, "failed to DeploySystemContract") } - creator := k.zetaobserverKeeper.GetParams(ctx).GetAdminPolicyAccount(observertypes.Policy_Type_deploy_fungible_coin) + creator := k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(observertypes.Policy_Type_deploy_fungible_coin) msg := types.NewMessageUpdateSystemContract(creator, SystemContractAddress.Hex()) _, err = k.UpdateSystemContract(ctx, msg) return err @@ -140,7 +140,7 @@ func (k Keeper) TestUpdateZRC20WithdrawFee(goCtx context.Context) error { ctx := sdk.UnwrapSDKContext(goCtx) foreignCoins := k.GetAllForeignCoins(ctx) - creator := k.zetaobserverKeeper.GetParams(ctx).GetAdminPolicyAccount(observertypes.Policy_Type_deploy_fungible_coin) + creator := k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(observertypes.Policy_Type_deploy_fungible_coin) for _, foreignCoin := range foreignCoins { msg := types.NewMsgUpdateZRC20WithdrawFee(creator, foreignCoin.Zrc20ContractAddress, sdk.NewUint(uint64(foreignCoin.ForeignChainId)))