From 0e46b4ba6178a53a464f88994582dfc819bf1c79 Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Fri, 23 Feb 2024 20:54:59 +0700 Subject: [PATCH] refactor: change logic migration acc (#326) * docs: update change log * refactor: change logic migration acc * chore: update v4.1.0 to v4.1.1 * test: update test --- CHANGELOG.md | 56 +------------- app/app.go | 2 +- app/upgrades/{v4_1_0 => v4_1_1}/constants.go | 2 +- .../{v4_1_0 => v4_1_1}/mainnet_account.go | 11 +++ app/upgrades/{v4_1_0 => v4_1_1}/upgrades.go | 74 ++++++++----------- .../{v4_1_0 => v4_1_1}/upgrades_test.go | 67 ++++++++--------- scripts/upgrade_test.sh | 2 +- 7 files changed, 77 insertions(+), 137 deletions(-) rename app/upgrades/{v4_1_0 => v4_1_1}/constants.go (87%) rename app/upgrades/{v4_1_0 => v4_1_1}/mainnet_account.go (89%) rename app/upgrades/{v4_1_0 => v4_1_1}/upgrades.go (75%) rename app/upgrades/{v4_1_0 => v4_1_1}/upgrades_test.go (54%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cc3897e..d7d75dfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,58 +1,6 @@ - -## [v4.1.x-testnet-rc4](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v4.1.x-testnet-rc3...v4.1.x-testnet-rc4) (2024-01-30) - -### Feat - -* add fork fix burning module permissions - - - -## [v4.1.x-testnet-rc3](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v4.1.0-testnet-rc2...v4.1.x-testnet-rc3) (2024-01-30) - -### Chore - -* add debugger - -### Feat - -* add testnet handler upgrade v4.1.1 -* add swagger client -* gen-swagger scripts - -### Fix - -* miss burning module permissions -* gen proto with cosmos sdk -* gen swagger scripts with ns - -### Lint - -* add space between comment and text - - - -## [v4.1.0-testnet-rc2](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v4.1.0-testnet-rc1...v4.1.0-testnet-rc2) (2024-01-19) - -### Refactor - -* update go.mod - - - -## [v4.1.0-testnet-rc1](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v4.1.0-testnet...v4.1.0-testnet-rc1) (2024-01-19) - -### Refactor - -* go lint - -### Test - -* remove cmd_test - - - -## [v4.1.0-testnet](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v3.0.4...v4.1.0-testnet) (2024-01-19) + +## [v4.1.0](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v3.0.4...v4.1.0) (2024-02-16) ### Chore diff --git a/app/app.go b/app/app.go index 6a15fb71..f9db889a 100644 --- a/app/app.go +++ b/app/app.go @@ -157,7 +157,7 @@ import ( "github.com/rakyll/statik/fs" v3_0_2 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v3_0_2" - v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_0" + v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_1" // unnamed import of statik for swagger UI support _ "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/client/docs/statik" diff --git a/app/upgrades/v4_1_0/constants.go b/app/upgrades/v4_1_1/constants.go similarity index 87% rename from app/upgrades/v4_1_0/constants.go rename to app/upgrades/v4_1_1/constants.go index 18b13a49..b3b72b4a 100644 --- a/app/upgrades/v4_1_0/constants.go +++ b/app/upgrades/v4_1_1/constants.go @@ -3,7 +3,7 @@ package v4 // UpgradeName defines the on-chain upgrade name for the Migaloo v4.1.0 upgrade. // this upgrade includes the fix for pfm const ( - UpgradeName = "v4.1.0" + UpgradeName = "v4.1.1" NotionalMultisigVestingAccount = "migaloo1alga5e8vr6ccr9yrg0kgxevpt5xgmgrvqgujs6" NewNotionalMultisigAccount = "migaloo1tx6f2hetpd9uhveja26kr074496hzk2zzqhsh0" ) diff --git a/app/upgrades/v4_1_0/mainnet_account.go b/app/upgrades/v4_1_1/mainnet_account.go similarity index 89% rename from app/upgrades/v4_1_0/mainnet_account.go rename to app/upgrades/v4_1_1/mainnet_account.go index d629e4cb..ea258d76 100644 --- a/app/upgrades/v4_1_0/mainnet_account.go +++ b/app/upgrades/v4_1_1/mainnet_account.go @@ -13,6 +13,11 @@ import ( banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" ) +const ( + TestDenom = "test" + TestAmount = 1000000 +) + func CreateMainnetVestingAccount(ctx sdk.Context, bankKeeper bankKeeper.Keeper, accountKeeper authkeeper.AccountKeeper, @@ -32,6 +37,12 @@ func CreateMainnetVestingAccount(ctx sdk.Context, panic(err) } + err = banktestutil.FundAccount(bankKeeper, ctx, acc.BaseAccount.GetAddress(), + sdk.NewCoins(sdk.NewCoin(TestDenom, sdk.NewInt(TestAmount)))) + if err != nil { + panic(err) + } + accountKeeper.SetAccount(ctx, &acc) return acc, vesting } diff --git a/app/upgrades/v4_1_0/upgrades.go b/app/upgrades/v4_1_1/upgrades.go similarity index 75% rename from app/upgrades/v4_1_0/upgrades.go rename to app/upgrades/v4_1_1/upgrades.go index 1a155f89..195c3f0b 100644 --- a/app/upgrades/v4_1_0/upgrades.go +++ b/app/upgrades/v4_1_1/upgrades.go @@ -4,19 +4,19 @@ import ( "fmt" "time" + vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" + "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper" paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" 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" icacontrollerkeeper "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/keeper" icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types" @@ -61,11 +61,10 @@ func CreateUpgradeHandler( } } -// migrateMultisigVesting moves the vested and reward token from the ContinuousVestingAccount -> the new multisig vesting account. +// migrateMultisigVesting moves the unvested from the ContinuousVestingAccount -> the new ContinuousVestingAccount // - Retrieves the old multisig vesting account // - Instantly finish all redelegations, then unbond all tokens. -// - Transfer all tokens vested and reward tokens to the new multisig account (including the previously held balance) -// - Delegates the vesting coins to the top 10 validators +// - Transfer unvested token to the new multisign ContinuousVestingAccount func migrateMultisigVesting(ctx sdk.Context, stakingKeeper stakingKeeper.Keeper, bankKeeper bankKeeper.Keeper, @@ -83,11 +82,13 @@ func migrateMultisigVesting(ctx sdk.Context, return } // process migrate - processMigrateMultisig(ctx, stakingKeeper, bankKeeper, currentAddr, newAddr, currentVestingAcc) + processMigrateMultisig(ctx, stakingKeeper, bankKeeper, accountKeeper, currentAddr, newAddr, currentVestingAcc) } func processMigrateMultisig(ctx sdk.Context, stakingKeeper stakingKeeper.Keeper, - bankKeeper bankKeeper.Keeper, currentAddr, newAddr sdk.AccAddress, + bankKeeper bankKeeper.Keeper, + accountKeeper authkeeper.AccountKeeper, + currentAddr, newAddr sdk.AccAddress, oldAcc *vestingtypes.ContinuousVestingAccount, ) { redelegated, err := completeAllRedelegations(ctx, ctx.BlockTime(), stakingKeeper, currentAddr) @@ -103,20 +104,30 @@ func processMigrateMultisig(ctx sdk.Context, stakingKeeper stakingKeeper.Keeper, fmt.Printf("currentAddr Instant Redelegations: %s\n", redelegated) fmt.Printf("currentAddr Instant Unbonding: %s\n", unbonded) - // delegate vesting coin to validator - err = delegateToValidator(ctx, stakingKeeper, currentAddr, oldAcc.GetVestingCoins(ctx.BlockTime())[0].Amount) - if err != nil { + // get vesting balance + vestingCoin := oldAcc.GetVestingCoins(ctx.BlockTime()) + fmt.Printf("Total vesting balance %v\n", vestingCoin) + if vestingCoin.Empty() { panic(err) } - // get vested + reward balance - for _, coin := range bankKeeper.GetAllBalances(ctx, currentAddr) { - fmt.Printf("demom %s, total balance send to new multisig addr: %v\n", coin.Denom, coin.Amount) - // send vested + reward balance no newAddr - err = bankKeeper.SendCoins(ctx, currentAddr, newAddr, sdk.NewCoins(sdk.NewCoin(coin.Denom, coin.Amount))) - if err != nil { - panic(err) - } + // setup NewContinuousVestingAccount + newAccount := authtypes.NewBaseAccountWithAddress(newAddr) + newVestingAcc := vestingtypes.NewContinuousVestingAccount(newAccount, vestingCoin, + ctx.BlockTime().Unix(), oldAcc.EndTime) + accountKeeper.SetAccount(ctx, newVestingAcc) + + // end current ContinuousVestingAccount + oldAcc.OriginalVesting = oldAcc.OriginalVesting.Sub(vestingCoin[0]) + oldAcc.DelegatedFree = sdk.NewCoins() + oldAcc.DelegatedVesting = sdk.NewCoins() + oldAcc.EndTime = ctx.BlockTime().Unix() + accountKeeper.SetAccount(ctx, oldAcc) + + // move vesting coin to new ContinuousVestingAccount + err = bankKeeper.SendCoins(ctx, currentAddr, newAddr, vestingCoin) + if err != nil { + panic(err) } } @@ -191,30 +202,3 @@ func unbondAllAndFinish(ctx sdk.Context, now time.Time, return unbondedAmt, nil } - -// delegate to top 10 validator -func delegateToValidator(ctx sdk.Context, - stakingKeeper stakingKeeper.Keeper, - accAddr sdk.AccAddress, - totalVestingBalance math.Int, -) error { - listValidator := stakingKeeper.GetBondedValidatorsByPower(ctx) - totalValidatorDelegate := math.Min(10, len(listValidator)) - balanceDelegate := totalVestingBalance.Quo(math.NewInt(int64(totalValidatorDelegate))) - totalBalanceDelegate := math.ZeroInt() - for i, validator := range listValidator { - if i >= totalValidatorDelegate { - break - } - if i == totalValidatorDelegate-1 { - balanceDelegate = totalVestingBalance.Sub(totalBalanceDelegate) - } - newShare, err := stakingKeeper.Delegate(ctx, accAddr, balanceDelegate, stakingtypes.Unbonded, validator, true) - if err != nil { - return err - } - fmt.Printf("delegate %v to validator %v, newShare: %v\n", balanceDelegate, validator.OperatorAddress, newShare) - totalBalanceDelegate = totalBalanceDelegate.Add(balanceDelegate) - } - return nil -} diff --git a/app/upgrades/v4_1_0/upgrades_test.go b/app/upgrades/v4_1_1/upgrades_test.go similarity index 54% rename from app/upgrades/v4_1_0/upgrades_test.go rename to app/upgrades/v4_1_1/upgrades_test.go index e27ca594..5179a324 100644 --- a/app/upgrades/v4_1_0/upgrades_test.go +++ b/app/upgrades/v4_1_1/upgrades_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/params" - v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_0" + v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_1" sdk "github.com/cosmos/cosmos-sdk/types" vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -32,34 +32,15 @@ func (s *UpgradeTestSuite) TestUpgrade() { accVestingBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, vestingAddr) fmt.Printf("Acc vesting bal: %s\n", accVestingBalance) - // create many validators to confirm the unbonding code works newVal1 := s.SetupValidator(stakingtypes.Bonded) newVal2 := s.SetupValidator(stakingtypes.Bonded) newVal3 := s.SetupValidator(stakingtypes.Bonded) - newVal4 := s.SetupValidator(stakingtypes.Bonded) - newVal5 := s.SetupValidator(stakingtypes.Bonded) - newVal6 := s.SetupValidator(stakingtypes.Bonded) - newVal7 := s.SetupValidator(stakingtypes.Bonded) - newVal8 := s.SetupValidator(stakingtypes.Bonded) - newVal9 := s.SetupValidator(stakingtypes.Bonded) - newVal10 := s.SetupValidator(stakingtypes.Bonded) - newVal11 := s.SetupValidator(stakingtypes.Bonded) - newVal12 := s.SetupValidator(stakingtypes.Bonded) // Delegate tokens of the vesting multisig account s.StakingHelper.Delegate(vestingAddr, newVal1, sdk.NewInt(300)) s.StakingHelper.Delegate(vestingAddr, newVal2, sdk.NewInt(300)) s.StakingHelper.Delegate(vestingAddr, newVal3, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal4, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal5, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal6, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal7, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal8, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal9, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal10, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal11, sdk.NewInt(300)) - s.StakingHelper.Delegate(vestingAddr, newVal12, sdk.NewInt(300)) // Undelegate part of the tokens from val2 (test instant unbonding on undelegation started before upgrade) s.StakingHelper.Undelegate(vestingAddr, newVal3, sdk.NewInt(10), true) @@ -68,8 +49,8 @@ func (s *UpgradeTestSuite) TestUpgrade() { _, err := s.App.StakingKeeper.BeginRedelegation(s.Ctx, vestingAddr, newVal2, newVal3, sdk.NewDec(1)) s.Require().NoError(err) - // Confirm delegated to 12 validators - s.Require().Equal(12, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) + // Confirm delegated to 3 validators + s.Require().Equal(3, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) // == UPGRADE == upgradeHeight := int64(5) @@ -81,25 +62,41 @@ func (s *UpgradeTestSuite) TestUpgrade() { // VERIFY MULTISIGN MIGRATION accAfter := s.App.AccountKeeper.GetAccount(s.Ctx, vestingAddr) - _, ok := accAfter.(*vestingtypes.ContinuousVestingAccount) + accAfterVestingAccount, ok := accAfter.(*vestingtypes.ContinuousVestingAccount) + s.Require().True(ok) + + newNotionalAcc := s.App.AccountKeeper.GetAccount(s.Ctx, sdk.MustAccAddressFromBech32(v4.NewNotionalMultisigAccount)) + newNotionalAccVesting, ok := newNotionalAcc.(*vestingtypes.ContinuousVestingAccount) s.Require().True(ok) - s.Require().Equal(0, len(s.App.BankKeeper.GetAllBalances(s.Ctx, vestingAddr))) - // now delegated to top 10 validator - s.Require().Equal(10, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) + s.Require().Equal(2, len(s.App.BankKeeper.GetAllBalances(s.Ctx, vestingAddr))) + // only move uwhale token + s.Require().Equal(1, len(s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NewNotionalMultisigAccount)))) + s.Require().Equal(0, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) s.Require().Equal(0, len(s.App.StakingKeeper.GetRedelegations(s.Ctx, vestingAddr, 65535))) + vestingBalance := cVesting.GetVestingCoins(s.Ctx.BlockTime()) + // check old multisign address balance + expectedBalance := accVestingBalance.AmountOf(params.BaseDenom).Sub(vestingBalance.AmountOf(params.BaseDenom)) oldMultisigBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NotionalMultisigVestingAccount)) - fmt.Printf("Old multisign address Upgrade Balance: %s\n", oldMultisigBalance) - s.Require().True(oldMultisigBalance.Empty()) - totalDelegateBalance := s.App.StakingKeeper.GetDelegatorBonded(s.Ctx, sdk.MustAccAddressFromBech32(v4.NotionalMultisigVestingAccount)) - fmt.Printf("old multisign address totalDelegateBalance %v\n", totalDelegateBalance) - s.Require().True(totalDelegateBalance.Equal(unvested)) + fmt.Printf("Old multisign address Upgrade Balance: %s, expectedBalance %s\n", oldMultisigBalance, expectedBalance) + // check base account balance + s.Require().True(oldMultisigBalance.AmountOf(params.BaseDenom).Equal(expectedBalance)) + s.Require().True(oldMultisigBalance.AmountOf(v4.TestDenom).Equal(sdk.NewInt(v4.TestAmount))) + // check vesting info + s.Require().True(accAfterVestingAccount.OriginalVesting.AmountOf(params.BaseDenom).Equal(expectedBalance)) + s.Require().True(accAfterVestingAccount.DelegatedVesting.Empty()) + s.Require().True(accAfterVestingAccount.DelegatedFree.Empty()) // check new multisign address balance - newBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NewNotionalMultisigAccount)) - vestedBalance := cVesting.GetVestedCoins(s.Ctx.BlockTime()) - fmt.Printf("New multisign Upgrade Balance: %s, vestedBalance %s\n", newBalance, vestedBalance) - s.Require().True(vestedBalance.AmountOf(params.BaseDenom).Equal(newBalance.AmountOf(params.BaseDenom))) + fmt.Printf("New multisign Upgrade Balance: %s, vestingBalance %s\n", newNotionalAccVesting.GetOriginalVesting(), vestingBalance) + // check vesting info + s.Require().True(vestingBalance.AmountOf(params.BaseDenom).Equal(newNotionalAccVesting.GetOriginalVesting().AmountOf(params.BaseDenom))) + s.Require().True(newNotionalAccVesting.DelegatedVesting.Empty()) + s.Require().True(newNotionalAccVesting.DelegatedFree.Empty()) + + // check base account balance + newMultisigBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NewNotionalMultisigAccount)) + s.Require().True(newMultisigBalance.AmountOf(params.BaseDenom).Equal(vestingBalance.AmountOf(params.BaseDenom))) } diff --git a/scripts/upgrade_test.sh b/scripts/upgrade_test.sh index e07c8ac9..366e941d 100755 --- a/scripts/upgrade_test.sh +++ b/scripts/upgrade_test.sh @@ -9,7 +9,7 @@ HOME=mytestnet ROOT=$(pwd) DENOM=uwhale CHAIN_ID=localmigaloo -SOFTWARE_UPGRADE_NAME="v4.1.0" +SOFTWARE_UPGRADE_NAME="v4.1.1" ADDITIONAL_PRE_SCRIPTS=${ADDITIONAL_PRE_SCRIPTS:-""} ADDITIONAL_AFTER_SCRIPTS=${ADDITIONAL_AFTER_SCRIPTS:-""}