diff --git a/app/upgrades.go b/app/upgrades.go index 37614e16..8b59613f 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -1,258 +1,11 @@ package app import ( - "fmt" - - sdkmath "cosmossdk.io/math" - storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/module" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" -) - -const ( - UpgradeName_Mainnet = "v0.25.0" - UpgradeName_Testnet = "v0.25.0-alpha.0" - UpgradeName_E2ETest = "v0.25.0-testing" -) - -var ( - // KAVA to ukava - 6 decimals - kavaConversionFactor = sdk.NewInt(1000_000) - secondsPerYear = sdk.NewInt(365 * 24 * 60 * 60) - - // 10 Million KAVA per year in staking rewards, inflation disable time 2024-01-01T00:00:00 UTC - // CommunityParams_Mainnet = communitytypes.NewParams( - // time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - // // before switchover - // sdkmath.LegacyZeroDec(), - // // after switchover - 10M KAVA to ukava per year / seconds per year - // sdkmath.LegacyNewDec(10_000_000). - // MulInt(kavaConversionFactor). - // QuoInt(secondsPerYear), - // ) - - // Testnet -- 15 Trillion KAVA per year in staking rewards, inflation disable time 2023-11-16T00:00:00 UTC - // CommunityParams_Testnet = communitytypes.NewParams( - // time.Date(2023, 11, 16, 0, 0, 0, 0, time.UTC), - // // before switchover - // sdkmath.LegacyZeroDec(), - // // after switchover - // sdkmath.LegacyNewDec(15_000_000). - // MulInt64(1_000_000). // 15M * 1M = 15T - // MulInt(kavaConversionFactor). - // QuoInt(secondsPerYear), - // ) - - // CommunityParams_E2E = communitytypes.NewParams( - // time.Now().Add(10*time.Second).UTC(), // relative time for testing - // sdkmath.LegacyNewDec(0), // stakingRewardsPerSecond - // sdkmath.LegacyNewDec(1000), // upgradeTimeSetstakingRewardsPerSecond - // ) - - // ValidatorMinimumCommission is the new 5% minimum commission rate for validators - ValidatorMinimumCommission = sdk.NewDecWithPrec(5, 2) + v1 "github.com/0glabs/0g-chain/app/upgrades/v1" ) // RegisterUpgradeHandlers registers the upgrade handlers for the app. func (app App) RegisterUpgradeHandlers() { - // app.upgradeKeeper.SetUpgradeHandler( - // UpgradeName_Mainnet, - // upgradeHandler(app, UpgradeName_Mainnet, CommunityParams_Mainnet), - // ) - // app.upgradeKeeper.SetUpgradeHandler( - // UpgradeName_Testnet, - // upgradeHandler(app, UpgradeName_Testnet, CommunityParams_Testnet), - // ) - // app.upgradeKeeper.SetUpgradeHandler( - // UpgradeName_E2ETest, - // upgradeHandler(app, UpgradeName_Testnet, CommunityParams_E2E), - // ) - - upgradeInfo, err := app.upgradeKeeper.ReadUpgradeInfoFromDisk() - if err != nil { - panic(err) - } - - doUpgrade := upgradeInfo.Name == UpgradeName_Mainnet || - upgradeInfo.Name == UpgradeName_Testnet || - upgradeInfo.Name == UpgradeName_E2ETest - - if doUpgrade && !app.upgradeKeeper.IsSkipHeight(upgradeInfo.Height) { - storeUpgrades := storetypes.StoreUpgrades{ - Added: []string{ - // x/community added store - // communitytypes.ModuleName, - }, - } - - // configure store loader that checks if version == upgradeHeight and applies store upgrades - app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) - } -} - -// upgradeHandler returns an UpgradeHandler for the given upgrade parameters. -func upgradeHandler( - app App, - name string, - // communityParams communitytypes.Params, -) upgradetypes.UpgradeHandler { - return func( - ctx sdk.Context, - plan upgradetypes.Plan, - fromVM module.VersionMap, - ) (module.VersionMap, error) { - app.Logger().Info(fmt.Sprintf("running %s upgrade handler", name)) - - toVM, err := app.mm.RunMigrations(ctx, app.configurator, fromVM) - if err != nil { - return toVM, err - } - - // - // Staking validator minimum commission - // - UpdateValidatorMinimumCommission(ctx, app) - - // - // Community Params - // - // app.communityKeeper.SetParams(ctx, communityParams) - // app.Logger().Info( - // "initialized x/community params", - // "UpgradeTimeDisableInflation", communityParams.UpgradeTimeDisableInflation, - // "StakingRewardsPerSecond", communityParams.StakingRewardsPerSecond, - // "UpgradeTimeSetStakingRewardsPerSecond", communityParams.UpgradeTimeSetStakingRewardsPerSecond, - // ) - - // - // Kavadist gov grant - // - // msgGrant, err := authz.NewMsgGrant( - // app.accountKeeper.GetModuleAddress(kavadisttypes.ModuleName), // granter - // app.accountKeeper.GetModuleAddress(govtypes.ModuleName), // grantee - // authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{})), // authorization - // nil, // expiration - // ) - // if err != nil { - // return toVM, err - // } - // _, err = app.authzKeeper.Grant(ctx, msgGrant) - // if err != nil { - // return toVM, err - // } - // app.Logger().Info("created gov grant for kavadist funds") - - // - // Gov Quorum - // - govTallyParams := app.govKeeper.GetTallyParams(ctx) - oldQuorum := govTallyParams.Quorum - govTallyParams.Quorum = sdkmath.LegacyMustNewDecFromStr("0.2").String() - app.govKeeper.SetTallyParams(ctx, govTallyParams) - app.Logger().Info(fmt.Sprintf("updated tally quorum from %s to %s", oldQuorum, govTallyParams.Quorum)) - - // - // Incentive Params - // - UpdateIncentiveParams(ctx, app) - - return toVM, nil - } -} - -// UpdateValidatorMinimumCommission updates the commission rate for all -// validators to be at least the new min commission rate, and sets the minimum -// commission rate in the staking params. -func UpdateValidatorMinimumCommission( - ctx sdk.Context, - app App, -) { - resultCount := make(map[stakingtypes.BondStatus]int) - - // Iterate over *all* validators including inactive - app.stakingKeeper.IterateValidators( - ctx, - func(index int64, validator stakingtypes.ValidatorI) (stop bool) { - // Skip if validator commission is already >= 5% - if validator.GetCommission().GTE(ValidatorMinimumCommission) { - return false - } - - val, ok := validator.(stakingtypes.Validator) - if !ok { - panic("expected stakingtypes.Validator") - } - - // Set minimum commission rate to 5%, when commission is < 5% - val.Commission.Rate = ValidatorMinimumCommission - val.Commission.UpdateTime = ctx.BlockTime() - - // Update MaxRate if necessary - if val.Commission.MaxRate.LT(ValidatorMinimumCommission) { - val.Commission.MaxRate = ValidatorMinimumCommission - } - - if err := app.stakingKeeper.BeforeValidatorModified(ctx, val.GetOperator()); err != nil { - panic(fmt.Sprintf("failed to call BeforeValidatorModified: %s", err)) - } - app.stakingKeeper.SetValidator(ctx, val) - - // Keep track of counts just for logging purposes - switch val.GetStatus() { - case stakingtypes.Bonded: - resultCount[stakingtypes.Bonded]++ - case stakingtypes.Unbonded: - resultCount[stakingtypes.Unbonded]++ - case stakingtypes.Unbonding: - resultCount[stakingtypes.Unbonding]++ - } - - return false - }, - ) - - app.Logger().Info( - "updated validator minimum commission rate for all existing validators", - stakingtypes.BondStatusBonded, resultCount[stakingtypes.Bonded], - stakingtypes.BondStatusUnbonded, resultCount[stakingtypes.Unbonded], - stakingtypes.BondStatusUnbonding, resultCount[stakingtypes.Unbonding], - ) - - stakingParams := app.stakingKeeper.GetParams(ctx) - stakingParams.MinCommissionRate = ValidatorMinimumCommission - app.stakingKeeper.SetParams(ctx, stakingParams) - - app.Logger().Info( - "updated x/staking params minimum commission rate", - "MinCommissionRate", stakingParams.MinCommissionRate, - ) -} - -// UpdateIncentiveParams modifies the earn rewards period for bkava to be 600K KAVA per year. -func UpdateIncentiveParams( - ctx sdk.Context, - app App, -) { - // incentiveParams := app.incentiveKeeper.GetParams(ctx) - - // bkava annualized rewards: 600K KAVA - // newAmount := sdkmath.LegacyNewDec(600_000). - // MulInt(kavaConversionFactor). - // QuoInt(secondsPerYear). - // TruncateInt() - - // for i := range incentiveParams.EarnRewardPeriods { - // if incentiveParams.EarnRewardPeriods[i].CollateralType != "bkava" { - // continue - // } - - // // Update rewards per second via index - // incentiveParams.EarnRewardPeriods[i].RewardsPerSecond = sdk.NewCoins( - // sdk.NewCoin("ukava", newAmount), - // ) - // } - - // app.incentiveKeeper.SetParams(ctx, incentiveParams) + app.upgradeKeeper.SetUpgradeHandler(v1.UpgradeName, + v1.CreateUpgradeHandler(app.mm, app.configurator, app.mintKeeper, app.paramsKeeper)) } diff --git a/app/upgrades/v1/upgrades.go b/app/upgrades/v1/upgrades.go new file mode 100644 index 00000000..f3aac681 --- /dev/null +++ b/app/upgrades/v1/upgrades.go @@ -0,0 +1,35 @@ +package v1 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" +) + +const ( + UpgradeName = "v1" +) + +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, + mk mintkeeper.Keeper, + pk paramskeeper.Keeper, +) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + subspace, _ := pk.GetSubspace(minttypes.ModuleName) + params := minttypes.DefaultParams() + subspace.Get(ctx, minttypes.KeyMintDenom, ¶ms.MintDenom) + subspace.Get(ctx, minttypes.KeyInflationRateChange, ¶ms.InflationRateChange) + subspace.Get(ctx, minttypes.KeyInflationMax, ¶ms.InflationMax) + subspace.Get(ctx, minttypes.KeyInflationMin, ¶ms.InflationMin) + subspace.Get(ctx, minttypes.KeyGoalBonded, ¶ms.GoalBonded) + subspace.Get(ctx, minttypes.KeyBlocksPerYear, ¶ms.BlocksPerYear) + ctx.Logger().Info("Mint module parameters", "params", params) + mk.SetParams(ctx, params) + return mm.RunMigrations(ctx, configurator, vm) + } +} diff --git a/go.mod b/go.mod index 9f4e4f35..47aa9948 100644 --- a/go.mod +++ b/go.mod @@ -199,6 +199,7 @@ require ( google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect @@ -212,7 +213,7 @@ replace ( // Use rocksdb 7.9.2 github.com/cometbft/cometbft-db => github.com/kava-labs/cometbft-db v0.7.0-rocksdb-v7.9.2-kava.1 // Use cosmos-sdk fork with backported fix for unsafe-reset-all, staking transfer events, and custom tally handler support - github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-0glabs.5 + github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-0glabs.7 // See https://github.com/cosmos/cosmos-sdk/pull/13093 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 // Use go-ethereum fork with precompiles diff --git a/go.sum b/go.sum index 227f3ecb..51b2b54c 100644 --- a/go.sum +++ b/go.sum @@ -206,6 +206,8 @@ github.com/0glabs/cometbft v0.34.27-0glabs.0 h1:jErty8aVtp2RiU/59QTEhUCi3xCoc67N github.com/0glabs/cometbft v0.34.27-0glabs.0/go.mod h1:BcCbhKv7ieM0KEddnYXvQZR+pZykTKReJJYf7YC7qhw= github.com/0glabs/cosmos-sdk v0.46.11-0glabs.5 h1:/7zqU8Az6n3UpKnypKQ92Yw8AgrE1v1AfatrL8elajs= github.com/0glabs/cosmos-sdk v0.46.11-0glabs.5/go.mod h1:jwgWoeAWxqMF5pZUZ4N+G4rD3q6oOLulq3/dGCFLEX4= +github.com/0glabs/cosmos-sdk v0.46.11-0glabs.7 h1:xX0ZeQu8yJCy+6wFf/PWjwvHt0dqEifhLmw2ydAXZ1A= +github.com/0glabs/cosmos-sdk v0.46.11-0glabs.7/go.mod h1:4uTpR8WwpNKawdsPj5uyUS8DvKilc2OyFKe4RBm4oso= github.com/0glabs/ethermint v0.21.0-0g.v2.0.2 h1:mFVOMra9lmeNk+CgL0UsBxqiXHx5JWVeiURJFgQmHzY= github.com/0glabs/ethermint v0.21.0-0g.v2.0.2/go.mod h1:KPLRino6lVDPV/cZCbQf7dZdR1nsVgptr0Htf6ZxZe8= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= @@ -1898,6 +1900,8 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=