Skip to content
Open
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: 4 additions & 19 deletions packages/beacon-node/src/api/impl/beacon/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {kzg} from "../../../../util/kzg.js";
import {promiseAllMaybeAsync} from "../../../../util/promises.js";
import {ApiModules} from "../../types.js";
import {assertUniqueItems} from "../../utils.js";
import {getBlockResponse, toBeaconHeaderResponse} from "./utils.js";
import {getBlobIndicesByVersionedHashes, getBlockResponse, toBeaconHeaderResponse} from "./utils.js";

type PublishBlockOpts = ImportBlockOpts;

Expand Down Expand Up @@ -983,15 +983,7 @@ export function getBeaconBlockApi({
const blockVersionedHashes = blobKzgCommitments.map((commitment) =>
toHex(kzgCommitmentToVersionedHash(commitment))
);
indicesToReconstruct = [];
for (const requestedHash of versionedHashes) {
const index = blockVersionedHashes.findIndex((hash) => hash === requestedHash);
if (index === -1) {
throw new ApiError(400, `Versioned hash ${requestedHash} not found in block`);
}
indicesToReconstruct.push(index);
}
indicesToReconstruct.sort((a, b) => a - b);
indicesToReconstruct = getBlobIndicesByVersionedHashes(blockVersionedHashes, versionedHashes);
} else {
indicesToReconstruct = Array.from({length: blobCount}, (_, i) => i);
}
Expand Down Expand Up @@ -1022,16 +1014,9 @@ export function getBeaconBlockApi({
toHex(kzgCommitmentToVersionedHash(commitment))
);

const requestedIndices: number[] = [];
for (const requestedHash of versionedHashes) {
const index = blockVersionedHashes.findIndex((hash) => hash === requestedHash);
if (index === -1) {
throw new ApiError(400, `Versioned hash ${requestedHash} not found in block`);
}
requestedIndices.push(index);
}
const requestedIndices = getBlobIndicesByVersionedHashes(blockVersionedHashes, versionedHashes);

blobs = requestedIndices.sort((a, b) => a - b).map((index) => blobs[index]);
blobs = requestedIndices.map((index) => blobs[index]);
}
} else {
blobs = [];
Expand Down
26 changes: 26 additions & 0 deletions packages/beacon-node/src/api/impl/beacon/blocks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,29 @@ export async function getBlockResponse(

return res;
}

export function getBlobIndicesByVersionedHashes(blockVersionedHashes: string[], requestedHashes: string[]): number[] {
const indicesByHash = new Map<string, number[]>();

for (const [index, hash] of blockVersionedHashes.entries()) {
const indices = indicesByHash.get(hash);
if (indices) {
indices.push(index);
} else {
indicesByHash.set(hash, [index]);
}
}

const requestedIndices: number[] = [];

for (const requestedHash of requestedHashes) {
const indices = indicesByHash.get(requestedHash);
if (!indices?.length) {
throw new ApiError(400, `Versioned hash ${requestedHash} not found in block`);
}

requestedIndices.push(...indices);
}

return requestedIndices;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {describe, expect, it} from "vitest";
import {getBlobIndicesByVersionedHashes} from "../../../../../../src/api/impl/beacon/blocks/utils.js";

describe("api - beacon - blocks utils", () => {
it("returns every matching blob index in request order", () => {
expect(getBlobIndicesByVersionedHashes(["0xaa", "0xbb", "0xaa", "0xcc"], ["0xbb", "0xaa"])).toEqual([1, 0, 2]);
});

it("throws when a requested versioned hash is missing", () => {
expect(() => getBlobIndicesByVersionedHashes(["0xaa"], ["0xbb"])).toThrow("Versioned hash 0xbb not found in block");
});
});