Skip to content

Commit

Permalink
UI: Implement Procedures and Interface (#481)
Browse files Browse the repository at this point in the history
* define procedure for talkResp subscription

* define procudure for ContentAdded subscription

* import subscription procudures from file

* define procedure for NodeAdded subscription

* add sub for utp events

* add new methods to router

* Fix Ping Button

* build trpc schema for portal_history rpc endpoints

* define trpc procedures for standard rpc methods

* externalize procudures code into separate files

* rpc: debug ts-errors

* comment out buggy nodeAddr code

* portalnetwork: add option to enable tickertape events

* portalnetwork: enable tickertape event log for talkReq and talkResp

* util: create utility function to format content to JSON

* implement basic rpc methods for WS client

* cleanup subscription procedures

* use message interfaces from discv5

* small fixes

* edit ws procedures

* add decodeENR trpc endpoint

* ui: create initial app state for both clients

* ui: update mock router

* cleanup app.tsx

* cleanup tabs.tsx

* delete unused

* clean up NodeInfo.tsx

* cleanup FunctionTabs.tsx

* update ping tab

* create message logs component

* create peer message logs component

* create content store tab

* create RPC call interface

* create enr helper

* small fixes/updates to rpc input

* update dependencies

* fix lint error

* commit lockfile

* enable RPC method send and logging

* move method definitions to rpcstate

* set methods in client state

* fix websocket error

* App improvements

* update http client functions

* debug routingtable and bootnodes calls

* debug client components

* edit react components

* include the rest of http client methods

* add README for UI

* edit README

* edit README

* cleanup

* rename option to eventLog

* edit readme

* move ui interface cli files to cli/ui

* remove fake test peer

* remove FakeKey tester

* fix server script

* cleanup port component

* add icon to button

* fix list key error

* fix websocket close error

* Nits

* enable event log  in portal constructor

* log messages to browser console

* only update node info at component load

* fix RPC action

* quiet liveness check log

* debug rpc menu

* debug messagelog and bootnodes

* default to only http

* disable log tab for http

* relocate UI server to UI package

* copy utils.ts over to server directory

* update package.json files for CLI and UI

* Start WS Client via procedure

* update README.md

---------

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
  • Loading branch information
ScottyPoi and acolytec3 authored Oct 19, 2023
1 parent 91ae74e commit ce56032
Show file tree
Hide file tree
Showing 42 changed files with 5,357 additions and 878 deletions.
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"node": "^18"
},
"devDependencies": {
"@types/cors": "2.8.14",
"@types/debug": "^4.1.7",
"@types/node": "18.11.18",
"@types/tape": "^4.13.2",
Expand Down Expand Up @@ -36,24 +35,19 @@
"@multiformats/multiaddr": "^11.0.0",
"debug": "^4.3.3",
"jayson": "^4.0.0",
"json-bigint": "^1.0.0",
"level": "^8.0.0",
"portalnetwork": "^0.0.1",
"prom-client": "^14.0.1",
"yargs": "^17.3.0",
"@lodestar/config": "^1.9.2",
"@lodestar/light-client": "^1.11.0",
"@lodestar/params": "^1.9.2",
"@lodestar/types": "^1.9.2",
"@trpc/server": "10.38.3",
"cors":"2.8.5",
"zod": "^3.0.0",
"ws":"^8.14.1"
"@lodestar/types": "^1.9.2"
},
"scripts": {
"start": "ts-node --esm src/index.ts",
"server:dev": "tsx watch src/server.ts",
"server:start": "node dist/server.js",
"server:dev": "tsx watch src/server/server.ts",
"server:start": "node dist/server/server.js",
"devnet": "npx ts-node --esm scripts/devnet.ts ",
"dev": "npx nodemon --esm src/index.ts -- --bindAddress=127.0.0.1:9000 --pk=0x0a27002508021221031947fd30ff7c87d8c7ff2c0ad1515624d247970f946efda872e884a432abb634122508021221031947fd30ff7c87d8c7ff2c0ad1515624d247970f946efda872e884a432abb6341a2408021220456aad29a26c39bf438813d30bb3f0730b8b776ebc4cb0721a3d9a5b3955380e --dataDir='./dist/data'",
"build": "tsc && cp bootnodes.txt ./dist",
Expand Down
55 changes: 39 additions & 16 deletions packages/cli/scripts/sendOffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jayson from 'jayson/promise/index.js'
import { ProtocolId, fromHexString, getContentKey, toHexString } from 'portalnetwork'
import { ENR, ProtocolId, fromHexString, getContentKey, toHexString } from 'portalnetwork'
import { Block, BlockData, BlockHeader } from '@ethereumjs/block'

const blocks = {
Expand Down Expand Up @@ -45,23 +45,46 @@ const blocks = {
},
}

const main = async () => {
const nodeA = jayson.Client.http({ host: '127.0.0.1', port: 8545 })
const nodeAEnr = await nodeA.request('discv5_nodeInfo', [])
console.log(nodeAEnr)
// Block number: 1
const blockHeaderContent_key =
'0x0088e96d4537bea4d9c05d12549907b32561d3bf31f45aae734cdc119f13406cb6'
const blockHeaderContent_value =
'0x080000001c020000f90211a0d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479405a56e2d52c817161883f50c441c3228cfe54d9fa0d67e4d450343046425ae4271474353857ab860dbc0a1dde64b41b5cd3a532bf3a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008503ff80000001821388808455ba422499476574682f76312e302e302f6c696e75782f676f312e342e32a0969b900de27b6ac6a67742365dd65f55a0526c41fd18e1b16f1a1215c2e66f5988539bd4979fef1ec401000080ff0700000000000000000000000000000000000000000000000000000023d6398abe4eba641e97a075b30780c12ebe18b24e83a9a9c7bdd94a910cf749bb6bb61aeab6bc5786067f7432bad790642b578881460279ad773a8191596c3087811c70634dbf2ea3abb7199cb5638713844db315d63467f40b5d38eeb884ddcb57866840a050f634417365e9515cd5e6826038ceb45659d85365cfcfceb7a6e9886aaff50b16b6af2bc3bde8b7e701b2cb5022ba49cac9d6c456834e692772b12acf7af78a8375b80ef177c9ad743a14ff0d4935f9ac105444fd57f802fed32495bab257b9585a149a7de4ac53eda7b6df7b9dac7f92325ba05eb1e6b588202048719c250620f4bfa71307470d6c835156db527294c6e6004f9de0c3595a7f1df43427c770506e7e3ca5d021f065544c6ba191d8ffc5fc0805b805d301c926c183ed9ec7e467b962e2304fa7945b6b18042dc2a53cb62b27b28af50fc06db5da2f83bd479f3719b9972fc723c69e4cd13877dcf7cc2a919a95cdf5d7805d9bd9a9f1fbf7a880d82ba9d7af9ed554ce01ea778db5d93d0665ca4fee11f4f873b0b1b58ff1337769b6ee458316030aeac65a5aab68d60fbf214bd44455f892260020000000000000000000000000000000000000000000000000000000000000'
const blockBodyContent_key = '0x0188e96d4537bea4d9c05d12549907b32561d3bf31f45aae734cdc119f13406cb6'
const blockBodyContent_value = '0x0800000008000000c0'

const nodeB = jayson.Client.http({ host: '127.0.0.1', port: 8545 })
const nodeBEnr = await nodeB.request('discv5_nodeInfo', [])
console.log(nodeBEnr)
const main = async () => {
const nodeA = jayson.Client.http({ host: '192.168.86.29', port: 8545 })
// const nodeAEnr = await nodeA.request('discv5_nodeInfo', [])
// console.log(nodeAEnr)

for (const [hash, block] of Object.entries(blocks)) {
await nodeA.request('ultralight_addBlockToHistory', [hash, block.rlp])
// const nodeB = jayson.Client.http({ host: '127.0.0.1', port: 8545 })
const nodeB = 'enr:-I24QLH4ZiTtmYzqPve_jx0_yNoEJWRLqe6Ds0a0e253TO5BIkLk0XZT0KN2obLDEh2vWAVLkerfOJP32hSvuFjsgbcEY4d1IDAuMC4xgmlkgnY0gmlwhMCoVh2Jc2VjcDI1NmsxoQLrLhwfPIrdoMeH9KPXEhmFoog3wGVbWhuXV33d0tnZl4N1ZHCCIWI'
const nodeBEnr = {
result: {
enr: nodeB,
nodeId: ENR.decodeTxt(nodeB).nodeId,
},
}
const blockHashes = Object.keys(blocks)
// console.log(nodeBEnr)

blockHashes.push(...blockHashes)
// for (const [hash, block] of Object.entries(blocks)) {
// await nodeA.request('ultralight_addBlockToHistory', [hash, block.rlp])
// }
// const blockHashes = Object.keys(blocks)

const types = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
// blockHashes.push(...blockHashes)

// const types = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

await nodeA.request('portal_historyStore', [
blockHeaderContent_key,
blockHeaderContent_value,
])
await nodeA.request('portal_historyStore', [
blockBodyContent_key,
blockBodyContent_value,
])

const ping = await nodeA.request('portal_historyPing', [
nodeBEnr.result.enr,
Expand All @@ -71,9 +94,9 @@ const main = async () => {
console.log(ping)

const offer = await nodeA.request('portal_historyOffer', [
nodeBEnr.result.nodeId.slice(2),
blockHashes.map((hash, idx) => getContentKey(idx < 10 ? 0 : 1, fromHexString(hash))),
types,
nodeBEnr.result.enr,
blockBodyContent_key,
blockBodyContent_value
])

console.log(offer)
Expand Down
52 changes: 34 additions & 18 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { GetEnrResult } from '../schema/types.js'
import { isValidId } from '../util.js'
import { middleware, validators } from '../validators.js'
import { Multiaddr } from '@multiformats/multiaddr'

const methods = [
'portal_historyRoutingTableInfo',
Expand Down Expand Up @@ -162,6 +163,23 @@ export class portal {
[validators.hex],
])
}

async sendPortalNetworkResponse(
nodeId: string,
socketAddr: Multiaddr,
requestId: bigint,
payload: Uint8Array,
) {
this._client.sendPortalNetworkResponse(
{
nodeId,
socketAddr,
},
BigInt(requestId),
payload,
)
}

async methods() {
return methods
}
Expand Down Expand Up @@ -307,10 +325,11 @@ export class portal {
})
this.logger.extend('PONG')(`Sent to ${shortId(enr.nodeId)}`)
try {
await this._client.sendPortalNetworkResponse(
{ nodeId: enr.nodeId, socketAddr: enr.getLocationMultiaddr('udp')! },
await this.sendPortalNetworkResponse(
enr.nodeId,
enr.getLocationMultiaddr('udp')!,
BigInt(requestId),
Buffer.from(pongMsg),
pongMsg,
)
} catch {
return false
Expand Down Expand Up @@ -364,13 +383,11 @@ export class portal {
selector: MessageCodes.NODES,
value: nodesPayload,
})
this._client.sendPortalNetworkResponse(
{
nodeId: dstId,
socketAddr: enr.getLocationMultiaddr('udp')!,
},
this.sendPortalNetworkResponse(
dstId,
enr.getLocationMultiaddr('udp')!,
BigInt(requestId),
Uint8Array.from(encodedPayload),
encodedPayload,
)

return enrs.length > 0 ? 1 : 0
Expand Down Expand Up @@ -453,10 +470,11 @@ export class portal {
value: fromHexString(content),
})
const enr = this._history.routingTable.getWithPending(nodeId)?.value
this._client.sendPortalNetworkResponse(
{ nodeId, socketAddr: enr?.getLocationMultiaddr('udp')! },
this.sendPortalNetworkResponse(
nodeId,
enr?.getLocationMultiaddr('udp')!,
enr!.seq,
Buffer.concat([Buffer.from([MessageCodes.CONTENT]), Buffer.from(payload)]),
Uint8Array.from(Buffer.concat([Buffer.from([MessageCodes.CONTENT]), Buffer.from(payload)])),
)
return '0x' + enr!.seq.toString(16)
}
Expand Down Expand Up @@ -532,13 +550,11 @@ export class portal {
selector: MessageCodes.ACCEPT,
value: payload,
})
this._client.sendPortalNetworkResponse(
{
nodeId: _enr.nodeId,
socketAddr: _enr.getLocationMultiaddr('udp')!,
},
this.sendPortalNetworkResponse(
_enr.nodeId,
_enr.getLocationMultiaddr('udp')!,
myEnr.seq,
Buffer.from(encodedPayload),
encodedPayload,
)

return '0x' + myEnr.seq.toString(16)
Expand Down
Loading

0 comments on commit ce56032

Please sign in to comment.