Skip to content

Commit

Permalink
add types and tests for beacon network content keys (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 authored Aug 5, 2023
1 parent 1741980 commit 6517e8d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
16 changes: 10 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions packages/portalnetwork/src/subprotocols/beacon/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { Union } from '@chainsafe/ssz/lib/interface.js'
import { fromHexString, toHexString } from '@chainsafe/ssz'
import { shortId } from '../../util/util.js'
import { createBeaconConfig, defaultChainConfig, BeaconConfig } from '@lodestar/config'
import {
Forks,
MainnetGenesisValidatorsRoot,
BeaconLightClientNetworkContentType,
} from './types.js'
import { MainnetGenesisValidatorsRoot, BeaconLightClientNetworkContentType } from './types.js'
import {
ContentMessageType,
FindContentMessage,
Expand Down
11 changes: 10 additions & 1 deletion packages/portalnetwork/src/subprotocols/beacon/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ByteListType, ListCompositeType } from '@chainsafe/ssz'
import { ByteListType, ContainerType, ListCompositeType, UintBigintType } from '@chainsafe/ssz'
import { ForkName } from '@lodestar/params'
import { Bytes32Type } from '../history/index.js'

export const MainnetGenesisValidatorsRoot =
'0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95'
Expand All @@ -21,3 +22,11 @@ export enum Forks {

// TODO - figure out what a theoretical maximum byte size for a LightClientUpdate is (the `ByteListType`) in the below ssz list
export const LightClientUpdatesByRange = new ListCompositeType(new ByteListType(2 ** 18), 128)

export const LightClientBootstrapKey = new ContainerType({ blockHash: Bytes32Type })
export const LightClientUpdatesByRangeKey = new ContainerType({
startPeriod: new UintBigintType(8),
count: new UintBigintType(8),
})
export const LightClientFinalityUpdateKey = new ContainerType({ zero: new UintBigintType(8) })
export const LightClientOptimisticUpdateKey = new ContainerType({ zero: new UintBigintType(8) })
42 changes: 38 additions & 4 deletions packages/portalnetwork/test/subprotocols/beacon/beacon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
import tape from 'tape'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
import { fromHexString } from '@chainsafe/ssz'
import { fromHexString, toHexString } from '@chainsafe/ssz'
import { ssz } from '@lodestar/types'
import { createBeaconConfig, defaultChainConfig } from '@lodestar/config'
import {
LightClientBootstrapKey,
LightClientFinalityUpdateKey,
LightClientOptimisticUpdateKey,
LightClientUpdatesByRange,
LightClientUpdatesByRangeKey,
MainnetGenesisValidatorsRoot,
} from '../../../src/subprotocols/beacon/types.js'

tape('portal network spec test vectors', (t) => {
const specTestVectors = require('./specTestVectors.json')
const genesisRoot = fromHexString(MainnetGenesisValidatorsRoot) // Genesis Validators Root
const config = createBeaconConfig(defaultChainConfig, genesisRoot)
const serializedOptimistincUpdate = fromHexString(
specTestVectors.optimisticUpdate['6718463'].content_value
)
const serializedOptimistincUpdateKey = fromHexString(
specTestVectors.optimisticUpdate['6718463'].content_key
)
const forkDigest = ssz.ForkDigest.deserialize(serializedOptimistincUpdate.slice(0, 4))

const config = createBeaconConfig(defaultChainConfig, genesisRoot)
t.equal(config.forkDigest2ForkName(forkDigest), 'capella', 'derived correct fork')
const deserializedOptimisticUpdate = ssz.capella.LightClientOptimisticUpdate.deserialize(
serializedOptimistincUpdate.slice(4)
)
const optimisticUpdateKey = LightClientOptimisticUpdateKey.deserialize(
serializedOptimistincUpdateKey.slice(1)
)
t.equal(
deserializedOptimisticUpdate.attestedHeader.beacon.slot,
6718463,
'deserialized optimistic update'
)
t.equal(optimisticUpdateKey.zero, 0n, 'correctly deserialized optimstic update key')

const finalityUpdate = fromHexString(specTestVectors.finalityUpdate['6718463'].content_value)
const finalityUpdateKey = fromHexString(
specTestVectors.finalityUpdate['6718463'].content_key
).slice(1)
const deserializedFinalityUpdate = ssz.capella.LightClientFinalityUpdate.deserialize(
finalityUpdate.slice(4)
)
Expand All @@ -36,23 +51,42 @@ tape('portal network spec test vectors', (t) => {
6718463,
'deserialized finality update'
)
t.equal(
LightClientFinalityUpdateKey.deserialize(finalityUpdateKey).zero,
0n,
'deserialized finality update key'
)
const bootstrap = specTestVectors.bootstrap['6718368']
const deserializedBootstrap = ssz.capella.LightClientBootstrap.deserialize(
fromHexString(bootstrap.content_value).slice(4)
)
t.equal(deserializedBootstrap.header.beacon.slot, 6718368, 'deserialized bootstrap')
const bootstrapKey = fromHexString(bootstrap.content_key).slice(1)

t.equal(deserializedBootstrap.header.beacon.slot, 6718368, 'deserialized bootstrap')
t.equal(
toHexString(LightClientBootstrapKey.deserialize(bootstrapKey).blockHash),
'0xbd9f42d9a42d972bdaf4dee84e5b419dd432b52867258acb7bcc7f567b6e3af1',
'deserialized light client bootstrap key'
)
const updateByRange = fromHexString(specTestVectors.updateByRange['6684738'].content_value)
const updateByRangeKey = fromHexString(
specTestVectors.updateByRange['6684738'].content_key
).slice(1)
const deserializedRange = LightClientUpdatesByRange.deserialize(updateByRange)

let numUpdatesDeserialized = 0
for (const update of deserializedRange) {
const forkdigest = update.slice(0, 4)
const forkname = config.forkDigest2ForkName(forkdigest)
//@ts-ignore - ssz won't let me
//@ts-ignore - typescript won't let me set `forkname` to a value from of the Forks type
ssz[forkname].LightClientUpdate.deserialize(update.slice(4)).attestedHeader.beacon.slot
numUpdatesDeserialized++
}
t.equal(numUpdatesDeserialized, 4, 'deserialized LightClientUpdatesByRange')
t.equal(
LightClientUpdatesByRangeKey.deserialize(updateByRangeKey).count,
4n,
'deserialized update by range key'
)
t.end()
})

0 comments on commit 6517e8d

Please sign in to comment.