Skip to content

Commit

Permalink
NET-1252: external rpc to NetworkNodeFacade (#2698)
Browse files Browse the repository at this point in the history
## Summary

Add external rpc methods to the NetworkNodeStub

## Changes

Added better typing for the external rpc methods

## Future improvements 

- Create methods to use the methods from the public API
  • Loading branch information
juslesan committed Aug 1, 2024
1 parent ef16059 commit e45f3cb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@
"@babel/runtime-corejs3": "^7.24.7",
"@lit-protocol/core": "2.2.5",
"@lit-protocol/uint8arrays": "^6.1.0",
"@protobuf-ts/runtime": "^2.8.2",
"@protobuf-ts/runtime-rpc": "^2.8.2",
"@streamr/config": "^5.3.13",
"@streamr/dht": "101.0.1",
"@streamr/proto-rpc": "101.0.1",
"@streamr/trackerless-network": "101.0.1",
"@streamr/utils": "101.0.1",
"core-js": "^3.36.0",
Expand Down
18 changes: 18 additions & 0 deletions packages/sdk/src/NetworkNodeFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import { DhtAddress, PeerDescriptor } from '@streamr/dht'
import {
ExternalRpcClient,
ExternalRpcClientClass,
NetworkOptions,
StreamMessage as NewStreamMessage,
ProxyDirection,
Expand All @@ -20,6 +22,9 @@ import { StreamMessage as OldStreamMessage } from './protocol/StreamMessage'
import { StreamMessageTranslator } from './protocol/StreamMessageTranslator'
import { pOnce } from './utils/promises'
import { peerDescriptorTranslator } from './utils/utils'
import { ProtoRpcClient } from '@streamr/proto-rpc'
import { IMessageType } from '@protobuf-ts/runtime'
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'

// TODO should we make getNode() an internal method, and provide these all these services as client methods?
/** @deprecated This in an internal interface */
Expand Down Expand Up @@ -52,6 +57,19 @@ export interface NetworkNodeStub {
) => Promise<void>
isProxiedStreamPart(streamPartId: StreamPartID): boolean
setStreamPartEntryPoints: (streamPartId: StreamPartID, peerDescriptors: PeerDescriptor[]) => void
createExternalRpcClient<T extends ExternalRpcClient>(clientClass: ExternalRpcClientClass<T> ): ProtoRpcClient<T>
registerExternalNetworkRpcMethod<
RequestClass extends IMessageType<RequestType>,
ResponseClass extends IMessageType<ResponseType>,
RequestType extends object,
ResponseType extends object
>(
request: RequestClass,
response: ResponseClass,
name: string,
fn: (req: RequestType, context: ServerCallContext) => Promise<ResponseType>
): void

}

export interface Events {
Expand Down
17 changes: 16 additions & 1 deletion packages/sdk/test/test-utils/fake/FakeNetworkNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { DhtAddress, PeerDescriptor, getDhtAddressFromRaw } from '@streamr/dht'
import { NetworkOptions, StreamMessage as NewStreamMessage, ProxyDirection } from '@streamr/trackerless-network'
import {
ExternalRpcClient,
NetworkOptions,
StreamMessage as NewStreamMessage,
ProxyDirection
} from '@streamr/trackerless-network'
import { EthereumAddress, MetricsContext, StreamPartID } from '@streamr/utils'
import crypto from 'crypto'
import pull from 'lodash/pull'
import { Lifecycle, scoped } from 'tsyringe'
import { NetworkNodeFactory, NetworkNodeStub } from '../../../src/NetworkNodeFacade'
import { StreamMessageTranslator } from '../../../src/protocol/StreamMessageTranslator'
import { FakeNetwork } from './FakeNetwork'
import { ProtoRpcClient } from '@streamr/proto-rpc'

type MessageListener = (msg: NewStreamMessage) => void

Expand Down Expand Up @@ -127,6 +133,15 @@ export class FakeNetworkNode implements NetworkNodeStub {
getDiagnosticInfo(): Record<string, unknown> {
return {}
}

// eslint-disable-next-line class-methods-use-this
createExternalRpcClient<T extends ExternalRpcClient>(): ProtoRpcClient<T> {
return {} as any
}

// eslint-disable-next-line class-methods-use-this
registerExternalNetworkRpcMethod(): void {}

}

@scoped(Lifecycle.ContainerScoped)
Expand Down
9 changes: 4 additions & 5 deletions packages/trackerless-network/src/NetworkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { DhtAddress, PeerDescriptor } from '@streamr/dht'
import { EthereumAddress, MetricsContext, StreamPartID } from '@streamr/utils'
import { NetworkOptions, NetworkStack, NodeInfo } from './NetworkStack'
import { ProxyDirection, StreamMessage } from './proto/packages/trackerless-network/protos/NetworkRpc'
import { ExternalNetworkRpc } from './logic/ExternalNetworkRpc'
import { ExternalNetworkRpc, ExternalRpcClient, ExternalRpcClientClass } from './logic/ExternalNetworkRpc'
import { IMessageType } from '@protobuf-ts/runtime'
import { ServerCallContext, ServiceInfo } from '@protobuf-ts/runtime-rpc'
import { ClassType, ClientTransport, ProtoRpcClient } from '@streamr/proto-rpc'
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
import { ProtoRpcClient } from '@streamr/proto-rpc'

export const createNetworkNode = (opts: NetworkOptions): NetworkNode => {
return new NetworkNode(new NetworkStack(opts))
Expand Down Expand Up @@ -132,8 +132,7 @@ export class NetworkNode {
this.externalNetworkRpc!.registerRpcMethod(request, response, name, fn)
}

// eslint-disable-next-line @typescript-eslint/prefer-function-type
createExternalRpcClient<T extends ServiceInfo & ClassType>(clientClass: { new (clientTransport: ClientTransport): T }): ProtoRpcClient<T> {
createExternalRpcClient<T extends ExternalRpcClient>(clientClass: ExternalRpcClientClass<T> ): ProtoRpcClient<T> {
return this.externalNetworkRpc!.createRpcClient(clientClass)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/trackerless-network/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export {
SignatureType,
StreamMessage
} from './proto/packages/trackerless-network/protos/NetworkRpc'

export { ExternalRpcClient, ExternalRpcClientClass } from './logic/ExternalNetworkRpc'
8 changes: 6 additions & 2 deletions packages/trackerless-network/src/logic/ExternalNetworkRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { ClassType, ClientTransport, ProtoRpcClient, toProtoRpcClient } from '@s

export const SERVICE_ID = 'external-network-service'

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type ExternalRpcClient = ServiceInfo & ClassType
// eslint-disable-next-line @typescript-eslint/prefer-function-type, @typescript-eslint/consistent-type-definitions
export type ExternalRpcClientClass<T extends ExternalRpcClient> = { new (clientTransport: ClientTransport): T }

export class ExternalNetworkRpc {

private readonly rpcCommunicator: ListeningRpcCommunicator
Expand All @@ -27,8 +32,7 @@ export class ExternalNetworkRpc {
this.rpcCommunicator.registerRpcMethod(request, response, name, fn)
}

// eslint-disable-next-line @typescript-eslint/prefer-function-type
createRpcClient<T extends ServiceInfo & ClassType>(clientClass: { new (clientTransport: ClientTransport): T }): ProtoRpcClient<T> {
createRpcClient<T extends ExternalRpcClient>(clientClass: ExternalRpcClientClass<T>): ProtoRpcClient<T> {
return toProtoRpcClient(new clientClass(this.rpcCommunicator.getRpcClientTransport()))
}

Expand Down

0 comments on commit e45f3cb

Please sign in to comment.