2021-02-10 09:49:31 -08:00
|
|
|
package v042
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
v040auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v040"
|
|
|
|
v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v040"
|
2021-02-25 02:43:31 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2021-02-10 09:49:31 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// MigrateStore performs in-place store migrations from v0.40 to v0.42. The
|
|
|
|
// migration includes:
|
|
|
|
//
|
|
|
|
// - Change addresses to be length-prefixed.
|
|
|
|
// - Change balances prefix to 1 byte
|
|
|
|
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey) error {
|
|
|
|
store := ctx.KVStore(storeKey)
|
|
|
|
|
|
|
|
// old key is of format:
|
|
|
|
// prefix ("balances") || addrBytes (20 bytes) || denomBytes
|
|
|
|
// new key is of format
|
|
|
|
// prefix (0x02) || addrLen (1 byte) || addrBytes || denomBytes
|
|
|
|
oldStore := prefix.NewStore(store, v040bank.BalancesPrefix)
|
|
|
|
|
|
|
|
oldStoreIter := oldStore.Iterator(nil, nil)
|
|
|
|
defer oldStoreIter.Close()
|
|
|
|
|
|
|
|
for ; oldStoreIter.Valid(); oldStoreIter.Next() {
|
|
|
|
addr := v040bank.AddressFromBalancesStore(oldStoreIter.Key())
|
|
|
|
denom := oldStoreIter.Key()[v040auth.AddrLen:]
|
2021-02-25 02:43:31 -08:00
|
|
|
newStoreKey := append(types.CreateAccountBalancesPrefix(addr), denom...)
|
2021-02-10 09:49:31 -08:00
|
|
|
|
|
|
|
// Set new key on store. Values don't change.
|
|
|
|
store.Set(newStoreKey, oldStoreIter.Value())
|
|
|
|
oldStore.Delete(oldStoreIter.Key())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|