added attest-machine msg service

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-06-20 09:37:30 +02:00
parent 41365b23d5
commit 3edac9aea1
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
4 changed files with 27 additions and 33 deletions

View File

@ -10,16 +10,10 @@ import (
func (k Keeper) StoreMachine(ctx sdk.Context, machine types.Machine) { func (k Keeper) StoreMachine(ctx sdk.Context, machine types.Machine) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey)) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey))
appendValue := k.cdc.MustMarshal(&machine) appendValue := k.cdc.MustMarshal(&machine)
k.StoreMachineIndex(ctx, machine)
store.Set(GetMachineBytes(machine.IssuerPlanetmint), appendValue) store.Set(GetMachineBytes(machine.IssuerPlanetmint), appendValue)
} }
func (k Keeper) GetMachine(ctx sdk.Context, pubKey string) (val types.Machine, found bool) { func (k Keeper) GetMachine(ctx sdk.Context, index types.MachineIndex) (val types.Machine, found bool) {
index, found := k.GetMachineIndex(ctx, pubKey)
if !found {
return val, false
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey)) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey))
machine := store.Get(GetMachineBytes(index.IssuerPlanetmint)) machine := store.Get(GetMachineBytes(index.IssuerPlanetmint))

View File

@ -23,31 +23,16 @@ func createNMachine(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Machi
return items return items
} }
func TestGetMachineById(t *testing.T) { func TestGetMachine(t *testing.T) {
keeper, ctx := keepertest.MachineKeeper(t) keeper, ctx := keepertest.MachineKeeper(t)
items := createNMachine(keeper, ctx, 10) items := createNMachine(keeper, ctx, 10)
for _, item := range items { for _, item := range items {
machineById, found := keeper.GetMachine(ctx, item.MachineId) index := types.MachineIndex{
assert.True(t, found) MachineId: item.MachineId,
assert.Equal(t, item, machineById) IssuerPlanetmint: item.IssuerPlanetmint,
} IssuerLiquid: item.IssuerLiquid,
} }
machineById, found := keeper.GetMachine(ctx, index)
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)
assert.True(t, found) assert.True(t, found)
assert.Equal(t, item, machineById) assert.Equal(t, item, machineById)
} }

View File

@ -3,15 +3,16 @@ package keeper
import ( import (
"context" "context"
sdk "github.com/cosmos/cosmos-sdk/types"
"planetmint-go/x/machine/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) { func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMachine) (*types.MsgAttestMachineResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx) ctx := sdk.UnwrapSDKContext(goCtx)
// TODO: Handling the message k.StoreMachine(ctx, *msg.Machine)
_ = ctx k.StoreMachineIndex(ctx, *msg.Machine)
return &types.MsgAttestMachineResponse{}, nil return &types.MsgAttestMachineResponse{}, nil
} }

View File

@ -4,13 +4,27 @@ import (
"context" "context"
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "planetmint-go/testutil/keeper" keepertest "planetmint-go/testutil/keeper"
"planetmint-go/testutil/sample"
"planetmint-go/x/machine/keeper" "planetmint-go/x/machine/keeper"
"planetmint-go/x/machine/types" "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) { func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) {
k, ctx := keepertest.MachineKeeper(t) k, ctx := keepertest.MachineKeeper(t)
return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx) 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)
}
}