Skip to content

Commit

Permalink
Merge pull request #309 from terra-money/upgrade/v2.12
Browse files Browse the repository at this point in the history
Prepare for v2.12
  • Loading branch information
emidev98 authored Aug 8, 2024
2 parents cb4a461 + a43c0c8 commit ae6333e
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,5 @@ const (
Upgrade2_9 = "v2.9"
Upgrade2_10 = "v2.10"
Upgrade2_11 = "v2.11"
Upgrade2_12 = "v2.12"
)
6 changes: 4 additions & 2 deletions app/custom_queriers/custom_queriers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package custom_queriers
import (
"encoding/json"
"fmt"
"strings"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
alliancebindings "github.com/terra-money/alliance/x/alliance/bindings"
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"
tokenfactorybindings "github.com/terra-money/core/v2/x/tokenfactory/bindings"
tokenfactorykeeper "github.com/terra-money/core/v2/x/tokenfactory/keeper"
"strings"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func NewTerraAppKeepers(
)
keepers.TokenFactoryKeeper = tokenfactorykeeper.NewKeeper(
keys[tokenfactorytypes.StoreKey],
maccPerms,
keepers.AccountKeeper,
&keepers.BankKeeper,
keepers.DistrKeeper,
Expand Down
9 changes: 9 additions & 0 deletions app/upgrade_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
terraappconfig "github.com/terra-money/core/v2/app/config"
v2_10 "github.com/terra-money/core/v2/app/upgrades/v2.10"
v2_11 "github.com/terra-money/core/v2/app/upgrades/v2.11"
v2_12 "github.com/terra-money/core/v2/app/upgrades/v2.12"
v2_2_0 "github.com/terra-money/core/v2/app/upgrades/v2.2.0"
v2_3_0 "github.com/terra-money/core/v2/app/upgrades/v2.3.0"
v2_4 "github.com/terra-money/core/v2/app/upgrades/v2.4"
Expand Down Expand Up @@ -111,6 +112,14 @@ func (app *TerraApp) RegisterUpgradeHandlers() {
app.Keepers.TransferKeeper,
),
)
app.Keepers.UpgradeKeeper.SetUpgradeHandler(
terraappconfig.Upgrade2_12,
v2_12.CreateUpgradeHandler(
app.GetModuleManager(),
app.GetConfigurator(),
app.Keepers,
),
)
}

func (app *TerraApp) RegisterUpgradeStores() {
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v2.11/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func CreateUpgradeHandler(
// https://github.com/strangelove-ventures/escrow-checker/commit/adf0d867e2210c9ff0a27d8dff1c74ed0c8a00dc
updates := []EscrowUpdate{
{
EscrowAddress: sdk.AccAddress("terra1s308jav50mgct9x4f87u23w2tfe8q6qe45y7s4"),
EscrowAddress: sdk.MustAccAddressFromBech32("terra1s308jav50mgct9x4f87u23w2tfe8q6qe45y7s4"),
Assets: []sdk.Coin{sdk.NewCoin("ibc/815FC81EB6BD612206BD9A9909A02F7691D24A5B97CDFE2124B1BDCA9D4AB14C", sdk.NewInt(1000000000))},
},
}
Expand Down
204 changes: 204 additions & 0 deletions app/upgrades/v2.12/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package v2_12

import (
"fmt"

"cosmossdk.io/math"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/terra-money/core/v2/app/keepers"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

type EscrowUpdate struct {
EscrowAddress sdk.AccAddress
Assets []sdk.Coin
}

// To test this upgrade handler set the following address "terra1v0eee20gjl68fuk0chyrkch2z7suw2mhg3wkxf"
// on the variables below: addr and multisigAddr.
// then run: npm run test:chain:upgrade:v12
func CreateUpgradeHandler(
mm *module.Manager,
cfg module.Configurator,
k keepers.TerraAppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {

if err := updateValidatorsMinCommissionRate(ctx, k.StakingKeeper); err != nil {
return nil, err
}

var addr sdk.AccAddress
var multisigAddr sdk.AccAddress

if ctx.ChainID() == "phoenix-1" {
addr = sdk.MustAccAddressFromBech32("terra1885dgdvn5u8sjfaefvr39arssaxgqmd29ht0aa")
multisigAddr = sdk.MustAccAddressFromBech32("terra19yxffalxzu88n5lnj40trehpryemqsz7pnnwxp8v73hxz0rl2u9q5qqwh4")
} else {
addr = sdk.MustAccAddressFromBech32("terra1wd8tc98um0x6c9l46vhg00gudzgleefl6tvshd")
multisigAddr = sdk.MustAccAddressFromBech32("terra1xduqpf6aah0nftppuez7upl6curmykl3cxdek4h5wacw7hn0fr9sr029ze")
}

if err := burnTokensFromAccount(ctx, k.StakingKeeper, k.BankKeeper, k.DistrKeeper, k.AccountKeeper, addr); err != nil {
return nil, err
}
if err := burnTokensFromAccount(ctx, k.StakingKeeper, k.BankKeeper, k.DistrKeeper, k.AccountKeeper, multisigAddr); err != nil {
return nil, err
}

return mm.RunMigrations(ctx, cfg, vm)
}
}

func updateValidatorsMinCommissionRate(ctx sdk.Context, sk *stakingkeeper.Keeper) error {
// Update min commission rate for new / validators who are updating
stakingParams := sk.GetParams(ctx)
stakingParams.MinCommissionRate = sdk.MustNewDecFromStr("0.05")
if err := sk.SetParams(ctx, stakingParams); err != nil {
return err
}

// Update all validators to have a min commission rate of 5%
validators := sk.GetAllValidators(ctx)
for _, validator := range validators {
update := false
if validator.Commission.MaxRate.LT(sdk.MustNewDecFromStr("0.05")) {
validator.Commission.MaxRate = sdk.MustNewDecFromStr("0.05")
update = true
}
if validator.Commission.Rate.LT(sdk.MustNewDecFromStr("0.05")) {
// force update without checking the <24h restriction and the max update rate
validator.Commission.Rate = sdk.MustNewDecFromStr("0.05")
update = true
}
if update {
validator.Commission.UpdateTime = ctx.BlockTime()
if err := sk.Hooks().BeforeValidatorModified(ctx, validator.GetOperator()); err != nil {
return err
}
sk.SetValidator(ctx, validator)
}
}
return nil
}

func burnTokensFromAccount(ctx sdk.Context, sk *stakingkeeper.Keeper, bk bankkeeper.Keeper, dk distributionkeeper.Keeper, ak accountkeeper.AccountKeeper, addr sdk.AccAddress) error {
acc := ak.GetAccount(ctx, addr)
if acc == nil {
return fmt.Errorf("account %s not found", addr)
}
// Iterate delegations and unbond all shares
// burning the coins immediately
bondDenom := sk.GetParams(ctx).BondDenom
var err error
sk.IterateDelegatorDelegations(ctx, addr, func(d stakingtypes.Delegation) (stop bool) {
var valAddr sdk.ValAddress
valAddr, err = sdk.ValAddressFromBech32(d.ValidatorAddress)
if err != nil {
return true
}

// Withdraw delegation rewards first
_, err = dk.WithdrawDelegationRewards(ctx, addr, valAddr)
if err != nil {
return true
}
// Use this method without adding unbonding to the unbondings queue
// because it's not necessary to wait for the unbonding period
var unbondedAmount math.Int
unbondedAmount, err = sk.Unbond(ctx, addr, valAddr, d.Shares)
if err != nil {
return true
}

// After unbonding, burn the coins depending on the validator's status
validator := sk.Validator(ctx, valAddr)
if validator.IsBonded() {
if err = bk.BurnCoins(ctx, stakingtypes.BondedPoolName, sdk.NewCoins(sdk.NewCoin(bondDenom, unbondedAmount))); err != nil {
return true
}
} else {
if err = bk.BurnCoins(ctx, stakingtypes.NotBondedPoolName, sdk.NewCoins(sdk.NewCoin(bondDenom, unbondedAmount))); err != nil {
return true
}
}

return false
})
if err != nil {
return err
}

// Given one of the states can be undelegating, we need to iterate over all unbonding delegations
// and remove them manually to ensure that the undelegated coins are burned.
sk.IterateDelegatorUnbondingDelegations(ctx, addr, func(ubd stakingtypes.UnbondingDelegation) (stop bool) {
for i := 0; i < len(ubd.Entries); i++ {
entry := ubd.Entries[i]
ubd.RemoveEntry(int64(i))
i--
sk.DeleteUnbondingIndex(ctx, entry.UnbondingId)

// track undelegation only when remaining or truncated shares are non-zero
if !entry.Balance.IsZero() {
amt := sdk.NewCoin(bondDenom, entry.Balance)
if err = bk.BurnCoins(
ctx, stakingtypes.NotBondedPoolName, sdk.NewCoins(amt),
); err != nil {
return true
}
}
}
sk.RemoveUnbondingDelegation(ctx, ubd)
return false
})
if err != nil {
return err
}

// Redelegations are two queues but no coins are custodied in any "redelegations_pool",
// so we can just iterate over all redelegations and remove the indices to prevent issues.
sk.IterateDelegatorRedelegations(ctx, addr, func(red stakingtypes.Redelegation) (stop bool) {
for i := 0; i < len(red.Entries); i++ {
entry := red.Entries[i]
red.RemoveEntry(int64(i))
i--
sk.DeleteUnbondingIndex(ctx, entry.UnbondingId)
}
sk.RemoveRedelegation(ctx, red)
return false
})

// Set account back to a base account before burning to vest everything
switch vestingAcc := acc.(type) {
case *types.ContinuousVestingAccount:
ak.SetAccount(ctx, vestingAcc.BaseVestingAccount)
case *types.DelayedVestingAccount:
ak.SetAccount(ctx, vestingAcc.BaseVestingAccount)
case *types.PeriodicVestingAccount:
ak.SetAccount(ctx, vestingAcc.BaseVestingAccount)
default:
// do nothing
}

// Burn all coins in the addr
bk.IterateAccountBalances(ctx, addr, func(balance sdk.Coin) bool {
err = bk.SendCoinsFromAccountToModule(ctx, addr, stakingtypes.NotBondedPoolName, sdk.NewCoins(balance))
if err != nil {
return true
}
err = bk.BurnCoins(ctx, stakingtypes.NotBondedPoolName, sdk.NewCoins(balance))
if err != nil {
return true
}
return false
})
return err
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d
cosmossdk.io/tools/rosetta v0.2.1
github.com/CosmWasm/wasmd v0.45.0
github.com/CosmWasm/wasmvm v1.5.2
github.com/CosmWasm/wasmvm v1.5.3
github.com/cometbft/cometbft v0.37.4
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.4
Expand All @@ -18,7 +18,7 @@ require (
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.3-0.20240228213828-cce7f56d000b
github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.0.0
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79
github.com/cosmos/ibc-go/v7 v7.3.1
github.com/cosmos/ibc-go/v7 v7.4.1
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
Expand Down Expand Up @@ -192,7 +192,6 @@ require (

replace (
github.com/cosmos/cosmos-sdk => github.com/terra-money/cosmos-sdk v0.47.10-terra.0
github.com/cosmos/ibc-go/v7 => github.com/terra-money/ibc-go/v7 v7.3.1-terra.0
github.com/cosmos/ledger-cosmos-go => github.com/terra-money/ledger-terra-go v0.11.2
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRr
github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4=
github.com/CosmWasm/wasmd v0.45.0 h1:9zBqrturKJwC2kVsfHvbrA++EN0PS7UTXCffCGbg6JI=
github.com/CosmWasm/wasmd v0.45.0/go.mod h1:RnSAiqbNIZu4QhO+0pd7qGZgnYAMBPGmXpzTADag944=
github.com/CosmWasm/wasmvm v1.5.2 h1:+pKB1Mz9GZVt1vadxB+EDdD1FOz3dMNjIKq/58/lrag=
github.com/CosmWasm/wasmvm v1.5.2/go.mod h1:Q0bSEtlktzh7W2hhEaifrFp1Erx11ckQZmjq8FLCyys=
github.com/CosmWasm/wasmvm v1.5.3 h1:wcmkey/WkTGwCTHGBD+fRS3cbhhipR9q34kbCZUQSsc=
github.com/CosmWasm/wasmvm v1.5.3/go.mod h1:Q0bSEtlktzh7W2hhEaifrFp1Erx11ckQZmjq8FLCyys=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.5.0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
Expand Down Expand Up @@ -408,6 +408,8 @@ github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.0.0 h1:mMHedP3Q+mz5gpOWNz0P+X
github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.0.0/go.mod h1:/P6l2bWo2AR3rrsfs0DHuFZO3Imzb93sxFD3ihrIgw4=
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79 h1:pCxyhIxgWTabAQC5UerkITraHG3SwajdLKKMCFDWCv4=
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79/go.mod h1:JwHFbo1oX/ht4fPpnPvmhZr+dCkYK1Vihw+vZE9umR4=
github.com/cosmos/ibc-go/v7 v7.4.1 h1:95hR5Mdgk2/Z6Ynsq537BOU8LJAOsHR5g0N0ffhFLYg=
github.com/cosmos/ibc-go/v7 v7.4.1/go.mod h1:L/KaEhzV5TGUCTfGysVgMBQtl5Dm7hHitfpk+GIeoAo=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0=
github.com/cosmos/ledger-go v0.9.2/go.mod h1:oZJ2hHAZROdlHiwTg4t7kP+GKIIkBT+o6c9QWFanOyI=
Expand Down Expand Up @@ -1138,8 +1140,6 @@ github.com/terra-money/alliance v0.3.6 h1:FWfix+mKcCrXvdk29MgfXGj0JThOsBxzK81OiS
github.com/terra-money/alliance v0.3.6/go.mod h1:gyenuDQEwyN6mfiOEkaRBaokgk9ryBeU3eCAiZpVKZg=
github.com/terra-money/cosmos-sdk v0.47.10-terra.0 h1:vpod9zXeBp8S8JfP0++YzwCvCEMkJnz3qRmz0pciEQI=
github.com/terra-money/cosmos-sdk v0.47.10-terra.0/go.mod h1:UWpgWkhcsBIATS68uUC0del7IiBN4hPv/vqg8Zz23uw=
github.com/terra-money/ibc-go/v7 v7.3.1-terra.0 h1:CF+iicqyI4BJsW2zjUrUrTxRRrPWFZC30VqvlRyVl28=
github.com/terra-money/ibc-go/v7 v7.3.1-terra.0/go.mod h1:wvx4pPBofe5ZdMNV3OFRxSI4auEP5Qfqf8JXLLNV04g=
github.com/terra-money/ledger-terra-go v0.11.2 h1:BVXZl+OhJOri6vFNjjVaTabRLApw9MuG7mxWL4V718c=
github.com/terra-money/ledger-terra-go v0.11.2/go.mod h1:ClJ2XMj1ptcnONzKH+GhVPi7Y8pXIT+UzJ0TNt0tfZE=
github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg=
Expand Down
1 change: 1 addition & 0 deletions integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test:init" : "bash src/setup/init-test-framework.sh",
"test:relayer": "bash src/setup/relayer/init-relayer.sh",
"test:chain:upgrade" : "bash src/setup/chain-upgrade/chain-upgrade.sh",
"test:chain:upgrade:v12" : "bash src/setup/chain-upgrade/upgrade-simulation-12.sh",
"test:start" : "jest --runInBand --detectOpenHandles",
"start": "npm run test:init && npm run test:relayer && npm run test:start",
"test:clean": "rm -rf src/test-data chain-upgrade-data && pkill terrad && pkill terrad && pkill relayer"
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/setup/chain-upgrade/chain-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ fi
# init genesis
$OLD_BINARY init test --home $CHAIN_HOME --chain-id=$CHAIN_ID
echo $VAL_MNEMONIC_1 | $OLD_BINARY keys add val1 --home $CHAIN_HOME --recover --keyring-backend=test
VAL_ADDR_1=$($OLD_BINARY keys list emi --output=json | jq .[0].address -r)
VAL_ADDR_1=$($OLD_BINARY keys list val1 --output=json | jq .[0].address -r)

echo $WALLET_MNEMONIC_1 | $OLD_BINARY keys add wallet1 --home $CHAIN_HOME --recover --keyring-backend=test
WALLET_ADDR_1=$($OLD_BINARY keys list emi --output=json | jq .[0].address -r)
WALLET_ADDR_1=$($OLD_BINARY keys list wallet1 --output=json | jq .[0].address -r)

$OLD_BINARY genesis add-genesis-account $($OLD_BINARY --home $CHAIN_HOME keys show val1 --keyring-backend test -a) 100000000000uluna --home $CHAIN_HOME
$OLD_BINARY genesis gentx val1 1000000000uluna --home $CHAIN_HOME --chain-id $CHAIN_ID --keyring-backend test
Expand Down
Loading

0 comments on commit ae6333e

Please sign in to comment.