fix tree watcher loop

This commit is contained in:
poma 2021-03-31 11:35:43 +03:00
parent d22bb59de5
commit 19054c0c0a
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
2 changed files with 9 additions and 8 deletions

@ -1,6 +1,6 @@
{ {
"name": "relay", "name": "relay",
"version": "4.0.10", "version": "4.0.11",
"description": "Relayer for Tornado.cash privacy solution. https://tornado.cash", "description": "Relayer for Tornado.cash privacy solution. https://tornado.cash",
"scripts": { "scripts": {
"server": "node src/server.js", "server": "node src/server.js",

@ -59,7 +59,7 @@ async function processNewEvent(err, event) {
await updateRedis() await updateRedis()
} else { } else {
console.log(`Invalid element index ${index}, rebuilding tree`) console.log(`Invalid element index ${index}, rebuilding tree`)
await rebuild() rebuild()
} }
} }
@ -77,7 +77,7 @@ async function updateRedis() {
const rootOnContract = await contract.methods.getLastAccountRoot().call() const rootOnContract = await contract.methods.getLastAccountRoot().call()
if (!tree.root().eq(toBN(rootOnContract))) { if (!tree.root().eq(toBN(rootOnContract))) {
console.log(`Invalid tree root: ${tree.root()} != ${toBN(rootOnContract)}, rebuilding tree`) console.log(`Invalid tree root: ${tree.root()} != ${toBN(rootOnContract)}, rebuilding tree`)
await rebuild() rebuild()
return return
} }
const rootInRedis = await redis.get('tree:root') const rootInRedis = await redis.get('tree:root')
@ -92,10 +92,11 @@ async function updateRedis() {
} }
} }
async function rebuild() { function rebuild() {
await eventSubscription.unsubscribe() process.exit(1)
await blockSubscription.unsubscribe() // await eventSubscription.unsubscribe()
setTimeout(init, 3000) // await blockSubscription.unsubscribe()
// setTimeout(init, 3000)
} }
async function init() { async function init() {
@ -106,10 +107,10 @@ async function init() {
const block = await web3.eth.getBlockNumber() const block = await web3.eth.getBlockNumber()
const events = await fetchEvents(0, block) const events = await fetchEvents(0, block)
tree = new MerkleTree(minerMerkleTreeHeight, events, { hashFunction: poseidonHash2 }) tree = new MerkleTree(minerMerkleTreeHeight, events, { hashFunction: poseidonHash2 })
await updateRedis()
console.log(`Rebuilt tree with ${events.length} elements, root: ${tree.root()}`) console.log(`Rebuilt tree with ${events.length} elements, root: ${tree.root()}`)
eventSubscription = contract.events.NewAccount({ fromBlock: block + 1 }, processNewEvent) eventSubscription = contract.events.NewAccount({ fromBlock: block + 1 }, processNewEvent)
blockSubscription = web3.eth.subscribe('newBlockHeaders', processNewBlock) blockSubscription = web3.eth.subscribe('newBlockHeaders', processNewBlock)
await updateRedis()
} catch (e) { } catch (e) {
console.error('error on init treeWatcher', e.message) console.error('error on init treeWatcher', e.message)
} }