From 68e47714a451540e1cff889fb822e2ee7020c711 Mon Sep 17 00:00:00 2001 From: andresobandoalfaro Date: Mon, 13 May 2024 17:36:38 +0000 Subject: [PATCH] feat: change mutex scope --- merkle-tree/src/merkle-tree-controller.js | 83 ++++++++++---------- merkle-tree/src/routes/merkle-tree.routes.js | 14 ++-- 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/merkle-tree/src/merkle-tree-controller.js b/merkle-tree/src/merkle-tree-controller.js index 809f399..ac1f284 100644 --- a/merkle-tree/src/merkle-tree-controller.js +++ b/merkle-tree/src/merkle-tree-controller.js @@ -10,7 +10,6 @@ import utilsMT from './utils-merkle-tree'; import logger from './logger'; import { LeafService, NodeService, MetadataService } from './db/service'; -import { mutex } from './mutex-controller'; /** Check the leaves of the tree are all there. @@ -25,64 +24,62 @@ async function checkLeaves(db) { // count all the leaves // get the max leafIndex of all the leaves - return await mutex.runExclusive(async () => { - let { leafCount, maxLeafIndex } = await leafService.getCountAndMaxLeafIndex(); - logger.debug(`leaf count: ${leafCount}, max leaf index: ${maxLeafIndex}`); - if (maxLeafIndex === undefined) maxLeafIndex = -1; + let { leafCount, maxLeafIndex } = await leafService.getCountAndMaxLeafIndex(); + logger.debug(`leaf count: ${leafCount}, max leaf index: ${maxLeafIndex}`); + if (maxLeafIndex === undefined) maxLeafIndex = -1; - let maxReliableLeafIndex; + let maxReliableLeafIndex; - // then we can quickly see if there are NOT any missing leaves: - if (leafCount < maxLeafIndex + 1) { - // then we are missing values. Let's do a slower search to find the earliest missing value: - logger.error( - `There are missing leaves in the db. Found ${leafCount} leaves, but expected ${maxLeafIndex + - 1}. Performing a slower check to find the missing leaves...`, - ); - const missingLeaves = await leafService.findMissingLeaves(0, maxLeafIndex); - - logger.warn(`missing leaves: ${JSON.stringify(missingLeaves, null, 2)}`); + // then we can quickly see if there are NOT any missing leaves: + if (leafCount < maxLeafIndex + 1) { + // then we are missing values. Let's do a slower search to find the earliest missing value: + logger.error( + `There are missing leaves in the db. Found ${leafCount} leaves, but expected ${maxLeafIndex + + 1}. Performing a slower check to find the missing leaves...`, + ); + const missingLeaves = await leafService.findMissingLeaves(0, maxLeafIndex); - const minMissingLeafIndex = missingLeaves[0] || 0; + logger.warn(`missing leaves: ${JSON.stringify(missingLeaves, null, 2)}`); - maxReliableLeafIndex = minMissingLeafIndex - 1; + const minMissingLeafIndex = missingLeaves[0] || 0; - let fromBlock; + maxReliableLeafIndex = minMissingLeafIndex - 1; - if (minMissingLeafIndex > 0) { - // get the prior leaf: - const latestConsecutiveLeaf = await leafService.getLeafByLeafIndex(maxReliableLeafIndex); + let fromBlock; - fromBlock = latestConsecutiveLeaf.blockNumber; - } else { - // start from scratch: - fromBlock = config.FILTER_GENESIS_BLOCK_NUMBER; - return maxReliableLeafIndex; // the maximum reliable leafIndex is -1; i.e. nothing's reliable. Let's start again. - } + if (minMissingLeafIndex > 0) { + // get the prior leaf: + const latestConsecutiveLeaf = await leafService.getLeafByLeafIndex(maxReliableLeafIndex); - const currentBlock = await utilsWeb3.getBlockNumber(); + fromBlock = latestConsecutiveLeaf.blockNumber; + } else { + // start from scratch: + fromBlock = config.FILTER_GENESIS_BLOCK_NUMBER; + return maxReliableLeafIndex; // the maximum reliable leafIndex is -1; i.e. nothing's reliable. Let's start again. + } - const lag = currentBlock - fromBlock; + const currentBlock = await utilsWeb3.getBlockNumber(); - const lagTolerance = config.tolerances.LAG_BEHIND_CURRENT_BLOCK; + const lag = currentBlock - fromBlock; - if (lag <= lagTolerance) { - logger.info( - `Ideally, we would re-filter from block ${fromBlock}, but the filter is only ${lag} blocks behind the current block ${currentBlock}. Since the user's config specifies a 'lag' tolerance of ${lagTolerance} blocks, we will not re-filter.`, - ); + const lagTolerance = config.tolerances.LAG_BEHIND_CURRENT_BLOCK; - return maxReliableLeafIndex; // return the latest reliable leaf index up to which we can update the tree - } - logger.error( - `We need to re-filter from block ${fromBlock}, but this feature hasn't been built yet!`, + if (lag <= lagTolerance) { + logger.info( + `Ideally, we would re-filter from block ${fromBlock}, but the filter is only ${lag} blocks behind the current block ${currentBlock}. Since the user's config specifies a 'lag' tolerance of ${lagTolerance} blocks, we will not re-filter.`, ); - // TODO: re-filter the blockchain for events from this fromBlock. + + return maxReliableLeafIndex; // return the latest reliable leaf index up to which we can update the tree } + logger.error( + `We need to re-filter from block ${fromBlock}, but this feature hasn't been built yet!`, + ); + // TODO: re-filter the blockchain for events from this fromBlock. + } - maxReliableLeafIndex = maxLeafIndex; + maxReliableLeafIndex = maxLeafIndex; - return maxReliableLeafIndex; - }); + return maxReliableLeafIndex; } /** diff --git a/merkle-tree/src/routes/merkle-tree.routes.js b/merkle-tree/src/routes/merkle-tree.routes.js index 4fe1430..17db01d 100644 --- a/merkle-tree/src/routes/merkle-tree.routes.js +++ b/merkle-tree/src/routes/merkle-tree.routes.js @@ -8,6 +8,7 @@ import contractController from '../contract-controller'; import filterController from '../filter-controller'; import merkleTreeController from '../merkle-tree-controller'; import logger from '../logger'; +import { mutex } from '../mutex-controller'; const alreadyStarted = {}; // initialises as false const alreadyStarting = {}; // initialises as false @@ -90,11 +91,14 @@ async function getSiblingPathByLeafIndex(req, res, next) { leafIndex = Number(leafIndex); // force to number try { - // first update all nodes in the DB to be in line with the latest-known leaf: - await merkleTreeController.update(db); - - // get the sibling path: - const siblingPath = await merkleTreeController.getSiblingPathByLeafIndex(db, leafIndex); + let siblingPath = null; + await mutex.runExclusive(async () => { + // first update all nodes in the DB to be in line with the latest-known leaf: + await merkleTreeController.update(db); + + // get the sibling path: + siblingPath = await merkleTreeController.getSiblingPathByLeafIndex(db, leafIndex); + }); res.data = siblingPath; next();