github.com/Finschia/finschia-sdk@v0.48.1/x/gov/legacy/v043/store.go (about)

     1  package v043
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Finschia/finschia-sdk/codec"
     7  	"github.com/Finschia/finschia-sdk/store/prefix"
     8  	sdk "github.com/Finschia/finschia-sdk/types"
     9  	"github.com/Finschia/finschia-sdk/types/address"
    10  	"github.com/Finschia/finschia-sdk/x/gov/types"
    11  )
    12  
    13  const proposalIDLen = 8
    14  
    15  // migratePrefixProposalAddress is a helper function that migrates all keys of format:
    16  // <prefix_bytes><proposal_id (8 bytes)><address_bytes>
    17  // into format:
    18  // <prefix_bytes><proposal_id (8 bytes)><address_len (1 byte)><address_bytes>
    19  func migratePrefixProposalAddress(store sdk.KVStore, prefixBz []byte) {
    20  	oldStore := prefix.NewStore(store, prefixBz)
    21  
    22  	oldStoreIter := oldStore.Iterator(nil, nil)
    23  	defer oldStoreIter.Close()
    24  
    25  	for ; oldStoreIter.Valid(); oldStoreIter.Next() {
    26  		proposalID := oldStoreIter.Key()[:proposalIDLen]
    27  		addr := oldStoreIter.Key()[proposalIDLen:]
    28  		newStoreKey := append(append(prefixBz, proposalID...), address.MustLengthPrefix(addr)...)
    29  
    30  		// Set new key on store. Values don't change.
    31  		store.Set(newStoreKey, oldStoreIter.Value())
    32  		oldStore.Delete(oldStoreIter.Key())
    33  	}
    34  }
    35  
    36  // migrateStoreWeightedVotes migrates a legacy vote to an ADR-037 weighted vote.
    37  // Important: the `oldVote` has its `Option` field set, whereas the new weighted
    38  // vote has its `Options` field set.
    39  func migrateVote(oldVote types.Vote) types.Vote {
    40  	return types.Vote{
    41  		ProposalId: oldVote.ProposalId,
    42  		Voter:      oldVote.Voter,
    43  		Options:    types.NewNonSplitVoteOption(oldVote.Option), //nolint:staticcheck
    44  	}
    45  }
    46  
    47  // migrateStoreWeightedVotes migrates in-place all legacy votes to ADR-037 weighted votes.
    48  func migrateStoreWeightedVotes(store sdk.KVStore, cdc codec.BinaryCodec) error {
    49  	iterator := sdk.KVStorePrefixIterator(store, types.VotesKeyPrefix)
    50  
    51  	defer iterator.Close()
    52  	for ; iterator.Valid(); iterator.Next() {
    53  		var oldVote types.Vote
    54  		err := cdc.Unmarshal(iterator.Value(), &oldVote)
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  		newVote := migrateVote(oldVote)
    60  		fmt.Println("migrateStoreWeightedVotes newVote=", newVote)
    61  		bz, err := cdc.Marshal(&newVote)
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  		store.Set(iterator.Key(), bz)
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  // MigrateStore performs in-place store migrations from v0.40 to v0.43. The
    73  // migration includes:
    74  //
    75  // - Change addresses to be length-prefixed.
    76  func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
    77  	store := ctx.KVStore(storeKey)
    78  	migratePrefixProposalAddress(store, types.DepositsKeyPrefix)
    79  	migratePrefixProposalAddress(store, types.VotesKeyPrefix)
    80  	return migrateStoreWeightedVotes(store, cdc)
    81  }