Skip to content
Merged
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
55 changes: 53 additions & 2 deletions packages/beacon-node/src/execution/engine/mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import crypto from "node:crypto";
import {blobToKzgCommitment, BYTES_PER_FIELD_ELEMENT, FIELD_ELEMENTS_PER_BLOB} from "c-kzg";
import {
kzgCommitmentToVersionedHash,
OPAQUE_TX_BLOB_VERSIONED_HASHES_OFFSET,
OPAQUE_TX_MESSAGE_OFFSET,
} from "@lodestar/state-transition";
import {allForks, bellatrix, eip4844, RootHex, ssz} from "@lodestar/types";
import {fromHex, toHex} from "@lodestar/utils";
import {ForkName} from "@lodestar/params";
import {BLOB_TX_TYPE, ForkName, ForkSeq} from "@lodestar/params";
import {ZERO_HASH_HEX} from "../../constants/index.js";
import {
ExecutePayloadStatus,
Expand Down Expand Up @@ -215,7 +221,17 @@ export class ExecutionEngineMock implements IExecutionEngine {
const kzgs: eip4844.KZGCommitment[] = [];
const blobs: eip4844.Blob[] = [];

// TODO EIP-4844: Populate blobs and KZG commitments
// if post eip4844, add between 0 and 2 blob transactions
if (ForkSeq[payloadAttributes.fork] >= ForkSeq.eip4844) {
const eip4844TxCount = Math.round(2 * Math.random());
for (let i = 0; i < eip4844TxCount; i++) {
const blob = generateRandomBlob();
const kzg = blobToKzgCommitment(blob);
executionPayload.transactions.push(transactionForKzgCommitment(kzg));
kzgs.push(kzg);
blobs.push(blob);
}
}

this.preparingPayloads.set(payloadId, {
executionPayload,
Expand Down Expand Up @@ -302,3 +318,38 @@ export class ExecutionEngineMock implements IExecutionEngine {
});
}
}

function transactionForKzgCommitment(kzgCommitment: eip4844.KZGCommitment): bellatrix.Transaction {
// Some random value that after the offset's position
const blobVersionedHashesOffset = OPAQUE_TX_BLOB_VERSIONED_HASHES_OFFSET + 64;

// +32 for the size of versionedHash
const ab = new ArrayBuffer(blobVersionedHashesOffset + 32);
const dv = new DataView(ab);
const ua = new Uint8Array(ab);

// Set tx type
dv.setUint8(0, BLOB_TX_TYPE);

// Set offset to hashes array
// const blobVersionedHashesOffset =
// OPAQUE_TX_MESSAGE_OFFSET + opaqueTxDv.getUint32(OPAQUE_TX_BLOB_VERSIONED_HASHES_OFFSET, true);
dv.setUint32(OPAQUE_TX_BLOB_VERSIONED_HASHES_OFFSET, blobVersionedHashesOffset - OPAQUE_TX_MESSAGE_OFFSET, true);

const versionedHash = kzgCommitmentToVersionedHash(kzgCommitment);
ua.set(versionedHash, blobVersionedHashesOffset);

return ua;
}

/**
* Generate random blob of sequential integers such that each element is < BLS_MODULUS
*/
function generateRandomBlob(): eip4844.Blob {
const blob = new Uint8Array(FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT);
const dv = new DataView(blob.buffer, blob.byteOffset, blob.byteLength);
for (let i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
dv.setUint32(i * BYTES_PER_FIELD_ELEMENT, i);
}
return blob;
}