Skip to content

Commit

Permalink
chore: benchmark BeaconBlock hashTreeRoot()
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 4, 2024
1 parent f106a2d commit 33e8267
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions packages/ssz/test/perf/eth2/beaconBlock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {itBench, setBenchOpts} from "@dapplion/benchmark";
import {ValueWithCachedPermanentRoot, symbolCachedPermanentRoot} from "../../../src/util/merkleize";
import {deneb, ssz} from "../../lodestarTypes";
import {preset} from "../../lodestarTypes/params";
import {BitArray, toHexString} from "../../../src";
const {MAX_ATTESTATIONS, MAX_DEPOSITS, MAX_VOLUNTARY_EXITS, MAX_BLS_TO_EXECUTION_CHANGES} = preset;

describe("Benchmark BeaconBlock.hashTreeRoot()", function () {
setBenchOpts({
minMs: 10_000,
});

const block = ssz.deneb.BeaconBlock.defaultValue();
for (let i = 0; i < MAX_ATTESTATIONS; i++) {
block.body.attestations.push({
aggregationBits: BitArray.fromBoolArray(Array.from({length: 64}, () => true)),
data: {
slot: 1,
index: 1,
beaconBlockRoot: Buffer.alloc(32, 1),
source: {
epoch: 1,
root: Buffer.alloc(32, 1),
},
target: {
epoch: 1,
root: Buffer.alloc(32, 1),
},
},
signature: Buffer.alloc(96, 1),
});
}
for (let i = 0; i < MAX_DEPOSITS; i++) {
block.body.deposits.push({
proof: ssz.phase0.Deposit.fields.proof.defaultValue(),
data: {
pubkey: Buffer.alloc(48, 1),
withdrawalCredentials: Buffer.alloc(32, 1),
amount: 32 * 1e9,
signature: Buffer.alloc(96, 1),
},
});
}
for (let i = 0; i < MAX_VOLUNTARY_EXITS; i++) {
block.body.voluntaryExits.push({
signature: Buffer.alloc(96, 1),
message: {
epoch: 1,
validatorIndex: 1,
},
});
}
// common data on mainnet as of Jun 2024
const numTransaction = 200;
const transactionLen = 500;
for (let i = 0; i < numTransaction; i++) {
block.body.executionPayload.transactions.push(Buffer.alloc(transactionLen, 1));
}
for (let i = 0; i < MAX_BLS_TO_EXECUTION_CHANGES; i++) {
block.body.blsToExecutionChanges.push({
signature: Buffer.alloc(96, 1),
message: {
validatorIndex: 1,
fromBlsPubkey: Buffer.alloc(48, 1),
toExecutionAddress: Buffer.alloc(20, 1),
},
});
}

const root = ssz.deneb.BeaconBlock.hashTreeRoot(block);
console.log("BeaconBlock.hashTreeRoot() root", toHexString(root));
itBench({
id: `Deneb BeaconBlock.hashTreeRoot(), numTransaciton=${numTransaction}`,
beforeEach: () => {
clearCachedRoots(block);
return block;
},
fn: (block: deneb.BeaconBlock) => {
ssz.deneb.BeaconBlock.hashTreeRoot(block);
},
});
});

function clearCachedRoots(block: deneb.BeaconBlock): void {
(block as ValueWithCachedPermanentRoot)[symbolCachedPermanentRoot] = undefined;
(block.body as ValueWithCachedPermanentRoot)[symbolCachedPermanentRoot] = undefined;
const attestations = block.body.attestations;
for (const attestation of attestations) {
(attestation.data as ValueWithCachedPermanentRoot)[symbolCachedPermanentRoot] = undefined;
}
for (const exit of block.body.voluntaryExits) {
(exit as ValueWithCachedPermanentRoot)[symbolCachedPermanentRoot] = undefined;
}
}

0 comments on commit 33e8267

Please sign in to comment.