Skip to content

Commit

Permalink
fix: ensure state compatibility with v1 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey authored Oct 18, 2024
1 parent f2109c8 commit 8ef40ef
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 42 deletions.
12 changes: 6 additions & 6 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type Keeper struct {
AllowedDenoms collections.KeySet[string]
Owner collections.Map[string, string]
PendingOwner collections.Map[string, string]
Admins collections.KeySet[collections.Pair[string, string]]
Systems collections.KeySet[collections.Pair[string, string]]
MintAllowance collections.Map[collections.Pair[string, string], []byte]
Systems collections.KeySet[[]byte]
Admins collections.KeySet[[]byte]
MintAllowance collections.Map[[]byte, []byte]
MaxMintAllowance collections.Map[string, []byte]

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

BlacklistOwner: collections.NewItem(builder, blacklist.OwnerKey, "blacklistOwner", collections.StringValue),
Expand Down
12 changes: 6 additions & 6 deletions keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestAddAdminAccount(t *testing.T) {
tmp := k.Admins
k.Admins = collections.NewKeySet(
collections.NewSchemaBuilder(mocks.FailingStore(mocks.Set, utils.GetKVStore(ctx, types.ModuleName))),
types.AdminPrefix, "admins", collections.PairKeyCodec(collections.StringKey, collections.StringKey),
types.AdminPrefix, "admins", collections.BytesKey,
)

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

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

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

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

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

// ACT: Attempt to set mint allowance with failing MintAllowance collection store.
Expand Down
70 changes: 44 additions & 26 deletions keeper/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ func (k *Keeper) SetPendingOwner(ctx context.Context, denom string, pendingOwner
//

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

func (k *Keeper) GetSystemsByDenom(ctx context.Context, denom string) (systems []string) {
rng := collections.NewPrefixedPairRange[string, string](denom)
_ = k.Systems.Walk(ctx, rng, func(key collections.Pair[string, string]) (stop bool, err error) {
systems = append(systems, key.K2())
return false, nil
})
prefix := []byte(denom)
itr, _ := k.Systems.Iterate(ctx, new(collections.Range[[]byte]).Prefix(prefix))

defer itr.Close()

for ; itr.Valid(); itr.Next() {
key, _ := itr.Key()
systems = append(systems, string(key[len(prefix):]))
}

return
}

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

func (k *Keeper) IsSystem(ctx context.Context, denom string, address string) bool {
system, _ := k.Systems.Has(ctx, collections.Join(denom, address))
system, _ := k.Systems.Has(ctx, types.SystemKey(denom, address))
return system
}

func (k *Keeper) SetSystem(ctx context.Context, denom string, address string) error {
return k.Systems.Set(ctx, collections.Join(denom, address))
return k.Systems.Set(ctx, types.SystemKey(denom, address))
}

//

func (k *Keeper) DeleteAdmin(ctx context.Context, denom string, admin string) error {
return k.Admins.Remove(ctx, collections.Join(denom, admin))
return k.Admins.Remove(ctx, types.AdminKey(denom, admin))
}

func (k *Keeper) GetAdminsByDenom(ctx context.Context, denom string) (admins []string) {
rng := collections.NewPrefixedPairRange[string, string](denom)
_ = k.Admins.Walk(ctx, rng, func(key collections.Pair[string, string]) (stop bool, err error) {
admins = append(admins, key.K2())
return false, nil
})
prefix := []byte(denom)
itr, _ := k.Admins.Iterate(ctx, new(collections.Range[[]byte]).Prefix(prefix))

defer itr.Close()

for ; itr.Valid(); itr.Next() {
key, _ := itr.Key()
admins = append(admins, string(key[len(prefix):]))
}

return
}

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

func (k *Keeper) IsAdmin(ctx context.Context, denom string, admin string) bool {
isAdmin, _ := k.Admins.Has(ctx, collections.Join(denom, admin))
isAdmin, _ := k.Admins.Has(ctx, types.AdminKey(denom, admin))
return isAdmin
}

func (k *Keeper) SetAdmin(ctx context.Context, denom string, admin string) error {
return k.Admins.Set(ctx, collections.Join(denom, admin))
return k.Admins.Set(ctx, types.AdminKey(denom, admin))
}

//

func (k *Keeper) GetMintAllowance(ctx context.Context, denom string, address string) (allowance math.Int) {
allowance = math.ZeroInt()
bz, err := k.MintAllowance.Get(ctx, collections.Join(denom, address))
bz, err := k.MintAllowance.Get(ctx, types.MintAllowanceKey(denom, address))
if err != nil {
return
}
Expand All @@ -173,20 +183,28 @@ func (k *Keeper) GetMintAllowance(ctx context.Context, denom string, address str
}

func (k *Keeper) GetMintAllowancesByDenom(ctx context.Context, denom string) (allowances []types.Allowance) {
rng := collections.NewPrefixedPairRange[string, string](denom)
_ = k.MintAllowance.Walk(ctx, rng, func(key collections.Pair[string, string], value []byte) (stop bool, err error) {
prefix := []byte(denom)
itr, _ := k.MintAllowance.Iterate(ctx, new(collections.Range[[]byte]).Prefix(prefix))

defer itr.Close()

for ; itr.Valid(); itr.Next() {
key, _ := itr.Key()
value, _ := itr.Value()

var allowance math.Int
err = allowance.Unmarshal(value)
err := allowance.Unmarshal(value)
if err != nil {
return true, err
continue
}

allowances = append(allowances, types.Allowance{
Denom: key.K1(),
Address: key.K2(),
Denom: denom,
Address: string(key[len(prefix):]),
Allowance: allowance,
})
return false, nil
})
}

return
}

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

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

//
Expand Down
18 changes: 14 additions & 4 deletions keeper/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package keeper_test
import (
"testing"

"cosmossdk.io/collections"
"github.com/monerium/module-noble/v2/types"
"github.com/monerium/module-noble/v2/utils"
"github.com/monerium/module-noble/v2/utils/mocks"
Expand Down Expand Up @@ -56,11 +55,22 @@ func TestGetMintAllowances(t *testing.T) {
})

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

// ACT: Attempt to get mint allowances.
allowances := k.GetMintAllowancesByDenom(ctx, "ueure")
// ASSERT: The action should've succeeded, returns empty.
require.Empty(t, allowances)
// ASSERT: The action should've succeeded, skipping the invalid entry.
require.NoError(t, err)
require.Len(t, allowances, 2)
require.Contains(t, allowances, types.Allowance{
Denom: "ueure",
Address: minter1.Address,
Allowance: One,
})
require.Contains(t, allowances, types.Allowance{
Denom: "ueure",
Address: minter2.Address,
Allowance: One.MulRaw(2),
})
}
12 changes: 12 additions & 0 deletions types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ var (
MintAllowancePrefix = []byte("mint_allowance/")
MaxMintAllowancePrefix = []byte("max_mint_allowance/")
)

func SystemKey(denom string, address string) []byte {
return append([]byte(denom), []byte(address)...)
}

func AdminKey(denom string, address string) []byte {
return append([]byte(denom), []byte(address)...)
}

func MintAllowanceKey(denom string, address string) []byte {
return append([]byte(denom), []byte(address)...)
}

0 comments on commit 8ef40ef

Please sign in to comment.