Skip to content

Commit

Permalink
Merge pull request #465 from neutron-org/fix/restore-burned-by-pfm-to…
Browse files Browse the repository at this point in the history
…kens

release: v3.0.1 handler to remove discrepancy between osmosis and neutron #NTRN-237
  • Loading branch information
pr0n00gler authored Mar 18, 2024
2 parents 63cb358 + dc15719 commit 70b661d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
6 changes: 4 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
v044 "github.com/neutron-org/neutron/v3/app/upgrades/v0.4.4"
v200 "github.com/neutron-org/neutron/v3/app/upgrades/v2.0.0"
v202 "github.com/neutron-org/neutron/v3/app/upgrades/v2.0.2"
v300 "github.com/neutron-org/neutron/v3/app/upgrades/v3.0.0"
v301 "github.com/neutron-org/neutron/v3/app/upgrades/v3.0.1"

"github.com/neutron-org/neutron/v3/x/cron"

Expand Down Expand Up @@ -183,7 +183,7 @@ const (
)

var (
Upgrades = []upgrades.Upgrade{v030.Upgrade, v044.Upgrade, v200.Upgrade, v202.Upgrade, v300.Upgrade}
Upgrades = []upgrades.Upgrade{v030.Upgrade, v044.Upgrade, v200.Upgrade, v202.Upgrade, v301.Upgrade}

// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
Expand Down Expand Up @@ -1134,6 +1134,8 @@ func (app *App) setupUpgradeHandlers() {
&upgrades.UpgradeKeepers{
AccountKeeper: app.AccountKeeper,
FeeBurnerKeeper: app.FeeBurnerKeeper,
BankKeeper: app.BankKeeper,
TransferKeeper: app.TransferKeeper.Keeper,
CronKeeper: app.CronKeeper,
IcqKeeper: app.InterchainQueriesKeeper,
TokenFactoryKeeper: app.TokenFactoryKeeper,
Expand Down
4 changes: 4 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
store "github.com/cosmos/cosmos-sdk/store/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"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
Expand All @@ -21,6 +22,7 @@ import (
tokenfactorykeeper "github.com/neutron-org/neutron/v3/x/tokenfactory/keeper"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
transferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
)

// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal
Expand All @@ -41,6 +43,8 @@ type Upgrade struct {
type UpgradeKeepers struct {
// keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
TransferKeeper transferkeeper.Keeper
IcqKeeper icqkeeper.Keeper
CronKeeper cronkeeper.Keeper
TokenFactoryKeeper *tokenfactorykeeper.Keeper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v300
package v301

import (
"cosmossdk.io/math"
Expand All @@ -12,10 +12,10 @@ import (

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v3.0.0"
UpgradeName = "v3.0.1"

AuctionParamsMaxBundleSize = 2
AuctionParamsFrontRunningProtection = true
AuctionParamsMaxBundleSize = 4
AuctionParamsFrontRunningProtection = false
)

var (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package v300
package v301

import (
"fmt"

"cosmossdk.io/math"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
transferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
consumerkeeper "github.com/cosmos/interchain-security/v4/x/ccv/consumer/keeper"
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
Expand All @@ -19,6 +26,10 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
)

// CreateUpgradeHandler is an upgrade major version 2 -> 3
// Upgrade v3.0.0 was applied on pion-1 chain and later renamed to v3.0.1
// Upgrade v3.0.1 is in a nutshell the same v3.0.0 but with additional storage migrations
// specially for neutron-1 chain - setICSParams and removeDiscrepancies
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
Expand All @@ -43,9 +54,18 @@ func CreateUpgradeHandler(
return vm, fmt.Errorf("failed to migrate ICS outstanding downtime: %w", err)
}

setICSParams(ctx, keepers.ConsumerKeeper)

recalculateSlashingMissedBlocksCounter(ctx, keepers)

if ctx.ChainID() == "neutron-1" {
if err := removeDiscrepancies(ctx, keepers.BankKeeper, keepers.TransferKeeper); err != nil {
return vm, fmt.Errorf("failed to remove discrepancy: %w", err)
}
}

ctx.Logger().Info(fmt.Sprintf("Migration {%s} applied", UpgradeName))

return vm, nil
}
}
Expand All @@ -68,6 +88,28 @@ func setAuctionParams(ctx sdk.Context, feeBurnerKeeper *feeburnerkeeper.Keeper,
return auctionKeeper.SetParams(ctx, auctionParams)
}

func setICSParams(ctx sdk.Context, consumerKeeper *consumerkeeper.Keeper) {
newParams := ccvtypes.NewParams(
consumerKeeper.GetEnabled(ctx),
consumerKeeper.GetBlocksPerDistributionTransmission(ctx),
consumerKeeper.GetDistributionTransmissionChannel(ctx),
consumerKeeper.GetProviderFeePoolAddrStr(ctx),
consumerKeeper.GetCCVTimeoutPeriod(ctx),
consumerKeeper.GetTransferTimeoutPeriod(ctx),
consumerKeeper.GetConsumerRedistributionFrac(ctx),
consumerKeeper.GetHistoricalEntries(ctx),
consumerKeeper.GetUnbondingPeriod(ctx),
consumerKeeper.GetSoftOptOutThreshold(ctx),
consumerKeeper.GetRewardDenoms(ctx),
consumerKeeper.GetProviderRewardDenoms(ctx),

// Currently it's an empty string, we need to set to default value
ccvtypes.DefaultRetryDelayPeriod,
)

consumerKeeper.SetParams(ctx, newParams)
}

// Sometime long ago we decreased SlashWindow to 36k on pion-1 testnet (the param is untouched on neutron-1 mainnet),
// from that time MissedBlockCounter is wrong
// We need to set to a proper value.
Expand Down Expand Up @@ -122,3 +164,40 @@ func migrateICSOutstandingDowntime(ctx sdk.Context, keepers *upgrades.UpgradeKee

return nil
}

// There is a discrepancy happens due to bug in PFM module.
// Check all disrepencies you can with a tool - https://github.com/strangelove-ventures/escrow-checker, only one found for `neutron-1`
// ```
// Discrepancy found!
// Counterparty Chain ID: osmosis-1
// Escrow Account Address: neutron1fp9wuhq58pz53wxvv3tnrxkw8m8s6swpf2fkv9
// Asset Base Denom: stuatom
// Asset IBC Denom: ibc/B7864B03E1B9FD4F049243E92ABD691586F682137037A9F3FCA5222815620B3C
// Escrow Balance: 10481
// Counterparty Total Supply: 2447077ibc/8FCFAF3AE6BA4C5BDFF85B41449FBACE547E2BAC23895E839230404FB0EC3837
// ^^^
// bug was already fixed in releases v2.0.3/v3.0.0
// the aim of the function is to remove the discrepancy
func removeDiscrepancies(ctx sdk.Context, bankKeeper bankkeeper.Keeper, transferKeeper transferkeeper.Keeper) error {
coin := sdk.Coin{
Denom: "ibc/B7864B03E1B9FD4F049243E92ABD691586F682137037A9F3FCA5222815620B3C",
Amount: math.NewInt(2436596),
}
EscrowAddress := sdk.MustAccAddressFromBech32("neutron1fp9wuhq58pz53wxvv3tnrxkw8m8s6swpf2fkv9")
coins := sdk.NewCoins(coin)

if err := bankKeeper.MintCoins(ctx, transfertypes.ModuleName, coins); err != nil {
return err
}

if err := bankKeeper.SendCoinsFromModuleToAccount(ctx, transfertypes.ModuleName, EscrowAddress, coins); err != nil {
return err
}

// For ibc-go v7+ you will also need to update the transfer module's store for the total escrow amounts.
currentTotalEscrow := transferKeeper.GetTotalEscrowForDenom(ctx, coin.GetDenom())
newTotalEscrow := currentTotalEscrow.Add(coin)
transferKeeper.SetTotalEscrowForDenom(ctx, newTotalEscrow)

return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v300_test
package v301_test

import (
"testing"
Expand All @@ -13,7 +13,7 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/stretchr/testify/require"

v300 "github.com/neutron-org/neutron/v3/app/upgrades/v3.0.0"
v300 "github.com/neutron-org/neutron/v3/app/upgrades/v3.0.1"
"github.com/neutron-org/neutron/v3/testutil"
)

Expand Down

0 comments on commit 70b661d

Please sign in to comment.