From 86b378c0591cb814e810fe57eacad04c35efaedd Mon Sep 17 00:00:00 2001 From: winprn Date: Mon, 16 Dec 2024 14:43:42 +0700 Subject: [PATCH 1/3] feat: add reflection service to rpc --- packages/node/package.json | 2 ++ packages/node/src/rpc/index.ts | 11 +++++++++++ pnpm-lock.yaml | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/packages/node/package.json b/packages/node/package.json index 6daa5a4a..e898a0aa 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -34,6 +34,8 @@ "dependencies": { "@chainsafe/libp2p-gossipsub": "^14.1.0", "@grpc/grpc-js": "^1.12.2", + "@grpc/proto-loader": "^0.7.13", + "@grpc/reflection": "^1.0.4", "@libp2p/interface": "^2.1.3", "dotenv": "^16.4.5", "@ts-drp/blueprints": "0.4.0", diff --git a/packages/node/src/rpc/index.ts b/packages/node/src/rpc/index.ts index b3cd0c01..6a340298 100644 --- a/packages/node/src/rpc/index.ts +++ b/packages/node/src/rpc/index.ts @@ -1,4 +1,6 @@ import * as grpc from "@grpc/grpc-js"; +import * as protoLoader from "@grpc/proto-loader"; +import * as reflection from "@grpc/reflection"; import type { ServerUnaryCall, sendUnaryData } from "@grpc/grpc-js"; import { type DRPNode, log } from "../index.js"; @@ -11,6 +13,11 @@ import type { UnsubscribeDRPRequest, UnsubscribeDRPResponse, } from "../proto/drp/node/v1/rpc_pb.js"; +import path, { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const PROTO_PATH = path.resolve(__dirname, "../proto/topology/node/rpc.proto"); export function init(node: DRPNode) { function subscribeDRP( @@ -70,7 +77,11 @@ export function init(node: DRPNode) { callback(null, response); } + const packageDefinition = protoLoader.loadSync(PROTO_PATH); + const reflectionService = new reflection.ReflectionService(packageDefinition); + const server = new grpc.Server(); + reflectionService.addToServer(server); server.addService(DRPRpcService, { subscribeDRP, unsubscribeDRP, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a7039d2c..b7420d28 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -227,6 +227,12 @@ importers: '@grpc/grpc-js': specifier: ^1.12.2 version: 1.12.4 + '@grpc/proto-loader': + specifier: ^0.7.13 + version: 0.7.13 + '@grpc/reflection': + specifier: ^1.0.4 + version: 1.0.4(@grpc/grpc-js@1.12.4) '@libp2p/interface': specifier: ^2.1.3 version: 2.2.1 @@ -1474,6 +1480,11 @@ packages: engines: {node: '>=6'} hasBin: true + '@grpc/reflection@1.0.4': + resolution: {integrity: sha512-znA8v4AviOD3OPOxy11pxrtP8k8DanpefeTymS8iGW1fVr1U2cHuzfhYqDPHnVNDf4qvF9E25KtSihPy2DBWfQ==} + peerDependencies: + '@grpc/grpc-js': ^1.8.21 + '@iarna/toml@2.2.5': resolution: {integrity: sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==} @@ -6304,6 +6315,12 @@ snapshots: protobufjs: 7.4.0 yargs: 17.7.2 + '@grpc/reflection@1.0.4(@grpc/grpc-js@1.12.4)': + dependencies: + '@grpc/grpc-js': 1.12.4 + '@grpc/proto-loader': 0.7.13 + protobufjs: 7.4.0 + '@iarna/toml@2.2.5': {} '@inquirer/figures@1.0.8': {} From 7fafcce23a8001c1eba9798946f383187290eac0 Mon Sep 17 00:00:00 2001 From: winprn Date: Mon, 16 Dec 2024 14:50:49 +0700 Subject: [PATCH 2/3] fix: biome --- packages/node/src/rpc/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/node/src/rpc/index.ts b/packages/node/src/rpc/index.ts index 6a340298..b7b5156c 100644 --- a/packages/node/src/rpc/index.ts +++ b/packages/node/src/rpc/index.ts @@ -2,6 +2,8 @@ import * as grpc from "@grpc/grpc-js"; import * as protoLoader from "@grpc/proto-loader"; import * as reflection from "@grpc/reflection"; +import path, { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; import type { ServerUnaryCall, sendUnaryData } from "@grpc/grpc-js"; import { type DRPNode, log } from "../index.js"; import { DRPRpcService } from "../proto/drp/node/v1/rpc_grpc_pb.js"; @@ -13,8 +15,6 @@ import type { UnsubscribeDRPRequest, UnsubscribeDRPResponse, } from "../proto/drp/node/v1/rpc_pb.js"; -import path, { dirname } from "node:path"; -import { fileURLToPath } from "node:url"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const PROTO_PATH = path.resolve(__dirname, "../proto/topology/node/rpc.proto"); From c9d6a38c5637934ffd58c172d461dd7074a114c7 Mon Sep 17 00:00:00 2001 From: winprn Date: Tue, 17 Dec 2024 11:39:44 +0700 Subject: [PATCH 3/3] fix: update proto stuff --- .../node/src/proto/drp/node/v1/rpc_grpc_pb.js | 56 ++++++++++++------- packages/node/src/rpc/index.ts | 9 +-- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/packages/node/src/proto/drp/node/v1/rpc_grpc_pb.js b/packages/node/src/proto/drp/node/v1/rpc_grpc_pb.js index 62562741..c80475cb 100644 --- a/packages/node/src/proto/drp/node/v1/rpc_grpc_pb.js +++ b/packages/node/src/proto/drp/node/v1/rpc_grpc_pb.js @@ -1,62 +1,78 @@ import * as node_src_proto_rpc_pb from './rpc_pb.js'; function serialize_drp_node_GetDRPHashGraphRequest(arg) { - let encoded = node_src_proto_rpc_pb.GetDRPHashGraphRequest.encode(arg).finish() - return Buffer.from(encoded); + let encoded = + node_src_proto_rpc_pb.GetDRPHashGraphRequest.encode(arg).finish(); + return Buffer.from(encoded); } function deserialize_drp_node_GetDRPHashGraphRequest(buffer_arg) { - return node_src_proto_rpc_pb.GetDRPHashGraphRequest.decode(new Uint8Array(buffer_arg)); + return node_src_proto_rpc_pb.GetDRPHashGraphRequest.decode( + new Uint8Array(buffer_arg) + ); } function serialize_drp_node_GetDRPHashGraphResponse(arg) { - let encoded = node_src_proto_rpc_pb.GetDRPHashGraphResponse.encode(arg).finish() - return Buffer.from(encoded); + let encoded = + node_src_proto_rpc_pb.GetDRPHashGraphResponse.encode(arg).finish(); + return Buffer.from(encoded); } function deserialize_drp_node_GetDRPHashGraphResponse(buffer_arg) { - return node_src_proto_rpc_pb.GetDRPHashGraphResponse.decode(new Uint8Array(buffer_arg)); + return node_src_proto_rpc_pb.GetDRPHashGraphResponse.decode( + new Uint8Array(buffer_arg) + ); } function serialize_drp_node_SubscribeDRPRequest(arg) { - let encoded = node_src_proto_rpc_pb.SubscribeDRPRequest.encode(arg).finish() - return Buffer.from(encoded); + let encoded = node_src_proto_rpc_pb.SubscribeDRPRequest.encode(arg).finish(); + return Buffer.from(encoded); } function deserialize_drp_node_SubscribeDRPRequest(buffer_arg) { - return node_src_proto_rpc_pb.SubscribeDRPRequest.decode(new Uint8Array(buffer_arg)); + return node_src_proto_rpc_pb.SubscribeDRPRequest.decode( + new Uint8Array(buffer_arg) + ); } function serialize_drp_node_SubscribeDRPResponse(arg) { - let encoded = node_src_proto_rpc_pb.SubscribeDRPResponse.encode(arg).finish() + let encoded = node_src_proto_rpc_pb.SubscribeDRPResponse.encode(arg).finish(); return Buffer.from(encoded); } function deserialize_drp_node_SubscribeDRPResponse(buffer_arg) { - return node_src_proto_rpc_pb.SubscribeDRPResponse.decode(new Uint8Array(buffer_arg)); + return node_src_proto_rpc_pb.SubscribeDRPResponse.decode( + new Uint8Array(buffer_arg) + ); } function serialize_drp_node_UnsubscribeDRPRequest(arg) { - let encoded = node_src_proto_rpc_pb.UnsubscribeDRPRequest.encode(arg).finish() - return Buffer.from(encoded); + let encoded = + node_src_proto_rpc_pb.UnsubscribeDRPRequest.encode(arg).finish(); + return Buffer.from(encoded); } function deserialize_drp_node_UnsubscribeDRPRequest(buffer_arg) { - return node_src_proto_rpc_pb.UnsubscribeDRPRequest.decode(new Uint8Array(buffer_arg)); + return node_src_proto_rpc_pb.UnsubscribeDRPRequest.decode( + new Uint8Array(buffer_arg) + ); } function serialize_drp_node_UnsubscribeDRPResponse(arg) { - let encoded = node_src_proto_rpc_pb.UnsubscribeDRPResponse.encode(arg).finish() - return Buffer.from(encoded); + let encoded = + node_src_proto_rpc_pb.UnsubscribeDRPResponse.encode(arg).finish(); + return Buffer.from(encoded); } function deserialize_drp_node_UnsubscribeDRPResponse(buffer_arg) { - return node_src_proto_rpc_pb.UnsubscribeDRPResponse.decode(new Uint8Array(buffer_arg)); + return node_src_proto_rpc_pb.UnsubscribeDRPResponse.decode( + new Uint8Array(buffer_arg) + ); } export const DRPRpcService = { subscribeDRP: { - path: '/drp.node.drpRpc/subscribeDRP', + path: '/drp.node.v1.DrpRpcService/SubscribeDRP', requestStream: false, responseStream: false, requestType: node_src_proto_rpc_pb.SubscribeDRPRequest, @@ -67,7 +83,7 @@ export const DRPRpcService = { responseDeserialize: deserialize_drp_node_SubscribeDRPResponse, }, unsubscribeDRP: { - path: '/drp.node.drpRpc/unsubscribeDRP', + path: '/drp.node.v1.DrpRpcService/UnsubscribeDRP', requestStream: false, responseStream: false, requestType: node_src_proto_rpc_pb.UnsubscribeDRPRequest, @@ -78,7 +94,7 @@ export const DRPRpcService = { responseDeserialize: deserialize_drp_node_UnsubscribeDRPResponse, }, getDRPHashGraph: { - path: '/drp.node.drpRpc/getDRPHashGraph', + path: '/drp.node.v1.DrpRpcService/GetDRPHashGraph', requestStream: false, responseStream: false, requestType: node_src_proto_rpc_pb.GetDRPHashGraphRequest, diff --git a/packages/node/src/rpc/index.ts b/packages/node/src/rpc/index.ts index b7b5156c..3c0bce33 100644 --- a/packages/node/src/rpc/index.ts +++ b/packages/node/src/rpc/index.ts @@ -15,9 +15,6 @@ import type { UnsubscribeDRPRequest, UnsubscribeDRPResponse, } from "../proto/drp/node/v1/rpc_pb.js"; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); -const PROTO_PATH = path.resolve(__dirname, "../proto/topology/node/rpc.proto"); export function init(node: DRPNode) { function subscribeDRP( @@ -77,7 +74,11 @@ export function init(node: DRPNode) { callback(null, response); } - const packageDefinition = protoLoader.loadSync(PROTO_PATH); + const protoPath = path.resolve( + dirname(fileURLToPath(import.meta.url)), + "../proto/drp/node/v1/rpc.proto", + ); + const packageDefinition = protoLoader.loadSync(protoPath); const reflectionService = new reflection.ReflectionService(packageDefinition); const server = new grpc.Server();