Skip to content

Commit

Permalink
Release/v1.6.3 (#1713)
Browse files Browse the repository at this point in the history
* add upgrade handler for v1.6.3

* add test, fix escrow account as source of refund

* remove unused functions

---------

Co-authored-by: Jacob Gadikian <[email protected]>
  • Loading branch information
Joe Bowman and faddat authored Aug 16, 2024
1 parent efda503 commit 965a6c8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1,548 deletions.
142 changes: 0 additions & 142 deletions app/upgrades/account_migrations.go
Original file line number Diff line number Diff line change
@@ -1,151 +1,9 @@
package upgrades

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"

"github.com/quicksilver-zone/quicksilver/app/keepers"
"github.com/quicksilver-zone/quicksilver/utils"
"github.com/quicksilver-zone/quicksilver/utils/addressutils"
)

type ProcessMigrateAccountStrategy func(ctx sdk.Context, appKeepers *keepers.AppKeepers, from sdk.AccAddress, to sdk.AccAddress) error

// Migrate a map of address pairs and migrate from key -> value
func migrateVestingAccounts(ctx sdk.Context, appKeepers *keepers.AppKeepers, migrations map[string]string, strategy ProcessMigrateAccountStrategy) error {
for _, fromBech32 := range utils.Keys(migrations) {
toBech32 := migrations[fromBech32]

from, err := addressutils.AccAddressFromBech32(fromBech32, "quick")
if err != nil {
return err
}
to, err := addressutils.AccAddressFromBech32(toBech32, "quick")
if err != nil {
return err
}
err = strategy(ctx, appKeepers, from, to)
if err != nil {
return err
}
}
return nil
}

// Migrate a PeriodicVestingAccount from address A to address B, maintaining periods, amounts and end date.
func migratePeriodicVestingAccount(ctx sdk.Context, appKeepers *keepers.AppKeepers, from sdk.AccAddress, to sdk.AccAddress) error {
oldAccount := appKeepers.AccountKeeper.GetAccount(ctx, from)
// if the new account already exists in the account keeper, we should fail.
if newAccount := appKeepers.AccountKeeper.GetAccount(ctx, to); newAccount != nil {
return fmt.Errorf("unable to migrate vesting account; destination is already an account")
}

oldPva, ok := oldAccount.(*vestingtypes.PeriodicVestingAccount)
if !ok {
return fmt.Errorf("from account is not a PeriodicVestingAccount")
}

// copy the existing PVA.
newPva := *oldPva

// create a new baseVesting account with the address provided.
newBva := vestingtypes.NewBaseVestingAccount(authtypes.NewBaseAccountWithAddress(to), oldPva.OriginalVesting, oldPva.EndTime)
// change vesting end time so we are able to negate the token lock.
// if the endDate has passed, we circumvent the period checking logic.
oldPva.BaseVestingAccount.EndTime = ctx.BlockTime().Unix() - 1
newPva.BaseVestingAccount = newBva

// set the old pva (with the altered date), so we can transfer assets.
appKeepers.AccountKeeper.SetAccount(ctx, oldPva)
// set the new pva with the correct period and end dates, and new address.
appKeepers.AccountKeeper.SetAccount(ctx, &newPva)

// send coins from old account to new.
err := appKeepers.BankKeeper.SendCoins(ctx, from, to, appKeepers.BankKeeper.GetAllBalances(ctx, from))
if err != nil {
return err
}

// delete the old account from the account keeper.
appKeepers.AccountKeeper.RemoveAccount(ctx, oldPva)
return nil
}

// migrateVestingAccountWithActions migrate from A to B with actions before migration executed
func migrateVestingAccountWithActions(ctx sdk.Context, appKeepers *keepers.AppKeepers, from sdk.AccAddress, to sdk.AccAddress) error {
// Complete all re-delegation before unbonding
err := completeAllRedelegations(ctx, ctx.BlockTime(), appKeepers, from)
if err != nil {
fmt.Printf("processMigratePeriodicVestingAccount: complete all re-delegation for %s failed: %v", from.String(), err)
return err
}

// Unbond all delegation of account
err = unbondAllDelegation(ctx, ctx.BlockTime(), appKeepers, from)
if err != nil {
fmt.Printf("processMigratePeriodicVestingAccount: unbonded all delegation for %s failed: %v", from.String(), err)
return err
}

return migratePeriodicVestingAccount(ctx, appKeepers, from, to)
}

func completeAllRedelegations(ctx sdk.Context, now time.Time,
appKeepers *keepers.AppKeepers,
accAddr sdk.AccAddress,
) error {
for _, activeRedelegation := range appKeepers.StakingKeeper.GetRedelegations(ctx, accAddr, 100) {
redelegationSrc, _ := sdk.ValAddressFromBech32(activeRedelegation.ValidatorSrcAddress)
redelegationDst, _ := sdk.ValAddressFromBech32(activeRedelegation.ValidatorDstAddress)

for i := range activeRedelegation.Entries {
activeRedelegation.Entries[i].CompletionTime = now
}

appKeepers.StakingKeeper.SetRedelegation(ctx, activeRedelegation)
_, err := appKeepers.StakingKeeper.CompleteRedelegation(ctx, accAddr, redelegationSrc, redelegationDst)
if err != nil {
return err
}
}

return nil
}

func unbondAllDelegation(ctx sdk.Context, now time.Time, appKeepers *keepers.AppKeepers, accAddr sdk.AccAddress) error {
// Undelegate all delegations from the account
for _, delegation := range appKeepers.StakingKeeper.GetAllDelegatorDelegations(ctx, accAddr) {
validatorValAddr := delegation.GetValidatorAddr()
_, found := appKeepers.StakingKeeper.GetValidator(ctx, validatorValAddr)
if !found {
continue
}

_, err := appKeepers.StakingKeeper.Undelegate(ctx, accAddr, validatorValAddr, delegation.GetShares())
if err != nil {
return err
}
}

// Complete unbonding of all account's delegations
for _, unbondingDelegation := range appKeepers.StakingKeeper.GetAllUnbondingDelegations(ctx, accAddr) {
validatorStringAddr := unbondingDelegation.ValidatorAddress
validatorValAddr, _ := sdk.ValAddressFromBech32(validatorStringAddr)

for i := range unbondingDelegation.Entries {
unbondingDelegation.Entries[i].CompletionTime = now
}

appKeepers.StakingKeeper.SetUnbondingDelegation(ctx, unbondingDelegation)
_, err := appKeepers.StakingKeeper.CompleteUnbonding(ctx, accAddr, validatorValAddr)
if err != nil {
return err
}
}

return nil
}
1 change: 1 addition & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
V010505UpgradeName = "v1.5.5"
V010601UpgradeName = "v1.6.1"
V010602UpgradeName = "v1.6.2"
V010603UpgradeName = "v1.6.3"
)

// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal
Expand Down
9 changes: 1 addition & 8 deletions app/upgrades/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
func Upgrades() []Upgrade {
return []Upgrade{
// testnet upgrades
{UpgradeName: V010500rc0UpgradeName, CreateUpgradeHandler: NoOpHandler},
{UpgradeName: V010500rc1UpgradeName, CreateUpgradeHandler: V010500rc1UpgradeHandler},
{UpgradeName: V010503rc0UpgradeName, CreateUpgradeHandler: V010503rc0UpgradeHandler},
{UpgradeName: V010600beta0UpgradeName, CreateUpgradeHandler: V010600beta0UpgradeHandler},
{UpgradeName: V010600beta1UpgradeName, CreateUpgradeHandler: V010600beta1UpgradeHandler},
{UpgradeName: V010600rc0UpgradeName, CreateUpgradeHandler: V010600rc0UpgradeHandler},
Expand All @@ -23,13 +20,9 @@ func Upgrades() []Upgrade {
{UpgradeName: V010601rc4UpgradeName, CreateUpgradeHandler: NoOpHandler},

// v1.5: this needs to be present to support upgrade on mainnet
{UpgradeName: V010500UpgradeName, CreateUpgradeHandler: V010500UpgradeHandler},
{UpgradeName: V010501UpgradeName, CreateUpgradeHandler: V010501UpgradeHandler},
{UpgradeName: V010503UpgradeName, CreateUpgradeHandler: V010503UpgradeHandler},
{UpgradeName: V010504UpgradeName, CreateUpgradeHandler: V010504UpgradeHandler},
{UpgradeName: V010505UpgradeName, CreateUpgradeHandler: V010505UpgradeHandler},
{UpgradeName: V010601UpgradeName, CreateUpgradeHandler: V010601UpgradeHandler},
{UpgradeName: V010602UpgradeName, CreateUpgradeHandler: NoOpHandler},
{UpgradeName: V010603UpgradeName, CreateUpgradeHandler: V010603UpgradeHandler},
}
}

Expand Down
Loading

0 comments on commit 965a6c8

Please sign in to comment.