From 923b4c2de16bd1a9a6916d4727aae9533026fdbc Mon Sep 17 00:00:00 2001 From: insumity Date: Fri, 27 Sep 2024 14:18:11 +0200 Subject: [PATCH] init commit --- app/upgrades/v21/constants.go | 15 ++++ app/upgrades/v21/upgrades.go | 159 ++++++++++++++++++++++++++++++++++ go.mod | 6 +- go.sum | 8 +- 4 files changed, 182 insertions(+), 6 deletions(-) create mode 100644 app/upgrades/v21/constants.go create mode 100644 app/upgrades/v21/upgrades.go diff --git a/app/upgrades/v21/constants.go b/app/upgrades/v21/constants.go new file mode 100644 index 00000000000..ca58423d985 --- /dev/null +++ b/app/upgrades/v21/constants.go @@ -0,0 +1,15 @@ +package v21 + +import ( + "github.com/cosmos/gaia/v20/app/upgrades" +) + +const ( + // UpgradeName defines the on-chain upgrade name. + UpgradeName = "v21" +) + +var Upgrade = upgrades.Upgrade{ + UpgradeName: UpgradeName, + CreateUpgradeHandler: CreateUpgradeHandler, +} diff --git a/app/upgrades/v21/upgrades.go b/app/upgrades/v21/upgrades.go new file mode 100644 index 00000000000..590a2563486 --- /dev/null +++ b/app/upgrades/v21/upgrades.go @@ -0,0 +1,159 @@ +package v21 + +import ( + "context" + errorsmod "cosmossdk.io/errors" + "cosmossdk.io/math" + upgradetypes "cosmossdk.io/x/upgrade/types" + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + "github.com/cosmos/gaia/v20/app/keepers" + providerkeeper "github.com/cosmos/interchain-security/v5/x/ccv/provider/keeper" + types2 "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. +// Price in $ for each denom corresponds to an approximation fo the current amount stored in the consumer rewards pool +// as of 27.09.2024. Only denoms with amounts more than $10 are included. +const ( + NeutronUusdc = "ibc/4E0D0854C0F846150FA8389D75EA5B5129B17703D7F4992D0356B4FE7C013D42" // ~$40 + NeutronUtia = "ibc/7054742D02E4F28B7DB5B44D97A496CF5AD16C2AE6948028A5FD57DCE7C5E271" // ~$300 + + StrideStutia = "ibc/17DABEBAC71C388DA064A3D54FB7E68BAF0687965EC39DEADA1FB78C0F1447E6" // ~$18,000 + StrideStadym = "ibc/3F0A41ECB6FAF27E315583DBF39B5B69A7149D23959A0E4B319F7EF5C618DCD7" // ~$800 + StrideStaISLM = "ibc/61A6F21D6AFF9835F66056461F1CAE24AA3323820259856B485FE7C063CA4FA6" // ~$1650 + StrideStuband = "ibc/E9401AC885592AC2023E0FB9BA7C8BC66D346CEE04CED8E9F545F3C25290708A" // ~$300 + StrideStadydx = "ibc/EEFD952A6DE346F2649039E99A16430B05FFEDF628A4DE99F34BB4B5F6A9346E" // ~$21,000 + StrideStusaga = "ibc/F918765AC289257B35DECC52BD92EBCDBA3C139658BD6F2670D70A6E10B97F58" // ~$300 +) + +// 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, + keepers *keepers.AppKeepers, +) upgradetypes.UpgradeHandler { + return func(c context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + ctx := sdk.UnwrapSDKContext(c) + ctx.Logger().Info("Starting module migrations...") + + vm, err := mm.RunMigrations(ctx, configurator, vm) + if err != nil { + return vm, errorsmod.Wrapf(err, "running module migrations") + } + + ctx.Logger().Info("distributing previously sent rewards") + err = AllocatePreviousAllocatedRewards(ctx, keepers.ProviderKeeper) + if err != nil { + return vm, errorsmod.Wrapf(err, "could not distribute previously allocate rewards") + } + + ctx.Logger().Info("distributing rewards of Neutron and Stride unaccounted denoms") + err = DistributeNeutronAndStrideUnaccountedDenoms(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") + } + + ctx.Logger().Info("Upgrade v21 complete") + return vm, nil + } +} + +// AllocatePreviousAllocatedRewards distributes any previously-allocated rewards that were not yet distributed +// to the validators of the corresponding consumer chain. +func AllocatePreviousAllocatedRewards(ctx sdk.Context, providerKeeper providerkeeper.Keeper) error { + // only consider launched chains + for _, consumerId := range providerKeeper.GetAllConsumersWithIBCClients(ctx) { + rewards := providerKeeper.GetConsumerRewardsAllocation(ctx, consumerId) + + remainingRewards, err := providerKeeper.AllocateConsumerRewards(ctx, consumerId, rewards) + if err != nil { + return err + } + + ctx.Logger().Info("allocated previously-allocated consumer rewards", + "consumerId", consumerId, + "rewards", rewards, + "remainingRewards", remainingRewards) + } + + return 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 { + 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) + + 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 where you were + 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` +func HasExpectedChainIdSanityCheck(ctx sdk.Context, providerKeeper providerkeeper.Keeper, consumerId string, expectedChainId string) bool { + actualChainId, err := providerKeeper.GetConsumerChainId(ctx, consumerId) + if err != nil { + return false + } + if expectedChainId != actualChainId { + return false + } + 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() + + const NeutronConsumerId = "0" + const NeutronChainId = "neutron-1" + + if !HasExpectedChainIdSanityCheck(ctx, providerKeeper, NeutronConsumerId, NeutronChainId) { + return fmt.Errorf("failed sanity check: consumer id (%s) does not correspond to chain id (%s)", NeutronConsumerId, NeutronChainId) + } + + neutronUnaccountedDenoms := []string{NeutronUusdc, NeutronUtia} + err := DistributeDenoms(ctx, providerKeeper, bankKeeper, consumerRewardsPoolAddress, NeutronConsumerId, neutronUnaccountedDenoms) + if err != nil { + return fmt.Errorf("cannot distribute rewards for consumer id (%s): %w", NeutronConsumerId, err.Error()) + } + + const StrideConsumerId = "1" + const StrideChainId = "stride-1" + + if !HasExpectedChainIdSanityCheck(ctx, providerKeeper, StrideConsumerId, StrideChainId) { + return fmt.Errorf("failed sanity check: consumer id (%d) does not correspond to chain id (%s)", StrideConsumerId, StrideChainId) + } + + strideUnaccountedDenoms := []string{StrideStutia, StrideStadym, StrideStaISLM, StrideStuband, StrideStadydx, StrideStusaga} + err = DistributeDenoms(ctx, providerKeeper, bankKeeper, consumerRewardsPoolAddress, StrideConsumerId, strideUnaccountedDenoms) + if err != nil { + return fmt.Errorf("cannot distribute rewards for consumer id (%s): %w", StrideConsumerId, err.Error()) + } + + return nil +} diff --git a/go.mod b/go.mod index c968d51d8d1..c25553d4216 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( cosmossdk.io/tools/rosetta v0.2.1-0.20230613133644-0a778132a60f cosmossdk.io/x/evidence v0.1.1 cosmossdk.io/x/feegrant v0.1.1 - cosmossdk.io/x/tx v0.13.5 + cosmossdk.io/x/tx v0.13.4 cosmossdk.io/x/upgrade v0.1.4 github.com/CosmWasm/wasmd v0.53.0 github.com/cometbft/cometbft v0.38.11 @@ -28,11 +28,12 @@ require ( github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0 github.com/cosmos/ibc-go/modules/capability v1.0.1 github.com/cosmos/ibc-go/v8 v8.5.1 + github.com/cosmos/interchain-security/v5 v5.0.0-20240904071545-7301916eeafa github.com/cosmos/interchain-security/v6 v6.1.0 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.4 + github.com/prometheus/client_golang v1.20.2 github.com/rakyll/statik v0.1.7 github.com/skip-mev/feemarket v1.1.1 github.com/spf13/cast v1.7.0 @@ -250,6 +251,7 @@ replace ( // Use special SDK v0.50.x release with support for both ICS and LSM github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.50.9-lsm + github.com/cosmos/interchain-security/v5 => /Users/karolos/interchain-security // TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 diff --git a/go.sum b/go.sum index 37ad3e61794..4978e97c115 100644 --- a/go.sum +++ b/go.sum @@ -220,8 +220,8 @@ cosmossdk.io/x/feegrant v0.1.1 h1:EKFWOeo/pup0yF0svDisWWKAA9Zags6Zd0P3nRvVvw8= cosmossdk.io/x/feegrant v0.1.1/go.mod h1:2GjVVxX6G2fta8LWj7pC/ytHjryA6MHAJroBWHFNiEQ= cosmossdk.io/x/nft v0.1.1 h1:pslAVS8P5NkW080+LWOamInjDcq+v2GSCo+BjN9sxZ8= cosmossdk.io/x/nft v0.1.1/go.mod h1:Kac6F6y2gsKvoxU+fy8uvxRTi4BIhLOor2zgCNQwVgY= -cosmossdk.io/x/tx v0.13.5 h1:FdnU+MdmFWn1pTsbfU0OCf2u6mJ8cqc1H4OMG418MLw= -cosmossdk.io/x/tx v0.13.5/go.mod h1:V6DImnwJMTq5qFjeGWpXNiT/fjgE4HtmclRmTqRVM3w= +cosmossdk.io/x/tx v0.13.4 h1:Eg0PbJgeO0gM8p5wx6xa0fKR7hIV6+8lC56UrsvSo0Y= +cosmossdk.io/x/tx v0.13.4/go.mod h1:BkFqrnGGgW50Y6cwTy+JvgAhiffbGEKW6KF9ufcDpvk= cosmossdk.io/x/upgrade v0.1.4 h1:/BWJim24QHoXde8Bc64/2BSEB6W4eTydq0X/2f8+g38= cosmossdk.io/x/upgrade v0.1.4/go.mod h1:9v0Aj+fs97O+Ztw+tG3/tp5JSlrmT7IcFhAebQHmOPo= dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= @@ -1122,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.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= -github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +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_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=