From 1efff1c80da78fe42ae656ac7fb2d9cd80143bf7 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Fri, 20 Oct 2023 12:04:01 -0400 Subject: [PATCH] Switch finality slot back to finality slot --- packages/cli/scripts/beaconBridge.ts | 10 ++++--- .../src/subprotocols/beacon/beacon.ts | 4 +-- .../src/subprotocols/beacon/types.ts | 2 +- .../beacon/ultralightTransport.ts | 26 +++++++++++++------ .../test/integration/beaconProtocol.spec.ts | 12 +++++---- .../test/subprotocols/beacon/beacon.spec.ts | 4 +-- .../test/subprotocols/beacon/types.spec.ts | 2 +- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/packages/cli/scripts/beaconBridge.ts b/packages/cli/scripts/beaconBridge.ts index f517f97f7..96fbd293a 100644 --- a/packages/cli/scripts/beaconBridge.ts +++ b/packages/cli/scripts/beaconBridge.ts @@ -2,7 +2,7 @@ import jayson from 'jayson/promise/index.js' import { createBeaconConfig, defaultChainConfig, BeaconConfig } from '@lodestar/config' import { genesisData } from '@lodestar/config/networks' -import { BeaconLightClientNetworkContentType, fromHexString, getBeaconContentKey, LightClientBootstrapKey, LightClientOptimisticUpdateKey, LightClientUpdatesByRange, LightClientUpdatesByRangeKey, ProtocolId, toHexString } from 'portalnetwork' +import { BeaconLightClientNetworkContentType, fromHexString, getBeaconContentKey, LightClientBootstrapKey, LightClientFinalityUpdateKey, LightClientOptimisticUpdateKey, LightClientUpdatesByRange, LightClientUpdatesByRangeKey, ProtocolId, toHexString } from 'portalnetwork' import { ssz } from '@lodestar/types' import { ForkName } from '@lodestar/params' import { computeSyncPeriodAtSlot } from '@lodestar/light-client/utils' @@ -42,7 +42,7 @@ const main = async () => { const res = await ultralight.request('portal_beaconStore', [getBeaconContentKey(BeaconLightClientNetworkContentType.LightClientBootstrap, LightClientBootstrapKey.serialize({ blockHash: hexToBytes(bootstrapRoot) })), toHexString(concatBytes(capellaForkDigest, ssz.capella.LightClientBootstrap.serialize(bootstrap)))]) console.log('Pushed bootstrap into Portal Network', res) const res2 = await ultralight.request('portal_beaconStore', [rangeKey, toHexString(serializedRange)]) - console.log(`Pushed light client updates for range ${oldPeriod}-${oldPeriod + 3} into Portal Network`, res2) + console.log(`Pushed light client updates for range ${oldPeriod}-${currentPeriod} into Portal Network`, res2) const res3 = await ultralight.request('portal_beaconStore', [optimisticUpdateKey, toHexString(concatBytes(capellaForkDigest, ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate)))]) console.log(`Pushed optimistic update for signature slot ${optimisticUpdate.signatureSlot}`, res3) const res4 = await ultralight.request('portal_beaconStartLightClient',[bootstrapRoot]) @@ -59,8 +59,12 @@ const main = async () => { await new Promise(resolve => setTimeout(() => resolve(undefined), 13000)) let optimisticUpdate = ssz.capella.LightClientOptimisticUpdate.fromJson((await (await fetch(beaconNode + 'eth/v1/beacon/light_client/optimistic_update')).json()).data) let optimisticUpdateKey = getBeaconContentKey(BeaconLightClientNetworkContentType.LightClientOptimisticUpdate, LightClientOptimisticUpdateKey.serialize({ signatureSlot: BigInt(optimisticUpdate.signatureSlot) })) - const res = await ultralight.request('portal_beaconStore', [optimisticUpdateKey, toHexString(concatBytes(capellaForkDigest, ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate)))]) + let res = await ultralight.request('portal_beaconStore', [optimisticUpdateKey, toHexString(concatBytes(capellaForkDigest, ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate)))]) console.log(`Pushed optimistic update for signature slot ${optimisticUpdate.signatureSlot}`, res) + let finalityUpdate = ssz.capella.LightClientFinalityUpdate.fromJson((await (await fetch(beaconNode + 'eth/v1/beacon/light_client/finality_update')).json()).data) + let finalityUpdateKey = getBeaconContentKey(BeaconLightClientNetworkContentType.LightClientFinalityUpdate, LightClientFinalityUpdateKey.serialize({ finalitySlot: BigInt(finalityUpdate.finalizedHeader.beacon.slot) })) + res = await ultralight.request('portal_beaconStore', [finalityUpdateKey, toHexString(concatBytes(capellaForkDigest, ssz.capella.LightClientFinalityUpdate.serialize(finalityUpdate)))]) + console.log(`Pushed finality update for signature slot ${finalityUpdate.finalizedHeader.beacon.slot}`, res) } } diff --git a/packages/portalnetwork/src/subprotocols/beacon/beacon.ts b/packages/portalnetwork/src/subprotocols/beacon/beacon.ts index e09bc1179..4908295f5 100644 --- a/packages/portalnetwork/src/subprotocols/beacon/beacon.ts +++ b/packages/portalnetwork/src/subprotocols/beacon/beacon.ts @@ -365,7 +365,7 @@ export class BeaconLightClientNetwork extends BaseProtocol { key = LightClientFinalityUpdateKey.deserialize(contentKey.slice(1)) if ( this.lightClient !== undefined && - key.signatureSlot === BigInt(this.lightClient.getFinalized().beacon.slot) + key.finalitySlot === BigInt(this.lightClient.getFinalized().beacon.slot) ) { // We only store the most recent finality update so only retrieve the optimistic update if the slot // in the key matches the current finalized slot known to our light client @@ -802,7 +802,7 @@ export class BeaconLightClientNetwork extends BaseProtocol { } case BeaconLightClientNetworkContentType.LightClientFinalityUpdate: { - const slot = LightClientFinalityUpdateKey.deserialize(key.slice(1)).signatureSlot + const slot = LightClientFinalityUpdateKey.deserialize(key.slice(1)).finalitySlot if ( this.lightClient !== undefined && slot > this.lightClient.getFinalized().beacon.slot diff --git a/packages/portalnetwork/src/subprotocols/beacon/types.ts b/packages/portalnetwork/src/subprotocols/beacon/types.ts index 73c4b3197..68f2d5d93 100644 --- a/packages/portalnetwork/src/subprotocols/beacon/types.ts +++ b/packages/portalnetwork/src/subprotocols/beacon/types.ts @@ -28,7 +28,7 @@ export const LightClientUpdatesByRangeKey = new ContainerType({ count: new UintBigintType(8), }) export const LightClientFinalityUpdateKey = new ContainerType({ - signatureSlot: new UintBigintType(8), + finalitySlot: new UintBigintType(8), }) export const LightClientOptimisticUpdateKey = new ContainerType({ signatureSlot: new UintBigintType(8), diff --git a/packages/portalnetwork/src/subprotocols/beacon/ultralightTransport.ts b/packages/portalnetwork/src/subprotocols/beacon/ultralightTransport.ts index 8f4f04a5a..67e457dca 100644 --- a/packages/portalnetwork/src/subprotocols/beacon/ultralightTransport.ts +++ b/packages/portalnetwork/src/subprotocols/beacon/ultralightTransport.ts @@ -134,10 +134,13 @@ export class UltralightTransport implements LightClientTransport { }> { let finalityUpdate, forkname - const currentSlot = BigInt( - getCurrentSlot(this.protocol.beaconConfig, genesisData.mainnet.genesisTime), - ) - this.logger(`requesting LightClientFinalityUpdate for ${currentSlot.toString(10)}`) + const currentFinalitySlot = this.protocol.lightClient?.getFinalized().beacon.slot + if (currentFinalitySlot === undefined) { + throw new Error('Light Client is not running or no Finality Update is available') + } + // Ask for the next possible finality update + const nextFinalitySlot = BigInt(currentFinalitySlot + 32) + this.logger(`requesting LightClientFinalityUpdate for ${nextFinalitySlot.toString(10)}`) // Try retrieving finality update locally const localUpdate = await this.protocol.findContentLocally( @@ -145,7 +148,7 @@ export class UltralightTransport implements LightClientTransport { getBeaconContentKey( BeaconLightClientNetworkContentType.LightClientFinalityUpdate, LightClientFinalityUpdateKey.serialize({ - signatureSlot: currentSlot, + finalitySlot: nextFinalitySlot, }), ), ), @@ -166,7 +169,7 @@ export class UltralightTransport implements LightClientTransport { this.protocol.routingTable.random()!.nodeId, concatBytes( new Uint8Array([BeaconLightClientNetworkContentType.LightClientFinalityUpdate]), - LightClientFinalityUpdateKey.serialize({ signatureSlot: currentSlot }), + LightClientFinalityUpdateKey.serialize({ finalitySlot: nextFinalitySlot }), ), ) if (decoded !== undefined) { @@ -258,7 +261,7 @@ export class UltralightTransport implements LightClientTransport { ssz[forkname].LightClientOptimisticUpdate.deserialize((value as Uint8Array).slice(4)), ) } catch (err) { - this.logger('something went wrong on Optimistic Update') + this.logger('something went wrong trying to process Optimistic Update') this.logger(err) } } @@ -272,7 +275,14 @@ export class UltralightTransport implements LightClientTransport { const forkname = this.protocol.beaconConfig.forkDigest2ForkName( forkhash, ) as LightClientForkName - handler(ssz[forkname].LightClientFinalityUpdate.deserialize((value as Uint8Array).slice(4))) + try { + handler( + ssz[forkname].LightClientFinalityUpdate.deserialize((value as Uint8Array).slice(4)), + ) + } catch (err) { + this.logger('something went wrong trying to process Finality Update') + this.logger(err) + } } }) } diff --git a/packages/portalnetwork/test/integration/beaconProtocol.spec.ts b/packages/portalnetwork/test/integration/beaconProtocol.spec.ts index 3a6091b32..7cd60ef0b 100644 --- a/packages/portalnetwork/test/integration/beaconProtocol.spec.ts +++ b/packages/portalnetwork/test/integration/beaconProtocol.spec.ts @@ -154,7 +154,7 @@ describe('Find Content tests', () => { getHead: () => { return { beacon: { - slot: 6718463, + slot: 6718462, }, } }, @@ -164,7 +164,7 @@ describe('Find Content tests', () => { getHead: () => { return { beacon: { - slot: 6718463, + slot: 6718462, }, } }, @@ -324,6 +324,8 @@ describe('OFFER/ACCEPT tests', () => { }, }) + node1.enableLog('*BeaconLightClientNetwork*') + node2.enableLog('*BeaconLightClientNetwork') await node1.start() await node2.start() const protocol1 = node1.protocols.get( @@ -339,7 +341,7 @@ describe('OFFER/ACCEPT tests', () => { getHead: () => { return { beacon: { - slot: 6718463, + slot: 6718462, }, } }, @@ -351,7 +353,7 @@ describe('OFFER/ACCEPT tests', () => { getHead: () => { return { beacon: { - slot: 6718462, + slot: 6718461, }, } }, @@ -383,7 +385,7 @@ describe('OFFER/ACCEPT tests', () => { getHead: () => { return { beacon: { - slot: 6718463, + slot: 6718462, }, } }, diff --git a/packages/portalnetwork/test/subprotocols/beacon/beacon.spec.ts b/packages/portalnetwork/test/subprotocols/beacon/beacon.spec.ts index 8ee8628b6..dac1c89a0 100644 --- a/packages/portalnetwork/test/subprotocols/beacon/beacon.spec.ts +++ b/packages/portalnetwork/test/subprotocols/beacon/beacon.spec.ts @@ -81,7 +81,7 @@ describe('portal network spec test vectors', () => { it('deserializes finality update key', () => { assert.equal( - LightClientFinalityUpdateKey.deserialize(finalityUpdateKey).signatureSlot, + LightClientFinalityUpdateKey.deserialize(finalityUpdateKey).finalitySlot, 6718464n, 'deserialized finality update key', ) @@ -191,7 +191,7 @@ describe('API tests', async () => { const retrievedFinalityUpdate = await protocol.findContentLocally( concatBytes( new Uint8Array([BeaconLightClientNetworkContentType.LightClientFinalityUpdate]), - LightClientFinalityUpdateKey.serialize({ signatureSlot: 6718463n }), + LightClientFinalityUpdateKey.serialize({ finalitySlot: 6718463n }), ), ) diff --git a/packages/portalnetwork/test/subprotocols/beacon/types.spec.ts b/packages/portalnetwork/test/subprotocols/beacon/types.spec.ts index a93a92c37..c5a093eb4 100644 --- a/packages/portalnetwork/test/subprotocols/beacon/types.spec.ts +++ b/packages/portalnetwork/test/subprotocols/beacon/types.spec.ts @@ -74,7 +74,7 @@ describe('Beacon network type tests using portal network spec test vectors', () it('deserializes finality update key', () => { assert.equal( - LightClientFinalityUpdateKey.deserialize(finalityUpdateKey).signatureSlot, + LightClientFinalityUpdateKey.deserialize(finalityUpdateKey).finalitySlot, 6718464n, 'deserialized finality update key', )