Skip to content
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 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions packages/api/src/beacon/routes/lightclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,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,
Expand All @@ -19,7 +21,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
Expand Down Expand Up @@ -90,7 +91,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;
Copy link
Member Author

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.

Copy link
Member Author

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

if (!(network in networksChainConfig)) {
throw Error(`Invalid network name "${network}". Valid options are: ${Object.keys(networksChainConfig).join()}`);
}

beaconConfig = createBeaconConfig(config, genesisValidatorsRoot ? fromHex(genesisValidatorsRoot) : ZERO_HASH);
}
return beaconConfig;
};

return {
getLightClientUpdatesByRange: {
url: "/eth/v1/beacon/light_client/updates",
Expand All @@ -100,7 +112,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[] = [];
Expand All @@ -118,12 +130,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[])};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other clients won't be setting the header which means the metadata (res.meta()) will be empty, however this is not a big issue since it's not required to deserialize the response.

},
},
transform: {
toResponse: (data, meta) => {
Expand All @@ -147,7 +191,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
return {data: updates, meta};
},
},
}),
},
},
getLightClientOptimisticUpdate: {
url: "/eth/v1/beacon/light_client/optimistic_update",
Expand Down
2 changes: 0 additions & 2 deletions packages/api/test/unit/beacon/oapiSpec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ const ignoredOperations = [
/* missing route */
"getDepositSnapshot", // Won't fix for now, see https://github.com/ChainSafe/lodestar/issues/5697
"getNextWithdrawals", // https://github.com/ChainSafe/lodestar/issues/5696
/* Must support ssz response body */
"getLightClientUpdatesByRange", // https://github.com/ChainSafe/lodestar/issues/6841
];

const ignoredProperties: Record<string, IgnoredProperty> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/unit/beacon/testData/lightclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const signatureSlot = ssz.Slot.defaultValue();
export const testData: GenericServerTestCases<Endpoints> = {
getLightClientUpdatesByRange: {
args: {startPeriod: 1, count: 2},
res: {data: [lightClientUpdate], meta: {versions: [ForkName.bellatrix]}},
res: {data: [lightClientUpdate, lightClientUpdate], meta: {versions: [ForkName.altair, ForkName.altair]}},
},
getLightClientOptimisticUpdate: {
args: undefined,
Expand Down
Loading