Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hive Test Failures #422

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
})
Expand Down
40 changes: 25 additions & 15 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand All @@ -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, [
Expand All @@ -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, [
Expand Down Expand Up @@ -178,11 +178,8 @@ export class portal {
async historyGetEnr(params: [string]): Promise<GetEnrResult> {
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<boolean> {
Expand All @@ -203,8 +200,8 @@ export class portal {
async historyDeleteEnr(params: [string]): Promise<boolean> {
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<any> {
this.logger(`portal_historyRoutingTableInfo request received.`)
Expand All @@ -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
Expand All @@ -243,8 +240,8 @@ export class portal {
}
return (
pong && {
enrSeq: pong.enrSeq,
dataRadius: pong.customPayload,
enrSeq: Number(pong.enrSeq),
dataRadius: toHexString(pong.customPayload),
}
)
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/rpc/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContentType, ProtocolId } from 'portalnetwork'
import { ContentType, ProtocolId, toHexString } from 'portalnetwork'

const INVALID_PARAMS = -32602

Expand Down Expand Up @@ -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')) {
Expand Down
Loading