Skip to content

Commit

Permalink
Merge f75c533 into 669239b
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain authored Apr 12, 2024
2 parents 669239b + f75c533 commit d1fc151
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 70 deletions.
42 changes: 35 additions & 7 deletions packages/cli/test/utils/simulation/assertions/blobsAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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 "../web3JsPlugins.js";
import {BlobsEIP4844Transaction} from "../web3js/blobsEIP4844Transaction.js";

const numberOfBlobs = 6;
const sentBlobs: Uint8Array[] = [];

export function createBlobsAssertion(
nodes: NodePair[],
{sendBlobsAtSlot, validateBlobsAt}: {sendBlobsAtSlot: number; validateBlobsAt: number}
): SimulationAssertion<string, number> {
): SimulationAssertion<string, Uint8Array[]> {
return {
id: `blobs-${nodes.map((n) => n.id).join("-")}`,
match: ({slot}) => {
Expand Down Expand Up @@ -45,33 +46,60 @@ export function createBlobsAssertion(
});
const signedTx = tx.sign(fromHex(`0x${EL_GENESIS_SECRET_KEY}`));
await node.execution.provider?.extended.sendRawTransaction(toHex(signedTx.serialize()));

sentBlobs.push(...blobs.map((b) => fromHex(b)));
}

const blobSideCars = await node.beacon.api.beacon.getBlobSidecars(slot);
ApiError.assert(blobSideCars);

return blobSideCars.response.data.length;
return blobSideCars.response.data.map((b) => b.blob);
},

assert: async ({store}) => {
const errors: AssertionResult[] = [];

let eip4844Blobs = 0;
const blobs: Uint8Array[] = [];

for (let slot = sendBlobsAtSlot; slot <= validateBlobsAt; slot++) {
eip4844Blobs += store[slot] ?? 0;
blobs.push(...(store[slot] ?? []));
}

if (eip4844Blobs !== numberOfBlobs) {
if (blobs.length !== numberOfBlobs) {
errors.push([
"Node does not have right number of blobs",
{
expectedBlobs: numberOfBlobs,
currentBlobs: eip4844Blobs,
currentBlobs: blobs.length,
},
]);
}

for (let i = 0; i < blobs.length; i++) {
if (!Buffer.from(blobs[i]).equals(Buffer.from(sentBlobs[i]))) {
errors.push(["Node does not have the correct blobs", {index: i}]);
}
}
return errors;
},

async dump({store, nodes}) {
const result: Record<string, string> = {
"expectedBlobs.txt": sentBlobs.map(toHex).join("\n"),
};

for (const node of nodes) {
const blobs: Uint8Array[] = [];
for (let slot = sendBlobsAtSlot; slot <= validateBlobsAt; slot++) {
if (store[node.beacon.id] !== undefined) {
blobs.push(...(store[node.beacon.id][slot] ?? []));
}
}

result[`blobs-${node.beacon.id}.txt`] = blobs.map(toHex).join("\n");
}

return result;
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
SHARED_JWT_SECRET,
SIM_ENV_NETWORK_ID,
} from "../../constants.js";
import {registerWeb3JsPlugins} from "../../web3JsPlugins.js";
import {registerWeb3JsPlugins} from "../../web3js/plugins/index.js";
import {ExecutionClient, ExecutionNodeGenerator, ExecutionStartMode, JobOptions, RunnerType} from "../../interfaces.js";
import {getNodeMountedPaths} from "../../utils/paths.js";
import {getNodePorts} from "../../utils/ports.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {writeFile} from "node:fs/promises";
import path from "node:path";
import got from "got";
import {Web3} from "web3";
import {registerWeb3JsPlugins} from "../../web3JsPlugins.js";
import {registerWeb3JsPlugins} from "../../web3js/plugins/index.js";
import {ExecutionClient, ExecutionNodeGenerator, JobOptions, RunnerType} from "../../interfaces.js";
import {getNethermindChainSpec} from "../../utils/executionGenesis.js";
import {getNodeMountedPaths} from "../../utils/paths.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Web3PluginBase, Web3} from "web3";
import {RLP} from "@ethereumjs/rlp";
import {keccak256} from "ethereum-cryptography/keccak.js";
import {
Expand Down Expand Up @@ -158,63 +157,3 @@ export class BlobsEIP4844Transaction extends FeeMarketEIP1559Transaction {
);
}
}

class Web3AdminPlugin extends Web3PluginBase {
/**
* The admin plugin as available via the provider object
* like in the example below.
*
* await node.web3.admin.addPeer(elIdentity.enode);
*/
pluginNamespace = "admin";

async nodeInfo(): Promise<{
enode: string;
id: string;
ip: string;
listenAddr: string;
name: string;
ports: {
discovery: number;
listener: number;
};
protocols: {
eth: {
difficulty: number;
genesis: string;
head: string;
network: number;
};
};
}> {
return this.requestManager.send({method: "admin_nodeInfo", params: []});
}

async addPeer(enode: string): Promise<boolean> {
return this.requestManager.send({method: "admin_addPeer", params: [enode]});
}
}

class Web3ExtendedEthPlugin extends Web3PluginBase {
pluginNamespace = "extended";

async sendRawTransaction(tx: string): Promise<string> {
return this.requestManager.send({method: "eth_sendRawTransaction", params: [tx]});
}

async sendPlainTransaction(...params: unknown[]): Promise<string> {
return this.requestManager.send({method: "eth_sendTransaction", params: [...params]});
}
}

declare module "web3" {
interface Web3Context {
admin: Web3AdminPlugin;
extended: Web3ExtendedEthPlugin;
}
}

export function registerWeb3JsPlugins(web3: Web3): void {
web3.registerPlugin(new Web3AdminPlugin());
web3.registerPlugin(new Web3ExtendedEthPlugin());
}
8 changes: 8 additions & 0 deletions packages/cli/test/utils/simulation/web3js/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {Web3} from "web3";
import {Web3AdminPlugin} from "./web3AdminPlugin.js";
import {Web3ExtendedEthPlugin} from "./web3ExtendedEthPlugin.js";

export function registerWeb3JsPlugins(web3: Web3): void {
web3.registerPlugin(new Web3AdminPlugin());
web3.registerPlugin(new Web3ExtendedEthPlugin());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Web3PluginBase} from "web3";

export class Web3AdminPlugin extends Web3PluginBase {
/**
* The admin plugin as available via the provider object
* like in the example below.
*
* await node.web3.admin.addPeer(elIdentity.enode);
*/
pluginNamespace = "admin";

async nodeInfo(): Promise<{
enode: string;
id: string;
ip: string;
listenAddr: string;
name: string;
ports: {
discovery: number;
listener: number;
};
protocols: {
eth: {
difficulty: number;
genesis: string;
head: string;
network: number;
};
};
}> {
return this.requestManager.send({method: "admin_nodeInfo", params: []});
}

async addPeer(enode: string): Promise<boolean> {
return this.requestManager.send({method: "admin_addPeer", params: [enode]});
}
}

declare module "web3" {
interface Web3Context {
admin: Web3AdminPlugin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Web3PluginBase} from "web3";

export class Web3ExtendedEthPlugin extends Web3PluginBase {
pluginNamespace = "extended";

async sendRawTransaction(tx: string): Promise<string> {
return this.requestManager.send({method: "eth_sendRawTransaction", params: [tx]});
}

async sendPlainTransaction(...params: unknown[]): Promise<string> {
return this.requestManager.send({method: "eth_sendTransaction", params: [...params]});
}
}

declare module "web3" {
interface Web3Context {
extended: Web3ExtendedEthPlugin;
}
}

0 comments on commit d1fc151

Please sign in to comment.