diff --git a/app/app.go b/app/app.go index 357464a..b2bb5d6 100644 --- a/app/app.go +++ b/app/app.go @@ -8,8 +8,6 @@ import ( "os" "path/filepath" - // v0 "github.com/onomyprotocol/onex/app/upgrades/" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/grpc/tmservice" @@ -20,7 +18,6 @@ import ( "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" "github.com/cosmos/cosmos-sdk/simapp" - storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/version" @@ -120,15 +117,20 @@ import ( marketclient "github.com/pendulum-labs/market/x/market/client" marketkeeper "github.com/pendulum-labs/market/x/market/keeper" markettypes "github.com/pendulum-labs/market/x/market/types" + + "github.com/onomyprotocol/onex/app/upgrades" + v1_1_6 "github.com/onomyprotocol/onex/app/upgrades/v1.1.6" ) const ( AppName = "onex" - upgradeName = "v1.1.0" AccountAddressPrefix = "onomy" ) var ( + Upgrades = []upgrades.Upgrade{} + Forks = []upgrades.Fork{v1_1_6.Fork} + // DefaultNodeHome default home directories for the application daemon DefaultNodeHome string @@ -554,6 +556,7 @@ func New( consumertypes.ModuleName, ) app.MM.SetOrderEndBlockers( + upgradetypes.ModuleName, crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName, @@ -567,7 +570,6 @@ func New( authz.ModuleName, feegrant.ModuleName, paramstypes.ModuleName, - upgradetypes.ModuleName, vestingtypes.ModuleName, ibctransfertypes.ModuleName, ibchost.ModuleName, @@ -582,6 +584,7 @@ func New( // so that other modules that want to create or claim capabilities afterwards in InitChain // can do so safely. app.MM.SetOrderInitGenesis( + upgradetypes.ModuleName, capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, @@ -595,7 +598,6 @@ func New( authz.ModuleName, feegrant.ModuleName, paramstypes.ModuleName, - upgradetypes.ModuleName, vestingtypes.ModuleName, ibchost.ModuleName, ibctransfertypes.ModuleName, @@ -659,7 +661,8 @@ func New( app.SetBeginBlocker(app.BeginBlocker) app.SetEndBlocker(app.EndBlocker) - app.setupUpgradeHandlers(app.configurator) + app.setupUpgradeStoreLoaders() + app.setupUpgradeHandlers() if loadLatest { if err := app.LoadLatestVersion(); err != nil { @@ -679,6 +682,7 @@ func (app *App) Name() string { return app.BaseApp.Name() } // BeginBlocker application updates every begin block func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { + BeginBlockForks(ctx, app) return app.MM.BeginBlock(ctx, req) } @@ -918,29 +922,35 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino return paramsKeeper } -func (app *App) setupUpgradeHandlers(cfg module.Configurator) { - app.UpgradeKeeper.SetUpgradeHandler(upgradeName, func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { - return app.MM.RunMigrations(ctx, cfg, vm) - }) - +func (app *App) setupUpgradeStoreLoaders() { upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() if err != nil { - panic(fmt.Errorf("failed to read upgrade info from disk: %w", err)) + panic(fmt.Sprintf("failed to read upgrade info from disk %s", err)) } - // configure store loader that checks if version == upgradeHeight and applies store upgrades if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { return } - var storeUpgrades *storetypes.StoreUpgrades - - switch upgradeInfo.Name { - default: - // no store upgrades + for _, upgrade := range Upgrades { + if upgradeInfo.Name == upgrade.UpgradeName { + app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &upgrade.StoreUpgrades)) + } } +} - if storeUpgrades != nil { - app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, storeUpgrades)) +func (app *App) setupUpgradeHandlers() { + for _, upgrade := range Upgrades { + app.UpgradeKeeper.SetUpgradeHandler( + upgrade.UpgradeName, + upgrade.CreateUpgradeHandler( + app.MM, + app.configurator, + &upgrades.UpgradeKeepers{ + BankKeeper: app.BankKeeper, + MarketKeeper: app.MarketKeeper, + }, + ), + ) } } diff --git a/app/fork.go b/app/fork.go new file mode 100644 index 0000000..9b36742 --- /dev/null +++ b/app/fork.go @@ -0,0 +1,19 @@ +package app + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/onomyprotocol/onex/app/upgrades" +) + +// BeginBlockForks is intended to be ran in a chain upgrade. +func BeginBlockForks(ctx sdk.Context, app *App) { + for _, fork := range Forks { + if ctx.BlockHeight() == fork.UpgradeHeight { + fork.BeginForkLogic(ctx, &upgrades.ForkKeepers{ + BankKeeper: app.BankKeeper, + MarketKeeper: app.MarketKeeper, + }) + return + } + } +} diff --git a/app/proposals_whitelisting.go b/app/proposals_whitelisting.go index d793fa6..1e74b1d 100644 --- a/app/proposals_whitelisting.go +++ b/app/proposals_whitelisting.go @@ -70,6 +70,7 @@ var WhitelistedParams = map[paramChangeKey]struct{}{ // ccv params (note: some CCV params should not be configurable or require special coordination with the provider chain) {Subspace: consumertypes.ModuleName, Key: "ProviderRewardDenoms"}: {}, {Subspace: consumertypes.ModuleName, Key: "RewardDenoms"}: {}, + {Subspace: consumertypes.ModuleName, Key: "DistributionTransmissionChannel"}: {}, {Subspace: consumertypes.ModuleName, Key: "ConsumerRedistributionFraction"}: {}, {Subspace: consumertypes.ModuleName, Key: "BlocksPerDistributionTransmission"}: {}, {Subspace: consumertypes.ModuleName, Key: "TransferTimeoutPeriod"}: {}, diff --git a/app/upgrades/types.go b/app/upgrades/types.go new file mode 100644 index 0000000..8379495 --- /dev/null +++ b/app/upgrades/types.go @@ -0,0 +1,52 @@ +package upgrades + +import ( + store "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + marketkeeper "github.com/pendulum-labs/market/x/market/keeper" +) + +// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal +// must have written, in order for the state migration to go smoothly. +// An upgrade must implement this struct, and then set it in the app.go. +// The app.go will then define the handler. +type Upgrade struct { + // Upgrade version name, for the upgrade handler, e.g. `v7` + UpgradeName string + + // CreateUpgradeHandler defines the function that creates an upgrade handler + CreateUpgradeHandler func(*module.Manager, module.Configurator, *UpgradeKeepers) upgradetypes.UpgradeHandler + + // Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed. + StoreUpgrades store.StoreUpgrades +} + +type UpgradeKeepers struct { + // keepers + BankKeeper bankkeeper.Keeper + MarketKeeper marketkeeper.Keeper +} + +// Fork defines a struct containing the requisite fields for a non-software upgrade proposal +// Hard Fork at a given height to implement. +// There is one time code that can be added for the start of the Fork, in `BeginForkLogic`. +// Any other change in the code should be height-gated, if the goal is to have old and new binaries +// to be compatible prior to the upgrade height. +type Fork struct { + // Upgrade version name, for the upgrade handler, e.g. `v7` + UpgradeName string + // height the upgrade occurs at + UpgradeHeight int64 + + // Function that runs some custom state transition code at the beginning of a fork. + BeginForkLogic func(ctx sdk.Context, keepers *ForkKeepers) +} + +type ForkKeepers struct { + // keepers + BankKeeper bankkeeper.Keeper + MarketKeeper marketkeeper.Keeper +} diff --git a/app/upgrades/v1.1.6/constants.go b/app/upgrades/v1.1.6/constants.go new file mode 100644 index 0000000..2f6208f --- /dev/null +++ b/app/upgrades/v1.1.6/constants.go @@ -0,0 +1,17 @@ +package v1_1_6 + +import ( + "github.com/onomyprotocol/onex/app/upgrades" +) + +const ( + // UpgradeName defines the on-chain upgrades name. + UpgradeName = "v1_1_6" + UpgradeHeight = 389310 +) + +var Fork = upgrades.Fork{ + UpgradeName: UpgradeName, + UpgradeHeight: UpgradeHeight, + BeginForkLogic: RunForkLogic, +} diff --git a/app/upgrades/v1.1.6/fork.go b/app/upgrades/v1.1.6/fork.go new file mode 100644 index 0000000..beff378 --- /dev/null +++ b/app/upgrades/v1.1.6/fork.go @@ -0,0 +1,59 @@ +// Package v1_1_6 is contains chain fork of the corresponding version. +package v1_1_6 //nolint:revive,stylecheck // app version + +import ( + "github.com/onomyprotocol/onex/app/upgrades" + + sdk "github.com/cosmos/cosmos-sdk/types" + markettypes "github.com/pendulum-labs/market/x/market/types" +) + +func RunForkLogic( + ctx sdk.Context, + keepers *upgrades.ForkKeepers, +) { + ctx.Logger().Info("Starting fork...") + + // Deactivate all drops + drops := keepers.MarketKeeper.GetAllDrop(ctx) + for _, drop := range drops { + drop.Active = false + keepers.MarketKeeper.SetDrop(ctx, drop) + } + + // Set pool drops to zero and wipe leaders + pools := keepers.MarketKeeper.GetAllPool(ctx) + for _, pool := range pools { + pool.Drops = sdk.ZeroInt() + pool.Leaders = []*markettypes.Leader{} + + keepers.MarketKeeper.SetPool(ctx, pool) + } + + // Set member balances to zero + members := keepers.MarketKeeper.GetAllMember(ctx) + for _, member := range members { + member.Balance = sdk.ZeroInt() + keepers.MarketKeeper.SetMember(ctx, member) + } + + onexAmount, _ := sdk.NewIntFromString("94784650277373001264452") + + ibc1Amount, _ := sdk.NewIntFromString("998915079") + + ibc2Amount, _ := sdk.NewIntFromString("28966246179579930912901") + + ibc3Amount, _ := sdk.NewIntFromString("2952795016") + + onexCoin := sdk.NewCoin("aonex", onexAmount) + ibc1 := sdk.NewCoin("ibc/30EDC220372A2C3D0FC1D987E19062E35375DECD1001A5EFA44EB92FF59D1867", ibc1Amount) + ibc2 := sdk.NewCoin("ibc/5BDD8875CC2AF7BC842BE44236ACD576EA4F53C36347F74903B852060D6BF29A", ibc2Amount) + ibc3 := sdk.NewCoin("ibc/CCCBD7307FEB70B0CF7ADF8503F711F6741F41623D25BAD8CB736E03BE384264", ibc3Amount) + + reclaimCoins := sdk.NewCoins(onexCoin, ibc1, ibc2, ibc3) + reclaimer, _ := sdk.AccAddressFromBech32("onomy1yc0lg97cy5e80jyajtkz0zke2rr4734anugf9g") + + keepers.BankKeeper.SendCoinsFromModuleToAccount(ctx, markettypes.ModuleName, reclaimer, reclaimCoins) + + ctx.Logger().Info("Fork complete") +} diff --git a/go.mod b/go.mod index 1f83262..2e83411 100644 --- a/go.mod +++ b/go.mod @@ -157,7 +157,7 @@ replace ( github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.45.16-ics github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 - github.com/pendulum-labs/market => github.com/onomyprotocol/market v1.1.2-dev + github.com/pendulum-labs/market => github.com/onomyprotocol/market v1.1.4-dev github.com/tendermint/tendermint => github.com/graphprotocol/tendermint v0.34.28-fh.0 google.golang.org/grpc => google.golang.org/grpc v1.33.2 ) diff --git a/go.sum b/go.sum index a2b0123..df13b3a 100644 --- a/go.sum +++ b/go.sum @@ -2209,8 +2209,8 @@ github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:v github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/olekukonko/tablewriter v0.0.2/go.mod h1:rSAaSIOAGT9odnlyGlUfAJaoc5w2fSBUmeGDbRWPxyQ= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= -github.com/onomyprotocol/market v1.1.2-dev h1:uLCI2DMclMrKJ9YGi7RyOz5EmG8b7EvJoNwgqfwp5Ks= -github.com/onomyprotocol/market v1.1.2-dev/go.mod h1:OFWUMLq8cfNRQnc6WqReQYq+hNYRqoc2RmMvECSxdwk= +github.com/onomyprotocol/market v1.1.4-dev h1:fA1O3NAatEBx/YU2++2LcdBNRs+aX9N50rY/bWxmXfU= +github.com/onomyprotocol/market v1.1.4-dev/go.mod h1:OFWUMLq8cfNRQnc6WqReQYq+hNYRqoc2RmMvECSxdwk= github.com/onsi/ginkgo v0.0.0-20151202141238-7f8ab55aaf3b/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=