Skip to content
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
3 changes: 3 additions & 0 deletions packages/config/src/chainConfig/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {fromHex, toHex} from "@lodestar/utils";
import {validateBlobSchedule} from "../utils/validateBlobSchedule.js";
import {
BlobSchedule,
BlobScheduleEntry,
Expand Down Expand Up @@ -168,5 +169,7 @@ export function deserializeBlobSchedule(input: unknown): BlobSchedule {
return out;
});

validateBlobSchedule(blobSchedule);

return blobSchedule;
}
7 changes: 1 addition & 6 deletions packages/config/src/forkConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,7 @@ export function createForkConfig(config: ChainConfig): ForkConfig {
}

// Sort by epoch in descending order to find the latest applicable value
const blobSchedule = [...config.BLOB_SCHEDULE].sort((a, b) => {
if (a.EPOCH !== b.EPOCH) {
return b.EPOCH - a.EPOCH;
}
return b.MAX_BLOBS_PER_BLOCK - a.MAX_BLOBS_PER_BLOCK;
});
const blobSchedule = [...config.BLOB_SCHEDULE].sort((a, b) => b.EPOCH - a.EPOCH);

for (const entry of blobSchedule) {
if (epoch >= entry.EPOCH) {
Expand Down
32 changes: 32 additions & 0 deletions packages/config/src/utils/validateBlobSchedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {MAX_BLOB_COMMITMENTS_PER_BLOCK} from "@lodestar/params";
import {BlobSchedule} from "../chainConfig/types.js";

export function validateBlobSchedule(blobSchedule: BlobSchedule): void {
if (blobSchedule.length === 0) {
return;
}

let previousEpoch: number | undefined;

for (const [i, entry] of blobSchedule.entries()) {
if (previousEpoch !== undefined) {
if (entry.EPOCH < previousEpoch) {
throw Error(
`Invalid BLOB_SCHEDULE expected entries to be sorted by EPOCH in ascending order, ${entry.EPOCH} < ${previousEpoch} at index ${i}`
);
}
if (entry.EPOCH === previousEpoch) {
throw Error(
`Invalid BLOB_SCHEDULE[${i}] entry with the same epoch value ${entry.EPOCH} as previous BLOB_SCHEDULE[${i - 1}] entry`
);
}
}
if (entry.MAX_BLOBS_PER_BLOCK > MAX_BLOB_COMMITMENTS_PER_BLOCK) {
throw Error(
`Invalid BLOB_SCHEDULE[${i}].MAX_BLOBS_PER_BLOCK value ${entry.MAX_BLOBS_PER_BLOCK} exceeds limit ${MAX_BLOB_COMMITMENTS_PER_BLOCK}`
);
}

previousEpoch = entry.EPOCH;
}
}
41 changes: 39 additions & 2 deletions packages/config/test/unit/json.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {MAX_BLOB_COMMITMENTS_PER_BLOCK} from "@lodestar/params";
import {describe, expect, it} from "vitest";
import {chainConfig} from "../../src/default.js";
import {BlobSchedule, chainConfigFromJson, chainConfigToJson} from "../../src/index.js";
Expand All @@ -13,9 +14,8 @@ describe("chainConfig JSON", () => {
it("Custom blob schedule", () => {
const blobSchedule: BlobSchedule = [
{EPOCH: 0, MAX_BLOBS_PER_BLOCK: 10},
{EPOCH: 10, MAX_BLOBS_PER_BLOCK: Infinity},
{EPOCH: 10, MAX_BLOBS_PER_BLOCK: 15},
{EPOCH: Infinity, MAX_BLOBS_PER_BLOCK: 20},
{EPOCH: Infinity, MAX_BLOBS_PER_BLOCK: Infinity},
];
const configWithCustomBlobSchedule = {...chainConfig, BLOB_SCHEDULE: blobSchedule};

Expand All @@ -24,4 +24,41 @@ describe("chainConfig JSON", () => {

expect(chainConfigRes).toEqual(configWithCustomBlobSchedule);
});

it("Blob schedule max blobs exceeds limit", () => {
const blobSchedule: BlobSchedule = [{EPOCH: 0, MAX_BLOBS_PER_BLOCK: MAX_BLOB_COMMITMENTS_PER_BLOCK + 1}];
const configWithCustomBlobSchedule = {...chainConfig, BLOB_SCHEDULE: blobSchedule};

const json = chainConfigToJson(configWithCustomBlobSchedule);

expect(() => chainConfigFromJson(json)).toThrow();
});

it("Blob schedule in wrong order", () => {
const blobSchedule: BlobSchedule = [
{EPOCH: 20, MAX_BLOBS_PER_BLOCK: 20},
{EPOCH: 10, MAX_BLOBS_PER_BLOCK: 15},
{EPOCH: 0, MAX_BLOBS_PER_BLOCK: 10},
];

const configWithCustomBlobSchedule = {...chainConfig, BLOB_SCHEDULE: blobSchedule};

const json = chainConfigToJson(configWithCustomBlobSchedule);

expect(() => chainConfigFromJson(json)).toThrow();
});

it("Blob schedule entries with the same epoch value", () => {
const blobSchedule: BlobSchedule = [
{EPOCH: 0, MAX_BLOBS_PER_BLOCK: 10},
{EPOCH: 10, MAX_BLOBS_PER_BLOCK: 15},
{EPOCH: 10, MAX_BLOBS_PER_BLOCK: 20},
];

const configWithCustomBlobSchedule = {...chainConfig, BLOB_SCHEDULE: blobSchedule};

const json = chainConfigToJson(configWithCustomBlobSchedule);

expect(() => chainConfigFromJson(json)).toThrow();
});
});
Loading