github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/migrations/v4/store.go (about)

     1  package v4
     2  
     3  import (
     4  	"sort"
     5  
     6  	sdkmath "cosmossdk.io/math"
     7  	storetypes "cosmossdk.io/store/types"
     8  
     9  	"github.com/cosmos/cosmos-sdk/codec"
    10  	sdk "github.com/cosmos/cosmos-sdk/types"
    11  	"github.com/cosmos/cosmos-sdk/x/staking/exported"
    12  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    13  )
    14  
    15  // MigrateStore performs in-place store migrations from v3 to v4.
    16  func MigrateStore(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, legacySubspace exported.Subspace) error {
    17  	// migrate params
    18  	if err := migrateParams(ctx, store, cdc, legacySubspace); err != nil {
    19  		return err
    20  	}
    21  
    22  	// migrate unbonding delegations
    23  	if err := migrateUBDEntries(ctx, store, cdc, legacySubspace); err != nil {
    24  		return err
    25  	}
    26  
    27  	return nil
    28  }
    29  
    30  // migrateParams will set the params to store from legacySubspace
    31  func migrateParams(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, legacySubspace exported.Subspace) error {
    32  	var legacyParams types.Params
    33  	legacySubspace.GetParamSet(ctx, &legacyParams)
    34  
    35  	if err := legacyParams.Validate(); err != nil {
    36  		return err
    37  	}
    38  
    39  	bz := cdc.MustMarshal(&legacyParams)
    40  	store.Set(types.ParamsKey, bz)
    41  	return nil
    42  }
    43  
    44  // migrateUBDEntries will remove the ubdEntries with same creation_height
    45  // and create a new ubdEntry with updated balance and initial_balance
    46  func migrateUBDEntries(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, legacySubspace exported.Subspace) error {
    47  	iterator := storetypes.KVStorePrefixIterator(store, types.UnbondingDelegationKey)
    48  	defer iterator.Close()
    49  
    50  	for ; iterator.Valid(); iterator.Next() {
    51  		ubd := types.MustUnmarshalUBD(cdc, iterator.Value())
    52  
    53  		entriesAtSameCreationHeight := make(map[int64][]types.UnbondingDelegationEntry)
    54  		for _, ubdEntry := range ubd.Entries {
    55  			entriesAtSameCreationHeight[ubdEntry.CreationHeight] = append(entriesAtSameCreationHeight[ubdEntry.CreationHeight], ubdEntry)
    56  		}
    57  
    58  		creationHeights := make([]int64, 0, len(entriesAtSameCreationHeight))
    59  		for k := range entriesAtSameCreationHeight {
    60  			creationHeights = append(creationHeights, k)
    61  		}
    62  
    63  		sort.Slice(creationHeights, func(i, j int) bool { return creationHeights[i] < creationHeights[j] })
    64  
    65  		ubd.Entries = make([]types.UnbondingDelegationEntry, 0, len(creationHeights))
    66  
    67  		for _, h := range creationHeights {
    68  			ubdEntry := types.UnbondingDelegationEntry{
    69  				Balance:        sdkmath.ZeroInt(),
    70  				InitialBalance: sdkmath.ZeroInt(),
    71  			}
    72  			for _, entry := range entriesAtSameCreationHeight[h] {
    73  				ubdEntry.Balance = ubdEntry.Balance.Add(entry.Balance)
    74  				ubdEntry.InitialBalance = ubdEntry.InitialBalance.Add(entry.InitialBalance)
    75  				ubdEntry.CreationHeight = entry.CreationHeight
    76  				ubdEntry.CompletionTime = entry.CompletionTime
    77  			}
    78  			ubd.Entries = append(ubd.Entries, ubdEntry)
    79  		}
    80  
    81  		// set the new ubd to the store
    82  		setUBDToStore(ctx, store, cdc, ubd)
    83  	}
    84  	return nil
    85  }
    86  
    87  func setUBDToStore(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, ubd types.UnbondingDelegation) {
    88  	delegatorAddress := sdk.MustAccAddressFromBech32(ubd.DelegatorAddress)
    89  
    90  	bz := types.MustMarshalUBD(cdc, ubd)
    91  
    92  	addr, err := sdk.ValAddressFromBech32(ubd.ValidatorAddress)
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  
    97  	key := types.GetUBDKey(delegatorAddress, addr)
    98  
    99  	store.Set(key, bz)
   100  }