Skip to content

Commit

Permalink
refactor: interchainstaking/keeper (#330)
Browse files Browse the repository at this point in the history
* refactor

* refactor

* unify keeper method

* refactor keeper signatures

* lint fix

* imports

* fix

* lint fix
  • Loading branch information
Alex Johnson authored Mar 3, 2023
1 parent f067363 commit 607f847
Show file tree
Hide file tree
Showing 44 changed files with 1,143 additions and 1,054 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (app *Quicksilver) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock
if !found {
panic("ERROR: unable to find expected stargaze-1 zone")
}
app.InterchainstakingKeeper.OverrideRedemptionRateNoCap(ctx, zone)
app.InterchainstakingKeeper.OverrideRedemptionRateNoCap(ctx, &zone)
}

return app.mm.BeginBlock(ctx, req)
Expand Down
14 changes: 7 additions & 7 deletions app/upgrades/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func V010400UpgradeHandler(
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// upgrade zones
keepers.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone icstypes.Zone) (stop bool) {
keepers.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone *icstypes.Zone) (stop bool) {
zone.DepositsEnabled = true
zone.ReturnToSender = false
zone.UnbondingEnabled = false
zone.Decimals = 6
keepers.InterchainstakingKeeper.SetZone(ctx, &zone)
keepers.InterchainstakingKeeper.SetZone(ctx, zone)
return false
})

Expand Down Expand Up @@ -137,7 +137,7 @@ func V010400rc6UpgradeHandler(
true,
false,
6)
err := icskeeper.HandleRegisterZoneProposal(ctx, keepers.InterchainstakingKeeper, regenProp)
err := keepers.InterchainstakingKeeper.HandleRegisterZoneProposal(ctx, regenProp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func V010400rc6UpgradeHandler(
})

if isTestnet(ctx) || isDevnet(ctx) {
keepers.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zoneInfo icstypes.Zone) (stop bool) {
keepers.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zoneInfo *icstypes.Zone) (stop bool) {
keepers.InterchainstakingKeeper.OverrideRedemptionRateNoCap(ctx, zoneInfo)
return false
})
Expand All @@ -184,11 +184,11 @@ func V010400rc8UpgradeHandler(
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// remove expired failed redelegation records
keepers.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone icstypes.Zone) (stop bool) {
keepers.InterchainstakingKeeper.IterateAllDelegations(ctx, &zone, func(delegation icstypes.Delegation) (stop bool) {
keepers.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone *icstypes.Zone) (stop bool) {
keepers.InterchainstakingKeeper.IterateAllDelegations(ctx, zone, func(delegation icstypes.Delegation) (stop bool) {
if delegation.RedelegationEnd < 0 {
delegation.RedelegationEnd = 0
keepers.InterchainstakingKeeper.SetDelegation(ctx, &zone, delegation)
keepers.InterchainstakingKeeper.SetDelegation(ctx, zone, delegation)
}
return false
})
Expand Down
8 changes: 4 additions & 4 deletions app/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func (s *AppTestSuite) TestV010400rc8UpgradeHandler() {
zone, _ = app.InterchainstakingKeeper.GetZone(ctx, "uni-5")

var negRedelEndsBefore []icstypes.Delegation
app.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone icstypes.Zone) (stop bool) {
app.InterchainstakingKeeper.IterateAllDelegations(ctx, &zone, func(delegation icstypes.Delegation) (stop bool) {
app.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone *icstypes.Zone) (stop bool) {
app.InterchainstakingKeeper.IterateAllDelegations(ctx, zone, func(delegation icstypes.Delegation) (stop bool) {
if delegation.RedelegationEnd < 0 {
negRedelEndsBefore = append(negRedelEndsBefore, delegation)
}
Expand All @@ -331,8 +331,8 @@ func (s *AppTestSuite) TestV010400rc8UpgradeHandler() {

var negRedelEndsAfter []icstypes.Delegation

app.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone icstypes.Zone) (stop bool) {
app.InterchainstakingKeeper.IterateAllDelegations(ctx, &zone, func(delegation icstypes.Delegation) (stop bool) {
app.InterchainstakingKeeper.IterateZones(ctx, func(index int64, zone *icstypes.Zone) (stop bool) {
app.InterchainstakingKeeper.IterateAllDelegations(ctx, zone, func(delegation icstypes.Delegation) (stop bool) {
if delegation.RedelegationEnd < 0 {
negRedelEndsAfter = append(negRedelEndsAfter, delegation)
}
Expand Down
6 changes: 3 additions & 3 deletions x/airdrop/keeper/claim_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (k Keeper) verifyZoneIntent(ctx sdk.Context, chainID string, address string
return fmt.Errorf("zone %s not found", chainID)
}

intent, ok := k.icsKeeper.GetIntent(ctx, zone, addr.String(), false)
intent, ok := k.icsKeeper.GetIntent(ctx, &zone, addr.String(), false)
if !ok || len(intent.Intents) == 0 {
return fmt.Errorf("intent not found or no intents set for %s", addr)
}
Expand Down Expand Up @@ -226,9 +226,9 @@ func (k Keeper) verifyGovernanceParticipation(ctx sdk.Context, address string) e
func (k Keeper) verifyOsmosisLP(ctx sdk.Context, proofs []*cmtypes.Proof, cr types.ClaimRecord) error {
// get Osmosis zone
var osmoZone *icstypes.Zone
k.icsKeeper.IterateZones(ctx, func(_ int64, zone icstypes.Zone) (stop bool) {
k.icsKeeper.IterateZones(ctx, func(_ int64, zone *icstypes.Zone) (stop bool) {
if zone.AccountPrefix == "osmo" {
osmoZone = &zone
osmoZone = zone
return true
}
return false
Expand Down
2 changes: 1 addition & 1 deletion x/airdrop/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (suite *KeeperTestSuite) Test_msgServer_Claim() {
}
appA.InterchainstakingKeeper.SetIntent(
suite.chainA.GetContext(),
zone,
&zone,
intent,
false,
)
Expand Down
12 changes: 6 additions & 6 deletions x/interchainstaking/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
panic("unable to find zone for delegation")
}
for _, delegatorIntent := range delegatorIntentsForZone.DelegationIntent {
k.SetIntent(ctx, zone, *delegatorIntent, false)
k.SetIntent(ctx, &zone, *delegatorIntent, false)
}
}

Expand Down Expand Up @@ -78,25 +78,25 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {

func ExportDelegationsPerZone(ctx sdk.Context, k keeper.Keeper) []types.DelegationsForZone {
delegationsForZones := make([]types.DelegationsForZone, 0)
k.IterateZones(ctx, func(_ int64, zoneInfo types.Zone) (stop bool) {
delegationsForZones = append(delegationsForZones, types.DelegationsForZone{ChainId: zoneInfo.ChainId, Delegations: k.GetAllDelegationsAsPointer(ctx, &zoneInfo)})
k.IterateZones(ctx, func(_ int64, zoneInfo *types.Zone) (stop bool) {
delegationsForZones = append(delegationsForZones, types.DelegationsForZone{ChainId: zoneInfo.ChainId, Delegations: k.GetAllDelegationsAsPointer(ctx, zoneInfo)})
return false
})
return delegationsForZones
}

func ExportPerformanceDelegationsPerZone(ctx sdk.Context, k keeper.Keeper) []types.DelegationsForZone {
delegationsForZones := make([]types.DelegationsForZone, 0)
k.IterateZones(ctx, func(_ int64, zoneInfo types.Zone) (stop bool) {
delegationsForZones = append(delegationsForZones, types.DelegationsForZone{ChainId: zoneInfo.ChainId, Delegations: k.GetAllPerformanceDelegationsAsPointer(ctx, &zoneInfo)})
k.IterateZones(ctx, func(_ int64, zoneInfo *types.Zone) (stop bool) {
delegationsForZones = append(delegationsForZones, types.DelegationsForZone{ChainId: zoneInfo.ChainId, Delegations: k.GetAllPerformanceDelegationsAsPointer(ctx, zoneInfo)})
return false
})
return delegationsForZones
}

func ExportDelegatorIntentsPerZone(ctx sdk.Context, k keeper.Keeper) []types.DelegatorIntentsForZone {
delegatorIntentsForZones := make([]types.DelegatorIntentsForZone, 0)
k.IterateZones(ctx, func(_ int64, zoneInfo types.Zone) (stop bool) {
k.IterateZones(ctx, func(_ int64, zoneInfo *types.Zone) (stop bool) {
// export current epoch intents
delegatorIntentsForZones = append(delegatorIntentsForZones, types.DelegatorIntentsForZone{ChainId: zoneInfo.ChainId, DelegationIntent: k.AllIntentsAsPointer(ctx, zoneInfo, false), Snapshot: false})
// export last epoch intents
Expand Down
4 changes: 2 additions & 2 deletions x/interchainstaking/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func NewProposalHandler(k keeper.Keeper) govv1beta1.Handler {
return func(ctx sdk.Context, content govv1beta1.Content) error {
switch c := content.(type) {
case *types.RegisterZoneProposal:
return keeper.HandleRegisterZoneProposal(ctx, k, c)
return k.HandleRegisterZoneProposal(ctx, c)
case *types.UpdateZoneProposal:
return keeper.HandleUpdateZoneProposal(ctx, k, c)
return k.HandleUpdateZoneProposal(ctx, c)

default:
return sdkioerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized interchainstaking proposal content type: %T", c)
Expand Down
22 changes: 13 additions & 9 deletions x/interchainstaking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,43 @@ import (
"bytes"
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmtypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"

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

const blockInterval = 30

type zoneItrFn func(index int64, zoneInfo types.Zone) (stop bool)
type zoneItrFn func(index int64, zoneInfo *types.Zone) (stop bool)

// BeginBlocker of interchainstaking module
func (k Keeper) BeginBlocker(ctx sdk.Context) {
func (k *Keeper) BeginBlocker(ctx sdk.Context) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

if ctx.BlockHeight()%blockInterval == 0 {
if err := k.GCCompletedRedelegations(ctx); err != nil {
k.Logger(ctx).Error("error in GCCompletedRedelegations", "error", err)
}
}
k.IterateZones(ctx, func(index int64, zone types.Zone) (stop bool) {
if ctx.BlockHeight()%blockInterval == 0 {
k.IterateZones(ctx, func(index int64, zone *types.Zone) (stop bool) {
if ctx.BlockHeight()%30 == 0 {
// for the tasks below, we cannot panic in begin blocker; as this will crash the chain.
// and as failing here is not terminal panicking is not necessary, but we should log
// as an error. we don't return on failure here as we still want to attempt the unrelated
// tasks below.
// commenting this out until we can revisit. in its current state it causes more issues than it fixes.

if err := k.EnsureWithdrawalAddresses(ctx, &zone); err != nil {
if err := k.EnsureWithdrawalAddresses(ctx, zone); err != nil {
k.Logger(ctx).Error("error in EnsureWithdrawalAddresses", "error", err)
}
if err := k.HandleMaturedUnbondings(ctx, &zone); err != nil {
if err := k.HandleMaturedUnbondings(ctx, zone); err != nil {
k.Logger(ctx).Error("error in HandleMaturedUnbondings", "error", err)
}
if err := k.GCCompletedUnbondings(ctx, &zone); err != nil {
if err := k.GCCompletedUnbondings(ctx, zone); err != nil {
k.Logger(ctx).Error("error in GCCompletedUnbondings", "error", err)
}
}
Expand All @@ -52,15 +54,17 @@ func (k Keeper) BeginBlocker(ctx sdk.Context) {
if len(zone.IbcNextValidatorsHash) == 0 || !bytes.Equal(zone.IbcNextValidatorsHash, tmConsState.NextValidatorsHash.Bytes()) {
k.Logger(ctx).Info("IBC ValSet has changed; requerying valset")
// trigger valset update.
err := k.EmitValsetRequery(ctx, zone.ConnectionId, zone.ChainId)
period := int64(k.GetParam(ctx, types.KeyValidatorSetInterval))
query := stakingTypes.QueryValidatorsRequest{}
err := k.EmitValSetQuery(ctx, zone, query, sdkmath.NewInt(period))
if err != nil {
k.Logger(ctx).Error("unable to trigger valset update query", "error", err)
// failing to emit the valset update is not terminal but constitutes
// an error, as if this starts happening frequent it is something
// we should investigate.
}
zone.IbcNextValidatorsHash = tmConsState.NextValidatorsHash.Bytes()
k.SetZone(ctx, &zone)
k.SetZone(ctx, zone)
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions x/interchainstaking/keeper/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import (

// ___________________________________________________________________________________________________

// Callbacks wrapper struct for interchainstaking keeper
type Callback func(Keeper, sdk.Context, []byte, icqtypes.Query) error
type Callback func(*Keeper, sdk.Context, []byte, icqtypes.Query) error

// Callbacks wrapper struct for interchainstaking keeper
type Callbacks struct {
k Keeper
k *Keeper
callbacks map[string]Callback
}

var _ icqtypes.QueryCallbacks = Callbacks{}

func (k Keeper) CallbackHandler() Callbacks {
func (k *Keeper) CallbackHandler() Callbacks {
return Callbacks{k, make(map[string]Callback)}
}

Expand Down Expand Up @@ -75,23 +75,23 @@ func (c Callbacks) RegisterCallbacks() icqtypes.QueryCallbacks {
// Callback Handlers
// -----------------------------------

func ValsetCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func ValsetCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
}
return SetValidatorsForZone(&k, ctx, zone, args, query.Request)
return k.SetValidatorsForZone(ctx, &zone, args, query.Request)
}

func ValidatorCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func ValidatorCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
}
return SetValidatorForZone(&k, ctx, zone, args)
return k.SetValidatorForZone(ctx, &zone, args)
}

func RewardsCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func RewardsCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
Expand All @@ -118,7 +118,7 @@ func RewardsCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Quer
return k.WithdrawDelegationRewardsForResponse(ctx, &zone, rewardsQuery.DelegatorAddress, args)
}

func DelegationsCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func DelegationsCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
Expand All @@ -138,7 +138,7 @@ func DelegationsCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.
return k.UpdateDelegationRecordsForAddress(ctx, zone, delegationQuery.DelegatorAddr, args)
}

func DelegationCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func DelegationCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
Expand Down Expand Up @@ -186,7 +186,7 @@ func DelegationCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Q
return k.UpdateDelegationRecordForAddress(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress, sdk.NewCoin(zone.BaseDenom, val.SharesToTokens(delegation.Shares)), &zone, true)
}

func PerfBalanceCallback(k Keeper, ctx sdk.Context, response []byte, query icqtypes.Query) error {
func PerfBalanceCallback(k *Keeper, ctx sdk.Context, response []byte, query icqtypes.Query) error {
// update account balance first.
if err := AccountBalanceCallback(k, ctx, response, query); err != nil {
return err
Expand All @@ -206,7 +206,7 @@ func PerfBalanceCallback(k Keeper, ctx sdk.Context, response []byte, query icqty
return nil
}

func DepositIntervalCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func DepositIntervalCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
Expand All @@ -232,7 +232,7 @@ func DepositIntervalCallback(k Keeper, ctx sdk.Context, args []byte, query icqty
for _, txn := range txs.TxResponses {
req := tx.GetTxRequest{Hash: txn.TxHash}
hashBytes := k.cdc.MustMarshal(&req)
_, found = k.GetReceipt(ctx, GetReceiptKey(zone.ChainId, txn.TxHash))
_, found = k.GetReceipt(ctx, types.GetReceiptKey(zone.ChainId, txn.TxHash))
if found {
k.Logger(ctx).Debug("Found previously handled tx. Ignoring.", "txhash", txn.TxHash)
continue
Expand Down Expand Up @@ -347,7 +347,7 @@ func checkValidity(
return nil
}

func DepositTx(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func DepositTx(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
Expand All @@ -368,7 +368,7 @@ func DepositTx(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) err
return err
}

_, found = k.GetReceipt(ctx, GetReceiptKey(zone.ChainId, res.GetTxResponse().TxHash))
_, found = k.GetReceipt(ctx, types.GetReceiptKey(zone.ChainId, res.GetTxResponse().TxHash))
if found {
k.Logger(ctx).Debug("Found previously handled tx. Ignoring.", "txhash", res.GetTxResponse().TxHash)
return nil
Expand Down Expand Up @@ -414,11 +414,11 @@ func DepositTx(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) err
return fmt.Errorf("unable to validate proof: %w", err)
}

return k.HandleReceiptTransaction(ctx, res.GetTxResponse(), res.GetTx(), zone)
return k.HandleReceiptTransaction(ctx, res.GetTxResponse(), res.GetTx(), &zone)
}

// AccountBalanceCallback is a callback handler for Balance queries.
func AccountBalanceCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func AccountBalanceCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
zone, found := k.GetZone(ctx, query.GetChainId())
if !found {
return fmt.Errorf("no registered zone for chain id: %s", query.GetChainId())
Expand Down Expand Up @@ -457,10 +457,10 @@ func AccountBalanceCallback(k Keeper, ctx sdk.Context, args []byte, query icqtyp
return err
}

return SetAccountBalanceForDenom(k, ctx, zone, address, coin)
return k.SetAccountBalanceForDenom(ctx, &zone, address, coin)
}

func AllBalancesCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
func AllBalancesCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes.Query) error {
balanceQuery := banktypes.QueryAllBalancesRequest{}
// this shouldn't happen because query.Request comes from Quicksilver
if len(query.Request) == 0 {
Expand Down
Loading

0 comments on commit 607f847

Please sign in to comment.