Skip to content

Commit

Permalink
Add new beacon endpoints (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 authored Feb 8, 2025
1 parent 4b0bc8c commit d738798
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
12 changes: 5 additions & 7 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const main = async () => {
let web3: jayson.Client | undefined

const portalConfig = await cliConfig(args)

log(`portalConfig: ${JSON.stringify(args, null, 2)}`)
portalConfig.operatingSystemAndCpuArchitecture = args.arch
portalConfig.shortCommit = args.commit ?? execSync('git rev-parse HEAD').toString().slice(0, 7)

Expand Down Expand Up @@ -70,13 +70,11 @@ const main = async () => {
})
} else {
log(
`Received ${method} with params: ${
params !== undefined &&
(params as any[]).map((p, idx) => {
return `${idx}: ${p.toString().slice(0, 64)}${
p.toString().length > 64 ? '...' : ''
`Received ${method} with params: ${params !== undefined &&
(params as any[]).map((p, idx) => {
return `${idx}: ${p.toString().slice(0, 64)}${p.toString().length > 64 ? '...' : ''
}`
})
})
}`,
)
return this.getMethod(method)
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/rpc/error-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const METHOD_NOT_FOUND = -32601
export const INVALID_PARAMS = -32602
export const INTERNAL_ERROR = -32603
export const CONTENT_NOT_FOUND = -39002
export const BEACON_CLIENT_NOT_INITIALIZED = -39003
36 changes: 35 additions & 1 deletion packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
shortId
} from 'portalnetwork'

import { CONTENT_NOT_FOUND, INVALID_PARAMS } from '../error-code.js'
import { BEACON_CLIENT_NOT_INITIALIZED, CONTENT_NOT_FOUND, INVALID_PARAMS } from '../error-code.js'
import { content_params } from '../schema/index.js'
import { callWithStackTrace, isValidId } from '../util.js'
import { middleware, validators } from '../validators.js'
Expand All @@ -23,6 +23,7 @@ import type {
StateNetwork,
} from 'portalnetwork'
import type { GetEnrResult } from '../schema/types.js'
import { RunStatusCode } from '@lodestar/light-client'

const methods = [
// state
Expand Down Expand Up @@ -74,6 +75,8 @@ const methods = [
'portal_beaconDeleteEnr',
'portal_beaconLookupEnr',
'portal_beaconOffer',
'portal_beaconOptimisticStateRoot',
'portal_beaconFinalizedStateRoot',

// not included in portal-network-specs
'portal_historyAddEnrs',
Expand Down Expand Up @@ -271,6 +274,9 @@ export class portal {
this.beaconStartLightClient = middleware(this.beaconStartLightClient.bind(this), 1, [
[validators.hex],
])

this.beaconOptimisticStateRoot = middleware(this.beaconOptimisticStateRoot.bind(this), 0, [])
this.beaconFinalizedStateRoot = middleware(this.beaconFinalizedStateRoot.bind(this), 0, [])
}

async methods() {
Expand Down Expand Up @@ -1181,6 +1187,34 @@ export class portal {

// other

async beaconOptimisticStateRoot(params: []): Promise<string> {
this.logger(`beaconOptimisticStateRoot request received`)
if (this._beacon.lightClient?.status === RunStatusCode.uninitialized || this._beacon.lightClient?.status === RunStatusCode.stopped) {
const error = {
code: BEACON_CLIENT_NOT_INITIALIZED,
message: 'beacon client not initialized',
}
throw error
}
const res = await this._beacon.lightClient?.getHead()
this.logger(`beaconOptimisticStateRoot request returned ${res !== undefined ? bytesToHex(res?.beacon.stateRoot) : '0x'}`)
return res !== undefined ? bytesToHex(res?.beacon.stateRoot) : '0x'
}

async beaconFinalizedStateRoot(params: []): Promise<string> {
this.logger(`beaconFinalizedStateRoot request received`)
if (this._beacon.lightClient?.status === RunStatusCode.uninitialized || this._beacon.lightClient?.status === RunStatusCode.stopped) {
const error = {
code: BEACON_CLIENT_NOT_INITIALIZED,
message: 'beacon client not initialized',
}
throw error
}
const res = await this._beacon.lightClient?.getFinalized()
this.logger(`beaconFinalizedStateRoot request returned ${res !== undefined ? bytesToHex(res?.beacon.stateRoot) : '0x'}`)
return res !== undefined ? bytesToHex(res?.beacon.stateRoot) : '0x'
}

async beaconStartLightClient(params: [string]): Promise<boolean | string> {
const [bootstrapHash] = params
this.logger(`portal_beaconStartLightClient request received for ${bootstrapHash}`)
Expand Down

0 comments on commit d738798

Please sign in to comment.