Skip to content

Commit

Permalink
Revert "chore: update message tree depth programmatically in benchmar…
Browse files Browse the repository at this point in the history
…ks (privacy-scaling-explorations#1799)"

This reverts commit eb1ce58.
  • Loading branch information
djanluka committed Sep 2, 2024
1 parent ecdffdd commit a51d3b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
16 changes: 0 additions & 16 deletions packages/contracts/tasks/helpers/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,22 +396,6 @@ 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: 25 additions & 15 deletions packages/contracts/tasks/helpers/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,40 @@ export async function publishBatch(
const messageBatch = Array.from({ length: batchSize }, () => message.asContractParam());
const pubKeyBatch = Array.from({ length: batchSize }, () => keypair.pubKey.asContractParam());

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

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

try {
// eslint-disable-next-line no-await-in-loop
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
await pollContract.publishMessageBatch.estimateGas(testMessageBatch, testPubKeyBatch);
optimalBatchSize = mid;
low = mid + 1;
} catch (error) {
// If this size doesn't work, reduce by 1 and try again
optimalBatchSize -= 1;
high = mid - 1;
}
}

if (optimalBatchSize > 0) {
console.log(`Found optimal batch size: ${optimalBatchSize}`);
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`);
}
} else {
console.error("Unable to publish even a single message");
console.error("Unable to submit even a single message\n");
}
}
7 changes: 0 additions & 7 deletions packages/contracts/tasks/runner/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 @@ -16,14 +15,8 @@ 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 a51d3b0

Please sign in to comment.