Skip to content

Commit

Permalink
Update startLightClient endpoint (#660)
Browse files Browse the repository at this point in the history
* have startLightClient retrieve bootstrap first

* Simplify bootstrap finding
  • Loading branch information
acolytec3 authored Oct 24, 2024
1 parent 7e5f3e0 commit f8375ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 38 deletions.
2 changes: 2 additions & 0 deletions packages/cli/src/rpc/modules/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ENR } from '@chainsafe/enr'
import { BitArray } from '@chainsafe/ssz'
import { hexToBytes, short } from '@ethereumjs/util'
import {
BeaconLightClientNetworkContentType,
ContentLookup,
ContentMessageType,
FoundContent,
Expand All @@ -12,6 +13,7 @@ import {
PingPongCustomDataType,
PortalWireMessageType,
fromHexString,
getBeaconContentKey,
shortId,
toHexString,
} from 'portalnetwork'
Expand Down
51 changes: 13 additions & 38 deletions packages/portalnetwork/src/networks/beacon/ultralightTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { genesisData } from '@lodestar/config/networks'
import { getCurrentSlot } from '@lodestar/light-client/utils'
import { ssz } from '@lodestar/types'

import { ContentLookup } from '../contentLookup.js'

import {
BeaconLightClientNetworkContentType,
LightClientBootstrapKey,
LightClientFinalityUpdateKey,
LightClientOptimisticUpdateKey,
LightClientUpdatesByRange,
Expand Down Expand Up @@ -200,47 +201,21 @@ export class UltralightTransport implements LightClientTransport {
blockRoot: string,
): Promise<{ version: ForkName; data: LightClientBootstrap }> {
let forkname, bootstrap
const localBootstrap = await this.network.findContentLocally(

this.logger(`requesting lightClientBootstrap for trusted block root ${blockRoot}`)
// Try to get bootstrap from Portal Network
const lookup = new ContentLookup(
this.network,
getBeaconContentKey(
BeaconLightClientNetworkContentType.LightClientBootstrap,
LightClientBootstrapKey.serialize({ blockHash: hexToBytes(blockRoot) }),
hexToBytes(blockRoot),
),
)
if (localBootstrap !== undefined && localBootstrap.length !== 0) {
this.logger('Found LightClientBootstrap locally. Initializing light client...')
try {
forkname = this.network.beaconConfig.forkDigest2ForkName(
localBootstrap.slice(0, 4),
) as LightClientForkName
bootstrap = ssz[forkname].LightClientBootstrap.deserialize(localBootstrap.slice(4))
} catch (err) {
this.logger('Error loading local bootstrap error')
this.logger(err)
}
} else {
const peers = this.network.routingTable.nearest(this.network.enr.nodeId, 5)

let forkname

this.logger(`requesting lightClientBootstrap for trusted block root ${blockRoot}`)
// Try to get bootstrap from Portal Network
for (const peer of peers) {
const decoded = await this.network.sendFindContent(
peer.nodeId,
concatBytes(
new Uint8Array([BeaconLightClientNetworkContentType.LightClientBootstrap]),
LightClientBootstrapKey.serialize({ blockHash: hexToBytes(blockRoot) }),
),
)
if (decoded !== undefined && 'content' in decoded) {
const forkhash = decoded.content.slice(0, 4) as Uint8Array
forkname = this.network.beaconConfig.forkDigest2ForkName(forkhash) as LightClientForkName
bootstrap = ssz[forkname].LightClientBootstrap.deserialize(
(decoded.content as Uint8Array).slice(4),
)
break
}
}
const res = await lookup.startLookup()
if (res !== undefined && 'content' in res) {
const forkhash = res.content.slice(0, 4) as Uint8Array
forkname = this.network.beaconConfig.forkDigest2ForkName(forkhash) as LightClientForkName
bootstrap = ssz[forkname].LightClientBootstrap.deserialize(res.content.slice(4))
}

if (bootstrap === undefined) {
Expand Down

0 comments on commit f8375ef

Please sign in to comment.