Skip to content

Commit

Permalink
refactor: expected keepers (#512)
Browse files Browse the repository at this point in the history
* start airdrop

* unify and add nil check

* unify and add nil check

* upate
  • Loading branch information
Alex Johnson authored Aug 1, 2023
1 parent 367e221 commit 2618ab0
Show file tree
Hide file tree
Showing 28 changed files with 178 additions and 67 deletions.
10 changes: 6 additions & 4 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (appKeepers *AppKeepers) InitKeepers(
appKeepers.ClaimsManagerKeeper = claimsmanagerkeeper.NewKeeper(
appCodec,
appKeepers.keys[claimsmanagertypes.StoreKey],
*appKeepers.IBCKeeper,
appKeepers.IBCKeeper,
)

// claimsmanagerModule := claimsmanager.NewAppModule(appCodec, appKeepers.ClaimsManagerKeeper)
Expand All @@ -391,7 +391,7 @@ func (appKeepers *AppKeepers) InitKeepers(
appKeepers.ICAControllerKeeper,
&scopedInterchainStakingKeeper,
appKeepers.InterchainQueryKeeper,
*appKeepers.IBCKeeper,
appKeepers.IBCKeeper,
appKeepers.TransferKeeper,
appKeepers.ClaimsManagerKeeper,
appKeepers.GetSubspace(interchainstakingtypes.ModuleName),
Expand All @@ -408,8 +408,10 @@ func (appKeepers *AppKeepers) InitKeepers(
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.StakingKeeper,
appKeepers.InterchainQueryKeeper,
appKeepers.IBCKeeper,
&appKeepers.InterchainQueryKeeper,
appKeepers.InterchainstakingKeeper,
appKeepers.ClaimsManagerKeeper,
authtypes.FeeCollectorName,
proofOpsFn,
selfProofOpsFn,
Expand Down Expand Up @@ -533,8 +535,8 @@ func (appKeepers *AppKeepers) InitKeepers(
appKeepers.BankKeeper,
appKeepers.StakingKeeper,
appKeepers.GovKeeper,
appKeepers.IBCKeeper,
appKeepers.InterchainstakingKeeper,
appKeepers.InterchainQueryKeeper,
appKeepers.ParticipationRewardsKeeper,
proofOpsFn,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
proto "github.com/gogo/protobuf/proto"

"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdkioerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"

"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stableswap

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm"
)

Expand Down
13 changes: 13 additions & 0 deletions utils/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
sdk "github.com/cosmos/cosmos-sdk/types"
ibctmtypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"

claimsmanagertypes "github.com/ingenuity-build/quicksilver/x/claimsmanager/types"
)

type ClaimsManagerKeeper interface {
IterateLastEpochUserClaims(ctx sdk.Context, chainID, address string, fn func(index int64, data claimsmanagertypes.Claim) (stop bool))
GetSelfConsensusState(ctx sdk.Context, key string) (ibctmtypes.ConsensusState, bool)
}
8 changes: 3 additions & 5 deletions utils/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
tmclienttypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"
"github.com/tendermint/tendermint/proto/tendermint/crypto"

claimsmanagerkeeper "github.com/ingenuity-build/quicksilver/x/claimsmanager/keeper"
)

type ProofOpsFn func(ctx sdk.Context, ibcKeeper *ibckeeper.Keeper, connectionID, chainID string, height int64, module string, key []byte, data []byte, proofOps *crypto.ProofOps) error

type SelfProofOpsFn func(ctx sdk.Context, claimsKeeper claimsmanagerkeeper.Keeper, consensusStateKey, module string, key []byte, data []byte, proofOps *crypto.ProofOps) error
type SelfProofOpsFn func(ctx sdk.Context, claimsKeeper ClaimsManagerKeeper, consensusStateKey, module string, key []byte, data []byte, proofOps *crypto.ProofOps) error

func ValidateProofOps(
ctx sdk.Context,
Expand Down Expand Up @@ -74,7 +72,7 @@ func ValidateProofOps(
return nil
}

func ValidateSelfProofOps(ctx sdk.Context, claimsKeeper claimsmanagerkeeper.Keeper, consensusStateKey, module string, key, data []byte, proofOps *crypto.ProofOps) error {
func ValidateSelfProofOps(ctx sdk.Context, claimsKeeper ClaimsManagerKeeper, consensusStateKey, module string, key, data []byte, proofOps *crypto.ProofOps) error {
if proofOps == nil {
return errors.New("unable to validate proof. No proof submitted")
}
Expand Down Expand Up @@ -108,7 +106,7 @@ func ValidateSelfProofOps(ctx sdk.Context, claimsKeeper claimsmanagerkeeper.Keep
return nil
}

func MockSelfProofOps(_ sdk.Context, _ claimsmanagerkeeper.Keeper, _, _ string, _, _ []byte, _ *crypto.ProofOps) error {
func MockSelfProofOps(_ sdk.Context, _ ClaimsManagerKeeper, _, _ string, _, _ []byte, _ *crypto.ProofOps) error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion x/airdrop/keeper/claim_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (k *Keeper) verifyOsmosisLP(ctx sdk.Context, proofs []*cmtypes.Proof, cr ty
// validate proof tx
if err := k.ValidateProofOps(
ctx,
&k.icsKeeper.IBCKeeper,
k.ibcKeeper,
osmoZone.ConnectionId,
osmoZone.ChainId,
proof.Height,
Expand Down
27 changes: 14 additions & 13 deletions x/airdrop/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
"github.com/tendermint/tendermint/libs/log"

"github.com/ingenuity-build/quicksilver/utils"
"github.com/ingenuity-build/quicksilver/x/airdrop/types"
icqkeeper "github.com/ingenuity-build/quicksilver/x/interchainquery/keeper"
icskeeper "github.com/ingenuity-build/quicksilver/x/interchainstaking/keeper"
prkeeper "github.com/ingenuity-build/quicksilver/x/participationrewards/keeper"
)

type Keeper struct {
Expand All @@ -24,10 +21,10 @@ type Keeper struct {
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper
govKeeper govkeeper.Keeper
icsKeeper *icskeeper.Keeper
icqKeeper icqkeeper.Keeper
prKeeper *prkeeper.Keeper
govKeeper types.GovKeeper
ibcKeeper *ibckeeper.Keeper
icsKeeper types.InterchainStakingKeeper
prKeeper types.ParticipationRewardsKeeper

ValidateProofOps utils.ProofOpsFn

Expand All @@ -45,10 +42,10 @@ func NewKeeper(
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.StakingKeeper,
gk govkeeper.Keeper,
icsk *icskeeper.Keeper,
icqk icqkeeper.Keeper,
prk *prkeeper.Keeper,
gk types.GovKeeper,
ibcKeeper *ibckeeper.Keeper,
icsk types.InterchainStakingKeeper,
prk types.ParticipationRewardsKeeper,
pofn utils.ProofOpsFn,
authority string,
) *Keeper {
Expand All @@ -61,6 +58,10 @@ func NewKeeper(
ps = ps.WithKeyTable(types.ParamKeyTable())
}

if ibcKeeper == nil {
panic("ibcKeeper is nil")
}

return &Keeper{
cdc: cdc,
storeKey: key,
Expand All @@ -69,8 +70,8 @@ func NewKeeper(
bankKeeper: bk,
stakingKeeper: sk,
govKeeper: gk,
ibcKeeper: ibcKeeper,
icsKeeper: icsk,
icqKeeper: icqk,
prKeeper: prk,
ValidateProofOps: pofn,
authority: authority,
Expand Down
20 changes: 20 additions & 0 deletions x/airdrop/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package types // noalias
import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

icstypes "github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
participationrewardstypes "github.com/ingenuity-build/quicksilver/x/participationrewards/types"
)

// AccountKeeper defines the contract required for account APIs.
Expand All @@ -27,3 +31,19 @@ type StakingKeeper interface {
BondDenom(ctx sdk.Context) string
GetDelegatorBonded(ctx sdk.Context, delegator sdk.AccAddress) sdkmath.Int
}

type GovKeeper interface {
IterateProposals(ctx sdk.Context, cb func(proposal v1.Proposal) (stop bool))
GetVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) (vote v1.Vote, found bool)
}

type InterchainStakingKeeper interface {
GetZone(ctx sdk.Context, chainID string) (icstypes.Zone, bool)
GetDelegatorIntent(ctx sdk.Context, zone *icstypes.Zone, delegator string, snapshot bool) (icstypes.DelegatorIntent, bool)
IterateZones(ctx sdk.Context, fn func(index int64, zone *icstypes.Zone) (stop bool))
UserZoneReceipts(ctx sdk.Context, zone *icstypes.Zone, addr sdk.AccAddress) ([]icstypes.Receipt, error)
}

type ParticipationRewardsKeeper interface {
GetProtocolData(ctx sdk.Context, pdType participationrewardstypes.ProtocolDataType, key string) (participationrewardstypes.ProtocolData, bool)
}
4 changes: 0 additions & 4 deletions x/claimsmanager/keeper/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"github.com/ingenuity-build/quicksilver/x/claimsmanager/types"
)

func (k Keeper) NewClaim(address, chainID string, module types.ClaimType, srcChainID string, amount uint64) types.Claim {
return types.Claim{UserAddress: address, ChainId: chainID, Module: module, SourceChainId: srcChainID, Amount: amount}
}

// GetClaim returns claim.
func (k Keeper) GetClaim(ctx sdk.Context, chainID, address string, module types.ClaimType, srcChainID string) (types.Claim, bool) {
data := types.Claim{}
Expand Down
3 changes: 1 addition & 2 deletions x/claimsmanager/keeper/claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ func (s *KeeperTestSuite) TestKeeper_NewClaim() {
},
}

k := s.GetQuicksilverApp(s.chainA).ClaimsManagerKeeper
for _, tt := range tests {
s.Run(tt.name, func() {
got := k.NewClaim(tt.args.address, tt.args.chainID, tt.args.module, tt.args.srcChainID, tt.args.amount)
got := types.NewClaim(tt.args.address, tt.args.chainID, tt.args.module, tt.args.srcChainID, tt.args.amount)
s.Require().Equal(tt.want, got)
})
}
Expand Down
8 changes: 6 additions & 2 deletions x/claimsmanager/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ import (
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
IBCKeeper ibckeeper.Keeper
IBCKeeper *ibckeeper.Keeper
}

// NewKeeper returns a new instance of participationrewards Keeper.
// This function will panic on failure.
func NewKeeper(
cdc codec.Codec,
key storetypes.StoreKey,
ibcKeeper ibckeeper.Keeper,
ibcKeeper *ibckeeper.Keeper,
) Keeper {
if ibcKeeper == nil {
panic("ibcKeeper is nil")
}

return Keeper{
cdc: cdc,
storeKey: key,
Expand Down
4 changes: 4 additions & 0 deletions x/claimsmanager/types/claimsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"github.com/ingenuity-build/multierror"
)

func NewClaim(address, chainID string, module ClaimType, srcChainID string, amount uint64) Claim {
return Claim{UserAddress: address, ChainId: chainID, Module: module, SourceChainId: srcChainID, Amount: amount}
}

// ValidateBasic performs stateless validation of a Claim.
func (c *Claim) ValidateBasic() error {
errs := make(map[string]error)
Expand Down
1 change: 1 addition & 0 deletions x/claimsmanager/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"

"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm"
"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm/pool-models/balancer"
"github.com/ingenuity-build/quicksilver/third-party-chains/osmosis-types/gamm/pool-models/stableswap"
Expand Down
4 changes: 4 additions & 0 deletions x/interchainquery/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type Keeper struct {

// NewKeeper returns a new instance of zones Keeper.
func NewKeeper(cdc codec.Codec, storeKey storetypes.StoreKey, ibcKeeper *ibckeeper.Keeper) Keeper {
if ibcKeeper == nil {
panic("ibcKeeper is nil")
}

return Keeper{
cdc: cdc,
storeKey: storeKey,
Expand Down
15 changes: 9 additions & 6 deletions x/interchainstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
icqtypes "github.com/ingenuity-build/quicksilver/x/interchainquery/types"

"github.com/ingenuity-build/quicksilver/utils"
claimsmanagerkeeper "github.com/ingenuity-build/quicksilver/x/claimsmanager/keeper"
interchainquerykeeper "github.com/ingenuity-build/quicksilver/x/interchainquery/keeper"
"github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
)
Expand All @@ -45,9 +44,9 @@ type Keeper struct {
ICQKeeper interchainquerykeeper.Keeper
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
IBCKeeper ibckeeper.Keeper
IBCKeeper *ibckeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
ClaimsManagerKeeper claimsmanagerkeeper.Keeper
ClaimsManagerKeeper types.ClaimsManagerKeeper
Ir codectypes.InterfaceRegistry
hooks types.IcsHooks
paramStore paramtypes.Subspace
Expand All @@ -63,23 +62,27 @@ func NewKeeper(
icaControllerKeeper icacontrollerkeeper.Keeper,
scopedKeeper *capabilitykeeper.ScopedKeeper,
icqKeeper interchainquerykeeper.Keeper,
ibcKeeper ibckeeper.Keeper,
ibcKeeper *ibckeeper.Keeper,
transferKeeper ibctransferkeeper.Keeper,
claimsManagerKeeper claimsmanagerkeeper.Keeper,
claimsManagerKeeper types.ClaimsManagerKeeper,
ps paramtypes.Subspace,
) *Keeper {
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

if addr := accountKeeper.GetModuleAddress(types.EscrowModuleAccount); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
panic(fmt.Sprintf("%s escrow account has not been set", types.EscrowModuleAccount))
}

if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

if ibcKeeper == nil {
panic("ibcKeeper is nil")
}

return &Keeper{
cdc: cdc,
storeKey: storeKey,
Expand Down
8 changes: 7 additions & 1 deletion x/interchainstaking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v5/modules/core/exported"

claimsmanagertypes "github.com/ingenuity-build/quicksilver/x/claimsmanager/types"
)

// ChannelKeeper defines the expected IBC channel keeper.
Expand Down Expand Up @@ -41,3 +42,8 @@ type BankKeeper interface {
type IcsHooks interface {
AfterZoneCreated(ctx sdk.Context, connectionID, chainID, accountPrefix string) error
}

type ClaimsManagerKeeper interface {
IterateLastEpochUserClaims(ctx sdk.Context, chainID, address string, fn func(index int64, data claimsmanagertypes.Claim) (stop bool))
SetClaim(ctx sdk.Context, claim *claimsmanagertypes.Claim)
}
3 changes: 2 additions & 1 deletion x/interchainstaking/types/rebalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/ingenuity-build/quicksilver/utils/addressutils"
"github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
"github.com/stretchr/testify/require"
)

func TestDetermineAllocationsForRebalancing(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions x/participationrewards/keeper/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

icqtypes "github.com/ingenuity-build/quicksilver/x/interchainquery/types"
"github.com/ingenuity-build/quicksilver/x/participationrewards/types"
)
Expand Down
Loading

0 comments on commit 2618ab0

Please sign in to comment.