github.com/cosmos/cosmos-sdk@v0.50.10/x/group/migrations/v2/gen_state.go (about)

     1  package v2
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
     8  )
     9  
    10  // MigrateGenState accepts exported v0.46 x/auth genesis state and migrates it to
    11  // v0.47 x/auth genesis state. The migration includes:
    12  // - If the group module is enabled, replace group policy accounts from module accounts to base accounts.
    13  func MigrateGenState(oldState *authtypes.GenesisState) *authtypes.GenesisState {
    14  	newState := *oldState
    15  
    16  	accounts, err := authtypes.UnpackAccounts(newState.Accounts)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	groupPolicyAccountCounter := uint64(0)
    22  	for i, acc := range accounts {
    23  		modAcc, ok := acc.(sdk.ModuleAccountI)
    24  		if !ok {
    25  			continue
    26  		}
    27  
    28  		if modAcc.GetName() != modAcc.GetAddress().String() {
    29  			continue
    30  		}
    31  
    32  		// Replace group policy accounts from module accounts to base accounts.
    33  		// These accounts were wrongly created and the address was equal to the module name.
    34  		derivationKey := make([]byte, 8)
    35  		binary.BigEndian.PutUint64(derivationKey, groupPolicyAccountCounter)
    36  
    37  		cred, err := authtypes.NewModuleCredential(ModuleName, []byte{GroupPolicyTablePrefix}, derivationKey)
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  		baseAccount, err := authtypes.NewBaseAccountWithPubKey(cred)
    42  		if err != nil {
    43  			panic(err)
    44  		}
    45  
    46  		if err := baseAccount.SetAccountNumber(modAcc.GetAccountNumber()); err != nil {
    47  			panic(err)
    48  		}
    49  		accounts[i] = baseAccount
    50  		groupPolicyAccountCounter++
    51  	}
    52  
    53  	packedAccounts, err := authtypes.PackAccounts(accounts)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	newState.Accounts = packedAccounts
    58  
    59  	return &newState
    60  }