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 getCommitmentBatch = async ({ blockFrom, blockTo, cachedEvents, withCache }) => {
const events = [] const events = []
let { events: graphEvents, lastSyncBlock } = await getAllCommitments({ fromBlock: blockFrom, chainId }) let { events: graphEvents, lastSyncBlock } = await getAllCommitments({
fromBlock: blockFrom,
toBlock: blockTo,
chainId
})
if (lastSyncBlock) { if (lastSyncBlock) {
graphEvents = graphEvents.map(({ blockNumber, transactionHash, index, commitment, encryptedOutput }) => ({ graphEvents = graphEvents
blockNumber, .filter(({ blockNumber }) => {
transactionHash, if (blockFrom && blockTo) {
index: Number(index), return Number(blockFrom) <= Number(blockNumber) && Number(blockNumber) <= Number(blockTo)
commitment, } else if (blockTo) {
encryptedOutput, 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({ console.log({
graphEvents graphEvents

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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long