diff --git a/testutil/keeper/crosschain.go b/testutil/keeper/crosschain.go index 69f2ae68f4..45a68a5627 100644 --- a/testutil/keeper/crosschain.go +++ b/testutil/keeper/crosschain.go @@ -32,7 +32,7 @@ var ( CrosschainNoMocks = CrosschainMockOptions{} ) -// CrosschainKeeper initializes a crosschain keeper for testing purposes with option to mock specific keepers +// CrosschainKeeperWithMocks initializes a crosschain keeper for testing purposes with option to mock specific keepers func CrosschainKeeperWithMocks( t testing.TB, mockOptions CrosschainMockOptions, diff --git a/testutil/keeper/mocks/crosschain/account.go b/testutil/keeper/mocks/crosschain/account.go index 0560f21be8..99b7f1cf5b 100644 --- a/testutil/keeper/mocks/crosschain/account.go +++ b/testutil/keeper/mocks/crosschain/account.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/crosschain/bank.go b/testutil/keeper/mocks/crosschain/bank.go index 9ac2b1278b..267f2b45b4 100644 --- a/testutil/keeper/mocks/crosschain/bank.go +++ b/testutil/keeper/mocks/crosschain/bank.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/crosschain/fungible.go b/testutil/keeper/mocks/crosschain/fungible.go index 3cd55097ed..a720711b79 100644 --- a/testutil/keeper/mocks/crosschain/fungible.go +++ b/testutil/keeper/mocks/crosschain/fungible.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/crosschain/observer.go b/testutil/keeper/mocks/crosschain/observer.go index a299839f02..5953a5de34 100644 --- a/testutil/keeper/mocks/crosschain/observer.go +++ b/testutil/keeper/mocks/crosschain/observer.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/crosschain/staking.go b/testutil/keeper/mocks/crosschain/staking.go index 1fb8a14061..64bbe0fed6 100644 --- a/testutil/keeper/mocks/crosschain/staking.go +++ b/testutil/keeper/mocks/crosschain/staking.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/fungible/account.go b/testutil/keeper/mocks/fungible/account.go index f7a2788969..94b7a84d75 100644 --- a/testutil/keeper/mocks/fungible/account.go +++ b/testutil/keeper/mocks/fungible/account.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/fungible/bank.go b/testutil/keeper/mocks/fungible/bank.go index 599ba3453f..20a2590911 100644 --- a/testutil/keeper/mocks/fungible/bank.go +++ b/testutil/keeper/mocks/fungible/bank.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/fungible/evm.go b/testutil/keeper/mocks/fungible/evm.go index b59f7d477e..f0dcd01094 100644 --- a/testutil/keeper/mocks/fungible/evm.go +++ b/testutil/keeper/mocks/fungible/evm.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/fungible/observer.go b/testutil/keeper/mocks/fungible/observer.go index b5736cd960..3a4685f3b2 100644 --- a/testutil/keeper/mocks/fungible/observer.go +++ b/testutil/keeper/mocks/fungible/observer.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.41.0. DO NOT EDIT. +// Code generated by mockery v2.39.1. DO NOT EDIT. package mocks diff --git a/testutil/keeper/mocks/mocks.go b/testutil/keeper/mocks/mocks.go index dbbdeccd76..a0349f7d3d 100644 --- a/testutil/keeper/mocks/mocks.go +++ b/testutil/keeper/mocks/mocks.go @@ -3,6 +3,7 @@ package mocks import ( crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" ) /** @@ -57,3 +58,17 @@ type FungibleObserverKeeper interface { type FungibleEVMKeeper interface { fungibletypes.EVMKeeper } + +/** + * Observer Mocks + */ + +//go:generate mockery --name ObserverStakingKeeper --filename staking.go --case underscore --output ./observer +type ObserverStakingKeeper interface { + observertypes.StakingKeeper +} + +//go:generate mockery --name ObserverSlashingKeeper --filename slashing.go --case underscore --output ./observer +type ObserverSlashingKeeper interface { + observertypes.SlashingKeeper +} diff --git a/testutil/keeper/mocks/observer/slashing.go b/testutil/keeper/mocks/observer/slashing.go new file mode 100644 index 0000000000..d3f64ff726 --- /dev/null +++ b/testutil/keeper/mocks/observer/slashing.go @@ -0,0 +1,53 @@ +// Code generated by mockery v2.39.1. DO NOT EDIT. + +package mocks + +import ( + mock "github.com/stretchr/testify/mock" + + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" + + types "github.com/cosmos/cosmos-sdk/types" +) + +// ObserverSlashingKeeper is an autogenerated mock type for the ObserverSlashingKeeper type +type ObserverSlashingKeeper struct { + mock.Mock +} + +// IsTombstoned provides a mock function with given fields: ctx, addr +func (_m *ObserverSlashingKeeper) IsTombstoned(ctx types.Context, addr types.ConsAddress) bool { + ret := _m.Called(ctx, addr) + + if len(ret) == 0 { + panic("no return value specified for IsTombstoned") + } + + var r0 bool + if rf, ok := ret.Get(0).(func(types.Context, types.ConsAddress) bool); ok { + r0 = rf(ctx, addr) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// SetValidatorSigningInfo provides a mock function with given fields: ctx, address, info +func (_m *ObserverSlashingKeeper) SetValidatorSigningInfo(ctx types.Context, address types.ConsAddress, info slashingtypes.ValidatorSigningInfo) { + _m.Called(ctx, address, info) +} + +// NewObserverSlashingKeeper creates a new instance of ObserverSlashingKeeper. 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 NewObserverSlashingKeeper(t interface { + mock.TestingT + Cleanup(func()) +}) *ObserverSlashingKeeper { + mock := &ObserverSlashingKeeper{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/testutil/keeper/mocks/observer/staking.go b/testutil/keeper/mocks/observer/staking.go new file mode 100644 index 0000000000..72fe2be116 --- /dev/null +++ b/testutil/keeper/mocks/observer/staking.go @@ -0,0 +1,91 @@ +// Code generated by mockery v2.39.1. DO NOT EDIT. + +package mocks + +import ( + mock "github.com/stretchr/testify/mock" + + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + types "github.com/cosmos/cosmos-sdk/types" +) + +// ObserverStakingKeeper is an autogenerated mock type for the ObserverStakingKeeper type +type ObserverStakingKeeper struct { + mock.Mock +} + +// GetDelegation provides a mock function with given fields: ctx, delAddr, valAddr +func (_m *ObserverStakingKeeper) GetDelegation(ctx types.Context, delAddr types.AccAddress, valAddr types.ValAddress) (stakingtypes.Delegation, bool) { + ret := _m.Called(ctx, delAddr, valAddr) + + if len(ret) == 0 { + panic("no return value specified for GetDelegation") + } + + var r0 stakingtypes.Delegation + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress, types.ValAddress) (stakingtypes.Delegation, bool)); ok { + return rf(ctx, delAddr, valAddr) + } + if rf, ok := ret.Get(0).(func(types.Context, types.AccAddress, types.ValAddress) stakingtypes.Delegation); ok { + r0 = rf(ctx, delAddr, valAddr) + } else { + r0 = ret.Get(0).(stakingtypes.Delegation) + } + + if rf, ok := ret.Get(1).(func(types.Context, types.AccAddress, types.ValAddress) bool); ok { + r1 = rf(ctx, delAddr, valAddr) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// GetValidator provides a mock function with given fields: ctx, addr +func (_m *ObserverStakingKeeper) GetValidator(ctx types.Context, addr types.ValAddress) (stakingtypes.Validator, bool) { + ret := _m.Called(ctx, addr) + + if len(ret) == 0 { + panic("no return value specified for GetValidator") + } + + var r0 stakingtypes.Validator + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, types.ValAddress) (stakingtypes.Validator, bool)); ok { + return rf(ctx, addr) + } + if rf, ok := ret.Get(0).(func(types.Context, types.ValAddress) stakingtypes.Validator); ok { + r0 = rf(ctx, addr) + } else { + r0 = ret.Get(0).(stakingtypes.Validator) + } + + if rf, ok := ret.Get(1).(func(types.Context, types.ValAddress) bool); ok { + r1 = rf(ctx, addr) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// SetValidator provides a mock function with given fields: ctx, validator +func (_m *ObserverStakingKeeper) SetValidator(ctx types.Context, validator stakingtypes.Validator) { + _m.Called(ctx, validator) +} + +// NewObserverStakingKeeper creates a new instance of ObserverStakingKeeper. 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 NewObserverStakingKeeper(t interface { + mock.TestingT + Cleanup(func()) +}) *ObserverStakingKeeper { + mock := &ObserverStakingKeeper{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/testutil/keeper/observer.go b/testutil/keeper/observer.go index 40f46bee8d..1a6dd2e257 100644 --- a/testutil/keeper/observer.go +++ b/testutil/keeper/observer.go @@ -3,20 +3,36 @@ package keeper import ( "testing" - slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" + slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" tmdb "github.com/tendermint/tm-db" + observermocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/observer" "github.com/zeta-chain/zetacore/x/observer/keeper" "github.com/zeta-chain/zetacore/x/observer/types" ) +// ObserverMockOptions represents options for instantiating an observer keeper with mocks +type ObserverMockOptions struct { + UseStakingMock bool + UseSlashingMock bool +} + +var ( + ObserverMocksAll = ObserverMockOptions{ + UseStakingMock: true, + UseSlashingMock: true, + } + ObserverNoMocks = ObserverMockOptions{} +) + func initObserverKeeper( cdc codec.Codec, db *tmdb.MemDB, @@ -40,8 +56,8 @@ func initObserverKeeper( ) } -// ObserverKeeper instantiates an observer keeper for testing purposes -func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers) { +// ObserverKeeperWithMocks instantiates an observer keeper for testing purposes with the option to mock specific keepers +func ObserverKeeperWithMocks(t testing.TB, mockOptions ObserverMockOptions) (*keeper.Keeper, sdk.Context, SDKKeepers) { storeKey := sdk.NewKVStoreKey(types.StoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) @@ -66,16 +82,67 @@ func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers) { // Add a proposer to the context ctx = sdkKeepers.InitBlockProposer(t, ctx) + // Initialize mocks for mocked keepers + var stakingKeeper types.StakingKeeper = sdkKeepers.StakingKeeper + var slashingKeeper types.SlashingKeeper = sdkKeepers.SlashingKeeper + if mockOptions.UseStakingMock { + stakingKeeper = observermocks.NewObserverStakingKeeper(t) + } + if mockOptions.UseSlashingMock { + slashingKeeper = observermocks.NewObserverSlashingKeeper(t) + } + k := keeper.NewKeeper( cdc, storeKey, memStoreKey, sdkKeepers.ParamsKeeper.Subspace(types.ModuleName), - sdkKeepers.StakingKeeper, - sdkKeepers.SlashingKeeper, + stakingKeeper, + slashingKeeper, ) k.SetParams(ctx, types.DefaultParams()) return k, ctx, sdkKeepers } + +// ObserverKeeper instantiates an observer keeper for testing purposes +func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers) { + return ObserverKeeperWithMocks(t, ObserverNoMocks) +} + +// GetObserverStakingMock returns a new observer staking keeper mock +func GetObserverStakingMock(t testing.TB, keeper *keeper.Keeper) *ObserverMockStakingKeeper { + k, ok := keeper.GetStakingKeeper().(*observermocks.ObserverStakingKeeper) + require.True(t, ok) + return &ObserverMockStakingKeeper{ + ObserverStakingKeeper: k, + } +} + +// GetObserverSlashingMock returns a new observer slashing keeper mock +func GetObserverSlashingMock(t testing.TB, keeper *keeper.Keeper) *ObserverMockSlashingKeeper { + k, ok := keeper.GetSlashingKeeper().(*observermocks.ObserverSlashingKeeper) + require.True(t, ok) + return &ObserverMockSlashingKeeper{ + ObserverSlashingKeeper: k, + } +} + +// ObserverMockStakingKeeper is a wrapper of the observer staking keeper mock that add methods to mock the GetValidator method +type ObserverMockStakingKeeper struct { + *observermocks.ObserverStakingKeeper +} + +func (m *ObserverMockStakingKeeper) MockGetValidator(validator stakingtypes.Validator) { + m.On("GetValidator", mock.Anything, mock.Anything).Return(validator, true) +} + +// ObserverMockSlashingKeeper is a wrapper of the observer slashing keeper mock that add methods to mock the IsTombstoned method +type ObserverMockSlashingKeeper struct { + *observermocks.ObserverSlashingKeeper +} + +func (m *ObserverMockSlashingKeeper) MockIsTombstoned(isTombstoned bool) { + m.On("IsTombstoned", mock.Anything, mock.Anything).Return(isTombstoned) +}