diff --git a/x/machine/keeper/machine.go b/x/machine/keeper/machine.go index 0f972c5..3359df9 100644 --- a/x/machine/keeper/machine.go +++ b/x/machine/keeper/machine.go @@ -10,16 +10,10 @@ import ( func (k Keeper) StoreMachine(ctx sdk.Context, machine types.Machine) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey)) appendValue := k.cdc.MustMarshal(&machine) - k.StoreMachineIndex(ctx, machine) store.Set(GetMachineBytes(machine.IssuerPlanetmint), appendValue) } -func (k Keeper) GetMachine(ctx sdk.Context, pubKey string) (val types.Machine, found bool) { - index, found := k.GetMachineIndex(ctx, pubKey) - if !found { - return val, false - } - +func (k Keeper) GetMachine(ctx sdk.Context, index types.MachineIndex) (val types.Machine, found bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey)) machine := store.Get(GetMachineBytes(index.IssuerPlanetmint)) diff --git a/x/machine/keeper/machine_test.go b/x/machine/keeper/machine_test.go index 65f324e..01a1a89 100644 --- a/x/machine/keeper/machine_test.go +++ b/x/machine/keeper/machine_test.go @@ -23,31 +23,16 @@ func createNMachine(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Machi return items } -func TestGetMachineById(t *testing.T) { +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.MachineId) - assert.True(t, found) - assert.Equal(t, item, machineById) - } -} - -func TestGetMachineByIssuerPlanetmint(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) - } -} - -func TestGetMachineByIssuerLiquid(t *testing.T) { - keeper, ctx := keepertest.MachineKeeper(t) - items := createNMachine(keeper, ctx, 10) - for _, item := range items { - machineById, found := keeper.GetMachine(ctx, item.IssuerLiquid) + index := types.MachineIndex{ + MachineId: item.MachineId, + IssuerPlanetmint: item.IssuerPlanetmint, + IssuerLiquid: item.IssuerLiquid, + } + machineById, found := keeper.GetMachine(ctx, index) assert.True(t, found) assert.Equal(t, item, machineById) } diff --git a/x/machine/keeper/msg_server_attest_machine.go b/x/machine/keeper/msg_server_attest_machine.go index 662badf..d90bf02 100644 --- a/x/machine/keeper/msg_server_attest_machine.go +++ b/x/machine/keeper/msg_server_attest_machine.go @@ -3,15 +3,16 @@ package keeper import ( "context" - sdk "github.com/cosmos/cosmos-sdk/types" "planetmint-go/x/machine/types" + + sdk "github.com/cosmos/cosmos-sdk/types" ) func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMachine) (*types.MsgAttestMachineResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Handling the message - _ = ctx + k.StoreMachine(ctx, *msg.Machine) + k.StoreMachineIndex(ctx, *msg.Machine) return &types.MsgAttestMachineResponse{}, nil } diff --git a/x/machine/keeper/msg_server_test.go b/x/machine/keeper/msg_server_test.go index 4fef33a..b00d750 100644 --- a/x/machine/keeper/msg_server_test.go +++ b/x/machine/keeper/msg_server_test.go @@ -4,13 +4,27 @@ import ( "context" "testing" - sdk "github.com/cosmos/cosmos-sdk/types" keepertest "planetmint-go/testutil/keeper" + "planetmint-go/testutil/sample" "planetmint-go/x/machine/keeper" "planetmint-go/x/machine/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/assert" ) func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) { k, ctx := keepertest.MachineKeeper(t) return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx) } + +func TestMsgServerAttestMachine(t *testing.T) { + _, pk := sample.KeyPair() + machine := sample.Machine(pk, pk, pk) + msg := types.NewMsgAttestMachine(pk, &machine) + msgServer, ctx := setupMsgServer(t) + res, err := msgServer.AttestMachine(ctx, msg) + if assert.NoError(t, err) { + assert.Equal(t, &types.MsgAttestMachineResponse{}, res) + } +}