Skip to content

Commit

Permalink
feat: blob sidecars can be filtered by indices (#6337)
Browse files Browse the repository at this point in the history
* feat: blob sidecars can be filtered by indices

* fix: properly filter blobs

* fix: type indices as number

* chore: remove use of set

* fix: make stringify more robust

* Update packages/beacon-node/src/api/impl/beacon/blocks/index.ts

Co-authored-by: Nico Flaig <nflaig@protonmail.com>

* fix: cleanup

* fix: remove useless types

---------

Co-authored-by: Nico Flaig <nflaig@protonmail.com>
  • Loading branch information
jeluard and nflaig committed Jan 29, 2024
1 parent 291e178 commit cbf349c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
19 changes: 16 additions & 3 deletions packages/api/src/beacon/routes/beacon/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,12 @@ export type Api = {
* Retrieves BlobSidecar included in requested block.
* @param blockId Block identifier.
* Can be one of: "head" (canonical head in node's view), "genesis", "finalized", \<slot\>, \<hex encoded blockRoot with 0x prefix\>.
* @param indices Array of indices for blob sidecars to request for in the specified block. Returns all blob sidecars in the block if not specified.
*/
getBlobSidecars(blockId: BlockId): Promise<
getBlobSidecars(
blockId: BlockId,
indices?: number[]
): Promise<
ApiClientResponse<{
[HttpStatusCode.OK]: {executionOptimistic: ExecutionOptimistic; data: deneb.BlobSidecars};
}>
Expand Down Expand Up @@ -270,7 +274,7 @@ export type ReqTypes = {
publishBlockV2: {body: unknown; query: {broadcast_validation?: string}};
publishBlindedBlock: {body: unknown};
publishBlindedBlockV2: {body: unknown; query: {broadcast_validation?: string}};
getBlobSidecars: BlockIdOnlyReq;
getBlobSidecars: {params: {block_id: string}; query: {indices?: number[]}};
};

export function getReqSerializers(config: ChainForkConfig): ReqSerializers<Api, ReqTypes> {
Expand Down Expand Up @@ -356,7 +360,16 @@ export function getReqSerializers(config: ChainForkConfig): ReqSerializers<Api,
query: {broadcast_validation: Schema.String},
},
},
getBlobSidecars: blockIdOnlyReq,
getBlobSidecars: {
writeReq: (block_id, indices) => ({
params: {block_id: String(block_id)},
query: {indices},
}),
parseReq: ({params, query}) => [params.block_id, query.indices],
schema: {
query: {indices: Schema.UintArray},
},
},
};
}

Expand Down
6 changes: 0 additions & 6 deletions packages/api/test/unit/beacon/oapiSpec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ const ignoredProperties: Record<string, IgnoredProperty> = {
*/
getHealth: {request: ["query.syncing_status"]},

/**
* https://github.com/ChainSafe/lodestar/issues/6185
* - must have required property 'query'
*/
getBlobSidecars: {request: ["query"]},

/*
https://github.com/ChainSafe/lodestar/issues/4638
/query - must have required property 'skip_randao_verification'
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/unit/beacon/testData/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const testData: GenericServerTestCases<Api> = {
res: undefined,
},
getBlobSidecars: {
args: ["head"],
args: ["head", [0]],
res: {executionOptimistic: true, data: ssz.deneb.BlobSidecars.defaultValue()},
},

Expand Down
15 changes: 12 additions & 3 deletions packages/api/test/utils/checkAgainstSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,21 @@ function prettyAjvErrors(errors: ErrorObject[] | null | undefined): string {
return errors.map((e) => `${e.instancePath ?? "."} - ${e.message}`).join("\n");
}

type StringifiedProperty = string | StringifiedProperty[];

function stringifyProperty(value: unknown): StringifiedProperty {
if (typeof value === "number") {
return value.toString(10);
} else if (Array.isArray(value)) {
return value.map(stringifyProperty);
}
return String(value);
}

function stringifyProperties(obj: Record<string, unknown>): Record<string, unknown> {
for (const key of Object.keys(obj)) {
const value = obj[key];
if (typeof value === "number") {
obj[key] = value.toString(10);
}
obj[key] = stringifyProperty(value);
}

return obj;
Expand Down
5 changes: 3 additions & 2 deletions packages/beacon-node/src/api/impl/beacon/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export function getBeaconBlockApi({
await publishBlock(signedBlockOrContents, opts);
},

async getBlobSidecars(blockId) {
async getBlobSidecars(blockId, indices) {
const {block, executionOptimistic} = await resolveBlockId(chain, blockId);
const blockRoot = config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message);

Expand All @@ -418,9 +418,10 @@ export function getBeaconBlockApi({
if (!blobSidecars) {
throw Error(`blobSidecars not found in db for slot=${block.message.slot} root=${toHexString(blockRoot)}`);
}

return {
executionOptimistic,
data: blobSidecars,
data: indices ? blobSidecars.filter(({index}) => indices.includes(index)) : blobSidecars,
};
},
};
Expand Down

0 comments on commit cbf349c

Please sign in to comment.