Skip to content

Commit

Permalink
put tests into suites in beacon.spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Aug 15, 2023
1 parent c8dad25 commit 365fc4e
Showing 1 changed file with 97 additions and 62 deletions.
159 changes: 97 additions & 62 deletions packages/portalnetwork/test/subprotocols/beacon/beacon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const specTestVectors = require('./specTestVectors.json')
const genesisRoot = fromHexString(MainnetGenesisValidatorsRoot) // Genesis Validators Root
const config = createBeaconConfig(defaultChainConfig, genesisRoot)

it('portal network spec test vectors', () => {
describe('portal network spec test vectors', () => {
const serializedOptimistincUpdate = fromHexString(
specTestVectors.optimisticUpdate['6718463'].content_value,
)
Expand All @@ -32,19 +32,28 @@ it('portal network spec test vectors', () => {
)
const forkDigest = ssz.ForkDigest.deserialize(serializedOptimistincUpdate.slice(0, 4))

assert.equal(config.forkDigest2ForkName(forkDigest), 'capella', 'derived correct fork')
it('forkDigest2ForkName', () => {
assert.equal(config.forkDigest2ForkName(forkDigest), 'capella', 'derived correct fork')
})

const deserializedOptimisticUpdate = ssz.capella.LightClientOptimisticUpdate.deserialize(
serializedOptimistincUpdate.slice(4),
)
const optimisticUpdateKey = LightClientOptimisticUpdateKey.deserialize(
serializedOptimistincUpdateKey.slice(1),
)
assert.equal(
deserializedOptimisticUpdate.attestedHeader.beacon.slot,
6718463,
'deserialized optimistic update',
)
assert.equal(optimisticUpdateKey.zero, 0n, 'correctly deserialized optimstic update key')

it('deserializes optimistic update', () => {
assert.equal(
deserializedOptimisticUpdate.attestedHeader.beacon.slot,
6718463,
'deserialized optimistic update',
)
})

it('deserializes optimistic update key', () => {
assert.equal(optimisticUpdateKey.zero, 0n, 'correctly deserialized optimstic update key')
})

const finalityUpdate = fromHexString(specTestVectors.finalityUpdate['6718463'].content_value)
const finalityUpdateKey = fromHexString(
Expand All @@ -53,28 +62,38 @@ it('portal network spec test vectors', () => {
const deserializedFinalityUpdate = ssz.capella.LightClientFinalityUpdate.deserialize(
finalityUpdate.slice(4),
)
assert.equal(
deserializedFinalityUpdate.attestedHeader.beacon.slot,
6718463,
'deserialized finality update',
)
assert.equal(
LightClientFinalityUpdateKey.deserialize(finalityUpdateKey).zero,
0n,
'deserialized finality update key',
)

it('deserializes finality update', () => {
assert.equal(
deserializedFinalityUpdate.attestedHeader.beacon.slot,
6718463,
'deserialized finality update',
)
})

it('deserializes finality update key', () => {
assert.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),
)
const bootstrapKey = fromHexString(bootstrap.content_key).slice(1)
it('deserializes bootstrap', () => {
assert.equal(deserializedBootstrap.header.beacon.slot, 6718368, 'deserialized bootstrap')
})

assert.equal(deserializedBootstrap.header.beacon.slot, 6718368, 'deserialized bootstrap')
assert.equal(
toHexString(LightClientBootstrapKey.deserialize(bootstrapKey).blockHash),
'0xbd9f42d9a42d972bdaf4dee84e5b419dd432b52867258acb7bcc7f567b6e3af1',
'deserialized light client bootstrap key',
)
it('deserializes bootstrap key', () => {
assert.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,
Expand All @@ -89,12 +108,17 @@ it('portal network spec test vectors', () => {
ssz[forkname].LightClientUpdate.deserialize(update.slice(4)).attestedHeader.beacon.slot
numUpdatesDeserialized++
}
assert.equal(numUpdatesDeserialized, 4, 'deserialized LightClientUpdatesByRange')
assert.equal(
LightClientUpdatesByRangeKey.deserialize(updateByRangeKey).count,
4n,
'deserialized update by range key',
)
it('deserializes update by range', () => {
assert.equal(numUpdatesDeserialized, 4, 'deserialized LightClientUpdatesByRange')
})

it('deserializes update by range key', () => {
assert.equal(
LightClientUpdatesByRangeKey.deserialize(updateByRangeKey).count,
4n,
'deserialized update by range key',
)
})
})

describe('API tests', async () => {
Expand Down Expand Up @@ -130,12 +154,15 @@ describe('API tests', async () => {
fromHexString(bootstrap.content_value),
)
const retrievedBootstrap = await protocol.findContentLocally(fromHexString(bootstrap.content_key))
assert.equal(
ssz.capella.LightClientBootstrap.deserialize(retrievedBootstrap!.slice(4)).header.beacon.slot,
ssz.capella.LightClientBootstrap.deserialize(fromHexString(bootstrap.content_value).slice(4))
.header.beacon.slot,
'successfully stored and retrieved bootstrap',
)

it('stores and retrieves bootstrap', () => {
assert.equal(
ssz.capella.LightClientBootstrap.deserialize(retrievedBootstrap!.slice(4)).header.beacon.slot,
ssz.capella.LightClientBootstrap.deserialize(fromHexString(bootstrap.content_value).slice(4))
.header.beacon.slot,
'successfully stored and retrieved bootstrap',
)
})

const finalityUpdate = specTestVectors.finalityUpdate['6718463']
await protocol.store(
Expand All @@ -146,14 +173,17 @@ describe('API tests', async () => {
const retrievedFinalityUpdate = await protocol.findContentLocally(
fromHexString(finalityUpdate.content_key),
)
assert.equal(
ssz.capella.LightClientFinalityUpdate.deserialize(retrievedFinalityUpdate!.slice(4))
.attestedHeader.beacon.slot,
ssz.capella.LightClientFinalityUpdate.deserialize(
fromHexString(finalityUpdate.content_value).slice(4),
).attestedHeader.beacon.slot,
'successfully stored and retrieved finality update',
)

it('stores and retrieves finality update', () => {
assert.equal(
ssz.capella.LightClientFinalityUpdate.deserialize(retrievedFinalityUpdate!.slice(4))
.attestedHeader.beacon.slot,
ssz.capella.LightClientFinalityUpdate.deserialize(
fromHexString(finalityUpdate.content_value).slice(4),
).attestedHeader.beacon.slot,
'successfully stored and retrieved finality update',
)
})
const optimisticUpdate = specTestVectors.optimisticUpdate['6718463']
await protocol.store(
BeaconLightClientNetworkContentType.LightClientFinalityUpdate,
Expand All @@ -163,26 +193,31 @@ describe('API tests', async () => {
const retrievedOptimisticUpdate = await protocol.findContentLocally(
fromHexString(optimisticUpdate.content_key),
)
assert.equal(
ssz.capella.LightClientOptimisticUpdate.deserialize(retrievedOptimisticUpdate!.slice(4))
.attestedHeader.beacon.slot,
ssz.capella.LightClientOptimisticUpdate.deserialize(
fromHexString(optimisticUpdate.content_value).slice(4),
).attestedHeader.beacon.slot,
'successfully stored and retrieved optimistic update',
)

it('stores and retrieves optimistic update', () => {
assert.equal(
ssz.capella.LightClientOptimisticUpdate.deserialize(retrievedOptimisticUpdate!.slice(4))
.attestedHeader.beacon.slot,
ssz.capella.LightClientOptimisticUpdate.deserialize(
fromHexString(optimisticUpdate.content_value).slice(4),
).attestedHeader.beacon.slot,
'successfully stored and retrieved optimistic update',
)
})
// TODO: Update this test once logic for handling light client updates is implemented
const updatesByRange = specTestVectors.updateByRange['6684738']
try {
await protocol.store(
BeaconLightClientNetworkContentType.LightClientUpdatesByRange,
updatesByRange.content_key,
fromHexString(optimisticUpdate.content_value),
)
assert.fail('should throw')
} catch {
assert.ok(true, 'throws when trying to store a batch of light client updates')
}
it('throws when trying to store a batch of light client updates', async () => {
try {
await protocol.store(
BeaconLightClientNetworkContentType.LightClientUpdatesByRange,
updatesByRange.content_key,
fromHexString(optimisticUpdate.content_value),
)
assert.fail('should throw')
} catch {
assert.ok(true, 'throws when trying to store a batch of light client updates')
}
})
})

it('API tests', async () => {
Expand Down

0 comments on commit 365fc4e

Please sign in to comment.