Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): Import Fork of Osmosis #252

Merged
merged 12 commits into from
Jun 27, 2023
52 changes: 5 additions & 47 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
Expand All @@ -48,6 +45,7 @@ import (

// upgrades
"github.com/classic-terra/core/v2/app/upgrades"
v1 "github.com/classic-terra/core/v2/app/upgrades/v1"
v2 "github.com/classic-terra/core/v2/app/upgrades/v2"
v3 "github.com/classic-terra/core/v2/app/upgrades/v3"
v4 "github.com/classic-terra/core/v2/app/upgrades/v4"
Expand All @@ -71,6 +69,9 @@ var (

// Upgrades defines upgrades to be applied to the network
Upgrades = []upgrades.Upgrade{v2.Upgrade, v3.Upgrade, v4.Upgrade}

// Forks defines forks to be applied to the network
Forks = v1.Forks
)

// Verify app interface at compile time
Expand Down Expand Up @@ -244,50 +245,7 @@ func (app *TerraApp) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *TerraApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.SwapDisableForkHeight { // Make min spread to one to disable swap
params := app.MarketKeeper.GetParams(ctx)
params.MinStabilitySpread = sdk.OneDec()
app.MarketKeeper.SetParams(ctx, params)

// Disable IBC Channels
channelIDs := []string{
"channel-1", // Osmosis
"channel-49", // Crescent
"channel-20", // Juno
}
for _, channelID := range channelIDs {
channel, found := app.IBCKeeper.ChannelKeeper.GetChannel(ctx, ibctransfertypes.PortID, channelID)
if !found {
panic(fmt.Sprintf("%s not found", channelID))
}

channel.State = ibcchanneltypes.CLOSED
app.IBCKeeper.ChannelKeeper.SetChannel(ctx, ibctransfertypes.PortID, channelID, channel)
}
}
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.SwapEnableForkHeight { // Re-enable IBCs
// Enable IBC Channels
channelIDs := []string{
"channel-1", // Osmosis
"channel-49", // Crescent
"channel-20", // Juno
}
for _, channelID := range channelIDs {
channel, found := app.IBCKeeper.ChannelKeeper.GetChannel(ctx, ibctransfertypes.PortID, channelID)
if !found {
panic(fmt.Sprintf("%s not found", channelID))
}

channel.State = ibcchanneltypes.OPEN
app.IBCKeeper.ChannelKeeper.SetChannel(ctx, ibctransfertypes.PortID, channelID, channel)
}
}

// trigger SetModuleVersionMap in upgrade keeper at the VersionMapEnableHeight
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.VersionMapEnableHeight {
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
}

BeginBlockForks(ctx, app)
return app.mm.BeginBlock(ctx, req)
}

Expand Down
15 changes: 15 additions & 0 deletions app/forks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// BeginBlockForks is intended to be ran in a chain upgrade.
func BeginBlockForks(ctx sdk.Context, app *TerraApp) {
for _, fork := range Forks {
if ctx.BlockHeight() == fork.UpgradeHeight {
fork.BeginForkLogic(ctx, &app.AppKeepers, app.mm)
return
}
}
}
50 changes: 50 additions & 0 deletions app/upgrades/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Classic Terra Upgrades

This folder contains sub-folders for every classic terra upgrade. (Both state
migrations, and hard forks) It also defines upgrade & hard fork structs,
that each upgrade implements. These then get included in the application
app.go to run the upgrade.

## Version History

* v1 - Initial release
* v1.0.0 - Disable swap
* v1.0.5 - Enable swap
* v1.1.0 - Set module version
* v2 - Set new FeeShare params
* v3 -
* v4 - Upgrade comos-sdk to v0.45.13 and migrate staking params

## Upgrade types

There are two upgrade types exposed, `Upgrade` and `Fork`. An `Upgrade`
defines an upgrade that is to be acted upon by state migrations from the
SDK `x/upgrade` module. A `Fork` defines a hard fork that changes some
logic at a block height. If the goal is to have a new binary be
compatible with the old binary prior to the upgrade height, as is the
case for all classic terra `Fork`s, then all logic changes must be
height-gated or in the `BeginForkLogic` code.

```go
type Upgrade struct {
// Upgrade version name, for the upgrade handler, e.g. `v7`
UpgradeName string
// Function that creates an upgrade handler
CreateUpgradeHandler func(mm *module.Manager, configurator module.Configurator, keepers *keepers.AppKeepers) upgradetypes.UpgradeHandler
// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades store.StoreUpgrades
}

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 ForkLogicFunc
}

// ForkLogicFunc defines a function that is run at the beginning of a fork.
type ForkLogicFunc = func(ctx sdk.Context, keppers *keepers.AppKeepers, mm *module.Manager)
```
18 changes: 18 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ type Upgrade struct {
// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades store.StoreUpgrades
}

// 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 ForkLogicFunc
}

// ForkLogicFunc defines a function that is run at the beginning of a fork.
type ForkLogicFunc = func(ctx sdk.Context, keppers *keepers.AppKeepers, mm *module.Manager)
29 changes: 29 additions & 0 deletions app/upgrades/v1/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v1

import (
"github.com/classic-terra/core/v2/app/upgrades"
core "github.com/classic-terra/core/v2/types"
)

var (

// UpgradeName defines the set of on-chain upgrade name for the classic terra v1 upgrade.
UpgradeNames = []string{"v1.0.0", "v1.0.5", "v1.1.0"}
// UpgradeHeight defines the set of block heights at which the classic terra v1 upgrade is
// triggered.
UpgradeHeights = []int64{core.SwapDisableForkHeight, core.SwapEnableForkHeight, core.VersionMapEnableHeight}
inon-man marked this conversation as resolved.
Show resolved Hide resolved
// ForkLogicFuncs defines the set of functions that are run at the beginning
ForkLogicFuncs = []upgrades.ForkLogicFunc{runForkLogic1_0_0, runForkLogic1_0_5, runForkLogic1_1_0}
)

var Forks = []upgrades.Fork{}

func init() {
for i := range UpgradeNames {
Forks = append(Forks, upgrades.Fork{
UpgradeName: UpgradeNames[i],
UpgradeHeight: UpgradeHeights[i],
BeginForkLogic: ForkLogicFuncs[i],
})
}
}
63 changes: 63 additions & 0 deletions app/upgrades/v1/forks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package v1

import (
"fmt"

"github.com/classic-terra/core/v2/app/keepers"
core "github.com/classic-terra/core/v2/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
)

func runForkLogic1_0_0(ctx sdk.Context, keppers *keepers.AppKeepers, _ *module.Manager) {
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.SwapDisableForkHeight { // Make min spread to one to disable swap
params := keppers.MarketKeeper.GetParams(ctx)
params.MinStabilitySpread = sdk.OneDec()
keppers.MarketKeeper.SetParams(ctx, params)

// Disable IBC Channels
channelIDs := []string{
"channel-1", // Osmosis
"channel-49", // Crescent
"channel-20", // Juno
}
for _, channelID := range channelIDs {
channel, found := keppers.IBCKeeper.ChannelKeeper.GetChannel(ctx, ibctransfertypes.PortID, channelID)
if !found {
panic(fmt.Sprintf("%s not found", channelID))
}

channel.State = ibcchanneltypes.CLOSED
keppers.IBCKeeper.ChannelKeeper.SetChannel(ctx, ibctransfertypes.PortID, channelID, channel)
}
}
}

func runForkLogic1_0_5(ctx sdk.Context, keppers *keepers.AppKeepers, _ *module.Manager) {
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.SwapEnableForkHeight { // Re-enable IBCs
// Enable IBC Channels
channelIDs := []string{
"channel-1", // Osmosis
"channel-49", // Crescent
"channel-20", // Juno
}
for _, channelID := range channelIDs {
channel, found := keppers.IBCKeeper.ChannelKeeper.GetChannel(ctx, ibctransfertypes.PortID, channelID)
if !found {
panic(fmt.Sprintf("%s not found", channelID))
}

channel.State = ibcchanneltypes.OPEN
keppers.IBCKeeper.ChannelKeeper.SetChannel(ctx, ibctransfertypes.PortID, channelID, channel)
}
}
}
inon-man marked this conversation as resolved.
Show resolved Hide resolved

func runForkLogic1_1_0(ctx sdk.Context, keppers *keepers.AppKeepers, mm *module.Manager) {
// trigger SetModuleVersionMap in upgrade keeper at the VersionMapEnableHeight
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.VersionMapEnableHeight {
keppers.UpgradeKeeper.SetModuleVersionMap(ctx, mm.GetVersionMap())
}
}