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

Hive rpc updates (complete) #436

Merged
merged 10 commits into from
Aug 12, 2023
29 changes: 19 additions & 10 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ 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.hex]])
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.hex]])
this.historyDeleteEnr = middleware(this.historyDeleteEnr.bind(this), 1, [[validators.hex]])
this.historyGetEnr = middleware(this.historyGetEnr.bind(this), 1, [[validators.dstId]])
this.historyDeleteEnr = middleware(this.historyDeleteEnr.bind(this), 1, [[validators.dstId]])
this.historyAddEnrs = middleware(this.historyAddEnrs.bind(this), 1, [
[validators.array(validators.enr)],
])
Expand Down Expand Up @@ -177,11 +177,17 @@ 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.getWithPending(nodeId.slice(2))?.value
if (nodeId === this._client.discv5.enr.nodeId) {
return this._client.discv5.enr.encodeTxt()
}
this.logger.extend('portal_historyGetEnr')(` request received for ${nodeId.slice(0, 10)}...`)
const enr = this._history.routingTable.getWithPending(nodeId)?.value
if (enr) {
return enr.encodeTxt()
const enrTxt = enr.encodeTxt()
this.logger.extend('portal_historyGetEnr')(enrTxt)
return enrTxt
}
this.logger.extend('portal_historyGetEnr')('ENR not found')
return ''
}

Expand All @@ -201,9 +207,9 @@ export class portal {
}
}
async historyDeleteEnr(params: [string]): Promise<boolean> {
this.logger(`portal_historyDeleteEnr request received.`)
const [nodeId] = params
const remove = this._history.routingTable.removeById(nodeId.slice(2))
this.logger(`portal_historyDeleteEnr request received for ${nodeId.slice(0, 10)}...`)
const remove = this._history.routingTable.removeById(nodeId)
return remove !== undefined
}
async historyRoutingTableInfo(_params: []): Promise<any> {
Expand All @@ -226,8 +232,11 @@ export class portal {
}
async historyLookupEnr(params: [string]) {
const [nodeId] = params
if (nodeId === this._client.discv5.enr.nodeId) {
return this._client.discv5.enr.encodeTxt()
}
this.logger(`Looking up ENR for NodeId: ${shortId(nodeId)}`)
const enr = this._history.routingTable.getWithPending(nodeId.slice(2))?.value.encodeTxt()
const enr = this._history.routingTable.getWithPending(nodeId)?.value.encodeTxt()
this.logger(`Found: ${enr}`)
return enr ?? ''
}
Expand Down Expand Up @@ -358,7 +367,7 @@ export class portal {
const res = await this._history.findContentLocally(fromHexString(contentKey))
this.logger.extend(`historyLocalContent`)(`request returned ${res.length} bytes`)
this.logger.extend(`historyLocalContent`)(`${toHexString(res)}`)
return res.length > 0 ? toHexString(res) : undefined
return res.length > 0 ? toHexString(res) : '0x'
}
async historyFindContent(params: [string, string]) {
const [enr, contentKey] = params
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/rpc/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ export const validators = {
}
}
if (params[index].startsWith('0x')) {
return {
code: INVALID_PARAMS,
message: `invalid argument ${index}: NodeId should be without 0x prefix`,
}
params[index] = params[index].slice(2)
}
}
},
Expand Down