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

     1  package v3
     2  
     3  import (
     4  	corestore "cosmossdk.io/core/store"
     5  	storetypes "cosmossdk.io/store/types"
     6  
     7  	"github.com/cosmos/cosmos-sdk/codec"
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	"github.com/cosmos/cosmos-sdk/x/auth/types"
    10  )
    11  
    12  func mapAccountAddressToAccountID(ctx sdk.Context, storeService corestore.KVStoreService, cdc codec.BinaryCodec) error {
    13  	store := storeService.OpenKVStore(ctx)
    14  	iterator, err := store.Iterator(types.AddressStoreKeyPrefix, storetypes.PrefixEndBytes(types.AddressStoreKeyPrefix))
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	defer iterator.Close()
    20  	for ; iterator.Valid(); iterator.Next() {
    21  		var acc sdk.AccountI
    22  		if err := cdc.UnmarshalInterface(iterator.Value(), &acc); err != nil {
    23  			return err
    24  		}
    25  		store.Set(accountNumberStoreKey(acc.GetAccountNumber()), acc.GetAddress().Bytes())
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  // MigrateStore performs in-place store migrations from v0.45 to v0.46. The
    32  // migration includes:
    33  // - Add an Account number as an index to get the account address
    34  func MigrateStore(ctx sdk.Context, storeService corestore.KVStoreService, cdc codec.BinaryCodec) error {
    35  	return mapAccountAddressToAccountID(ctx, storeService, cdc)
    36  }
    37  
    38  // accountNumberStoreKey turn an account number to key used to get the account address from account store
    39  // NOTE(tip): exists for legacy compatibility
    40  func accountNumberStoreKey(accountNumber uint64) []byte {
    41  	return append(types.AccountNumberStoreKeyPrefix, sdk.Uint64ToBigEndian(accountNumber)...)
    42  }