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 5, 2024
1 parent d4cebd9 commit e07778a
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 162 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 } = 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 === 2) 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
6 changes: 3 additions & 3 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 } = require('./lib/next_interval_calculator');
const { WithdrawalsDecoder } = require('./lib/withdrawals_decoder');

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

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

const { fromBlock, toBlock } = nextIntervalCalculator(this);
Expand Down
14 changes: 7 additions & 7 deletions blockchains/eth/lib/next_interval_calculator.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* 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
*
*
* @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) {
async function analyzeWorkerContext(worker) {
if (worker.lastExportedBlock < worker.lastConfirmedBlock) return 0;

const newConfirmedBlock = await worker.web3Wrapper.getBlockNumber() - worker.settings.CONFIRMATIONS;
Expand All @@ -30,7 +30,7 @@ async function analyzeWorkerProgress(worker) {
* @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 === 2) ? worker.settings.LOOP_INTERVAL_CURRENT_MODE_SEC * 1000 : 0;
}

/**
Expand All @@ -47,6 +47,6 @@ function nextIntervalCalculator(worker) {

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

0 comments on commit e07778a

Please sign in to comment.