Skip to content

Commit

Permalink
Refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyudmil Danailov authored and Lyudmil Danailov committed Feb 8, 2024
1 parent 461818f commit 258470d
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 176 deletions.
12 changes: 6 additions & 6 deletions blockchains/erc20/erc20_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { extendEventsWithPrimaryKey } = require('./lib/extend_events_key');
const { ContractOverwrite, changeContractAddresses, extractChangedContractAddresses } = require('./lib/contract_overwrite');
const { stableSort, readJsonFile } = require('./lib/util');
const BaseWorker = require('../../lib/worker_base');
const { nextIntervalCalculator } = require('../eth/lib/next_interval_calculator');
const { nextIntervalCalculator, setWorkerSleepTime, analyzeWorkerContext, NO_WORK_SLEEP } = require('../eth/lib/next_interval_calculator');
const Web3Wrapper = require('../eth/lib/web3_wrapper');
const { TimestampsCache } = require('./lib/timestamps_cache');
const { getPastEvents } = require('./lib/fetch_events');
Expand Down Expand Up @@ -98,13 +98,13 @@ class ERC20Worker extends BaseWorker {
}

async work() {
const workerContext = await analyzeWorkerContext(this);
setWorkerSleepTime(this, workerContext);
if (workerContext === NO_WORK_SLEEP) return [];

const interval = this.settings.EXPORT_BLOCKS_LIST ?
this.getBlocksListInterval() :
await nextIntervalCalculator(this);

if (!interval.success) {
return [];
}
nextIntervalCalculator(this);

logger.info(`Fetching transfer events for interval ${interval.fromBlock}:${interval.toBlock}`);

Expand Down
8 changes: 4 additions & 4 deletions blockchains/eth/eth_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BaseWorker = require('../../lib/worker_base');
const Web3Wrapper = require('./lib/web3_wrapper');
const { decodeTransferTrace } = require('./lib/decode_transfers');
const { FeesDecoder } = require('./lib/fees_decoder');
const { nextIntervalCalculator, analyzeWorkerProgress, setWorkerSleepTime } = require('./lib/next_interval_calculator');
const { nextIntervalCalculator, analyzeWorkerContext, setWorkerSleepTime, NO_WORK_SLEEP } = require('./lib/next_interval_calculator');
const { WithdrawalsDecoder } = require('./lib/withdrawals_decoder');

class ETHWorker extends BaseWorker {
Expand Down Expand Up @@ -146,9 +146,9 @@ class ETHWorker extends BaseWorker {
}

async work() {
const workerContext = await analyzeWorkerProgress(this);
setWorkerSleepTime(this);
if (workerContext === 2) return [];
const workerContext = await analyzeWorkerContext(this);
setWorkerSleepTime(this, workerContext);
if (workerContext === NO_WORK_SLEEP) return [];

const { fromBlock, toBlock } = nextIntervalCalculator(this);
logger.info(`Fetching transfer events for interval ${fromBlock}:${toBlock}`);
Expand Down
37 changes: 22 additions & 15 deletions blockchains/eth/lib/next_interval_calculator.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
const WORK_NO_SLEEP = 0;
const WORK_SLEEP = 1;
const NO_WORK_SLEEP = 2;

/**
* Returns the context in which the worker finds itself at a given moment:
*
* 0 : Exporting blocks that are behind the last confirmed block
*
* 1 : We've caught up to the last confirmed block. After a query to the node, we find out that there's a higher goal
*
* 2 : We've caught up to the last confirmed block. After a query to the node, we find out that we've caught up
*
*
* WORK_NO_SLEEP : Exporting blocks that are behind the last confirmed block
*
* WORK_SLEEP : We've caught up to the last confirmed block. After a query to the node,
* we find out that there's a higher goal
*
* NO_WORK_SLEEP : We've caught up to the last confirmed block. After a query to the node,
* we find out that we've caught up
*
* @param {BaseWorker} worker A worker instance, inherriting the BaseWorker class.
* @returns {number} A number, which points to one of the above-given scenarios
*/
async function analyzeWorkerProgress(worker) {
if (worker.lastExportedBlock < worker.lastConfirmedBlock) return 0;
async function analyzeWorkerContext(worker) {
if (worker.lastExportedBlock < worker.lastConfirmedBlock) return WORK_NO_SLEEP;

const newConfirmedBlock = await worker.web3Wrapper.getBlockNumber() - worker.settings.CONFIRMATIONS;
if (newConfirmedBlock > worker.lastConfirmedBlock) {
worker.lastConfirmedBlock = newConfirmedBlock;
return 1;
return WORK_SLEEP;
}

return 2;
return NO_WORK_SLEEP;
}

/**
* Function for setting the work loop's sleep time, after the end of the worker's work method.
* For the above given 0 and 1 scenarios, we'd want no sleep, because we have to work.
* For 2 we'd want to sleep, because we'd have caught up completely and should back off for some time.
* @param {BaseWorker} worker A worker instance, inherriting the BaseWorker class.
* @param {number} context The scenario used for setting the sleep time
*/
function setWorkerSleepTime(worker, context) {
worker.sleepTimeMsec = (context === 2) ? worker.settings.LOOP_INTERVAL_CURRENT_MODE_SEC : 0;
worker.sleepTimeMsec = (context !== WORK_NO_SLEEP) ? worker.settings.LOOP_INTERVAL_CURRENT_MODE_SEC * 1000 : 0;
}

/**
Expand All @@ -46,7 +50,10 @@ function nextIntervalCalculator(worker) {
}

module.exports = {
WORK_SLEEP,
NO_WORK_SLEEP,
WORK_NO_SLEEP,
setWorkerSleepTime,
analyzeWorkerProgress,
analyzeWorkerContext,
nextIntervalCalculator
};
Loading

0 comments on commit 258470d

Please sign in to comment.