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

fix: set consensus version header in state and block response #6152

Merged
merged 1 commit into from
Dec 2, 2023
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
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);
Copy link
Member Author

@nflaig nflaig Dec 2, 2023

Choose a reason for hiding this comment

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

The void is required here because eslint warns about floating promises due to header return value being thenable but it does not return a promise. Using await here will keep the process stuck when calling the API. This is a known issue, see fastify/fastify#4246 and fastify/help#775.

// 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);
}
Loading