Skip to content

Commit

Permalink
Switch finality slot back to finality slot
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Oct 20, 2023
1 parent af45b8b commit 1efff1c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
10 changes: 7 additions & 3 deletions packages/cli/scripts/beaconBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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])
Expand All @@ -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)
}

}
Expand Down
4 changes: 2 additions & 2 deletions packages/portalnetwork/src/subprotocols/beacon/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/portalnetwork/src/subprotocols/beacon/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,21 @@ 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(
hexToBytes(
getBeaconContentKey(
BeaconLightClientNetworkContentType.LightClientFinalityUpdate,
LightClientFinalityUpdateKey.serialize({
signatureSlot: currentSlot,
finalitySlot: nextFinalitySlot,
}),
),
),
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
})
}
Expand Down
12 changes: 7 additions & 5 deletions packages/portalnetwork/test/integration/beaconProtocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Find Content tests', () => {
getHead: () => {
return {
beacon: {
slot: 6718463,
slot: 6718462,
},
}
},
Expand All @@ -164,7 +164,7 @@ describe('Find Content tests', () => {
getHead: () => {
return {
beacon: {
slot: 6718463,
slot: 6718462,
},
}
},
Expand Down Expand Up @@ -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(
Expand All @@ -339,7 +341,7 @@ describe('OFFER/ACCEPT tests', () => {
getHead: () => {
return {
beacon: {
slot: 6718463,
slot: 6718462,
},
}
},
Expand All @@ -351,7 +353,7 @@ describe('OFFER/ACCEPT tests', () => {
getHead: () => {
return {
beacon: {
slot: 6718462,
slot: 6718461,
},
}
},
Expand Down Expand Up @@ -383,7 +385,7 @@ describe('OFFER/ACCEPT tests', () => {
getHead: () => {
return {
beacon: {
slot: 6718463,
slot: 6718462,
},
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
Expand Down Expand Up @@ -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 }),
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
Expand Down

0 comments on commit 1efff1c

Please sign in to comment.