Skip to content

Commit

Permalink
Merge 549f859 into 6208e2d
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig authored Dec 2, 2023
2 parents 6208e2d + 549f859 commit 781e765
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/api/src/beacon/server/beacon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ChainForkConfig} from "@lodestar/config";
import {ssz} from "@lodestar/types";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/beacon/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";
import {ServerApi} from "../../interfaces.js";
Expand Down Expand Up @@ -30,15 +31,35 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): ServerR
},
getBlockV2: {
...serverRoutes.getBlockV2,
handler: async (req) => {
handler: async (req, res) => {
const response = await api.getBlockV2(...reqSerializers.getBlockV2.parseReq(req));
if (response instanceof Uint8Array) {
const slot = extractSlotFromBlockBytes(response);
const version = config.getForkName(slot);
void res.header("Eth-Consensus-Version", version);
// Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer
return Buffer.from(response);
} else {
void res.header("Eth-Consensus-Version", response.version);
return returnTypes.getBlockV2.toJson(response);
}
},
},
};
}

function extractSlotFromBlockBytes(block: Uint8Array): number {
const {signature} = ssz.phase0.SignedBeaconBlock.fields;
/**
* class SignedBeaconBlock(Container):
* message: BeaconBlock [offset - 4 bytes]
* signature: BLSSignature [fixed - 96 bytes]
*
* class BeaconBlock(Container):
* slot: Slot [fixed - 8 bytes]
* ...
*/
const offset = 4 + signature.lengthBytes;
const bytes = block.subarray(offset, offset + ssz.Slot.byteLength);
return ssz.Slot.deserialize(bytes);
}
21 changes: 20 additions & 1 deletion packages/api/src/beacon/server/debug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ChainForkConfig} from "@lodestar/config";
import {ssz} from "@lodestar/types";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/debug.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";
import {ServerApi} from "../../interfaces.js";
Expand Down Expand Up @@ -31,15 +32,33 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): ServerR
},
getStateV2: {
...serverRoutes.getStateV2,
handler: async (req) => {
handler: async (req, res) => {
const response = await api.getStateV2(...reqSerializers.getStateV2.parseReq(req));
if (response instanceof Uint8Array) {
const slot = extractSlotFromStateBytes(response);
const version = config.getForkName(slot);
void res.header("Eth-Consensus-Version", version);
// Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer
return Buffer.from(response);
} else {
void res.header("Eth-Consensus-Version", response.version);
return returnTypes.getStateV2.toJson(response);
}
},
},
};
}

function extractSlotFromStateBytes(state: Uint8Array): number {
const {genesisTime, genesisValidatorsRoot} = ssz.phase0.BeaconState.fields;
/**
* class BeaconState(Container):
* genesisTime: BeaconBlock [offset - 4 bytes]
* genesisValidatorsRoot: BLSSignature [fixed - 96 bytes]
* slot: Slot [fixed - 8 bytes]
* ...
*/
const offset = genesisTime.byteLength + genesisValidatorsRoot.lengthBytes;
const bytes = state.subarray(offset, offset + ssz.Slot.byteLength);
return ssz.Slot.deserialize(bytes);
}

0 comments on commit 781e765

Please sign in to comment.