Skip to content

Commit

Permalink
test: rough out fixture building functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Sep 16, 2024
1 parent c55c282 commit dea3b7d
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 38 deletions.
28 changes: 28 additions & 0 deletions packages/beacon-node/test/fixtures/altair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {ssz, altair} from "@lodestar/types";
import {BlockGenerationOptionsPhase0, generatePhase0BeaconBlocks} from "./phase0.js";
import {generateSignature} from "./utils.js";

export function generateSyncAggregate(
state: CachedBeaconStateAllForks,
block: altair.BeaconBlock
): altair.SyncAggregate {
return {
syncCommitteeBits: ssz.altair.SyncCommitteeBits.defaultValue(),
syncCommitteeSignature: generateSignature(),
};
}

export interface BlockGenerationOptionsAltair extends BlockGenerationOptionsPhase0 {}

export function generateAltairBeaconBlocks(
state: CachedBeaconStateAllForks,
count: number,
opts?: BlockGenerationOptionsAltair
): altair.BeaconBlock[] {
const blocks = generatePhase0BeaconBlocks(state, count, opts) as altair.BeaconBlock[];
for (const block of blocks) {
block.body.syncAggregate = generateSyncAggregate(state, block);
}
return blocks;
}
36 changes: 36 additions & 0 deletions packages/beacon-node/test/fixtures/bellatrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {ssz, bellatrix} from "@lodestar/types";
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {BlockGenerationOptionsAltair, generateAltairBeaconBlocks} from "./altair.js";

export function generateBellatrixExecutionPayload(): bellatrix.ExecutionPayload {
return {
baseFeePerGas: BigInt(0),
blockHash: new Uint8Array(),
blockNumber: 0,
extraData: new Uint8Array(),
feeRecipient: new Uint8Array(),
gasLimit: 0,
gasUsed: 0,
logsBloom: new Uint8Array(),
parentHash: new Uint8Array(),
prevRandao: new Uint8Array(),
receiptsRoot: new Uint8Array(),
stateRoot: new Uint8Array(),
timestamp: 0,
transactions: [ssz.bellatrix.Transaction.defaultValue()],
};
}

export interface BlockGenerationOptionsBellatrix extends BlockGenerationOptionsAltair {}

export function generateBellatrixBeaconBlocks(
state: CachedBeaconStateAllForks,
count: number,
opts?: BlockGenerationOptionsBellatrix
): bellatrix.BeaconBlock[] {
const blocks = generateAltairBeaconBlocks(state, count, opts) as bellatrix.BeaconBlock[];
for (const block of blocks) {
block.body.executionPayload = generateBellatrixExecutionPayload();
}
return blocks;
}
29 changes: 26 additions & 3 deletions packages/beacon-node/test/fixtures/capella.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {CachedBeaconStateAltair} from "@lodestar/state-transition";
import {capella} from "@lodestar/types";
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {ssz, bellatrix, capella} from "@lodestar/types";
import {BlockGenerationOptionsBellatrix, generateBellatrixBeaconBlocks} from "./bellatrix.js";

export function generateCapellaExecutionPayload(payload: bellatrix.ExecutionPayload): capella.ExecutionPayload {
return {
...payload,
withdrawals: [ssz.capella.Withdrawal.defaultValue()],
};
}

export function generateBlsToExecutionChanges(
state: CachedBeaconStateAltair,
state: CachedBeaconStateAllForks,
count: number
): capella.SignedBLSToExecutionChange[] {
const result: capella.SignedBLSToExecutionChange[] = [];
Expand All @@ -22,3 +30,18 @@ export function generateBlsToExecutionChanges(

return result;
}

export interface BlockGenerationOptionsCapella extends BlockGenerationOptionsBellatrix {}

export function generateCapellaBeaconBlocks(
state: CachedBeaconStateAllForks,
count: number,
opts?: BlockGenerationOptionsCapella
): capella.BeaconBlock[] {
const blocks = generateBellatrixBeaconBlocks(state, count, opts) as capella.BeaconBlock[];
for (const block of blocks) {
block.body.executionPayload = generateCapellaExecutionPayload(block.body.executionPayload);
block.body.blsToExecutionChanges = generateBlsToExecutionChanges(state, count);
}
return blocks;
}
34 changes: 34 additions & 0 deletions packages/beacon-node/test/fixtures/deneb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import crypto from "node:crypto";
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {capella, deneb} from "@lodestar/types";
import {MAX_BLOBS_PER_BLOCK} from "@lodestar/params";
import {generateCapellaBeaconBlocks, BlockGenerationOptionsCapella} from "./capella.js";

export function generateDenebExecutionPayload(payload: capella.ExecutionPayload): deneb.ExecutionPayload {
return {
...payload,
blobGasUsed: BigInt(0),
excessBlobGas: BigInt(0),
};
}

export function generateKzgCommitments(count: number): deneb.BlobKzgCommitments {
return Array.from({length: count}, () => Uint8Array.from(crypto.randomBytes(48)));
}

export interface BlockGenerationOptionsDeneb extends BlockGenerationOptionsCapella {
numKzgCommitments: number;
}

export function generateDenebBeaconBlocks(
state: CachedBeaconStateAllForks,
count: number,
opts?: BlockGenerationOptionsDeneb
): capella.BeaconBlock[] {
const blocks = generateCapellaBeaconBlocks(state, count, opts) as deneb.BeaconBlock[];
for (const block of blocks) {
block.body.executionPayload = generateDenebExecutionPayload(block.body.executionPayload);
block.body.blobKzgCommitments = generateKzgCommitments(opts?.numKzgCommitments ?? MAX_BLOBS_PER_BLOCK);
}
return blocks;
}
26 changes: 26 additions & 0 deletions packages/beacon-node/test/fixtures/electra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {deneb, electra} from "@lodestar/types";
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {BlockGenerationOptionsDeneb, generateDenebBeaconBlocks} from "./deneb.js";

export function generateElectraExecutionPayload(payload: deneb.ExecutionPayload): electra.ExecutionPayload {
return {
...payload,
depositRequests: [],
withdrawalRequests: [],
consolidationRequests: [],
};
}

export interface BlockGenerationOptionsElectra extends BlockGenerationOptionsDeneb {}

export function generateElectraBeaconBlocks(
state: CachedBeaconStateAllForks,
count: number,
opts?: BlockGenerationOptionsElectra
): electra.BeaconBlock[] {
const blocks = generateDenebBeaconBlocks(state, count, opts) as electra.BeaconBlock[];
for (const block of blocks) {
block.body.executionPayload = generateElectraExecutionPayload(block.body.executionPayload);
}
return blocks;
}
Loading

0 comments on commit dea3b7d

Please sign in to comment.