Support toBlock condition for commitments to prevent balance being double counted

This commit is contained in:
Tornado Contrib 2024-05-07 13:19:02 +00:00
parent 6447563ea0
commit d86d3ff125
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
4 changed files with 30 additions and 12 deletions

@ -73,16 +73,30 @@ const setTornadoPool = (chainId, provider) => {
const getCommitmentBatch = async ({ blockFrom, blockTo, cachedEvents, withCache }) => {
const events = []
let { events: graphEvents, lastSyncBlock } = await getAllCommitments({ fromBlock: blockFrom, chainId })
let { events: graphEvents, lastSyncBlock } = await getAllCommitments({
fromBlock: blockFrom,
toBlock: blockTo,
chainId
})
if (lastSyncBlock) {
graphEvents = graphEvents.map(({ blockNumber, transactionHash, index, commitment, encryptedOutput }) => ({
blockNumber,
transactionHash,
index: Number(index),
commitment,
encryptedOutput,
}))
graphEvents = graphEvents
.filter(({ blockNumber }) => {
if (blockFrom && blockTo) {
return Number(blockFrom) <= Number(blockNumber) && Number(blockNumber) <= Number(blockTo)
} else if (blockTo) {
return Number(blockNumber) <= Number(blockTo)
}
// does not filter by default
return true
})
.map(({ blockNumber, transactionHash, index, commitment, encryptedOutput }) => ({
blockNumber,
transactionHash,
index: Number(index),
commitment,
encryptedOutput,
}))
console.log({
graphEvents

@ -45,7 +45,7 @@ async function getCommitments({ fromBlock, chainId }) {
}
}
async function getAllCommitments({ fromBlock, chainId }) {
async function getAllCommitments({ fromBlock, toBlock, chainId }) {
try {
let commitments = []
let lastSyncBlock
@ -70,6 +70,10 @@ async function getAllCommitments({ fromBlock, chainId }) {
fromBlock = Number(lastEvent.blockNumber)
commitments = commitments.concat(results)
if (toBlock && fromBlock >= Number(toBlock)) {
break
}
}
if (!commitments) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long