-
-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ssz support to LC updates by range endpoint #7119
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,12 @@ import { | |
ssz, | ||
SyncPeriod, | ||
} from "@lodestar/types"; | ||
import {ForkName} from "@lodestar/params"; | ||
import {ChainForkConfig} from "@lodestar/config"; | ||
import {fromHex} from "@lodestar/utils"; | ||
import {ForkName, ZERO_HASH} from "@lodestar/params"; | ||
import {BeaconConfig, ChainForkConfig, createBeaconConfig} from "@lodestar/config"; | ||
import {genesisData, NetworkName} from "@lodestar/config/networks"; | ||
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js"; | ||
import {VersionCodec, VersionMeta} from "../../utils/metadata.js"; | ||
import {MetaHeader, VersionCodec, VersionMeta} from "../../utils/metadata.js"; | ||
import {getLightClientForkTypes, toForkName} from "../../utils/fork.js"; | ||
import { | ||
EmptyArgs, | ||
|
@@ -20,7 +22,6 @@ import { | |
EmptyMetaCodec, | ||
EmptyRequest, | ||
WithVersion, | ||
JsonOnlyResp, | ||
} from "../../utils/codecs.js"; | ||
|
||
// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes | ||
|
@@ -91,7 +92,18 @@ export type Endpoints = { | |
>; | ||
}; | ||
|
||
export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpoints> { | ||
export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoints> { | ||
// Cache config so fork digests don't need to be recomputed | ||
let beaconConfig: BeaconConfig | undefined; | ||
|
||
const cachedBeaconConfig = (): BeaconConfig => { | ||
if (beaconConfig === undefined) { | ||
const genesisValidatorsRoot = genesisData[config.CONFIG_NAME as NetworkName]?.genesisValidatorsRoot; | ||
beaconConfig = createBeaconConfig(config, genesisValidatorsRoot ? fromHex(genesisValidatorsRoot) : ZERO_HASH); | ||
} | ||
return beaconConfig; | ||
}; | ||
|
||
return { | ||
getLightClientUpdatesByRange: { | ||
url: "/eth/v1/beacon/light_client/updates", | ||
|
@@ -101,7 +113,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo | |
parseReq: ({query}) => ({startPeriod: query.start_period, count: query.count}), | ||
schema: {query: {start_period: Schema.UintRequired, count: Schema.UintRequired}}, | ||
}, | ||
resp: JsonOnlyResp({ | ||
resp: { | ||
data: { | ||
toJson: (data, meta) => { | ||
const json: unknown[] = []; | ||
|
@@ -119,12 +131,44 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo | |
} | ||
return value; | ||
}, | ||
serialize: (data, meta) => { | ||
const chunks: Uint8Array[] = []; | ||
for (const [i, update] of data.entries()) { | ||
const version = meta.versions[i]; | ||
const forkDigest = cachedBeaconConfig().forkName2ForkDigest(version); | ||
const serialized = getLightClientForkTypes(version).LightClientUpdate.serialize(update); | ||
const length = ssz.UintNum64.serialize(4 + serialized.length); | ||
chunks.push(length, forkDigest, serialized); | ||
} | ||
return Buffer.concat(chunks); | ||
}, | ||
deserialize: (data) => { | ||
let offset = 0; | ||
const updates: LightClientUpdate[] = []; | ||
while (offset < data.length) { | ||
const length = ssz.UintNum64.deserialize(data.subarray(offset, offset + 8)); | ||
const forkDigest = ssz.ForkDigest.deserialize(data.subarray(offset + 8, offset + 12)); | ||
const version = cachedBeaconConfig().forkDigest2ForkName(forkDigest); | ||
updates.push( | ||
getLightClientForkTypes(version).LightClientUpdate.deserialize( | ||
data.subarray(offset + 12, offset + 8 + length) | ||
) | ||
); | ||
offset += 8 + length; | ||
} | ||
return updates; | ||
}, | ||
}, | ||
meta: { | ||
toJson: (meta) => meta, | ||
fromJson: (val) => val as {versions: ForkName[]}, | ||
toHeadersObject: () => ({}), | ||
fromHeaders: () => ({versions: []}), | ||
toHeadersObject: (meta) => ({ | ||
[MetaHeader.Version]: meta.versions.join(","), | ||
}), | ||
fromHeaders: (headers) => { | ||
const versions = headers.getOrDefault(MetaHeader.Version, ""); | ||
return {versions: versions === "" ? [] : (versions.split(",") as ForkName[])}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other clients won't be setting the header which means the metadata ( |
||
}, | ||
}, | ||
transform: { | ||
toResponse: (data, meta) => { | ||
|
@@ -148,7 +192,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo | |
return {data: updates, meta}; | ||
}, | ||
}, | ||
}), | ||
}, | ||
}, | ||
getLightClientOptimisticUpdate: { | ||
url: "/eth/v1/beacon/light_client/optimistic_update", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caveat with this is that custom networks won't be supported across different clients, however, as long as Lodestar is both the client and server this still works as both use zero hash as genesis validators root.
There is also always the option to use JSON encoding, so I don't think this is a big issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also noticed that as of right now, it's not possible to configure the LC for custom networks
lodestar/packages/light-client/src/utils/api.ts
Lines 6 to 8 in c4952ee