diff --git a/app/Cli.ts b/app/Cli.ts index 151dffc..14b9ef3 100644 --- a/app/Cli.ts +++ b/app/Cli.ts @@ -17,11 +17,10 @@ let app: App; const version = await getVersion(__dirname); console.log(`PowerPool Agent Node version: ${version}`); - let config: Config; - let configPath; + let config: Config, configPath: string; try { - const configPath = process.env.CONFIG_PATH || path.resolve(__dirname, '../config/main.yaml'); + configPath = process.env.CONFIG_PATH || path.resolve(__dirname, '../config/main.yaml'); logger.info(`CLI: Reading configuration from ${configPath} ...`); config = YAML.parse(fs.readFileSync(configPath).toString()) as Config; } catch (error) { diff --git a/app/ConfigGetters.ts b/app/ConfigGetters.ts index f5062f9..60cb333 100644 --- a/app/ConfigGetters.ts +++ b/app/ConfigGetters.ts @@ -98,6 +98,10 @@ export function getMaxBlocksSubgraphDelay(_ /*networkName*/) { return 10000; } +export function getMaxBlockEventsQuery(_ /*networkName*/) { + return 4900; +} + export function setConfigDefaultValues(config, defaultValues) { Object.keys(defaultValues).forEach(name => { if (typeof config[name] === 'undefined') { diff --git a/app/Network.ts b/app/Network.ts index 77dfe8f..4597da7 100644 --- a/app/Network.ts +++ b/app/Network.ts @@ -15,6 +15,7 @@ import { getAverageBlockTime, getDefaultNetworkConfig, getExternalLensAddress, + getMaxBlockEventsQuery, getMulticall2Address, getResolverCallSkipBlocksNumber, setConfigDefaultValues, @@ -402,8 +403,8 @@ export class Network { if (this.contractEventsEmitter.blockLogsMode) { this.contractEventsEmitter.emitByBlockQuery({ - fromBlock: Number(this.agentsStartBlockNumber) + 1, - toBlock: Number(this.agentsStartBlockNumber) + count, + fromBlock: startBlockNumber + 1, + toBlock: startBlockNumber + count, }); } @@ -484,7 +485,7 @@ export class Network { blockNumber = BigInt(blockNumber.toString()); const before = this.nowMs(); - const oldLatestBlockNumber = this.latestBlockNumber ? BigInt(this.latestBlockNumber) : null; + let oldLatestBlockNumber = this.latestBlockNumber ? BigInt(this.latestBlockNumber) : null; if (this.latestBlockNumber && blockNumber <= this.latestBlockNumber) { return null; } @@ -505,6 +506,9 @@ export class Network { if (this.contractEventsEmitter.blockLogsMode) { const blocksDiff = oldLatestBlockNumber ? blockNumber - oldLatestBlockNumber : 0n; + if (blocksDiff > getMaxBlockEventsQuery(this.name)) { + oldLatestBlockNumber = blockNumber - BigInt(getMaxBlockEventsQuery(this.name)); + } const fromBlock = bigintToHex(blocksDiff > 1n ? oldLatestBlockNumber + 1n : blockNumber); const toBlock = bigintToHex(blockNumber); this.contractEventsEmitter.emitByBlockQuery({ fromBlock, toBlock }); @@ -594,6 +598,7 @@ export class Network { // TODO: protect from handlers queueing on the networks with < 3s block time const resolversToCall = []; const callbacks = []; + for (const [jobKey, jobData] of Object.entries(this.resolverJobData)) { callbacks.push(jobData.callback); resolversToCall.push({ @@ -622,6 +627,13 @@ export class Network { for (let i = 0; i < results.length; i++) { const { jobKey } = resolversToCall[i]; const agent = this.getAgent(jobKey.split('/')[0]); + + // Watch job in case of blockchain node lag + if (!this.resolverJobData[jobKey]) { + const jobEntity = await agent.getJob(jobKey.split('/')[1]); + jobEntity.watch(); + } + let decoded; try { decoded = results[i].success diff --git a/app/jobs/AbstractJob.ts b/app/jobs/AbstractJob.ts index 6731503..9f43e2b 100644 --- a/app/jobs/AbstractJob.ts +++ b/app/jobs/AbstractJob.ts @@ -319,6 +319,7 @@ export abstract class AbstractJob { e.message.includes('Tx not mined, max attempts') || e.message.includes('Too many requests') || e.message.includes('InsufficientFunds') || + e.message.includes('exceeds the configured cap') || e.message.toLowerCase().includes('insufficient funds') || e.message.includes('could not replace existing tx') || e.message.includes('replacement fee too low') ||