@@ -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.
3549func (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.
67129func (ak AccountKeeper ) embeddedBaseAccount (bz []byte , depth int ) (* types.BaseAccount , error ) {
0 commit comments