From 52969474accec2182fa760c87c05dbafcb42705b Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Wed, 21 Jun 2023 10:03:24 +0200 Subject: [PATCH] added QueryGetMachineByPublicKey handler and updated proto files Signed-off-by: Lorenz Herzberger --- proto/planetmintgo/machine/query.proto | 5 +- x/machine/keeper/machine_test.go | 1 + .../keeper/query_get_machine_by_public_key.go | 13 +- .../query_get_machine_by_public_key_test.go | 54 +++++++++ x/machine/types/query.pb.go | 111 ++++++++++++++---- 5 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 x/machine/keeper/query_get_machine_by_public_key_test.go diff --git a/proto/planetmintgo/machine/query.proto b/proto/planetmintgo/machine/query.proto index 37e9754..5c7d86b 100644 --- a/proto/planetmintgo/machine/query.proto +++ b/proto/planetmintgo/machine/query.proto @@ -6,6 +6,7 @@ import "gogoproto/gogo.proto"; import "google/api/annotations.proto"; import "cosmos/base/query/v1beta1/pagination.proto"; import "planetmintgo/machine/params.proto"; +import "planetmintgo/machine/machine.proto"; option go_package = "planetmint-go/x/machine/types"; @@ -38,5 +39,7 @@ message QueryGetMachineByPublicKeyRequest { string publicKey = 1; } -message QueryGetMachineByPublicKeyResponse {} +message QueryGetMachineByPublicKeyResponse { + Machine machine = 1; +} diff --git a/x/machine/keeper/machine_test.go b/x/machine/keeper/machine_test.go index 01a1a89..e93fb9b 100644 --- a/x/machine/keeper/machine_test.go +++ b/x/machine/keeper/machine_test.go @@ -19,6 +19,7 @@ func createNMachine(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Machi items[i].IssuerPlanetmint = fmt.Sprintf("issuerPlanetmint%v", i) items[i].IssuerLiquid = fmt.Sprintf("issuerLiquid%v", i) keeper.StoreMachine(ctx, items[i]) + keeper.StoreMachineIndex(ctx, items[i]) } return items } diff --git a/x/machine/keeper/query_get_machine_by_public_key.go b/x/machine/keeper/query_get_machine_by_public_key.go index 160bfb2..5effa15 100644 --- a/x/machine/keeper/query_get_machine_by_public_key.go +++ b/x/machine/keeper/query_get_machine_by_public_key.go @@ -3,10 +3,11 @@ package keeper import ( "context" + "planetmint-go/x/machine/types" + sdk "github.com/cosmos/cosmos-sdk/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "planetmint-go/x/machine/types" ) func (k Keeper) GetMachineByPublicKey(goCtx context.Context, req *types.QueryGetMachineByPublicKeyRequest) (*types.QueryGetMachineByPublicKeyResponse, error) { @@ -16,8 +17,12 @@ func (k Keeper) GetMachineByPublicKey(goCtx context.Context, req *types.QueryGet ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Process the query - _ = ctx + machineIndex, found := k.GetMachineIndex(ctx, req.PublicKey) + if !found { + return nil, status.Error(codes.NotFound, "machine not found") + } - return &types.QueryGetMachineByPublicKeyResponse{}, nil + machine, _ := k.GetMachine(ctx, machineIndex) + + return &types.QueryGetMachineByPublicKeyResponse{Machine: &machine}, nil } diff --git a/x/machine/keeper/query_get_machine_by_public_key_test.go b/x/machine/keeper/query_get_machine_by_public_key_test.go new file mode 100644 index 0000000..d981c4b --- /dev/null +++ b/x/machine/keeper/query_get_machine_by_public_key_test.go @@ -0,0 +1,54 @@ +package keeper_test + +import ( + keepertest "planetmint-go/testutil/keeper" + "planetmint-go/x/machine/types" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func TestGetMachineByPublicKey(t *testing.T) { + keeper, ctx := keepertest.MachineKeeper(t) + wctx := sdk.WrapSDKContext(ctx) + msgs := createNMachine(keeper, ctx, 1) + for _, tc := range []struct { + desc string + request *types.QueryGetMachineByPublicKeyRequest + response *types.QueryGetMachineByPublicKeyResponse + err error + }{ + { + desc: "GetByMachineId", + request: &types.QueryGetMachineByPublicKeyRequest{PublicKey: msgs[0].MachineId}, + response: &types.QueryGetMachineByPublicKeyResponse{Machine: &msgs[0]}, + }, + { + desc: "GetByIssuerPlanetmint", + request: &types.QueryGetMachineByPublicKeyRequest{PublicKey: msgs[0].IssuerPlanetmint}, + response: &types.QueryGetMachineByPublicKeyResponse{Machine: &msgs[0]}, + }, + { + desc: "GetByIssuerLiquid", + request: &types.QueryGetMachineByPublicKeyRequest{PublicKey: msgs[0].IssuerLiquid}, + response: &types.QueryGetMachineByPublicKeyResponse{Machine: &msgs[0]}, + }, { + desc: "MachineNotFound", + request: &types.QueryGetMachineByPublicKeyRequest{PublicKey: "invalid key"}, + err: status.Error(codes.NotFound, "machine not found"), + }, + } { + tc := tc + t.Run(tc.desc, func(t *testing.T) { + response, err := keeper.GetMachineByPublicKey(wctx, tc.request) + if tc.err != nil { + require.ErrorIs(t, err, tc.err) + } else { + require.Equal(t, tc.response, response) + } + }) + } +} diff --git a/x/machine/types/query.pb.go b/x/machine/types/query.pb.go index 0be4585..9568c3b 100644 --- a/x/machine/types/query.pb.go +++ b/x/machine/types/query.pb.go @@ -158,6 +158,7 @@ func (m *QueryGetMachineByPublicKeyRequest) GetPublicKey() string { } type QueryGetMachineByPublicKeyResponse struct { + Machine *Machine `protobuf:"bytes,1,opt,name=machine,proto3" json:"machine,omitempty"` } func (m *QueryGetMachineByPublicKeyResponse) Reset() { *m = QueryGetMachineByPublicKeyResponse{} } @@ -193,6 +194,13 @@ func (m *QueryGetMachineByPublicKeyResponse) XXX_DiscardUnknown() { var xxx_messageInfo_QueryGetMachineByPublicKeyResponse proto.InternalMessageInfo +func (m *QueryGetMachineByPublicKeyResponse) GetMachine() *Machine { + if m != nil { + return m.Machine + } + return nil +} + func init() { proto.RegisterType((*QueryParamsRequest)(nil), "planetmintgo.machine.QueryParamsRequest") proto.RegisterType((*QueryParamsResponse)(nil), "planetmintgo.machine.QueryParamsResponse") @@ -203,32 +211,33 @@ func init() { func init() { proto.RegisterFile("planetmintgo/machine/query.proto", fileDescriptor_bf7841d43d757203) } var fileDescriptor_bf7841d43d757203 = []byte{ - // 391 bytes of a gzipped FileDescriptorProto + // 412 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x92, 0xcf, 0x6a, 0x1a, 0x41, - 0x1c, 0xc7, 0x77, 0xa5, 0x15, 0x9c, 0xde, 0xa6, 0x16, 0xca, 0xa2, 0xab, 0x2e, 0x2d, 0xd8, 0x42, - 0x77, 0xd0, 0x1e, 0x2c, 0xa5, 0x14, 0x22, 0x81, 0x1c, 0x42, 0x40, 0x3d, 0xe6, 0xb2, 0xcc, 0xca, - 0xb0, 0x59, 0xe2, 0xce, 0x8c, 0xce, 0x18, 0xb2, 0x84, 0x5c, 0x42, 0x1e, 0x20, 0x90, 0x97, 0xf2, - 0x28, 0xe4, 0x92, 0x53, 0x48, 0x34, 0x0f, 0x12, 0x9c, 0x99, 0x68, 0xc4, 0x8d, 0x24, 0xb7, 0xe1, - 0xb7, 0x9f, 0xef, 0x9f, 0xf9, 0xed, 0x80, 0x2a, 0x1f, 0x60, 0x4a, 0x64, 0x12, 0x53, 0x19, 0x31, - 0x94, 0xe0, 0xfe, 0x51, 0x4c, 0x09, 0x1a, 0x8e, 0xc9, 0x28, 0xf5, 0xf9, 0x88, 0x49, 0x06, 0x8b, - 0x2f, 0x09, 0xdf, 0x10, 0x4e, 0x31, 0x62, 0x11, 0x53, 0x00, 0x5a, 0x9c, 0x34, 0xeb, 0x94, 0x22, - 0xc6, 0xa2, 0x01, 0x41, 0x98, 0xc7, 0x08, 0x53, 0xca, 0x24, 0x96, 0x31, 0xa3, 0xc2, 0x7c, 0xfd, - 0xd9, 0x67, 0x22, 0x61, 0x02, 0x85, 0x58, 0x98, 0x08, 0x74, 0xd2, 0x08, 0x89, 0xc4, 0x0d, 0xc4, - 0x71, 0x14, 0x53, 0x05, 0x1b, 0xb6, 0x96, 0xd9, 0x8b, 0xe3, 0x11, 0x4e, 0x8c, 0x9d, 0x57, 0x04, - 0xb0, 0xbb, 0x30, 0xe9, 0xa8, 0x61, 0x8f, 0x0c, 0xc7, 0x44, 0x48, 0x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d, 0x65, 0xca, 0x89, 0x08, 0xf3, 0xea, 0xd5, 0xfd, 0x7e, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x05, - 0xe2, 0x2b, 0xbf, 0x32, 0x03, 0x00, 0x00, + 0x1c, 0xc7, 0x77, 0xa5, 0xb5, 0x38, 0xbd, 0x4d, 0x2d, 0x94, 0x45, 0x57, 0x5d, 0x28, 0xd8, 0x42, + 0x77, 0xd0, 0x1e, 0x2c, 0xa5, 0x14, 0x2a, 0x85, 0x1e, 0x4a, 0x41, 0xf7, 0x58, 0x28, 0xcb, 0xac, + 0x0c, 0xdb, 0x25, 0xee, 0xcc, 0xe8, 0x8c, 0x21, 0x4b, 0xc8, 0x25, 0xe4, 0x01, 0x02, 0x79, 0x29, + 0x8f, 0x42, 0x2e, 0x39, 0x85, 0x44, 0xf3, 0x20, 0xc1, 0x99, 0x51, 0x23, 0x6e, 0x24, 0x39, 0x39, + 0x8c, 0x9f, 0xef, 0x9f, 0xdf, 0xfc, 0x16, 0xd4, 0xf9, 0x10, 0x53, 0x22, 0xd3, 0x84, 0xca, 0x98, + 0xa1, 0x14, 0x0f, 0xfe, 0x27, 0x94, 0xa0, 0xd1, 0x84, 0x8c, 0x33, 0x9f, 0x8f, 0x99, 0x64, 0xb0, + 0xfc, 0x90, 0xf0, 0x0d, 0xe1, 0x94, 0x63, 0x16, 0x33, 0x05, 0xa0, 0xe5, 0x49, 0xb3, 0x4e, 0x25, + 0x66, 0x2c, 0x1e, 0x12, 0x84, 0x79, 0x82, 0x30, 0xa5, 0x4c, 0x62, 0x99, 0x30, 0x2a, 0xcc, 0xbf, + 0x1f, 0x07, 0x4c, 0xa4, 0x4c, 0xa0, 0x08, 0x0b, 0x13, 0x81, 0x0e, 0x5b, 0x11, 0x91, 0xb8, 0x85, + 0x38, 0x8e, 0x13, 0xaa, 0x60, 0xc3, 0x36, 0x72, 0x7b, 0x71, 0x3c, 0xc6, 0xe9, 0xca, 0xce, 0xcb, + 0x45, 0xcc, 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5, 0x06, 0x4f, 0xba, 0x9d, 0xe9, 0xdc, 0xb5, 0x67, 0x73, + 0xd7, 0xbe, 0x99, 0xbb, 0xf6, 0xf9, 0xc2, 0xb5, 0x66, 0x0b, 0xd7, 0xba, 0x5a, 0xb8, 0xd6, 0xdf, + 0xea, 0xb6, 0xed, 0xd1, 0xda, 0x58, 0x66, 0x9c, 0x88, 0xa8, 0xa8, 0x3e, 0xd7, 0xcf, 0xf7, 0x01, + 0x00, 0x00, 0xff, 0xff, 0xd2, 0x46, 0x91, 0x7e, 0x8f, 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -457,6 +466,18 @@ func (m *QueryGetMachineByPublicKeyResponse) MarshalToSizedBuffer(dAtA []byte) ( _ = i var l int _ = l + if m.Machine != nil { + { + size, err := m.Machine.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } return len(dAtA) - i, nil } @@ -510,6 +531,10 @@ func (m *QueryGetMachineByPublicKeyResponse) Size() (n int) { } var l int _ = l + if m.Machine != nil { + l = m.Machine.Size() + n += 1 + l + sovQuery(uint64(l)) + } return n } @@ -763,6 +788,42 @@ func (m *QueryGetMachineByPublicKeyResponse) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: QueryGetMachineByPublicKeyResponse: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Machine", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Machine == nil { + m.Machine = &Machine{} + } + if err := m.Machine.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipQuery(dAtA[iNdEx:])