Skip to content

Commit

Permalink
refactor(oracle): use internal interface for external keeper reference
Browse files Browse the repository at this point in the history
  • Loading branch information
leonz789 committed Apr 13, 2024
1 parent e1c48ae commit bd5ffc8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 41 deletions.
21 changes: 13 additions & 8 deletions x/oracle/keeper/common/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
36 changes: 7 additions & 29 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
)

Expand All @@ -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() {
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions x/oracle/keeper/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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("-")
}

Expand Down
8 changes: 6 additions & 2 deletions x/oracle/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bd5ffc8

Please sign in to comment.