mirror of
https://github.com/tornadocash/tornado-cli.git
synced 2025-07-30 01:38:38 -04:00
fix: cache
This commit is contained in:
parent
58e99581e7
commit
7d071b2ac5
10 changed files with 907170 additions and 183 deletions
94
cli.js
94
cli.js
|
@ -113,20 +113,41 @@ async function deposit({ currency, amount }) {
|
|||
* in it and generates merkle proof
|
||||
* @param deposit Deposit object
|
||||
*/
|
||||
async function generateMerkleProof(deposit) {
|
||||
async function generateMerkleProof(deposit, amount) {
|
||||
let leafIndex = -1
|
||||
// Get all deposit events from smart contract and assemble merkle tree from them
|
||||
const events = await mixerContract.getPastEvents('Deposit', { fromBlock: 0, toBlock: 'latest' })
|
||||
const cachedEvents = loadCachedEvents({ type: 'Deposit', amount })
|
||||
|
||||
const startBlock = cachedEvents.lastBlock
|
||||
|
||||
let rpcEvents = await mixerContract.getPastEvents('Deposit', {
|
||||
fromBlock: startBlock,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
|
||||
rpcEvents = rpcEvents.map(({ blockNumber, transactionHash, returnValues }) => {
|
||||
const { commitment, leafIndex, timestamp } = returnValues
|
||||
return {
|
||||
blockNumber,
|
||||
transactionHash,
|
||||
commitment,
|
||||
leafIndex: Number(leafIndex),
|
||||
timestamp
|
||||
}
|
||||
})
|
||||
|
||||
const events = cachedEvents.events.concat(rpcEvents)
|
||||
console.log('events', events.length)
|
||||
|
||||
const leaves = events
|
||||
.sort((a, b) => a.returnValues.leafIndex - b.returnValues.leafIndex) // Sort events in chronological order
|
||||
.sort((a, b) => a.leafIndex - b.leafIndex) // Sort events in chronological order
|
||||
.map((e) => {
|
||||
const index = toBN(e.returnValues.leafIndex).toNumber()
|
||||
const index = toBN(e.leafIndex).toNumber()
|
||||
|
||||
if (toBN(e.returnValues.commitment).eq(toBN(deposit.commitmentHex))) {
|
||||
if (toBN(e.commitment).eq(toBN(deposit.commitmentHex))) {
|
||||
leafIndex = index
|
||||
}
|
||||
return e.returnValues.commitment.toString(10)
|
||||
return toBN(e.commitment).toString(10)
|
||||
})
|
||||
const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
|
||||
|
||||
|
@ -150,9 +171,9 @@ async function generateMerkleProof(deposit) {
|
|||
* @param fee Relayer fee
|
||||
* @param refund Receive ether for exchanged tokens
|
||||
*/
|
||||
async function generateProof({ deposit, recipient, relayerAddress = 0, fee = 0, refund = 0 }) {
|
||||
async function generateProof({ deposit, amount, recipient, relayerAddress = 0, fee = 0, refund = 0 }) {
|
||||
// Compute merkle proof of our commitment
|
||||
const { root, path_elements, path_index } = await generateMerkleProof(deposit)
|
||||
const { root, path_elements, path_index } = await generateMerkleProof(deposit, amount)
|
||||
|
||||
// Prepare circuit input
|
||||
const input = {
|
||||
|
@ -217,7 +238,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
|
|||
throw new Error('Too high refund')
|
||||
}
|
||||
|
||||
const { proof, args } = await generateProof({ deposit, recipient, relayerAddress: rewardAccount, fee, refund })
|
||||
const { proof, args } = await generateProof({ deposit, amount, recipient, relayerAddress: rewardAccount, fee, refund })
|
||||
|
||||
console.log('Sending withdraw transaction through relay')
|
||||
try {
|
||||
|
@ -450,6 +471,30 @@ function waitForTxReceipt({ txHash, attempts = 60, delay = 1000 }) {
|
|||
})
|
||||
}
|
||||
|
||||
function loadCachedEvents({ type, amount }) {
|
||||
try {
|
||||
if (netId !== 1) {
|
||||
return {
|
||||
events: [],
|
||||
lastBlock: 0,
|
||||
}
|
||||
}
|
||||
|
||||
const module = require(`./cache/${type.toLowerCase()}s_eth_${amount}.json`)
|
||||
|
||||
if (module) {
|
||||
const events = module
|
||||
|
||||
return {
|
||||
events,
|
||||
lastBlock: events[events.length - 1].blockNumber
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
throw new Error(`Method loadCachedEvents has error: ${err.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses Tornado.cash note
|
||||
* @param noteString the note
|
||||
|
@ -472,7 +517,7 @@ function parseNote(noteString) {
|
|||
|
||||
async function loadDepositData({ deposit }) {
|
||||
try {
|
||||
const eventWhenHappened = await tornado.getPastEvents('Deposit', {
|
||||
const eventWhenHappened = await mixerContract.getPastEvents('Deposit', {
|
||||
filter: {
|
||||
commitment: deposit.commitmentHex
|
||||
},
|
||||
|
@ -496,16 +541,33 @@ async function loadDepositData({ deposit }) {
|
|||
}
|
||||
async function loadWithdrawalData({ amount, currency, deposit }) {
|
||||
try {
|
||||
const events = await await tornado.getPastEvents('Withdrawal', {
|
||||
fromBlock: 0,
|
||||
const cachedEvents = loadCachedEvents({ type: 'Withdrawal', amount })
|
||||
|
||||
const startBlock = cachedEvents.lastBlock
|
||||
|
||||
let rpcEvents = await mixerContract.getPastEvents('Withdrawal', {
|
||||
fromBlock: startBlock,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
|
||||
rpcEvents = rpcEvents.map(({ blockNumber, transactionHash, returnValues }) => {
|
||||
const { nullifierHash, to, fee } = returnValues
|
||||
return {
|
||||
blockNumber,
|
||||
transactionHash,
|
||||
nullifierHash,
|
||||
to,
|
||||
fee
|
||||
}
|
||||
})
|
||||
|
||||
const events = cachedEvents.events.concat(rpcEvents)
|
||||
|
||||
const withdrawEvent = events.filter((event) => {
|
||||
return event.returnValues.nullifierHash === deposit.nullifierHex
|
||||
return event.nullifierHash === deposit.nullifierHex
|
||||
})[0]
|
||||
|
||||
const fee = withdrawEvent.returnValues.fee
|
||||
const fee = withdrawEvent.fee
|
||||
const decimals = config.deployments[`netId${netId}`][currency].decimals
|
||||
const withdrawalAmount = toBN(fromDecimals({ amount, decimals })).sub(
|
||||
toBN(fee)
|
||||
|
@ -514,7 +576,7 @@ async function loadWithdrawalData({ amount, currency, deposit }) {
|
|||
return {
|
||||
amount: toDecimals(withdrawalAmount, decimals, 9),
|
||||
txHash: withdrawEvent.transactionHash,
|
||||
to: withdrawEvent.returnValues.to,
|
||||
to: withdrawEvent.to,
|
||||
timestamp,
|
||||
nullifier: deposit.nullifierHex,
|
||||
fee: toDecimals(fee, decimals, 9)
|
||||
|
@ -579,7 +641,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100' }) {
|
|||
} else {
|
||||
try {
|
||||
tornadoAddress = config.deployments[`netId${netId}`].proxy
|
||||
tornadoInstance = config.deployments[`netId${netId}`][currency].instanceAddress[amount]
|
||||
tornadoInstance = config.deployments[`netId${netId}`][currency].mixerAddress[amount]
|
||||
|
||||
if (!tornadoAddress) {
|
||||
throw new Error()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue