github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/keeper/migrations.go (about) 1 package keeper 2 3 import ( 4 "github.com/cosmos/gogoproto/grpc" 5 6 sdk "github.com/cosmos/cosmos-sdk/types" 7 "github.com/cosmos/cosmos-sdk/x/auth/exported" 8 v2 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v2" 9 v3 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v3" 10 v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4" 11 v5 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v5" 12 "github.com/cosmos/cosmos-sdk/x/auth/types" 13 ) 14 15 // Migrator is a struct for handling in-place store migrations. 16 type Migrator struct { 17 keeper AccountKeeper 18 queryServer grpc.Server 19 legacySubspace exported.Subspace 20 } 21 22 // NewMigrator returns a new Migrator. 23 func NewMigrator(keeper AccountKeeper, queryServer grpc.Server, ss exported.Subspace) Migrator { 24 return Migrator{keeper: keeper, queryServer: queryServer, legacySubspace: ss} 25 } 26 27 // Migrate1to2 migrates from version 1 to 2. 28 func (m Migrator) Migrate1to2(ctx sdk.Context) error { 29 var iterErr error 30 31 m.keeper.IterateAccounts(ctx, func(account sdk.AccountI) (stop bool) { 32 wb, err := v2.MigrateAccount(ctx, account, m.queryServer) 33 if err != nil { 34 iterErr = err 35 return true 36 } 37 38 if wb == nil { 39 return false 40 } 41 42 m.keeper.SetAccount(ctx, wb) 43 return false 44 }) 45 46 return iterErr 47 } 48 49 // Migrate2to3 migrates from consensus version 2 to version 3. Specifically, for each account 50 // we index the account's ID to their address. 51 func (m Migrator) Migrate2to3(ctx sdk.Context) error { 52 return v3.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc) 53 } 54 55 // Migrate3to4 migrates the x/auth module state from the consensus version 3 to 56 // version 4. Specifically, it takes the parameters that are currently stored 57 // and managed by the x/params modules and stores them directly into the x/auth 58 // module state. 59 func (m Migrator) Migrate3to4(ctx sdk.Context) error { 60 return v4.Migrate(ctx, m.keeper.storeService, m.legacySubspace, m.keeper.cdc) 61 } 62 63 // Migrate4To5 migrates the x/auth module state from the consensus version 4 to 5. 64 // It migrates the GlobalAccountNumber from being a protobuf defined value to a 65 // big-endian encoded uint64, it also migrates it to use a more canonical prefix. 66 func (m Migrator) Migrate4To5(ctx sdk.Context) error { 67 return v5.Migrate(ctx, m.keeper.storeService, m.keeper.AccountNumber) 68 } 69 70 // V45_SetAccount implements V45_SetAccount 71 // set the account without map to accAddr to accNumber. 72 // 73 // NOTE: This is used for testing purposes only. 74 func (m Migrator) V45SetAccount(ctx sdk.Context, acc sdk.AccountI) error { 75 addr := acc.GetAddress() 76 store := m.keeper.storeService.OpenKVStore(ctx) 77 78 bz, err := m.keeper.Accounts.ValueCodec().Encode(acc) 79 if err != nil { 80 return err 81 } 82 83 return store.Set(addressStoreKey(addr), bz) 84 } 85 86 // addressStoreKey turn an address to key used to get it from the account store 87 // NOTE(tip): exists for legacy compatibility 88 func addressStoreKey(addr sdk.AccAddress) []byte { 89 return append(types.AddressStoreKeyPrefix, addr.Bytes()...) 90 }