-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 4844 tests in sim tests
- Loading branch information
1 parent
8c93a3f
commit 4a7fd0f
Showing
7 changed files
with
378 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/cli/test/utils/simulation/assertions/blobsAssertion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {randomBytes} from "node:crypto"; | ||
import {ApiError} from "@lodestar/api"; | ||
import {fromHex, toHex} from "@lodestar/utils"; | ||
import {SimulationAssertion, AssertionMatch, AssertionResult, NodePair} from "../interfaces.js"; | ||
import {EL_GENESIS_ACCOUNT, EL_GENESIS_SECRET_KEY, SIM_ENV_CHAIN_ID} from "../constants.js"; | ||
import {generateBlobsForTransaction} from "../utils/blobs.js"; | ||
import {BlobsEIP4844Transaction} from "../Web3Plugins.js"; | ||
|
||
const numberOfBlobs = 6; | ||
|
||
export function createBlobsAssertion( | ||
nodes: NodePair[], | ||
{sendBlobsAtSlot, validateBlobsAt}: {sendBlobsAtSlot: number; validateBlobsAt: number} | ||
): SimulationAssertion<string, number> { | ||
return { | ||
id: `blobs-${nodes.map((n) => n.id).join("-")}`, | ||
match: ({slot}) => { | ||
// Run capture every sendBlobsAtSlot -> validateBlobsAt and validate only at validateBlobsAt | ||
return slot === validateBlobsAt | ||
? AssertionMatch.Capture | AssertionMatch.Assert | ||
: slot >= sendBlobsAtSlot && slot <= validateBlobsAt | ||
? AssertionMatch.Capture | ||
: AssertionMatch.None; | ||
}, | ||
|
||
async capture({slot, node}) { | ||
// TODO: Add feature to detect the node index during capture process | ||
// slot starts from 1, so we add up 1 | ||
if (slot <= numberOfBlobs + 1 && node.id === "node-1") { | ||
const {blobs, kzgCommitments, blobVersionedHashes, kzgProofs} = generateBlobsForTransaction(1); | ||
const nonce = await node.execution.provider?.eth.getTransactionCount(EL_GENESIS_ACCOUNT); | ||
const tx = BlobsEIP4844Transaction.fromTxData({ | ||
chainId: `0x${SIM_ENV_CHAIN_ID.toString(16)}`, | ||
to: `0x${randomBytes(20).toString("hex")}`, | ||
gasLimit: "0xc350", | ||
maxPriorityFeePerGas: "0x3b9aca00", | ||
maxFeePerGas: "0x3ba26b20", | ||
maxFeePerBlobGas: "0x3e8", | ||
value: "0x10000", | ||
nonce: `0x${(nonce ?? 0).toString(16)}`, | ||
blobVersionedHashes, | ||
blobs, | ||
kzgCommitments, | ||
kzgProofs, | ||
}); | ||
const signedTx = tx.sign(fromHex(`0x${EL_GENESIS_SECRET_KEY}`)); | ||
await node.execution.provider?.extended.sendRawTransaction(toHex(signedTx.serialize())); | ||
} | ||
|
||
const blobSideCars = await node.beacon.api.beacon.getBlobSidecars(slot); | ||
ApiError.assert(blobSideCars); | ||
|
||
return blobSideCars.response.data.length; | ||
}, | ||
|
||
assert: async ({store}) => { | ||
const errors: AssertionResult[] = []; | ||
|
||
let eip4844Blobs = 0; | ||
for (let slot = sendBlobsAtSlot; slot <= validateBlobsAt; slot++) { | ||
eip4844Blobs += store[slot] ?? 0; | ||
} | ||
|
||
if (eip4844Blobs !== numberOfBlobs) { | ||
errors.push([ | ||
"Node does not have right number of blobs", | ||
{ | ||
expectedBlobs: numberOfBlobs, | ||
currentBlobs: eip4844Blobs, | ||
}, | ||
]); | ||
} | ||
|
||
return errors; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {digest as sha256Digest} from "@chainsafe/as-sha256"; | ||
import {ckzg} from "@lodestar/beacon-node/util"; | ||
import {BYTES_PER_FIELD_ELEMENT, FIELD_ELEMENTS_PER_BLOB, VERSIONED_HASH_VERSION_KZG} from "@lodestar/params"; | ||
import {deneb} from "@lodestar/types"; | ||
import {toHex} from "@lodestar/utils"; | ||
|
||
export function generateBlobsForTransaction(count: number): { | ||
blobs: string[]; | ||
kzgCommitments: string[]; | ||
blobVersionedHashes: string[]; | ||
kzgProofs: string[]; | ||
} { | ||
const blobs = Array.from({length: count}, () => generateRandomBlob()); | ||
const kzgCommitments = blobs.map((blob) => ckzg.blobToKzgCommitment(blob)); | ||
const versionedHash = kzgCommitments.map((kzgCommitment) => kzgCommitmentToVersionedHash(kzgCommitment)); | ||
const kzgProofs = blobs.map((blob, index) => ckzg.computeBlobKzgProof(blob, kzgCommitments[index])); | ||
|
||
return { | ||
blobs: blobs.map((blob) => toHex(blob)), | ||
kzgCommitments: kzgCommitments.map((kzgCommitment) => toHex(kzgCommitment)), | ||
blobVersionedHashes: versionedHash.map((hash) => toHex(hash)), | ||
kzgProofs: kzgProofs.map((proof) => toHex(proof)), | ||
}; | ||
} | ||
|
||
function generateRandomBlob(): deneb.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; | ||
} | ||
|
||
export function kzgCommitmentToVersionedHash(kzgCommitment: deneb.KZGCommitment): Uint8Array { | ||
const hash = sha256Digest(kzgCommitment); | ||
// Equivalent to `VERSIONED_HASH_VERSION_KZG + hash(kzg_commitment)[1:]` | ||
hash[0] = VERSIONED_HASH_VERSION_KZG; | ||
return hash; | ||
} |
Oops, something went wrong.