Skip to content

Commit

Permalink
feat: change mutex scope
Browse files Browse the repository at this point in the history
  • Loading branch information
andresobandoalfaro committed May 13, 2024
1 parent ed2c517 commit 68e4771
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 48 deletions.
83 changes: 40 additions & 43 deletions merkle-tree/src/merkle-tree-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down
14 changes: 9 additions & 5 deletions merkle-tree/src/routes/merkle-tree.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 68e4771

Please sign in to comment.