diff --git a/app/app.go b/app/app.go index 8277f61..fc6ba32 100644 --- a/app/app.go +++ b/app/app.go @@ -515,6 +515,7 @@ func New( app.MachineKeeper = *machinemodulekeeper.NewKeeper( appCodec, keys[machinemoduletypes.StoreKey], + keys[machinemoduletypes.IndexKey], keys[machinemoduletypes.MemStoreKey], app.GetSubspace(machinemoduletypes.ModuleName), ) diff --git a/testutil/keeper/machine.go b/testutil/keeper/machine.go index 3b508f1..86970db 100644 --- a/testutil/keeper/machine.go +++ b/testutil/keeper/machine.go @@ -3,6 +3,9 @@ package keeper import ( "testing" + "planetmint-go/x/machine/keeper" + "planetmint-go/x/machine/types" + "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/store" @@ -13,17 +16,17 @@ import ( "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmdb "github.com/tendermint/tm-db" - "planetmint-go/x/machine/keeper" - "planetmint-go/x/machine/types" ) func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { storeKey := sdk.NewKVStoreKey(types.StoreKey) + indexStoreKey := sdk.NewKVStoreKey(types.IndexKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) db := tmdb.NewMemDB() stateStore := store.NewCommitMultiStore(db) stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) + stateStore.MountStoreWithDB(indexStoreKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) require.NoError(t, stateStore.LoadLatestVersion()) @@ -39,6 +42,7 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { k := keeper.NewKeeper( cdc, storeKey, + indexStoreKey, memStoreKey, paramsSubspace, ) diff --git a/x/machine/keeper/keeper.go b/x/machine/keeper/keeper.go index d50beb9..19ac520 100644 --- a/x/machine/keeper/keeper.go +++ b/x/machine/keeper/keeper.go @@ -14,16 +14,18 @@ import ( type ( Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey - paramstore paramtypes.Subspace + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + indexStoreKey storetypes.StoreKey + memKey storetypes.StoreKey + paramstore paramtypes.Subspace } ) func NewKeeper( cdc codec.BinaryCodec, storeKey, + indexStoreKey, memKey storetypes.StoreKey, ps paramtypes.Subspace, @@ -34,10 +36,11 @@ func NewKeeper( } return &Keeper{ - cdc: cdc, - storeKey: storeKey, - memKey: memKey, - paramstore: ps, + cdc: cdc, + storeKey: storeKey, + indexStoreKey: indexStoreKey, + memKey: memKey, + paramstore: ps, } } diff --git a/x/machine/keeper/machine_test.go b/x/machine/keeper/machine_test.go new file mode 100644 index 0000000..95f8b88 --- /dev/null +++ b/x/machine/keeper/machine_test.go @@ -0,0 +1,33 @@ +package keeper_test + +import ( + "testing" + + keepertest "planetmint-go/testutil/keeper" + "planetmint-go/x/machine/keeper" + "planetmint-go/x/machine/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/assert" +) + +func createNMachine(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Machine { + items := make([]types.Machine, n) + for i := range items { + // fill out machine + items[i].IssuerPlanetmint = "asd" + items[i].IssuerLiquid = "dsa" + keeper.StoreMachine(ctx, items[i]) + } + return items +} + +func TestGetMachine(t *testing.T) { + keeper, ctx := keepertest.MachineKeeper(t) + items := createNMachine(keeper, ctx, 10) + for _, item := range items { + machineById, found := keeper.GetMachine(ctx, item.IssuerPlanetmint) + assert.True(t, found) + assert.Equal(t, item, machineById) + } +} diff --git a/x/machine/types/keys.go b/x/machine/types/keys.go index 1d13b47..ec9ea83 100644 --- a/x/machine/types/keys.go +++ b/x/machine/types/keys.go @@ -14,6 +14,8 @@ const ( MemStoreKey = "mem_machine" MachineKey = "Machine/value/" + + IndexKey = "Machine/index/" ) func KeyPrefix(p string) []byte {