Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change logic migration acc #326

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions app/upgrades/v4_1_0/mainnet_account.go
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
}
73 changes: 28 additions & 45 deletions app/upgrades/v4_1_0/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package v4

import (
"fmt"

Check failure on line 4 in app/upgrades/v4_1_0/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"time"

Check failure on line 6 in app/upgrades/v4_1_0/upgrades.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -10,13 +11,11 @@
"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 +60,10 @@
}
}

// migrateMultisigVesting moves the vested and reward token from the ContinuousVestingAccount -> the new multisig vesting account.
// migrateMultisigVesting moves the unvested from the ContinuousVestingAccount -> the new multisig vesting account.
// - 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 all tokens unvested token to the new multisig vesting account
func migrateMultisigVesting(ctx sdk.Context,
stakingKeeper stakingKeeper.Keeper,
bankKeeper bankKeeper.Keeper,
Expand All @@ -83,11 +81,13 @@
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 +103,30 @@
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 old continuous vesting account
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 newAddr
err = bankKeeper.SendCoins(ctx, currentAddr, newAddr, vestingCoin)
if err != nil {
panic(err)
}
}

Expand Down Expand Up @@ -191,30 +201,3 @@

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
}
36 changes: 22 additions & 14 deletions app/upgrades/v4_1_0/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ 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)
Expand Down Expand Up @@ -81,25 +80,34 @@ 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)
s.Require().True(oldMultisigBalance.AmountOf(params.BaseDenom).Equal(expectedBalance))
s.Require().True(accAfterVestingAccount.OriginalVesting.AmountOf(params.BaseDenom).Equal(expectedBalance))

hoank101 marked this conversation as resolved.
Show resolved Hide resolved
s.Require().True(oldMultisigBalance.AmountOf(v4.TestDenom).Equal(sdk.NewInt(v4.TestAmount)))

// 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)
s.Require().True(vestingBalance.AmountOf(params.BaseDenom).
Equal(newNotionalAccVesting.GetOriginalVesting().AmountOf(params.BaseDenom)))

newMultisigBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NewNotionalMultisigAccount))
s.Require().True(newMultisigBalance.AmountOf(params.BaseDenom).Equal(vestingBalance.AmountOf(params.BaseDenom)))
}
Loading