Skip to content

Commit 8ef40ef

Browse files
authored
fix: ensure state compatibility with v1 (#15)
1 parent f2109c8 commit 8ef40ef

File tree

5 files changed

+82
-42
lines changed

5 files changed

+82
-42
lines changed

keeper/keeper.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ type Keeper struct {
3838
AllowedDenoms collections.KeySet[string]
3939
Owner collections.Map[string, string]
4040
PendingOwner collections.Map[string, string]
41-
Admins collections.KeySet[collections.Pair[string, string]]
42-
Systems collections.KeySet[collections.Pair[string, string]]
43-
MintAllowance collections.Map[collections.Pair[string, string], []byte]
41+
Systems collections.KeySet[[]byte]
42+
Admins collections.KeySet[[]byte]
43+
MintAllowance collections.Map[[]byte, []byte]
4444
MaxMintAllowance collections.Map[string, []byte]
4545

4646
BlacklistOwner collections.Item[string]
@@ -72,9 +72,9 @@ func NewKeeper(
7272
AllowedDenoms: collections.NewKeySet(builder, types.AllowedDenomPrefix, "allowedDenoms", collections.StringKey),
7373
Owner: collections.NewMap(builder, types.OwnerPrefix, "owner", collections.StringKey, collections.StringValue),
7474
PendingOwner: collections.NewMap(builder, types.PendingOwnerPrefix, "pendingOwner", collections.StringKey, collections.StringValue),
75-
Systems: collections.NewKeySet(builder, types.SystemPrefix, "systems", collections.PairKeyCodec(collections.StringKey, collections.StringKey)),
76-
Admins: collections.NewKeySet(builder, types.AdminPrefix, "admins", collections.PairKeyCodec(collections.StringKey, collections.StringKey)),
77-
MintAllowance: collections.NewMap(builder, types.MintAllowancePrefix, "mintAllowance", collections.PairKeyCodec(collections.StringKey, collections.StringKey), collections.BytesValue),
75+
Systems: collections.NewKeySet(builder, types.SystemPrefix, "systems", collections.BytesKey),
76+
Admins: collections.NewKeySet(builder, types.AdminPrefix, "admins", collections.BytesKey),
77+
MintAllowance: collections.NewMap(builder, types.MintAllowancePrefix, "mintAllowance", collections.BytesKey, collections.BytesValue),
7878
MaxMintAllowance: collections.NewMap(builder, types.MaxMintAllowancePrefix, "maxMintAllowance", collections.StringKey, collections.BytesValue),
7979

8080
BlacklistOwner: collections.NewItem(builder, blacklist.OwnerKey, "blacklistOwner", collections.StringValue),

keeper/msg_server_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestAddAdminAccount(t *testing.T) {
150150
tmp := k.Admins
151151
k.Admins = collections.NewKeySet(
152152
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Set, utils.GetKVStore(ctx, types.ModuleName))),
153-
types.AdminPrefix, "admins", collections.PairKeyCodec(collections.StringKey, collections.StringKey),
153+
types.AdminPrefix, "admins", collections.BytesKey,
154154
)
155155

156156
// ACT: Attempt to add admin account with failing Admins collection store.
@@ -215,7 +215,7 @@ func TestAddSystemAccount(t *testing.T) {
215215
tmp := k.Systems
216216
k.Systems = collections.NewKeySet(
217217
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Set, utils.GetKVStore(ctx, types.ModuleName))),
218-
types.SystemPrefix, "systems", collections.PairKeyCodec(collections.StringKey, collections.StringKey),
218+
types.SystemPrefix, "systems", collections.BytesKey,
219219
)
220220

221221
// ACT: Attempt to add system account with failing Systems collection store.
@@ -490,7 +490,7 @@ func TestMint(t *testing.T) {
490490
tmp := k.MintAllowance
491491
k.MintAllowance = collections.NewMap(
492492
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Set, utils.GetKVStore(ctx, types.ModuleName))),
493-
types.MintAllowancePrefix, "mintAllowance", collections.PairKeyCodec(collections.StringKey, collections.StringKey), collections.BytesValue,
493+
types.MintAllowancePrefix, "mintAllowance", collections.BytesKey, collections.BytesValue,
494494
)
495495

496496
// ACT: Attempt to mint with failing MintAllowance collection store.
@@ -717,7 +717,7 @@ func TestRemoveAdminAccount(t *testing.T) {
717717
tmp := k.Admins
718718
k.Admins = collections.NewKeySet(
719719
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Delete, utils.GetKVStore(ctx, types.ModuleName))),
720-
types.AdminPrefix, "admins", collections.PairKeyCodec(collections.StringKey, collections.StringKey),
720+
types.AdminPrefix, "admins", collections.BytesKey,
721721
)
722722

723723
// ACT: Attempt to remove admin account with failing Admins collection store.
@@ -785,7 +785,7 @@ func TestRemoveSystemAccount(t *testing.T) {
785785
tmp := k.Systems
786786
k.Systems = collections.NewKeySet(
787787
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Delete, utils.GetKVStore(ctx, types.ModuleName))),
788-
types.SystemPrefix, "systems", collections.PairKeyCodec(collections.StringKey, collections.StringKey),
788+
types.SystemPrefix, "systems", collections.BytesKey,
789789
)
790790

791791
// ACT: Attempt to remove system account with failing Systems collection store.
@@ -927,7 +927,7 @@ func TestSetMintAllowance(t *testing.T) {
927927
tmp := k.MintAllowance
928928
k.MintAllowance = collections.NewMap(
929929
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Set, utils.GetKVStore(ctx, types.ModuleName))),
930-
types.MintAllowancePrefix, "mintAllowance", collections.PairKeyCodec(collections.StringKey, collections.StringKey), collections.BytesValue,
930+
types.MintAllowancePrefix, "mintAllowance", collections.BytesKey, collections.BytesValue,
931931
)
932932

933933
// ACT: Attempt to set mint allowance with failing MintAllowance collection store.

keeper/state.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,20 @@ func (k *Keeper) SetPendingOwner(ctx context.Context, denom string, pendingOwner
8888
//
8989

9090
func (k *Keeper) DeleteSystem(ctx context.Context, denom string, address string) error {
91-
return k.Systems.Remove(ctx, collections.Join(denom, address))
91+
return k.Systems.Remove(ctx, types.SystemKey(denom, address))
9292
}
9393

9494
func (k *Keeper) GetSystemsByDenom(ctx context.Context, denom string) (systems []string) {
95-
rng := collections.NewPrefixedPairRange[string, string](denom)
96-
_ = k.Systems.Walk(ctx, rng, func(key collections.Pair[string, string]) (stop bool, err error) {
97-
systems = append(systems, key.K2())
98-
return false, nil
99-
})
95+
prefix := []byte(denom)
96+
itr, _ := k.Systems.Iterate(ctx, new(collections.Range[[]byte]).Prefix(prefix))
97+
98+
defer itr.Close()
99+
100+
for ; itr.Valid(); itr.Next() {
101+
key, _ := itr.Key()
102+
systems = append(systems, string(key[len(prefix):]))
103+
}
104+
100105
return
101106
}
102107

@@ -114,26 +119,31 @@ func (k *Keeper) GetSystems(ctx context.Context) (systems []types.Account) {
114119
}
115120

116121
func (k *Keeper) IsSystem(ctx context.Context, denom string, address string) bool {
117-
system, _ := k.Systems.Has(ctx, collections.Join(denom, address))
122+
system, _ := k.Systems.Has(ctx, types.SystemKey(denom, address))
118123
return system
119124
}
120125

121126
func (k *Keeper) SetSystem(ctx context.Context, denom string, address string) error {
122-
return k.Systems.Set(ctx, collections.Join(denom, address))
127+
return k.Systems.Set(ctx, types.SystemKey(denom, address))
123128
}
124129

125130
//
126131

127132
func (k *Keeper) DeleteAdmin(ctx context.Context, denom string, admin string) error {
128-
return k.Admins.Remove(ctx, collections.Join(denom, admin))
133+
return k.Admins.Remove(ctx, types.AdminKey(denom, admin))
129134
}
130135

131136
func (k *Keeper) GetAdminsByDenom(ctx context.Context, denom string) (admins []string) {
132-
rng := collections.NewPrefixedPairRange[string, string](denom)
133-
_ = k.Admins.Walk(ctx, rng, func(key collections.Pair[string, string]) (stop bool, err error) {
134-
admins = append(admins, key.K2())
135-
return false, nil
136-
})
137+
prefix := []byte(denom)
138+
itr, _ := k.Admins.Iterate(ctx, new(collections.Range[[]byte]).Prefix(prefix))
139+
140+
defer itr.Close()
141+
142+
for ; itr.Valid(); itr.Next() {
143+
key, _ := itr.Key()
144+
admins = append(admins, string(key[len(prefix):]))
145+
}
146+
137147
return
138148
}
139149

@@ -151,19 +161,19 @@ func (k *Keeper) GetAdmins(ctx context.Context) (admins []types.Account) {
151161
}
152162

153163
func (k *Keeper) IsAdmin(ctx context.Context, denom string, admin string) bool {
154-
isAdmin, _ := k.Admins.Has(ctx, collections.Join(denom, admin))
164+
isAdmin, _ := k.Admins.Has(ctx, types.AdminKey(denom, admin))
155165
return isAdmin
156166
}
157167

158168
func (k *Keeper) SetAdmin(ctx context.Context, denom string, admin string) error {
159-
return k.Admins.Set(ctx, collections.Join(denom, admin))
169+
return k.Admins.Set(ctx, types.AdminKey(denom, admin))
160170
}
161171

162172
//
163173

164174
func (k *Keeper) GetMintAllowance(ctx context.Context, denom string, address string) (allowance math.Int) {
165175
allowance = math.ZeroInt()
166-
bz, err := k.MintAllowance.Get(ctx, collections.Join(denom, address))
176+
bz, err := k.MintAllowance.Get(ctx, types.MintAllowanceKey(denom, address))
167177
if err != nil {
168178
return
169179
}
@@ -173,20 +183,28 @@ func (k *Keeper) GetMintAllowance(ctx context.Context, denom string, address str
173183
}
174184

175185
func (k *Keeper) GetMintAllowancesByDenom(ctx context.Context, denom string) (allowances []types.Allowance) {
176-
rng := collections.NewPrefixedPairRange[string, string](denom)
177-
_ = k.MintAllowance.Walk(ctx, rng, func(key collections.Pair[string, string], value []byte) (stop bool, err error) {
186+
prefix := []byte(denom)
187+
itr, _ := k.MintAllowance.Iterate(ctx, new(collections.Range[[]byte]).Prefix(prefix))
188+
189+
defer itr.Close()
190+
191+
for ; itr.Valid(); itr.Next() {
192+
key, _ := itr.Key()
193+
value, _ := itr.Value()
194+
178195
var allowance math.Int
179-
err = allowance.Unmarshal(value)
196+
err := allowance.Unmarshal(value)
180197
if err != nil {
181-
return true, err
198+
continue
182199
}
200+
183201
allowances = append(allowances, types.Allowance{
184-
Denom: key.K1(),
185-
Address: key.K2(),
202+
Denom: denom,
203+
Address: string(key[len(prefix):]),
186204
Allowance: allowance,
187205
})
188-
return false, nil
189-
})
206+
}
207+
190208
return
191209
}
192210

@@ -200,7 +218,7 @@ func (k *Keeper) GetMintAllowances(ctx context.Context) (allowances []types.Allo
200218

201219
func (k *Keeper) SetMintAllowance(ctx context.Context, denom string, address string, allowance math.Int) error {
202220
bz, _ := allowance.Marshal()
203-
return k.MintAllowance.Set(ctx, collections.Join(denom, address), bz)
221+
return k.MintAllowance.Set(ctx, types.MintAllowanceKey(denom, address), bz)
204222
}
205223

206224
//

keeper/state_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package keeper_test
1717
import (
1818
"testing"
1919

20-
"cosmossdk.io/collections"
2120
"github.com/monerium/module-noble/v2/types"
2221
"github.com/monerium/module-noble/v2/utils"
2322
"github.com/monerium/module-noble/v2/utils/mocks"
@@ -56,11 +55,22 @@ func TestGetMintAllowances(t *testing.T) {
5655
})
5756

5857
// ARRANGE: Set invalid mint allowance
59-
key := collections.Join("ueure", "address")
58+
key := types.MintAllowanceKey("ueure", "address")
6059
_ = k.MintAllowance.Set(ctx, key, []byte("panic"))
6160

6261
// ACT: Attempt to get mint allowances.
6362
allowances := k.GetMintAllowancesByDenom(ctx, "ueure")
64-
// ASSERT: The action should've succeeded, returns empty.
65-
require.Empty(t, allowances)
63+
// ASSERT: The action should've succeeded, skipping the invalid entry.
64+
require.NoError(t, err)
65+
require.Len(t, allowances, 2)
66+
require.Contains(t, allowances, types.Allowance{
67+
Denom: "ueure",
68+
Address: minter1.Address,
69+
Allowance: One,
70+
})
71+
require.Contains(t, allowances, types.Allowance{
72+
Denom: "ueure",
73+
Address: minter2.Address,
74+
Allowance: One.MulRaw(2),
75+
})
6676
}

types/keys.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ var (
2525
MintAllowancePrefix = []byte("mint_allowance/")
2626
MaxMintAllowancePrefix = []byte("max_mint_allowance/")
2727
)
28+
29+
func SystemKey(denom string, address string) []byte {
30+
return append([]byte(denom), []byte(address)...)
31+
}
32+
33+
func AdminKey(denom string, address string) []byte {
34+
return append([]byte(denom), []byte(address)...)
35+
}
36+
37+
func MintAllowanceKey(denom string, address string) []byte {
38+
return append([]byte(denom), []byte(address)...)
39+
}

0 commit comments

Comments
 (0)