Skip to content

Commit

Permalink
Add initial RPC stuff for Beacon network
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Aug 21, 2023
1 parent eb37768 commit 0b3b5ea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ const main = async () => {
//@ts-ignore Because level doesn't know how to get along with itself
db,
metrics,
supportedProtocols: [ProtocolId.HistoryNetwork],
supportedProtocols: [ProtocolId.HistoryNetwork, ProtocolId.BeaconLightClientNetwork],
dataDir: args.datadir,
})
portal.discv5.enableLogs()

portal.enableLog('*ultralight*, *Portal*, *ultralight:RPC*')
let metricsServer: http.Server | undefined

Expand All @@ -132,6 +133,7 @@ const main = async () => {
}
await portal.start()

// TODO - make this more intelligent
const protocol = portal.protocols.get(ProtocolId.HistoryNetwork)
if (args.bootnode) {
protocol!.addBootNode(args.bootnode)
Expand Down
40 changes: 40 additions & 0 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
ContentMessageType,
AcceptMessage,
decodeHistoryNetworkContentKey,
BeaconLightClientNetwork,
BeaconLightClientNetworkContentType,
} from 'portalnetwork'
import { GetEnrResult } from '../schema/types.js'
import { isValidId } from '../util.js'
Expand Down Expand Up @@ -47,6 +49,8 @@ const methods = [
'portal_historyStore',
'portal_historyLocalContent',
'portal_historyGossip',
'portal_beaconSendFindContent',
'portal_beaconStore',

// not included in portal-network-specs
'portal_historyAddEnrs',
Expand All @@ -57,11 +61,15 @@ const methods = [
export class portal {
private _client: PortalNetwork
private _history: HistoryProtocol
private _beacon: BeaconLightClientNetwork
private logger: Debugger

constructor(client: PortalNetwork, logger: Debugger) {
this._client = client
this._history = this._client.protocols.get(ProtocolId.HistoryNetwork) as HistoryProtocol
this._beacon = this._client.protocols.get(
ProtocolId.BeaconLightClientNetwork,
) as BeaconLightClientNetwork
this.logger = logger
this.methods = middleware(this.methods.bind(this), 0, [])
this.historyNodeInfo = middleware(this.historyNodeInfo.bind(this), 0, [])
Expand Down Expand Up @@ -132,6 +140,15 @@ export class portal {
[validators.contentKey],
[validators.hex],
])
this.beaconSendFindContent = middleware(this.beaconSendFindContent.bind(this), 2, [
[validators.dstId],
[validators.hex],
])

this.beaconStore = middleware(this.beaconStore.bind(this), 2, [
[validators.hex],
[validators.hex],
])
}
async methods() {
return methods
Expand Down Expand Up @@ -512,4 +529,27 @@ export class portal {
return false
}
}

async beaconSendFindContent(params: [string, string]) {
const [nodeId, contentKey] = params
console.log(nodeId)
const res = await this._beacon.sendFindContent(nodeId, fromHexString(contentKey))
console.log(res)
const enr = this._beacon.routingTable.getWithPending(nodeId)?.value
return res && enr && '0x' + enr.seq.toString(16)
}

async beaconStore(params: [string, string]) {
const [contentKey, content] = params.map((param) => fromHexString(param))
try {
await this._beacon.store(
contentKey[0] as BeaconLightClientNetworkContentType,
toHexString(contentKey.slice(1)),
content,
)
return true
} catch {
return false
}
}
}
3 changes: 3 additions & 0 deletions packages/portalnetwork/src/subprotocols/beacon/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ export class BeaconLightClientNetwork extends BaseProtocol {
if (contentType === BeaconLightClientNetworkContentType.LightClientUpdatesByRange) {
throw new Error('special handling for update ranges not supported yet')
}
this.logger(
`storing ${BeaconLightClientNetworkContentType[contentType]} content corresponding to ${contentKey}`,
)
await this.put(this.protocolId, contentKey, toHexString(value))
}
}

0 comments on commit 0b3b5ea

Please sign in to comment.