Skip to content

Commit

Permalink
Revert rpc changes (#425)
Browse files Browse the repository at this point in the history
* revert changes to rpc code

* expect 0x prefex

* revert other enr changes

* revert cli-test
  • Loading branch information
ScottyPoi authored Jul 23, 2023
1 parent e748ca6 commit 698520c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 42 deletions.
60 changes: 25 additions & 35 deletions packages/cli/scripts/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import jayson, { HttpClient } from 'jayson/promise/index.js'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import {
fromHexString,
getContentKey,
ContentType,
ProtocolId,
toHexString,
} from 'portalnetwork'
import { fromHexString, getContentKey, ContentType, ProtocolId, toHexString } from 'portalnetwork'
import { createRequire } from 'module'
import { readFileSync } from 'fs'
import { Block } from '@ethereumjs/block'
Expand Down Expand Up @@ -75,7 +69,10 @@ const main = async () => {
const blockData = require(args.sourceFile)
const blocks = Object.entries(blockData)
const epoch = require('./testEpoch.json')
const epoch25 = readFileSync('./scripts/0x03f216a28afb2212269b634b9b44ff327a4a79f261640ff967f7e3283e3a184c70.portalcontent', {encoding: 'hex'})
const epoch25 = readFileSync(
'./scripts/0x03f216a28afb2212269b634b9b44ff327a4a79f261640ff967f7e3283e3a184c70.portalcontent',
{ encoding: 'hex' }
)

async function testRes(clients: HttpClient[], method: string, params: any[][]) {
for (const [i, client] of clients.entries()) {
Expand All @@ -86,10 +83,7 @@ const main = async () => {
}
console.log(`ok ${method} test`)
}
const epochKey = getContentKey(
ContentType.EpochAccumulator,
fromHexString(epoch.hash)
)
const epochKey = getContentKey(ContentType.EpochAccumulator, fromHexString(epoch.hash))
let res = await clientInfo.ultralight.client.request('ultralight_addContentToDB', [
epochKey,
epoch.serialized,
Expand All @@ -98,7 +92,7 @@ const main = async () => {
throw new Error(`ultralight_addContentToDB error`)
}
res = await clientInfo.ultralight.client.request('ultralight_addContentToDB', [
"0x03f216a28afb2212269b634b9b44ff327a4a79f261640ff967f7e3283e3a184c70",
'0x03f216a28afb2212269b634b9b44ff327a4a79f261640ff967f7e3283e3a184c70',
'0x' + epoch25,
])
if (res.error) {
Expand Down Expand Up @@ -150,43 +144,39 @@ const main = async () => {
// portal_historyRoutingTableInfo
await testRes(clients, 'portal_historyRoutingTableInfo', [[], [], [], []])
// portal_historyLookupEnr
await testRes([clients[0]], 'portal_historyLookupEnr', [[clientInfo.peer1.enr]])
await testRes([clients[0]], 'portal_historyLookupEnr', [['0x' + clientInfo.peer1.nodeId]])
// portal_historyPing
await testRes(clients.slice(1), 'portal_historyPing', [
[clientInfo.ultralight.enr, "0x00"],
[clientInfo.ultralight.enr, "0x00"],
[clientInfo.ultralight.enr, "0x00"],
[clientInfo.ultralight.enr, '0x00'],
[clientInfo.ultralight.enr, '0x00'],
[clientInfo.ultralight.enr, '0x00'],
])
// portal_historyFindNodes
await testRes([clients[1]], 'portal_historyFindNodes', [
[clientInfo.ultralight.enr, [255]],
])
await testRes([clients[1]], 'portal_historyFindNodes', [[clientInfo.ultralight.enr, [255]]])
// portal_historyFindContent
await testRes([clients[1]], 'portal_historyFindContent', [
[clientInfo.ultralight.enr, blocks[1][0]],
])
// portal_historyLocalContent
await testRes([clients[0]], 'portal_historyLocalContent', [[epochKey]])
// portal_historyOffer
for (const block of blocks.slice(0,1)){
await testRes([clients[0]], 'portal_historyOffer', [
[
clientInfo.peer1.enr,
getContentKey(
ContentType.BlockHeader,
fromHexString(block[0])
for (const block of blocks.slice(0, 1)) {
await testRes([clients[0]], 'portal_historyOffer', [
[
clientInfo.peer1.enr,
getContentKey(ContentType.BlockHeader, fromHexString(block[0])),
toHexString(
Block.fromRLPSerializedBlock(Buffer.from((block[1] as any).rlp.slice(2), 'hex'), {
hardforkByBlockNumber: true,
}).header.serialize()
),
toHexString(Block.fromRLPSerializedBlock(Buffer.from((block[1] as any).rlp.slice(2),'hex'), { hardforkByBlockNumber: true}).header.serialize())
,
],
])}
],
])
}
// eth_getBlockByHash
await testRes([clients[2]], 'eth_getBlockByHash', [[blocks[2][0], false]])
// eth_getBlockByNumber
await clientInfo.peer3.client.request('ultralight_addContentToDB', [
epochKey,
epoch.serialized,
])
await clientInfo.peer3.client.request('ultralight_addContentToDB', [epochKey, epoch.serialized])
await testRes([clients[3]], 'eth_getBlockByNumber', [['0x3e8', false]])
await testRes([clients[2]], 'eth_getBlockByNumber', [['0x3e8', false]])
}
Expand Down
17 changes: 10 additions & 7 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.dstId]])
this.historyLookupEnr = middleware(this.historyLookupEnr.bind(this), 1, [[validators.hex]])
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]])
this.historyDeleteEnr = middleware(this.historyDeleteEnr.bind(this), 1, [[validators.dstId]])
this.historyGetEnr = middleware(this.historyGetEnr.bind(this), 1, [[validators.hex]])
this.historyDeleteEnr = middleware(this.historyDeleteEnr.bind(this), 1, [[validators.hex]])
this.historyAddEnrs = middleware(this.historyAddEnrs.bind(this), 1, [
[validators.array(validators.enr)],
])
Expand Down Expand Up @@ -178,8 +178,11 @@ 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)
return enr?.value.encodeTxt() ?? ''
const enr = this._history.routingTable.getWithPending(nodeId.slice(2))?.value
if (enr) {
return enr.encodeTxt()
}
return ''
}

async historyAddEnr(params: [string]): Promise<boolean> {
Expand All @@ -200,7 +203,7 @@ 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)
const remove = this._history.routingTable.removeById(nodeId.slice(2))
return remove !== undefined
}
async historyRoutingTableInfo(_params: []): Promise<any> {
Expand All @@ -224,7 +227,7 @@ export class portal {
async historyLookupEnr(params: [string]) {
const [nodeId] = params
this.logger(`Looking up ENR for NodeId: ${shortId(nodeId)}`)
const enr = this._history.routingTable.getWithPending(nodeId)?.value.encodeTxt()
const enr = this._history.routingTable.getWithPending(nodeId.slice(2))?.value.encodeTxt()
this.logger(`Found: ${enr}`)
return enr ?? ''
}
Expand Down

0 comments on commit 698520c

Please sign in to comment.