Skip to content

Commit

Permalink
took into account comments
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Oct 3, 2024
1 parent dfa7b07 commit 14a8bbc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
45 changes: 15 additions & 30 deletions app/upgrades/v21/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v21
import (
"context"
"fmt"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand All @@ -13,7 +14,6 @@ import (
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/gaia/v21/app/keepers"
providerkeeper "github.com/cosmos/interchain-security/v6/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
)

// Neutron and Stride denoms that were not whitelisted but the consumer rewards pool contains amounts of those denoms.
Expand All @@ -32,8 +32,6 @@ const (
)

// CreateUpgradeHandler returns an upgrade handler for Gaia v21.
// It performs module migrations, as well as the following tasks:
// - Initializes the MaxProviderConsensusValidators parameter in the provider module to 180.
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
Expand All @@ -48,46 +46,33 @@ func CreateUpgradeHandler(
return vm, errorsmod.Wrapf(err, "running module migrations")
}

ctx.Logger().Info("distributing rewards of Neutron and Stride unaccounted denoms")
err = DistributeNeutronAndStrideUnaccountedDenoms(ctx, keepers.ProviderKeeper, keepers.BankKeeper, keepers.AccountKeeper)
ctx.Logger().Info("allocating rewards of Neutron and Stride unaccounted denoms")
err = AllocateNeutronAndStrideUnaccountedDenoms(ctx, keepers.ProviderKeeper, keepers.BankKeeper, keepers.AccountKeeper)
if err != nil {
return vm, errorsmod.Wrapf(err, "could not distribute rewards of Neutron and Stride unaccounted denoms")
return vm, errorsmod.Wrapf(err, "could not allocate rewards of Neutron and Stride unaccounted denoms")
}

ctx.Logger().Info("Upgrade v21 complete")
return vm, nil
}
}

// DistributeDenoms distributes all the `denoms` that reside in the `address` and are meant for the chain with `consumerId`
func DistributeDenoms(ctx sdk.Context, providerKeeper providerkeeper.Keeper, bankKeeper bankkeeper.Keeper, address sdk.AccAddress, consumerId string, denoms []string) error {
// AllocateRewards allocates all the `denoms` that reside in the `address` and are meant for the chain with `consumerId`
func AllocateRewards(ctx sdk.Context, providerKeeper providerkeeper.Keeper, bankKeeper bankkeeper.Keeper, address sdk.AccAddress, consumerId string, denoms []string) error {
for _, denom := range denoms {
coinRewards := bankKeeper.GetBalance(ctx, address, denom)
decCoinRewards := sdk.DecCoins{sdk.DecCoin{Denom: coinRewards.Denom, Amount: math.LegacyNewDecFromInt(coinRewards.Amount)}}
consumerRewardsAllocation := types2.ConsumerRewardsAllocation{Rewards: decCoinRewards}

isDenomAllowlisted := providerKeeper.ConsumerRewardDenomExists(ctx, denom)

// allowlist the denom that distribution can take place
providerKeeper.SetConsumerRewardDenom(ctx, denom)
consumerRewardsAllocation := providertypes.ConsumerRewardsAllocation{Rewards: decCoinRewards}

err := providerKeeper.SetConsumerRewardsAllocationByDenom(ctx, consumerId, denom, consumerRewardsAllocation)
if err != nil {
return err
}

// call `BeginBlockRD` to actually perform the distribution
providerKeeper.BeginBlockRD(ctx)

// if you were not allowlisted before, revert to initial state
if !isDenomAllowlisted {
providerKeeper.DeleteConsumerRewardDenom(ctx, denom)
}
}
return nil
}

// HasExpectedChainIdSanityCheck return true if the chain with the provided `consumerId` is of a chain with the `expectedChainId`
// HasExpectedChainIdSanityCheck returns true if the chain with the provided `consumerId` is of a chain with the `expectedChainId`
func HasExpectedChainIdSanityCheck(ctx sdk.Context, providerKeeper providerkeeper.Keeper, consumerId string, expectedChainId string) bool {
actualChainId, err := providerKeeper.GetConsumerChainId(ctx, consumerId)
if err != nil {
Expand All @@ -99,9 +84,9 @@ func HasExpectedChainIdSanityCheck(ctx sdk.Context, providerKeeper providerkeepe
return true
}

// DistributeNeutronAndStrideUnaccountedDenoms distributed previously unaccounted denoms to the Stride and Neutron consumer chains
func DistributeNeutronAndStrideUnaccountedDenoms(ctx sdk.Context, providerKeeper providerkeeper.Keeper, bankKeeper bankkeeper.Keeper, accountKeeper authkeeper.AccountKeeper) error {
consumerRewardsPoolAddress := accountKeeper.GetModuleAccount(ctx, types2.ConsumerRewardsPool).GetAddress()
// AllocateNeutronAndStrideUnaccountedDenoms allocates previously unaccounted denoms to the Stride and Neutron consumer chains
func AllocateNeutronAndStrideUnaccountedDenoms(ctx sdk.Context, providerKeeper providerkeeper.Keeper, bankKeeper bankkeeper.Keeper, accountKeeper authkeeper.AccountKeeper) error {
consumerRewardsPoolAddress := accountKeeper.GetModuleAccount(ctx, providertypes.ConsumerRewardsPool).GetAddress()

const NeutronConsumerId = "0"
const NeutronChainId = "neutron-1"
Expand All @@ -111,9 +96,9 @@ func DistributeNeutronAndStrideUnaccountedDenoms(ctx sdk.Context, providerKeeper
}

neutronUnaccountedDenoms := []string{NeutronUusdc, NeutronUtia}
err := DistributeDenoms(ctx, providerKeeper, bankKeeper, consumerRewardsPoolAddress, NeutronConsumerId, neutronUnaccountedDenoms)
err := AllocateRewards(ctx, providerKeeper, bankKeeper, consumerRewardsPoolAddress, NeutronConsumerId, neutronUnaccountedDenoms)
if err != nil {
return fmt.Errorf("cannot distribute rewards for consumer id (%s): %w", NeutronConsumerId, err)
return fmt.Errorf("cannot allocate rewards for consumer id (%s): %w", NeutronConsumerId, err)
}

const StrideConsumerId = "1"
Expand All @@ -124,9 +109,9 @@ func DistributeNeutronAndStrideUnaccountedDenoms(ctx sdk.Context, providerKeeper
}

strideUnaccountedDenoms := []string{StrideStutia, StrideStadym, StrideStaISLM, StrideStuband, StrideStadydx, StrideStusaga}
err = DistributeDenoms(ctx, providerKeeper, bankKeeper, consumerRewardsPoolAddress, StrideConsumerId, strideUnaccountedDenoms)
err = AllocateRewards(ctx, providerKeeper, bankKeeper, consumerRewardsPoolAddress, StrideConsumerId, strideUnaccountedDenoms)
if err != nil {
return fmt.Errorf("cannot distribute rewards for consumer id (%s): %w", StrideConsumerId, err)
return fmt.Errorf("cannot allocate rewards for consumer id (%s): %w", StrideConsumerId, err)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/cometbft/cometbft-db v0.12.0
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/cosmos-sdk v0.50.9
github.com/cosmos/gaia/v20 v20.0.0
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.7.0
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 v8.0.2
Expand All @@ -32,7 +33,7 @@ require (
github.com/google/gofuzz v1.2.0
github.com/gorilla/mux v1.8.1
github.com/ory/dockertest/v3 v3.11.0
github.com/prometheus/client_golang v1.20.2
github.com/prometheus/client_golang v1.20.3
github.com/rakyll/statik v0.1.7
github.com/skip-mev/feemarket v1.1.1
github.com/spf13/cast v1.7.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+R
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
github.com/cosmos/cosmos-sdk v0.50.9-lsm h1:nYQVX0YinJ3Zu3PHEee36OeZ8yAw42ctE52S2K3MleM=
github.com/cosmos/cosmos-sdk v0.50.9-lsm/go.mod h1:efdTFdUndXfLpSMkE1Kv/ra8pux9l1glcojMIhNreiA=
github.com/cosmos/gaia/v20 v20.0.0 h1:UAkAki/e/RushTEFPvzKlcl711YoqO4BBrzm3xQxSH8=
github.com/cosmos/gaia/v20 v20.0.0/go.mod h1:MborXs1u/T+Y2UsX1SDQl2x+EXGgyTzVo8ItQ/tOq2w=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
Expand Down Expand Up @@ -1120,8 +1122,8 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg=
github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4=
github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down

0 comments on commit 14a8bbc

Please sign in to comment.