Skip to content

Commit

Permalink
Clean up RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Dec 5, 2024
1 parent bba1737 commit 8d9219f
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EntryStatus } from '@chainsafe/discv5'
import { ENR } from '@chainsafe/enr'
import { BitArray } from '@chainsafe/ssz'
import { bigIntToHex, bytesToHex, hexToBytes, short } from '@ethereumjs/util'
Expand Down Expand Up @@ -411,29 +410,29 @@ export class portal {
return this._client.discv5.enr.encodeTxt()
}
this.logger(`Looking up ENR for NodeId: ${shortId(nodeId)}`)
const enr = this._history.routingTable.getWithPending(nodeId)?.value.encodeTxt()
const enr = this._client.enrCache.get(nodeId)?.encodeTxt()
this.logger(`Found: ${enr}`)
return enr ?? ''
return enr
}
async stateLookupEnr(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._state.routingTable.getWithPending(nodeId)?.value.encodeTxt()
const enr = this._client.enrCache.get(nodeId)?.encodeTxt()
this.logger(`Found: ${enr}`)
return enr ?? ''
return enr
}
async beaconLookupEnr(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._beacon.routingTable.getWithPending(nodeId)?.value.encodeTxt()
const enr = this._client.enrCache.get(nodeId)?.encodeTxt()
this.logger(`Found: ${enr}`)
return enr ?? ''
return enr
}
// portal_*AddEnr
async historyAddEnr(params: [string]): Promise<boolean> {
Expand All @@ -442,11 +441,11 @@ export class portal {
const shortEnr = encodedENR.nodeId.slice(0, 15) + '...'
this.logger(`portal_historyAddEnr request received for ${shortEnr}`)
try {
if (this._history.routingTable.getWithPending(encodedENR.nodeId)?.value) {
if (this._client.enrCache.get(encodedENR.nodeId) !== undefined) {
return true
}
this._client.discv5.addEnr(enr)
this._history.routingTable.insertOrUpdate(encodedENR, EntryStatus.Connected)
this._client.enrCache.set(encodedENR.nodeId, encodedENR)
return true
} catch {
return false
Expand All @@ -458,11 +457,11 @@ export class portal {
const shortEnr = encodedENR.nodeId.slice(0, 15) + '...'
this.logger(`portal_stateAddEnr request received for ${shortEnr}`)
try {
if (this._state.routingTable.getWithPending(encodedENR.nodeId)?.value) {
if (this._client.enrCache.get(encodedENR.nodeId) !== undefined) {
return true
}
this._client.discv5.addEnr(enr)
this._state.routingTable.insertOrUpdate(encodedENR, EntryStatus.Connected)
this._client.enrCache.set(encodedENR.nodeId, encodedENR)
return true
} catch {
return false
Expand All @@ -474,11 +473,11 @@ export class portal {
const shortEnr = encodedENR.nodeId.slice(0, 15) + '...'
this.logger(`portal_beaconAddEnr request received for ${shortEnr}`)
try {
if (this._beacon.routingTable.getWithPending(encodedENR.nodeId)?.value) {
if (this._client.enrCache.get(encodedENR.nodeId) !== undefined) {
return true
}
this._client.discv5.addEnr(enr)
this._beacon.routingTable.insertOrUpdate(encodedENR, EntryStatus.Connected)
this._client.enrCache.set(encodedENR.nodeId, encodedENR)
return true
} catch {
return false
Expand Down Expand Up @@ -1301,12 +1300,11 @@ export class portal {
async historySendFindContent(params: [string, string]) {
const [nodeId, contentKey] = params
const res = await this._history.sendFindContent(nodeId, hexToBytes(contentKey))
const enr = this._history.routingTable.getWithPending(nodeId)?.value
const enr = this._client.enrCache.get(nodeId)
return res && enr && '0x' + enr.seq.toString(16)
}
async beaconSendFindContent(params: [string, string]) {
const [nodeId, contentKey] = params
console.log(nodeId)
const res = await this._beacon.sendFindContent(nodeId, hexToBytes(contentKey))
if (res !== undefined && 'content' in res) return bytesToHex(res.content as Uint8Array)
return '0x'
Expand All @@ -1317,7 +1315,7 @@ export class portal {
selector: 1,
value: hexToBytes(content),
})
const enr = this._history.routingTable.getWithPending(nodeId)?.value
const enr = this._client.enrCache.get(nodeId)!
void this.sendPortalNetworkResponse(
nodeId,
enr?.getLocationMultiaddr('udp')!,
Expand Down

0 comments on commit 8d9219f

Please sign in to comment.