Skip to content

Commit

Permalink
Merge pull request onomyprotocol#169 from decentrio/fix
Browse files Browse the repository at this point in the history
Fix: remove invalid unbonding ids
  • Loading branch information
vuong177 authored Aug 4, 2024
2 parents b547aec + efd6d89 commit 66de4c1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import (
ibcproviderkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"

"github.com/onomyprotocol/onomy/app/upgrades"
v1_0_1 "github.com/onomyprotocol/onomy/app/upgrades/v1.0.1"
v1_0_3 "github.com/onomyprotocol/onomy/app/upgrades/v1.0.3"
v1_0_3_4 "github.com/onomyprotocol/onomy/app/upgrades/v1.0.3.4"
Expand All @@ -108,6 +109,7 @@ import (
v1_1_2 "github.com/onomyprotocol/onomy/app/upgrades/v1.1.2"
v1_1_4 "github.com/onomyprotocol/onomy/app/upgrades/v1.1.4"
v1_1_5 "github.com/onomyprotocol/onomy/app/upgrades/v1.1.5"
v1_1_5_fix "github.com/onomyprotocol/onomy/app/upgrades/v1.1.5-fix"
"github.com/onomyprotocol/onomy/docs"
"github.com/onomyprotocol/onomy/x/dao"
daoclient "github.com/onomyprotocol/onomy/x/dao/client"
Expand Down Expand Up @@ -192,6 +194,8 @@ var (
// provider chain note: the fee-pool is allowed to receive tokens
authtypes.FeeCollectorName: true,
}

Forks = []upgrades.Fork{}
)

var (
Expand Down Expand Up @@ -619,6 +623,7 @@ func New( // nolint:funlen // app new cosmos func
app.SetEndBlocker(app.EndBlocker)

app.setupUpgradeHandlers()
app.SetupForkLogic(v1_1_5_fix.CreateFork(&app.StakingKeeper, &app.ProviderKeeper, keys[providertypes.StoreKey]))

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
Expand All @@ -641,6 +646,11 @@ func (app OnomyApp) GetBaseApp() *baseapp.BaseApp { return app.BaseApp }

// BeginBlocker application updates every begin block.
func (app *OnomyApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
for _, fork := range Forks {
if ctx.BlockHeight() == fork.UpgradeHeight {
fork.BeginForkLogic(ctx)
}
}
return app.mm.BeginBlock(ctx, req)
}

Expand Down Expand Up @@ -738,6 +748,10 @@ func (app *OnomyApp) GetSubspace(moduleName string) paramstypes.Subspace {
return subspace
}

func (app *OnomyApp) SetupForkLogic(fork upgrades.Fork) {
Forks = append(Forks, fork)
}

// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *OnomyApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
Expand Down
20 changes: 20 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package upgrades

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

// 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)
}
10 changes: 10 additions & 0 deletions app/upgrades/v1.1.5-fix/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v1_1_5_fix

const (
// Name is migration name.
Name = "v1.1.5-fix"

// UpgradeHeight defines the block height at which the Osmosis v3 upgrade is
// triggered.
UpgradeHeight = 8_928_295
)
89 changes: 89 additions & 0 deletions app/upgrades/v1.1.5-fix/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Package v1_1_5 is contains chain upgrade of the corresponding version.
package v1_1_5_fix //nolint:revive,stylecheck // app version

import (
"fmt"
"slices"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
ibcproviderkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
ibcprovidertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
"github.com/onomyprotocol/onomy/app/upgrades"
)

var targetIds = []uint64{uint64(3372), uint64(3374), uint64(3373)}

func CreateFork(
sk *stakingkeeper.Keeper,
pk *ibcproviderkeeper.Keeper,
providerStoreKey sdk.StoreKey,
) upgrades.Fork {
forkLogic := func(ctx sdk.Context) {
for _, id := range targetIds {
var consumerChainIDS []string

for _, chain := range pk.GetAllConsumerChains(ctx) {
consumerChainIDS = append(consumerChainIDS, chain.ChainId)
}

if len(consumerChainIDS) == 0 {
break
}
// Add to indexes
for _, consumerChainID := range consumerChainIDS {
ubdOpIds := pk.GetAllUnbondingOpIndexes(ctx, consumerChainID)
for _, ubdIds := range ubdOpIds {
newIds := []uint64{}

for _, ubdId := range ubdIds.UnbondingOpIds {
if ubdId == id {
continue
}

newIds = append(newIds, ubdId)
}

// filter out invalid ID
pk.SetUnbondingOpIndex(ctx, consumerChainID, ubdIds.VscId, newIds)
}
}

// remove ubd entries
_, found := pk.GetUnbondingOp(ctx, id)
if found {
pk.DeleteUnbondingOp(ctx, id)
}
}

// clear invalid mature ubd entries
ids := []uint64{}
for _, id := range pk.GetMaturedUnbondingOps(ctx) {
if slices.Contains(targetIds, id) {
continue
}

ids = append(ids, id)
}

maturedOps := ccv.MaturedUnbondingOps{
Ids: ids,
}
bz, err := maturedOps.Marshal()
if err != nil {
// An error here would indicate something is very wrong,
// maturedOps is instantiated in this method and should be able to be marshaled.
panic(fmt.Sprintf("failed to marshal matured unbonding operations: %s", err))
}

// update mature ubd ids
store := ctx.KVStore(providerStoreKey)
store.Set(ibcprovidertypes.MaturedUnbondingOpsKey(), bz)
}
return upgrades.Fork{
UpgradeName: Name,
UpgradeHeight: UpgradeHeight,
BeginForkLogic: forkLogic,
}
}

0 comments on commit 66de4c1

Please sign in to comment.