Skip to content

Commit

Permalink
chore: update message tree depth programmatically in benchmarks (#1799)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 authored Aug 30, 2024
1 parent a97f395 commit eb1ce58
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
16 changes: 16 additions & 0 deletions packages/contracts/tasks/helpers/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,22 @@ export class Deployment {
return value;
}

/**
* Update deploy config field (see deploy-config.json)
* @param id - contract name
* @param field - config field key
* @param value - config field value
*/
updateDeployConfig<T = string | number | boolean, ID extends string = EContracts>(
id: ID,
field: string,
value: T,
): void {
this.checkHre();

this.config.set(`${this.hre!.network.name}.${id}.${field}`, value).write();
}

/**
* Get contract by name and group key
*
Expand Down
40 changes: 15 additions & 25 deletions packages/contracts/tasks/helpers/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,30 @@ export async function publishBatch(
const messageBatch = Array.from({ length: batchSize }, () => message.asContractParam());
const pubKeyBatch = Array.from({ length: batchSize }, () => keypair.pubKey.asContractParam());

let low = 1;
let high = batchSize;
let optimalBatchSize = 0;
let optimalBatchSize = batchSize;

while (low <= high) {
const mid = Math.floor((low + high) / 2);
const testMessageBatch = messageBatch.slice(0, mid);
const testPubKeyBatch = pubKeyBatch.slice(0, mid);
while (optimalBatchSize > 0) {
const finalMessageBatch = messageBatch.slice(0, optimalBatchSize);
const finalPubKeyBatch = pubKeyBatch.slice(0, optimalBatchSize);

try {
// eslint-disable-next-line no-await-in-loop
await pollContract.publishMessageBatch.estimateGas(testMessageBatch, testPubKeyBatch);
optimalBatchSize = mid;
low = mid + 1;
const tx = await pollContract.publishMessageBatch(finalMessageBatch, finalPubKeyBatch);
// eslint-disable-next-line no-await-in-loop
const receipt = await tx.wait();
console.log(`Successfully published batch of ${optimalBatchSize} messages`);
console.log(`Gas used: ${receipt?.gasUsed.toString()} wei`);
console.log(`Tx: ${tx.hash}`);
break; // If successful, we've found the largest working batch size
} catch (error) {
high = mid - 1;
// If this size doesn't work, reduce by 1 and try again
optimalBatchSize -= 1;
}
}

if (optimalBatchSize > 0) {
const finalMessageBatch = messageBatch.slice(0, optimalBatchSize);
const finalPubKeyBatch = pubKeyBatch.slice(0, optimalBatchSize);

try {
const tx = await pollContract.publishMessageBatch(finalMessageBatch, finalPubKeyBatch);
const receipt = await tx.wait();
console.log(`Gas used: ${receipt?.gasUsed.toString()} wei\n`);
console.log(`Tx: ${tx.hash}\n`);

console.log(`Submitted a batch of ${optimalBatchSize} messages\n`);
} catch (err) {
console.error(`Failed to submit a batch of ${optimalBatchSize} messages\n`);
}
console.log(`Found optimal batch size: ${optimalBatchSize}`);
} else {
console.error("Unable to submit even a single message\n");
console.error("Unable to publish even a single message");
}
}
7 changes: 7 additions & 0 deletions packages/contracts/tasks/runner/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { task } from "hardhat/config";
import { Keypair, PCommand } from "maci-domainobjs";

import { Deployment } from "../helpers/Deployment";
import { EContracts } from "../helpers/types";

task("benchmark", "Run benchmarks").setAction(async (_, hre) => {
const deployment = Deployment.getInstance(hre);
Expand All @@ -15,8 +16,14 @@ task("benchmark", "Run benchmarks").setAction(async (_, hre) => {
await deployment.runSteps(steps, 0);

// deploy a Poll
// get original tree depth
const messageTreeDepth = deployment.getDeployConfigField<number>(EContracts.VkRegistry, "messageTreeDepth");
// update it
deployment.updateDeployConfig(EContracts.VkRegistry, "messageTreeDepth", 3);
const pollSteps = await deployment.start("poll", { incremental: true, verify: false });
await deployment.runSteps(pollSteps, 0);
// restore it
deployment.updateDeployConfig(EContracts.VkRegistry, "messageTreeDepth", messageTreeDepth);

try {
const startBalance = await deployer.provider.getBalance(deployer);
Expand Down

0 comments on commit eb1ce58

Please sign in to comment.