Skip to content

Commit

Permalink
Add content handling to network scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Aug 5, 2023
1 parent 6517e8d commit 1a6d78f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
67 changes: 63 additions & 4 deletions packages/portalnetwork/src/subprotocols/beacon/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ 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 { MainnetGenesisValidatorsRoot, BeaconLightClientNetworkContentType } from './types.js'
import {
MainnetGenesisValidatorsRoot,
BeaconLightClientNetworkContentType,
LightClientUpdatesByRange,
} from './types.js'
import {
ContentMessageType,
FindContentMessage,
MessageCodes,
PortalWireMessageType,
} from '../../wire/types.js'
import { ssz } from '@lodestar/types'
import { getBeaconContentKey } from './util.js'

export class BeaconLightClientNetwork extends BaseProtocol {
protocolId: ProtocolId.BeaconLightClientNetwork
Expand Down Expand Up @@ -79,7 +84,23 @@ export class BeaconLightClientNetwork extends BaseProtocol {
try {
// TODO: Figure out how to use Forks type to limit selector in ssz[forkname] below and make typescript happy
;(ssz as any)[forkname].LightClientOptimisticUpdate.deserialize(
decoded.value as Uint8Array
(decoded.value as Uint8Array).slice(4)
)
} catch (err) {
this.logger(`received invalid content from ${shortId(dstId)}`)
break
}
this.logger(
`received ${
BeaconLightClientNetworkContentType[decoded.selector]
} content corresponding to ${contentHash}`
)
await this.store(decoded.selector, contentHash, decoded.value as Uint8Array)
break
case BeaconLightClientNetworkContentType.LightClientFinalityUpdate:
try {
;(ssz as any)[forkname].LightClientFinalityUpdate.deserialize(
(decoded.value as Uint8Array).slice(4)
)
} catch (err) {
this.logger(`received invalid content from ${shortId(dstId)}`)
Expand All @@ -92,6 +113,36 @@ export class BeaconLightClientNetwork extends BaseProtocol {
)
await this.store(decoded.selector, contentHash, decoded.value as Uint8Array)
break
case BeaconLightClientNetworkContentType.LightClientBootstrap:
try {
;(ssz as any)[forkname].LightClientBootstrap.deserialize(
(decoded.value as Uint8Array).slice(4)
)
} catch (err) {
this.logger(`received invalid content from ${shortId(dstId)}`)
break
}
this.logger(
`received ${
BeaconLightClientNetworkContentType[decoded.selector]
} content corresponding to ${contentHash}`
)
await this.store(decoded.selector, contentHash, decoded.value as Uint8Array)
break
case BeaconLightClientNetworkContentType.LightClientUpdatesByRange:
try {
LightClientUpdatesByRange.deserialize((decoded.value as Uint8Array).slice(4))
} catch (err) {
this.logger(`received invalid content from ${shortId(dstId)}`)
break
}
this.logger(
`received ${
BeaconLightClientNetworkContentType[decoded.selector]
} content corresponding to ${contentHash}`
)
await this.store(decoded.selector, contentHash, decoded.value as Uint8Array)
break
default:
this.logger(
`received ${
Expand All @@ -106,7 +157,15 @@ export class BeaconLightClientNetwork extends BaseProtocol {
}
}

public store = async (contentType: any, hashKey: string, value: Uint8Array): Promise<void> => {
await this.put(this.protocolId, hashKey, toHexString(value))
public store = async (
contentType: BeaconLightClientNetworkContentType,
hashKey: string,
value: Uint8Array
): Promise<void> => {
await this.put(
this.protocolId,
getBeaconContentKey(contentType, fromHexString(hashKey)),
toHexString(value)
)
}
}
2 changes: 1 addition & 1 deletion packages/portalnetwork/src/subprotocols/beacon/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './beacon.js'
export * from './types.js'
export * from './helpers.js'
export * from './util.js'
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BitArray, fromHexString } from '@chainsafe/ssz'
import { BitArray, fromHexString, toHexString } from '@chainsafe/ssz'
import { BeaconLightClientNetworkContentType } from './types.js'

export const attestedHeaderFromJson = (data: any) => {
return {
Expand Down Expand Up @@ -28,3 +29,17 @@ export const lightClientOptimisticUpdateFromJson = (data: any) => {
syncAggregate: syncAggregateFromJson(data.sync_aggregate),
}
}

/**
* Serializes a beacon network content key
* @param contentType content type as defined by `BeaconNetworkContentType`
* @param serializedKey the SSZ encoded key corresponding to the `BeaconNetworkContentType`
* @returns the content key encoded as a hex string
*/
export const getBeaconContentKey = (
contentType: BeaconLightClientNetworkContentType,
serializedKey: Uint8Array
) => {
const prefix = Buffer.alloc(1, contentType)
return toHexString(prefix) + toHexString(serializedKey).slice(2)
}

0 comments on commit 1a6d78f

Please sign in to comment.