Skip to content

Commit

Permalink
feat: RPC server for node while running in server mode (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak authored Oct 5, 2024
1 parent 21c32e4 commit 2aed724
Show file tree
Hide file tree
Showing 17 changed files with 741 additions and 27 deletions.
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ignore": [
"**/dist/*",
"**/docs/*",
"**/*_pb.js",
"**/*_pb.ts",
"**/bundle/*",
"**/package.json"
Expand Down
9 changes: 8 additions & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
version: v2
managed:
enabled: true
plugins:
- local: ./node_modules/ts-proto/protoc-gen-ts_proto
strategy: directory
out: ./packages
opt:
- esModuleInterop=true
- fileSuffix=_pb
- ts_proto_opt=outputServices=grpc-js
- remote: buf.build/grpc/node
out: ./packages
opt:
- ts_proto_opt=esModuleInterop=true
inputs:
- directory: ./packages
exclude_paths:
- packages/node/node_modules
- packages/network/node_modules
- packages/object/node_modules
2 changes: 2 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by buf. DO NOT EDIT.
version: v2
11 changes: 11 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v2
modules:
# - path: packages/node
# excludes:
# - packages/node/node_modules
- path: packages/network
excludes:
- packages/network/node_modules
- path: packages/object
excludes:
- packages/object/node_modules
1 change: 0 additions & 1 deletion examples/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@topology-foundation/object": "0.2.0",
"assemblyscript": "^0.27.29",
"crypto-browserify": "^3.12.0",
"memfs": "^4.11.1",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"ts-node": "^10.9.2",
Expand Down
4 changes: 1 addition & 3 deletions examples/chat/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export default defineConfig({
},
plugins: [
nodePolyfills({
overrides: {
fs: "memfs",
},
overrides: {},
}),
],
optimizeDeps: {
Expand Down
9 changes: 2 additions & 7 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@ import { dcutr } from "@libp2p/dcutr";
import { devToolsMetrics } from "@libp2p/devtools-metrics";
import { identify } from "@libp2p/identify";
import type {
Ed25519PeerId,
EventCallback,
PrivateKey,
PubSub,
RSAPeerId,
Secp256k1PeerId,
Stream,
StreamHandler,
URLPeerId,
} from "@libp2p/interface";
import { peerIdFromString } from "@libp2p/peer-id";
import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery";
import { webRTC, webRTCDirect } from "@libp2p/webrtc";
import { webSockets } from "@libp2p/websockets";
Expand All @@ -44,6 +38,7 @@ export interface TopologyNetworkNodeConfig {
addresses?: string[];
bootstrap?: boolean;
bootstrap_peers?: string[];
browser_metrics?: boolean;
private_key_seed?: string;
}

Expand Down Expand Up @@ -79,7 +74,7 @@ export class TopologyNetworkNode {
return false;
},
},
metrics: devToolsMetrics(),
metrics: this._config?.browser_metrics ? devToolsMetrics() : undefined,
peerDiscovery: [
pubsubPeerDiscovery({
interval: 10_000,
Expand Down
5 changes: 4 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@
"test": "vitest"
},
"devDependencies": {
"@bufbuild/protobuf": "^2.0.0",
"@types/node": "^22.5.4",
"tsx": "4.19.1",
"typescript": "^5.5.4",
"vitest": "^2.1.1"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^14.1.0",
"@grpc/grpc-js": "^1.11.3",
"@libp2p/interface": "^2.1.0",
"@topology-foundation/blueprints": "0.2.0",
"@topology-foundation/network": "0.2.0",
"@topology-foundation/object": "0.2.0",
"commander": "^12.1.0"
"commander": "^12.1.0",
"google-protobuf": "^3.21.2"
}
}
35 changes: 35 additions & 0 deletions packages/node/src/proto/rpc.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
syntax = "proto3";
package topology.node;

service TopologyRpc {
rpc subscribeCro (SubscribeCroRequest ) returns (SubscribeCroResponse );
rpc unsubscribeCro (UnsubscribeCroRequest ) returns (UnsubscribeCroRequest );
rpc getCroHashGraph (GetCroHashGraphRequest ) returns (GetCroHashGraphResponse );
}

message SubscribeCroRequest {
string cro_id = 1;
}

message SubscribeCroResponse {
// can return error codes if different than 0
int32 return_code = 1;
}

message UnsubscribeCroRequest {
string cro_id = 1;
}

message UnsubscribeCroResponse {
// can return error codes if different than 0
int32 return_code = 1;
}

message GetCroHashGraphRequest {
string cro_id = 1;
}

message GetCroHashGraphResponse {
// linearized vertices hashes
repeated string vertices_hashes = 1;
}
82 changes: 82 additions & 0 deletions packages/node/src/proto/rpc_grpc_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as node_src_proto_rpc_pb from './rpc_pb.js';

function serialize_topology_node_GetCroHashGraphRequest(arg) {
let encoded = node_src_proto_rpc_pb.GetCroHashGraphRequest.encode(arg).finish()
return Buffer.from(encoded);
}

function deserialize_topology_node_GetCroHashGraphRequest(buffer_arg) {
return node_src_proto_rpc_pb.GetCroHashGraphRequest.decode(new Uint8Array(buffer_arg));
}

function serialize_topology_node_GetCroHashGraphResponse(arg) {
let encoded = node_src_proto_rpc_pb.GetCroHashGraphResponse.encode(arg).finish()
return Buffer.from(encoded);
}

function deserialize_topology_node_GetCroHashGraphResponse(buffer_arg) {
return node_src_proto_rpc_pb.GetCroHashGraphResponse.decode(new Uint8Array(buffer_arg));
}

function serialize_topology_node_SubscribeCroRequest(arg) {
let encoded = node_src_proto_rpc_pb.SubscribeCroRequest.encode(arg).finish()
return Buffer.from(encoded);
}

function deserialize_topology_node_SubscribeCroRequest(buffer_arg) {
return node_src_proto_rpc_pb.SubscribeCroRequest.decode(new Uint8Array(buffer_arg));
}

function serialize_topology_node_SubscribeCroResponse(arg) {
let encoded = node_src_proto_rpc_pb.SubscribeCroResponse.encode(arg).finish()
return Buffer.from(encoded);
}

function deserialize_topology_node_SubscribeCroResponse(buffer_arg) {
return node_src_proto_rpc_pb.SubscribeCroResponse.decode(new Uint8Array(buffer_arg));
}

function serialize_topology_node_UnsubscribeCroRequest(arg) {
let encoded = node_src_proto_rpc_pb.UnsubscribeCroRequest.encode(arg).finish()
return Buffer.from(encoded);
}

function deserialize_topology_node_UnsubscribeCroRequest(buffer_arg) {
return node_src_proto_rpc_pb.UnsubscribeCroRequest.decode(new Uint8Array(buffer_arg));
}

export const TopologyRpcService = {
subscribeCro: {
path: '/topology.node.TopologyRpc/subscribeCro',
requestStream: false,
responseStream: false,
requestType: node_src_proto_rpc_pb.SubscribeCroRequest,
responseType: node_src_proto_rpc_pb.SubscribeCroResponse,
requestSerialize: serialize_topology_node_SubscribeCroRequest,
requestDeserialize: deserialize_topology_node_SubscribeCroRequest,
responseSerialize: serialize_topology_node_SubscribeCroResponse,
responseDeserialize: deserialize_topology_node_SubscribeCroResponse,
},
unsubscribeCro: {
path: '/topology.node.TopologyRpc/unsubscribeCro',
requestStream: false,
responseStream: false,
requestType: node_src_proto_rpc_pb.UnsubscribeCroRequest,
responseType: node_src_proto_rpc_pb.UnsubscribeCroRequest,
requestSerialize: serialize_topology_node_UnsubscribeCroRequest,
requestDeserialize: deserialize_topology_node_UnsubscribeCroRequest,
responseSerialize: serialize_topology_node_UnsubscribeCroRequest,
responseDeserialize: deserialize_topology_node_UnsubscribeCroRequest,
},
getCroHashGraph: {
path: '/topology.node.TopologyRpc/getCroHashGraph',
requestStream: false,
responseStream: false,
requestType: node_src_proto_rpc_pb.GetCroHashGraphRequest,
responseType: node_src_proto_rpc_pb.GetCroHashGraphResponse,
requestSerialize: serialize_topology_node_GetCroHashGraphRequest,
requestDeserialize: deserialize_topology_node_GetCroHashGraphRequest,
responseSerialize: serialize_topology_node_GetCroHashGraphResponse,
responseDeserialize: deserialize_topology_node_GetCroHashGraphResponse,
},
};
Loading

0 comments on commit 2aed724

Please sign in to comment.