Load groth16 worker only when withdrawal

Co-authored-by: Danil Kovtonyuk <danx.kov@gmail.com>
This commit is contained in:
Ayanami 2022-03-11 12:21:16 +09:00
parent 2ab7e7afb6
commit b7931adaa4
No known key found for this signature in database
GPG Key ID: 0CABDF03077D92E4

24
cli.js
View File

@ -22,7 +22,7 @@ const { GasPriceOracle } = require('gas-price-oracle');
const SocksProxyAgent = require('socks-proxy-agent'); const SocksProxyAgent = require('socks-proxy-agent');
const is_ip_private = require('private-ip'); const is_ip_private = require('private-ip');
let web3, torPort, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol, doNotSubmitTx, multiCall, privateRpc, subgraph; let web3, torPort, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol, doNotSubmitTx, multiCall, privateRpc, subgraph, isMobile;
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY; let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY;
/** Whether we are in a browser or node.js */ /** Whether we are in a browser or node.js */
@ -372,6 +372,22 @@ async function generateProof({ deposit, currency, amount, recipient, relayerAddr
pathIndices: pathIndices pathIndices: pathIndices
} }
if (!groth16) {
const wasmMemory = isMobile ? 1000 : 2000;
// groth16 initialises a lot of Promises that will never be resolved, that's why we need to use process.exit to terminate the CLI
groth16 = await buildGroth16({ wasmInitialMemory: wasmMemory });
}
if (!circuit && !proving_key) {
if (inBrowser) {
circuit = await (await fetch('build/circuits/tornado.json')).json();
proving_key = await (await fetch('build/circuits/tornadoProvingKey.bin')).arrayBuffer();
} else {
circuit = require('./build/circuits/tornado.json');
proving_key = fs.readFileSync('build/circuits/tornadoProvingKey.bin').buffer;
}
}
console.log('Generating SNARK proof'); console.log('Generating SNARK proof');
console.time('Proof time'); console.time('Proof time');
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key); const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key);
@ -1250,8 +1266,6 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
}); });
contractJson = await (await fetch('build/contracts/TornadoProxy.abi.json')).json(); contractJson = await (await fetch('build/contracts/TornadoProxy.abi.json')).json();
instanceJson = await (await fetch('build/contracts/Instance.abi.json')).json(); instanceJson = await (await fetch('build/contracts/Instance.abi.json')).json();
circuit = await (await fetch('build/circuits/tornado.json')).json();
proving_key = await (await fetch('build/circuits/tornadoProvingKey.bin')).arrayBuffer();
MERKLE_TREE_HEIGHT = 20; MERKLE_TREE_HEIGHT = 20;
ETH_AMOUNT = 1e18; ETH_AMOUNT = 1e18;
TOKEN_AMOUNT = 1e19; TOKEN_AMOUNT = 1e19;
@ -1297,8 +1311,6 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
} }
contractJson = require('./build/contracts/TornadoProxy.abi.json'); contractJson = require('./build/contracts/TornadoProxy.abi.json');
instanceJson = require('./build/contracts/Instance.abi.json'); instanceJson = require('./build/contracts/Instance.abi.json');
circuit = require('./build/circuits/tornado.json');
proving_key = fs.readFileSync('build/circuits/tornadoProvingKey.bin').buffer;
MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT || 20; MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT || 20;
ETH_AMOUNT = process.env.ETH_AMOUNT; ETH_AMOUNT = process.env.ETH_AMOUNT;
TOKEN_AMOUNT = process.env.TOKEN_AMOUNT; TOKEN_AMOUNT = process.env.TOKEN_AMOUNT;
@ -1319,8 +1331,6 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
erc20ContractJson = require('./build/contracts/ERC20Mock.json'); erc20ContractJson = require('./build/contracts/ERC20Mock.json');
erc20tornadoJson = require('./build/contracts/ERC20Tornado.json'); erc20tornadoJson = require('./build/contracts/ERC20Tornado.json');
} }
// groth16 initialises a lot of Promises that will never be resolved, that's why we need to use process.exit to terminate the CLI
groth16 = await buildGroth16();
netId = await web3.eth.net.getId(); netId = await web3.eth.net.getId();
netName = getCurrentNetworkName(); netName = getCurrentNetworkName();
netSymbol = getCurrentNetworkSymbol(); netSymbol = getCurrentNetworkSymbol();