Skip to content

Commit c681d56

Browse files
seidroid[bot]alexander-seicursoragent
authored
Backport release/v6.8: Rewrite pacific-1's vesting accounts by address in the v6.8 auth migration (#4869)
Backport of #4864 to `release/v6.8`. Co-authored-by: alexander-sei <alexanderh@seinetwork.io> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 72e427b commit c681d56

2 files changed

Lines changed: 160 additions & 18 deletions

File tree

‎sei-cosmos/x/auth/keeper/legacy_vesting.go‎

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,100 @@ var legacyVestingAccountDepths = map[string]int{
3030
"/cosmos.vesting.v1beta1.PermanentLockedAccount": 2,
3131
}
3232

33-
// legacyVestingAccounts returns, in store order, the BaseAccount embedded in
34-
// every account stored under a type of the removed vesting module.
33+
// legacyVestingAddresses maps a chain ID to the address of every account that
34+
// chain stores under a type of the removed vesting module.
35+
var legacyVestingAddresses = map[string][]string{
36+
// Creating vesting accounts has been disabled on pacific-1 since its v6.7
37+
// upgrade at height 236450000, so the set this lists cannot grow.
38+
"pacific-1": {
39+
"sei1ga833mj6jc6d2lye6j84stug9h2mpxcxuyw5ak",
40+
"sei15wfxcaz3phxa7hyl9z9wr064nc7zt6y79ywkvw",
41+
"sei1570e0uvdywv0wp79mz235v6zzuu69268xhvk6x",
42+
},
43+
}
44+
45+
// legacyVestingAccounts returns the BaseAccount embedded in every account
46+
// stored under a type of the removed vesting module. On a chain that
47+
// legacyVestingAddresses lists it reads only the listed addresses, and on any
48+
// other chain it scans the account store.
3549
func (ak AccountKeeper) legacyVestingAccounts(ctx sdk.Context) ([]*types.BaseAccount, error) {
50+
if addresses, ok := legacyVestingAddresses[ctx.ChainID()]; ok {
51+
return ak.listedLegacyVestingAccounts(ctx, addresses)
52+
}
53+
return ak.scanLegacyVestingAccounts(ctx)
54+
}
55+
56+
// listedLegacyVestingAccounts returns, in the order given, the BaseAccount
57+
// embedded in each of addresses that holds an account stored under a type of
58+
// the removed vesting module. An address holding no account, or an account of
59+
// another type, is skipped.
60+
func (ak AccountKeeper) listedLegacyVestingAccounts(ctx sdk.Context, addresses []string) ([]*types.BaseAccount, error) {
61+
store := ctx.KVStore(ak.key)
62+
accounts := make([]*types.BaseAccount, 0, len(addresses))
63+
for _, bech32Address := range addresses {
64+
address, err := sdk.AccAddressFromBech32(bech32Address)
65+
if err != nil {
66+
return nil, err
67+
}
68+
encoded := store.Get(types.AddressStoreKey(address))
69+
if encoded == nil {
70+
continue
71+
}
72+
account, ok, err := ak.legacyVestingAccount(address, encoded)
73+
if err != nil {
74+
return nil, err
75+
}
76+
if ok {
77+
accounts = append(accounts, account)
78+
}
79+
}
80+
return accounts, nil
81+
}
82+
83+
// scanLegacyVestingAccounts returns, in store order, the BaseAccount embedded in
84+
// every account stored under a type of the removed vesting module.
85+
func (ak AccountKeeper) scanLegacyVestingAccounts(ctx sdk.Context) ([]*types.BaseAccount, error) {
3686
store := prefix.NewStore(ctx.KVStore(ak.key), types.AddressStoreKeyPrefix)
3787
iterator := store.Iterator(nil, nil)
3888
defer func() { _ = iterator.Close() }()
3989

4090
var accounts []*types.BaseAccount
4191
for ; iterator.Valid(); iterator.Next() {
42-
address, encoded := iterator.Key(), iterator.Value()
43-
typeURL, _, err := lengthDelimitedField(encoded, anyTypeURLField)
44-
if err != nil {
45-
return nil, fmt.Errorf("account %X: %w", address, err)
46-
}
47-
depth, ok := legacyVestingAccountDepths[string(typeURL)]
48-
if !ok {
49-
continue
50-
}
51-
account, err := ak.embeddedBaseAccount(encoded, depth)
92+
account, ok, err := ak.legacyVestingAccount(iterator.Key(), iterator.Value())
5293
if err != nil {
53-
return nil, fmt.Errorf("%s at %X: %w", typeURL, address, err)
94+
return nil, err
5495
}
55-
// SetAccount keys the rewrite by the embedded address, so a mismatch
56-
// would write a second account rather than replace this one.
57-
if !bytes.Equal(account.GetAddress(), address) {
58-
return nil, fmt.Errorf("%s at %X embeds the base account of %q", typeURL, address, account.Address)
96+
if ok {
97+
accounts = append(accounts, account)
5998
}
60-
accounts = append(accounts, account)
6199
}
62100
return accounts, nil
63101
}
64102

103+
// legacyVestingAccount returns the BaseAccount embedded in the account stored at
104+
// address as encoded, and whether encoded is of a type of the removed vesting
105+
// module.
106+
func (ak AccountKeeper) legacyVestingAccount(address, encoded []byte) (*types.BaseAccount, bool, error) {
107+
typeURL, _, err := lengthDelimitedField(encoded, anyTypeURLField)
108+
if err != nil {
109+
return nil, false, fmt.Errorf("account %X: %w", address, err)
110+
}
111+
depth, ok := legacyVestingAccountDepths[string(typeURL)]
112+
if !ok {
113+
return nil, false, nil
114+
}
115+
account, err := ak.embeddedBaseAccount(encoded, depth)
116+
if err != nil {
117+
return nil, false, fmt.Errorf("%s at %X: %w", typeURL, address, err)
118+
}
119+
// SetAccount keys the rewrite by the embedded address, so a mismatch
120+
// would write a second account rather than replace this one.
121+
if !bytes.Equal(account.GetAddress(), address) {
122+
return nil, false, fmt.Errorf("%s at %X embeds the base account of %q", typeURL, address, account.Address)
123+
}
124+
return account, true, nil
125+
}
126+
65127
// embeddedBaseAccount decodes the BaseAccount that the vesting account encoded
66128
// in the Any bz embeds depth levels down.
67129
func (ak AccountKeeper) embeddedBaseAccount(bz []byte, depth int) (*types.BaseAccount, error) {

‎sei-cosmos/x/auth/keeper/v3_to_v4_test.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ func legacyVestingAccount(t *testing.T, typeURL string, accountNumber uint64) au
3636
return account
3737
}
3838

39+
// pacific1VestingAddresses are the accounts pacific-1 stores under a vesting
40+
// type.
41+
var pacific1VestingAddresses = []string{
42+
"sei1ga833mj6jc6d2lye6j84stug9h2mpxcxuyw5ak",
43+
"sei15wfxcaz3phxa7hyl9z9wr064nc7zt6y79ywkvw",
44+
"sei1570e0uvdywv0wp79mz235v6zzuu69268xhvk6x",
45+
}
46+
47+
// legacyVestingAccountAt is a legacyVestingAccount of typeURL stored at address.
48+
func legacyVestingAccountAt(t *testing.T, typeURL string, address sdk.AccAddress, accountNumber uint64) authtestutil.LegacyVestingAccount {
49+
t.Helper()
50+
account := legacyVestingAccount(t, typeURL, accountNumber)
51+
account.Base = types.NewBaseAccount(address, nil, accountNumber, 3)
52+
return account
53+
}
54+
3955
func setLegacyVestingAccount(t *testing.T, store sdk.KVStore, account authtestutil.LegacyVestingAccount) {
4056
t.Helper()
4157
encoded, err := account.Encode()
@@ -154,3 +170,67 @@ func TestMigrate3to4RejectsUnconvertibleAccounts(t *testing.T) {
154170
})
155171
}
156172
}
173+
174+
// On pacific-1 the migration reads only the accounts it lists, so it rewrites
175+
// each of them and leaves an unlisted account of a vesting type as it was.
176+
func TestMigrate3to4OnPacific1RewritesOnlyTheListedAccounts(t *testing.T) {
177+
app, ctx := createTestApp(false)
178+
ctx = ctx.WithChainID("pacific-1")
179+
store := ctx.KVStore(app.GetKey(types.StoreKey))
180+
181+
listed := make([]authtestutil.LegacyVestingAccount, 0, len(pacific1VestingAddresses))
182+
for i, address := range pacific1VestingAddresses {
183+
account := legacyVestingAccountAt(t, authtestutil.LegacyDelayedVestingAccountTypeURL,
184+
sdk.MustAccAddressFromBech32(address), uint64(100+i)) //nolint:gosec // small test index
185+
setLegacyVestingAccount(t, store, account)
186+
listed = append(listed, account)
187+
}
188+
setLegacyVestingAccount(t, store, legacyVestingAccount(t, authtestutil.LegacyDelayedVestingAccountTypeURL, 200))
189+
before := accountStoreEntries(store)
190+
191+
m := keeper.NewMigrator(app.AccountKeeper, app.QueryServiceRouter())
192+
require.NoError(t, m.Migrate3to4(ctx))
193+
194+
after := accountStoreEntries(store)
195+
require.Len(t, after, len(before), "the migration added or removed an account")
196+
for _, account := range listed {
197+
key := string(types.AddressStoreKey(account.Base.GetAddress()))
198+
want, err := app.AccountKeeper.MarshalAccount(account.Base)
199+
require.NoError(t, err)
200+
require.Equal(t, want, after[key], "%s was not rewritten as its base account", account.Base.Address)
201+
delete(before, key)
202+
delete(after, key)
203+
}
204+
require.Equal(t, before, after, "the migration changed an account pacific-1 does not list")
205+
}
206+
207+
// On pacific-1 a listed address that holds no account, or an account of another
208+
// type, is skipped: the migration neither fails nor writes an account there.
209+
func TestMigrate3to4OnPacific1SkipsListedAddressesWithoutVestingAccounts(t *testing.T) {
210+
app, ctx := createTestApp(false)
211+
ctx = ctx.WithChainID("pacific-1")
212+
store := ctx.KVStore(app.GetKey(types.StoreKey))
213+
plain := app.AccountKeeper.NewAccountWithAddress(ctx, sdk.MustAccAddressFromBech32(pacific1VestingAddresses[0]))
214+
app.AccountKeeper.SetAccount(ctx, plain)
215+
before := accountStoreEntries(store)
216+
217+
m := keeper.NewMigrator(app.AccountKeeper, app.QueryServiceRouter())
218+
require.NoError(t, m.Migrate3to4(ctx))
219+
require.Equal(t, before, accountStoreEntries(store))
220+
}
221+
222+
// On pacific-1 the migration refuses a listed account it cannot convert, as it
223+
// does on a chain whose account store it scans.
224+
func TestMigrate3to4OnPacific1RejectsAnUnconvertibleListedAccount(t *testing.T) {
225+
app, ctx := createTestApp(false)
226+
ctx = ctx.WithChainID("pacific-1")
227+
store := ctx.KVStore(app.GetKey(types.StoreKey))
228+
encoded, err := legacyVestingAccount(t, authtestutil.LegacyDelayedVestingAccountTypeURL, 1).Encode()
229+
require.NoError(t, err)
230+
store.Set(types.AddressStoreKey(sdk.MustAccAddressFromBech32(pacific1VestingAddresses[1])), encoded)
231+
before := accountStoreEntries(store)
232+
233+
m := keeper.NewMigrator(app.AccountKeeper, app.QueryServiceRouter())
234+
require.ErrorContains(t, m.Migrate3to4(ctx), "embeds the base account of")
235+
require.Equal(t, before, accountStoreEntries(store))
236+
}

0 commit comments

Comments
 (0)