Skip to content

Commit

Permalink
refactor: change logic migration acc (#326)
Browse files Browse the repository at this point in the history
* docs: update change log

* refactor: change logic migration acc

* chore: update v4.1.0 to v4.1.1

* test: update test
  • Loading branch information
hoank101 authored Feb 23, 2024
1 parent bc6a132 commit 0e46b4b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 137 deletions.
56 changes: 2 additions & 54 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,6 @@

<a name="v4.1.x-testnet-rc4"></a>
## [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


<a name="v4.1.x-testnet-rc3"></a>
## [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


<a name="v4.1.0-testnet-rc2"></a>
## [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


<a name="v4.1.0-testnet-rc1"></a>
## [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


<a name="v4.1.0-testnet"></a>
## [v4.1.0-testnet](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v3.0.4...v4.1.0-testnet) (2024-01-19)
<a name="v4.1.0"></a>
## [v4.1.0](https://github.com/White-Whale-Defi-Platform/migaloo-chain/compare/v3.0.4...v4.1.0) (2024-02-16)

### Chore

Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
74 changes: 29 additions & 45 deletions app/upgrades/v4_1_0/upgrades.go → app/upgrades/v4_1_1/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)))
}
2 changes: 1 addition & 1 deletion scripts/upgrade_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:-""}

Expand Down

0 comments on commit 0e46b4b

Please sign in to comment.