From 0b3b5eaddaa15b53fd75b8ee2b8a672db8246576 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Mon, 21 Aug 2023 15:35:56 -0400 Subject: [PATCH] Add initial RPC stuff for Beacon network --- packages/cli/src/index.ts | 4 +- packages/cli/src/rpc/modules/portal.ts | 40 +++++++++++++++++++ .../src/subprotocols/beacon/beacon.ts | 3 ++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index cb422f2ad..c59b124ed 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -115,10 +115,11 @@ const main = async () => { //@ts-ignore Because level doesn't know how to get along with itself db, metrics, - supportedProtocols: [ProtocolId.HistoryNetwork], + supportedProtocols: [ProtocolId.HistoryNetwork, ProtocolId.BeaconLightClientNetwork], dataDir: args.datadir, }) portal.discv5.enableLogs() + portal.enableLog('*ultralight*, *Portal*, *ultralight:RPC*') let metricsServer: http.Server | undefined @@ -132,6 +133,7 @@ const main = async () => { } await portal.start() + // TODO - make this more intelligent const protocol = portal.protocols.get(ProtocolId.HistoryNetwork) if (args.bootnode) { protocol!.addBootNode(args.bootnode) diff --git a/packages/cli/src/rpc/modules/portal.ts b/packages/cli/src/rpc/modules/portal.ts index 3022c5bd0..9228c472b 100644 --- a/packages/cli/src/rpc/modules/portal.ts +++ b/packages/cli/src/rpc/modules/portal.ts @@ -19,6 +19,8 @@ import { ContentMessageType, AcceptMessage, decodeHistoryNetworkContentKey, + BeaconLightClientNetwork, + BeaconLightClientNetworkContentType, } from 'portalnetwork' import { GetEnrResult } from '../schema/types.js' import { isValidId } from '../util.js' @@ -47,6 +49,8 @@ const methods = [ 'portal_historyStore', 'portal_historyLocalContent', 'portal_historyGossip', + 'portal_beaconSendFindContent', + 'portal_beaconStore', // not included in portal-network-specs 'portal_historyAddEnrs', @@ -57,11 +61,15 @@ const methods = [ export class portal { private _client: PortalNetwork private _history: HistoryProtocol + private _beacon: BeaconLightClientNetwork private logger: Debugger constructor(client: PortalNetwork, logger: Debugger) { this._client = client this._history = this._client.protocols.get(ProtocolId.HistoryNetwork) as HistoryProtocol + this._beacon = this._client.protocols.get( + ProtocolId.BeaconLightClientNetwork, + ) as BeaconLightClientNetwork this.logger = logger this.methods = middleware(this.methods.bind(this), 0, []) this.historyNodeInfo = middleware(this.historyNodeInfo.bind(this), 0, []) @@ -132,6 +140,15 @@ export class portal { [validators.contentKey], [validators.hex], ]) + this.beaconSendFindContent = middleware(this.beaconSendFindContent.bind(this), 2, [ + [validators.dstId], + [validators.hex], + ]) + + this.beaconStore = middleware(this.beaconStore.bind(this), 2, [ + [validators.hex], + [validators.hex], + ]) } async methods() { return methods @@ -512,4 +529,27 @@ export class portal { return false } } + + async beaconSendFindContent(params: [string, string]) { + const [nodeId, contentKey] = params + console.log(nodeId) + const res = await this._beacon.sendFindContent(nodeId, fromHexString(contentKey)) + console.log(res) + const enr = this._beacon.routingTable.getWithPending(nodeId)?.value + return res && enr && '0x' + enr.seq.toString(16) + } + + async beaconStore(params: [string, string]) { + const [contentKey, content] = params.map((param) => fromHexString(param)) + try { + await this._beacon.store( + contentKey[0] as BeaconLightClientNetworkContentType, + toHexString(contentKey.slice(1)), + content, + ) + return true + } catch { + return false + } + } } diff --git a/packages/portalnetwork/src/subprotocols/beacon/beacon.ts b/packages/portalnetwork/src/subprotocols/beacon/beacon.ts index 50ab9e1ac..52430e5b3 100644 --- a/packages/portalnetwork/src/subprotocols/beacon/beacon.ts +++ b/packages/portalnetwork/src/subprotocols/beacon/beacon.ts @@ -192,6 +192,9 @@ export class BeaconLightClientNetwork extends BaseProtocol { if (contentType === BeaconLightClientNetworkContentType.LightClientUpdatesByRange) { throw new Error('special handling for update ranges not supported yet') } + this.logger( + `storing ${BeaconLightClientNetworkContentType[contentType]} content corresponding to ${contentKey}`, + ) await this.put(this.protocolId, contentKey, toHexString(value)) } }