From bd5ffc83c919301f74be4913ea670272b2e56bee Mon Sep 17 00:00:00 2001 From: leonz789 Date: Sat, 13 Apr 2024 15:41:14 +0800 Subject: [PATCH] refactor(oracle): use internal interface for external keeper reference --- x/oracle/keeper/common/expected_keepers.go | 21 ++++++++----- x/oracle/keeper/keeper.go | 36 +++++----------------- x/oracle/keeper/single.go | 4 +-- x/oracle/module.go | 8 +++-- 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/x/oracle/keeper/common/expected_keepers.go b/x/oracle/keeper/common/expected_keepers.go index 0eec0d1ff..1c3afcbef 100644 --- a/x/oracle/keeper/common/expected_keepers.go +++ b/x/oracle/keeper/common/expected_keepers.go @@ -1,22 +1,18 @@ package common import ( - "math/big" - - // "cosmossdk.io/api/tendermint/abci" + "cosmossdk.io/math" "github.com/ExocoreNetwork/exocore/x/oracle/types" abci "github.com/cometbft/cometbft/abci/types" sdk "github.com/cosmos/cosmos-sdk/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) type KeeperOracle interface { - GetParams(sdk.Context) types.Params + KeeperStaking - IterateBondedValidatorsByPower(sdk.Context, func(index int64, validator stakingTypes.ValidatorI) bool) - GetLastTotalPower(sdk.Context) *big.Int - GetValidatorUpdates(sdk.Context) []abci.ValidatorUpdate - GetValidatorByConsAddr(sdk.Context, sdk.ConsAddress) (stakingTypes.Validator, bool) + GetParams(sdk.Context) types.Params GetIndexRecentMsg(sdk.Context) (types.IndexRecentMsg, bool) GetAllRecentMsgAsMap(sdk.Context) map[uint64][]*types.MsgItem @@ -37,3 +33,12 @@ type KeeperOracle interface { RemoveRecentParams(sdk.Context, uint64) RemoveRecentMsg(sdk.Context, uint64) } + +var _ KeeperStaking = stakingkeeper.Keeper{} + +type KeeperStaking interface { + GetLastTotalPower(ctx sdk.Context) math.Int + IterateBondedValidatorsByPower(ctx sdk.Context, fn func(index int64, validator stakingTypes.ValidatorI) (stop bool)) + GetValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate + GetValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) (validator stakingTypes.Validator, found bool) +} diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index a9fc27939..1c4148f81 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -2,10 +2,7 @@ package keeper import ( "fmt" - "math/big" - // "cosmossdk.io/api/tendermint/abci" - abci "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -14,18 +11,15 @@ import ( "github.com/ExocoreNetwork/exocore/x/oracle/keeper/common" "github.com/ExocoreNetwork/exocore/x/oracle/types" - - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) type ( Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey - paramstore paramtypes.Subspace - stakingKeeper stakingkeeper.Keeper + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + paramstore paramtypes.Subspace + common.KeeperStaking } ) @@ -36,7 +30,7 @@ func NewKeeper( storeKey storetypes.StoreKey, memKey storetypes.StoreKey, ps paramtypes.Subspace, - sKeeper stakingkeeper.Keeper, + sKeeper common.KeeperStaking, ) Keeper { // set KeyTable if it has not already been set if !ps.HasKeyTable() { @@ -48,26 +42,10 @@ func NewKeeper( storeKey: storeKey, memKey: memKey, paramstore: ps, - stakingKeeper: sKeeper, + KeeperStaking: sKeeper, } } func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } - -func (k Keeper) GetLastTotalPower(ctx sdk.Context) *big.Int { - return k.stakingKeeper.GetLastTotalPower(ctx).BigInt() -} - -func (k Keeper) IterateBondedValidatorsByPower(ctx sdk.Context, f func(index int64, validator stakingtypes.ValidatorI) bool) { - k.stakingKeeper.IterateBondedValidatorsByPower(ctx, f) -} - -func (k Keeper) GetValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate { - return k.stakingKeeper.GetValidatorUpdates(ctx) -} - -func (k Keeper) GetValidatorByConsAddr(ctx sdk.Context, addr sdk.ConsAddress) (stakingtypes.Validator, bool) { - return k.stakingKeeper.GetValidatorByConsAddr(ctx, addr) -} diff --git a/x/oracle/keeper/single.go b/x/oracle/keeper/single.go index db0a4adf8..65e4b433f 100644 --- a/x/oracle/keeper/single.go +++ b/x/oracle/keeper/single.go @@ -66,7 +66,7 @@ func recacheAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext }) agc.SetValidatorPowers(validatorPowers) //TODO: test only - if k.GetLastTotalPower(ctx).Cmp(totalPower) != 0 { + if k.GetLastTotalPower(ctx).BigInt().Cmp(totalPower) != 0 { panic("something wrong when get validatorsPower from staking module") } @@ -133,7 +133,7 @@ func initAggregatorContext(ctx sdk.Context, agc *aggregator.AggregatorContext, k }) // agc.SetTotalPower(totalPower) agc.SetValidatorPowers(validatorPowers) - if k.GetLastTotalPower(ctx).Cmp(totalPower) != 0 { + if k.GetLastTotalPower(ctx).BigInt().Cmp(totalPower) != 0 { panic("-") } diff --git a/x/oracle/module.go b/x/oracle/module.go index 165205034..c3c1b63dd 100644 --- a/x/oracle/module.go +++ b/x/oracle/module.go @@ -95,9 +95,13 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { type AppModule struct { AppModuleBasic - keeper keeper.Keeper + keeper keeper.Keeper + + // used for simulation accountKeeper types.AccountKeeper - bankKeeper types.BankKeeper + + // used for simulation + bankKeeper types.BankKeeper } func NewAppModule(