Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gas and proof time benchmark calculation to VAnchor #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions metrics/gas-metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"gasValues":[661286,661286,686895,661248,686847,1130005,699731,661250,724066,678023],"meanGas":725063.7,"medianGas":682435,"maxGas":1130005,"minGas":661248}
1 change: 1 addition & 0 deletions metrics/proof-time-metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"proofTimeBenchmark":[0.40818073800206184,0.32580487800017,0.3070519740022719,0.29866049300134184,0.31581392799690367,0.3105338900014758,0.3413618200011551,0.31213331399857996,0.3210563869997859,0.31590337700024246,0.3266429139971733,1.4795384709984065,0.4170037260018289,0.32156791399791834,0.32067060899734495,0.33638264499977233,0.3212308249995112,0.31559075000137093,0.3192889959998429,0.3091612539999187,0.3159814720004797,0.3263165629990399,0.31113440499827266,0.31878438099846246,0.32332615699991585,0.33013279699906706],"meanTime":0.37112517992278116,"medianTime":0.3208634979985654,"maxTime":1.4795384709984065,"minTime":0.29866049300134184}
42 changes: 41 additions & 1 deletion packages/anchors/src/VAnchor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber, BigNumberish, ethers } from 'ethers';
import { performance } from 'perf_hooks';
import { VAnchor as VAnchorContract, VAnchor__factory, VAnchorEncodeInputs__factory } from '@webb-tools/contracts';
import { p256, toHex, RootInfo, Keypair, FIELD_SIZE, getExtDataHash, toFixedHex, Utxo, getChainIdType } from '@webb-tools/utils';
import { p256, toHex, RootInfo, Keypair, FIELD_SIZE, getExtDataHash, toFixedHex, Utxo, getChainIdType, median, mean, max, min } from '@webb-tools/utils';
import { IAnchorDeposit, IAnchor, IExtData, IMerkleProofData, IUTXOInput, IVariableAnchorPublicInputs, IWitnessInput } from '@webb-tools/interfaces';
import { MerkleTree } from '@webb-tools/merkle-tree';

Expand All @@ -14,6 +15,8 @@ function checkNativeAddress(tokenAddress: string): boolean {
return false;
}

export var gasBenchmark = [];
export var proofTimeBenchmark = [];
// This convenience wrapper class is used in tests -
// It represents a deployed contract throughout its life (e.g. maintains merkle tree state)
// Functionality relevant to anchors in general (proving, verifying) is implemented in static methods
Expand Down Expand Up @@ -479,10 +482,15 @@ export class VAnchor implements IAnchor {
}

public async proveAndVerify(wtns: any, small: boolean) {
let start = performance.now();
let res = await snarkjs.groth16.prove(small
? this.smallCircuitZkeyPath
: this.largeCircuitZkeyPath, wtns
);
let proof_time = (performance.now() - start)/1000;

proofTimeBenchmark.push(proof_time)
// console.log("Proof gen took: ", proof_time, "s");
let proof = res.proof;
let publicSignals = res.publicSignals;

Expand All @@ -495,6 +503,35 @@ export class VAnchor implements IAnchor {
return proofEncoded;
}

public async getGasBenchmark() {
const gasValues = gasBenchmark.map(Number)
const meanGas = mean(gasValues);
const medianGas = median(gasValues);
const maxGas = max(gasValues);
const minGas = min(gasValues);
return {
gasValues,
meanGas,
medianGas,
maxGas,
minGas,
};
// return gasBenchmark;
}
public async getProofTimeBenchmark() {
const meanTime = mean(proofTimeBenchmark);
const medianTime = median(proofTimeBenchmark);
const maxTime = max(proofTimeBenchmark);
const minTime = min(proofTimeBenchmark);
return {
proofTimeBenchmark,
meanTime,
medianTime,
maxTime,
minTime,
}
}

public async setupTransaction(
inputs: Utxo[],
outputs: Utxo[],
Expand Down Expand Up @@ -602,6 +639,9 @@ export class VAnchor implements IAnchor {
);
const receipt = await tx.wait();
//console.log(`updated root (transact, contract) is ${toFixedHex(await this.contract.getLastRoot())}`);
// console.log("gas used: ", receipt.gasUsed.toString());
gasBenchmark.push(receipt.gasUsed.toString());

return receipt;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export const FIELD_SIZE = BigNumber.from(
'21888242871839275222246405745257275088548364400416034343698204186575808495617',
)

export const median = (arr: number[]): number => {
if (!arr.length) return undefined;
const s = [...arr].sort((a, b) => a - b);
const mid = Math.floor(s.length / 2);
return s.length % 2 === 0 ? ((s[mid - 1] + s[mid]) / 2) : s[mid];
};
export const mean = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
export const max = arr => arr.reduce((a,b)=>a>b?a:b);
export const min = arr => arr.reduce((a,b)=> a<=b?a:b);

/** Generate random number of specified byte length */
export const randomBN = (nbytes = 31) => BigNumber.from(crypto.randomBytes(nbytes))

Expand Down
10 changes: 10 additions & 0 deletions test/vanchor/vanchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { MerkleTree } from '../../packages/merkle-tree/src';
import { Utxo, poseidonHash, poseidonHash2 } from '../../packages/utils/src';
import { VAnchor } from '../../packages/anchors/src';
import { Verifier } from "../../packages/vbridge"
import { writeFileSync } from "fs";

const { NATIVE_AMOUNT } = process.env
const BN = require('bn.js');
Expand Down Expand Up @@ -1099,6 +1100,15 @@ describe('VAnchor for 2 max edges', () => {
wrappedToken.setFee(wrapFee, (await wrappedToken.proposalNonce()).add(1))
);
});
it('should print/save benchmarks', async () => {
// Alice deposits into tornado pool
const gasBenchmark = await anchor.getGasBenchmark()
const proofTimeBenchmark = await anchor.getProofTimeBenchmark()
console.log("Gas benchmark:\n", gasBenchmark);
console.log("Proof time benchmark:\n", proofTimeBenchmark);
writeFileSync("./metrics/gas-metrics.json", JSON.stringify(gasBenchmark));
writeFileSync("./metrics/proof-time-metrics.json", JSON.stringify(proofTimeBenchmark));
})
})
});