diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index b19556fd9..401ca12d6 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -162,17 +162,15 @@ const main = async () => { // `_methods` is not part of the jayson.Server interface but exists on the object // but the docs recommend this pattern for custom routing // https://github.com/tedeh/jayson/blob/HEAD/examples/method_routing/server.js - //@ts-expect-error - if (!this._methods[method] && web3) { + if (!this.getMethod && web3) { return new jayson.Method(async function () { const res = await web3!.request(method, params) if (res.result) return res.result else return res.error }) } else { - log(`Received ${method} with params: ${params}`) - //@ts-expect-error - return this._methods[method] + log(`Received ${method} with params: ${JSON.stringify(params)}`) + return this.getMethod(method) } }, }) diff --git a/packages/cli/src/rpc/modules/portal.ts b/packages/cli/src/rpc/modules/portal.ts index 48d8f1b4c..26dc212d1 100644 --- a/packages/cli/src/rpc/modules/portal.ts +++ b/packages/cli/src/rpc/modules/portal.ts @@ -66,7 +66,7 @@ export class portal { this.methods = middleware(this.methods.bind(this), 0, []) this.historyNodeInfo = middleware(this.historyNodeInfo.bind(this), 0, []) this.historyRoutingTableInfo = middleware(this.historyRoutingTableInfo.bind(this), 0, []) - this.historyLookupEnr = middleware(this.historyLookupEnr.bind(this), 1, [[validators.enr]]) + this.historyLookupEnr = middleware(this.historyLookupEnr.bind(this), 1, [[validators.dstId]]) this.historyAddBootNode = middleware(this.historyAddBootNode.bind(this), 1, [[validators.enr]]) this.historyAddEnr = middleware(this.historyAddEnr.bind(this), 1, [[validators.enr]]) this.historyGetEnr = middleware(this.historyGetEnr.bind(this), 1, [[validators.dstId]]) @@ -85,7 +85,7 @@ export class portal { [validators.hex], ]) this.historyFindNodes = middleware(this.historyFindNodes.bind(this), 2, [ - [validators.dstId], + [validators.enr], [validators.array(validators.distance)], ]) this.historySendFindNodes = middleware(this.historySendFindNodes.bind(this), 2, [ @@ -108,7 +108,7 @@ export class portal { [validators.hex], ]) this.historyFindContent = middleware(this.historyFindContent.bind(this), 2, [ - [validators.dstId], + [validators.enr], [validators.hex], ]) this.historyRecursiveFindContent = middleware(this.historyRecursiveFindContent.bind(this), 1, [ @@ -178,11 +178,8 @@ export class portal { async historyGetEnr(params: [string]): Promise { const [nodeId] = params this.logger(`portal_historyGetEnr request received for ${nodeId.slice(0, 10)}...`) - const enr = this._history.routingTable.getValue(nodeId) - if (enr) { - return enr.encodeTxt() - } - return '' + const enr = this._history.routingTable.getWithPending(nodeId) + return enr?.value.encodeTxt() ?? '' } async historyAddEnr(params: [string]): Promise { @@ -203,8 +200,8 @@ export class portal { async historyDeleteEnr(params: [string]): Promise { this.logger(`portal_historyDeleteEnr request received.`) const [nodeId] = params - this._history.routingTable.removeById(nodeId) - return true + const remove = this._history.routingTable.removeById(nodeId) + return remove !== undefined } async historyRoutingTableInfo(_params: []): Promise { this.logger(`portal_historyRoutingTableInfo request received.`) @@ -227,9 +224,9 @@ export class portal { async historyLookupEnr(params: [string]) { const [nodeId] = params this.logger(`Looking up ENR for NodeId: ${shortId(nodeId)}`) - const enr = this._history.routingTable.getValue(nodeId)?.encodeTxt() + const enr = this._history.routingTable.getWithPending(nodeId)?.value.encodeTxt() this.logger(`Found: ${enr}`) - return enr + return enr ?? '' } async historyPing(params: [string]) { const [enr] = params @@ -243,8 +240,8 @@ export class portal { } return ( pong && { - enrSeq: pong.enrSeq, - dataRadius: pong.customPayload, + enrSeq: Number(pong.enrSeq), + dataRadius: toHexString(pong.customPayload), } ) } @@ -283,6 +280,12 @@ export class portal { if (!isValidId(dstId)) { return 'invalid node id' } + if (!this._history.routingTable.getValue(dstId)) { + const pong = await this.historyPing([dstId]) + if (!pong) { + return '' + } + } const res = await this._history.sendFindNodes(dstId, distances) this.logger(`findNodes request returned ${res?.total} enrs`) return res?.enrs.map((v) => toHexString(v)) @@ -348,7 +351,14 @@ export class portal { return toHexString(res) } async historyFindContent(params: [string, string]) { - const [nodeId, contentKey] = params + const [enr, contentKey] = params + const nodeId = ENR.decodeTxt(enr).nodeId + if (!this._history.routingTable.getValue(nodeId)) { + const pong = await this._history.sendPing(enr) + if (!pong) { + return + } + } const res = await this._history.sendFindContent(nodeId, fromHexString(contentKey)) return res } diff --git a/packages/cli/src/rpc/validators.ts b/packages/cli/src/rpc/validators.ts index 4fc0d4f4e..a9449a5de 100644 --- a/packages/cli/src/rpc/validators.ts +++ b/packages/cli/src/rpc/validators.ts @@ -1,4 +1,4 @@ -import { ContentType, ProtocolId } from 'portalnetwork' +import { ContentType, ProtocolId, toHexString } from 'portalnetwork' const INVALID_PARAMS = -32602 @@ -103,10 +103,15 @@ export const validators = { */ get dstId() { return (params: any[], index: number) => { + if (params[index]['raw'] !== undefined) { + params[index] = toHexString(Uint8Array.from(params[index]['raw'])).slice(2) + } if (typeof params[index] !== 'string') { return { code: INVALID_PARAMS, - message: `invalid argument ${index}: argument must be a string`, + message: `invalid argument ${index}: argument must be a string - received ${typeof params[ + index + ]}`, } } if (params[index].startsWith('0x')) {