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

     1  package v3_test
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  
     8  	cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"cosmossdk.io/collections"
    12  	"cosmossdk.io/depinject"
    13  	"cosmossdk.io/log"
    14  	storetypes "cosmossdk.io/store/types"
    15  
    16  	"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
    17  	"github.com/cosmos/cosmos-sdk/runtime"
    18  	"github.com/cosmos/cosmos-sdk/testutil"
    19  	simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
    20  	sdk "github.com/cosmos/cosmos-sdk/types"
    21  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    22  	"github.com/cosmos/cosmos-sdk/x/auth"
    23  	authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
    24  	"github.com/cosmos/cosmos-sdk/x/auth/keeper"
    25  	v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
    26  	v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4"
    27  	authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil"
    28  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
    29  )
    30  
    31  type mockSubspace struct {
    32  	ps authtypes.Params
    33  }
    34  
    35  func newMockSubspace(ps authtypes.Params) mockSubspace {
    36  	return mockSubspace{ps: ps}
    37  }
    38  
    39  func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps authexported.ParamSet) {
    40  	*ps.(*authtypes.Params) = ms.ps
    41  }
    42  
    43  // TestMigrateMapAccAddressToAccNumberKey test cases for state migration of map to accAddr to accNum
    44  func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) {
    45  	encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
    46  	cdc := encCfg.Codec
    47  
    48  	storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
    49  	tKey := storetypes.NewTransientStoreKey("transient_test")
    50  	ctx := testutil.DefaultContext(storeKey, tKey)
    51  	storeService := runtime.NewKVStoreService(storeKey)
    52  
    53  	var accountKeeper keeper.AccountKeeper
    54  
    55  	app, err := simtestutil.Setup(
    56  		depinject.Configs(
    57  			authtestutil.AppConfig,
    58  			depinject.Supply(log.NewNopLogger()),
    59  		),
    60  		&accountKeeper,
    61  	)
    62  	require.NoError(t, err)
    63  
    64  	legacySubspace := newMockSubspace(authtypes.DefaultParams())
    65  	require.NoError(t, v4.Migrate(ctx, storeService, legacySubspace, cdc))
    66  
    67  	// new base account
    68  	senderPrivKey := secp256k1.GenPrivKey()
    69  	randAccNumber := uint64(rand.Intn(100000-10000) + 10000)
    70  	acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), randAccNumber, 0)
    71  
    72  	ctx = app.BaseApp.NewContextLegacy(false, cmtproto.Header{Time: time.Now()})
    73  
    74  	// migrator
    75  	m := keeper.NewMigrator(accountKeeper, app.GRPCQueryRouter(), legacySubspace)
    76  	// set the account to store with map acc addr to acc number
    77  	require.NoError(t, m.V45SetAccount(ctx, acc))
    78  
    79  	testCases := []struct {
    80  		name        string
    81  		doMigration bool
    82  		accNum      uint64
    83  	}{
    84  		{
    85  			name:        "without state migration",
    86  			doMigration: false,
    87  			accNum:      acc.AccountNumber,
    88  		},
    89  		{
    90  			name:        "with state migration",
    91  			doMigration: true,
    92  			accNum:      acc.AccountNumber,
    93  		},
    94  	}
    95  
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			if tc.doMigration {
    99  				require.NoError(t, m.Migrate2to3(ctx))
   100  			}
   101  
   102  			//  get the account address by acc id
   103  			accAddr, err := accountKeeper.Accounts.Indexes.Number.MatchExact(ctx, tc.accNum)
   104  			if tc.doMigration {
   105  				require.Equal(t, accAddr.String(), acc.Address)
   106  			} else {
   107  				require.ErrorIs(t, err, collections.ErrNotFound)
   108  			}
   109  		})
   110  	}
   111  }