Skip to content

Commit 905378e

Browse files
authored
Merge pull request #40 from catalystdao/jsanmi/overhaul-logs
[chore]: Overhaul logs
2 parents bb7ba67 + 726c15a commit 905378e

17 files changed

+399
-157
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"pino": "^8.15.1",
4444
"reflect-metadata": "^0.1.13",
4545
"rxjs": "^7.8.1",
46-
"viem": "^2.15.1"
46+
"viem": "^2.15.1",
47+
"winston": "^3.13.0",
48+
"winston-transport": "^4.7.0"
4749
},
4850
"devDependencies": {
4951
"@nestjs/cli": "^10.0.0",

pnpm-lock.yaml

+10-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/collector/layer-zero/layer-zero.worker.ts

+62-25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { LayerZeroEnpointV2__factory } from 'src/contracts';
4141
import { Resolver, loadResolver } from 'src/resolvers/resolver';
4242
import { ParsePayload } from 'src/payload/decode.payload';
4343
import { LayerZeroEnpointV2Interface, PacketSentEvent } from 'src/contracts/LayerZeroEnpointV2';
44+
import { STATUS_LOG_INTERVAL } from 'src/logger/logger.service';
4445

4546
interface LayerZeroWorkerDataWithMapping extends LayerZeroWorkerData {
4647
layerZeroChainIdMap: Record<string, string>;
@@ -64,6 +65,8 @@ class LayerZeroWorker {
6465
private currentStatus: MonitorStatus | null = null;
6566
private monitor: MonitorInterface;
6667

68+
private fromBlock: number = 0;
69+
6770
constructor() {
6871
this.config = workerData as LayerZeroWorkerDataWithMapping;
6972
this.chainId = this.config.chainId;
@@ -91,6 +94,8 @@ class LayerZeroWorker {
9194
this.receiveULN302Interface.getEvent('PayloadVerified').topicHash,]
9295
];
9396
this.monitor = this.startListeningToMonitor(this.config.monitorPort);
97+
98+
this.initiateIntervalStatusLog();
9499
}
95100

96101
// Initialization helpers
@@ -163,45 +168,30 @@ class LayerZeroWorker {
163168
},
164169
`LayerZero collector worker started.`,
165170
);
166-
let fromBlock = null;
167-
while (fromBlock == null) {
168-
if (this.currentStatus != null) {
169-
if (this.config.startingBlock != null) {
170-
if (this.config.startingBlock < 0) {
171-
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
172-
if (fromBlock < 0) {
173-
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
174-
}
175-
} else {
176-
fromBlock = this.config.startingBlock;
177-
}
178-
} else {
179-
fromBlock = this.currentStatus.blockNumber;
180-
}
181-
}
182-
await wait(this.config.processingInterval);
183-
}
171+
172+
this.fromBlock = await this.getStartingBlock();
184173
const stopBlock = this.config.stoppingBlock ?? Infinity;
174+
185175
while (true) {
186176
try {
187177
let toBlock = this.currentStatus?.blockNumber;
188-
if (!toBlock || fromBlock > toBlock) {
178+
if (!toBlock || this.fromBlock > toBlock) {
189179
await wait(this.config.processingInterval);
190180
continue;
191181
}
192182
if (toBlock > stopBlock) {
193183
toBlock = stopBlock;
194184
}
195-
const blocksToProcess = toBlock - fromBlock;
185+
const blocksToProcess = toBlock - this.fromBlock;
196186
if (
197187
this.config.maxBlocks != null &&
198188
blocksToProcess > this.config.maxBlocks
199189
) {
200-
toBlock = fromBlock + this.config.maxBlocks;
190+
toBlock = this.fromBlock + this.config.maxBlocks;
201191
}
202-
await this.queryAndProcessEvents(fromBlock, toBlock);
203-
this.logger.info(
204-
{ fromBlock, toBlock },
192+
await this.queryAndProcessEvents(this.fromBlock, toBlock);
193+
this.logger.debug(
194+
{ fromBlock: this.fromBlock, toBlock },
205195
`Scanning LayerZero events.`,
206196
);
207197
if (toBlock >= stopBlock) {
@@ -211,7 +201,7 @@ class LayerZeroWorker {
211201
);
212202
break;
213203
}
214-
fromBlock = toBlock + 1;
204+
this.fromBlock = toBlock + 1;
215205
} catch (error) {
216206
this.logger.error(error, `Error on Layer Zero worker: processing blocks.`);
217207
await wait(this.config.retryInterval);
@@ -222,6 +212,35 @@ class LayerZeroWorker {
222212
await this.store.quit();
223213
}
224214

215+
private async getStartingBlock(): Promise<number> {
216+
let fromBlock: number | null = null;
217+
while (fromBlock == null) {
218+
219+
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
220+
// 'startingBlock' is specified.
221+
if (this.currentStatus == null) {
222+
await wait(this.config.processingInterval);
223+
continue;
224+
}
225+
226+
if (this.config.startingBlock == null) {
227+
fromBlock = this.currentStatus.blockNumber;
228+
break;
229+
}
230+
231+
if (this.config.startingBlock < 0) {
232+
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
233+
if (fromBlock < 0) {
234+
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
235+
}
236+
} else {
237+
fromBlock = this.config.startingBlock;
238+
}
239+
}
240+
241+
return fromBlock;
242+
}
243+
225244
/**
226245
* Queries and processes events between two blocks.
227246
*
@@ -542,6 +561,24 @@ class LayerZeroWorker {
542561
const payload = `0x${guid}${message}`;
543562
return ethers.keccak256(payload);
544563
}
564+
565+
566+
567+
// Misc Helpers
568+
// ********************************************************************************************
569+
570+
private initiateIntervalStatusLog(): void {
571+
const logStatus = () => {
572+
this.logger.info(
573+
{
574+
latestBlock: this.currentStatus?.blockNumber,
575+
currentBlock: this.fromBlock,
576+
},
577+
'LayerZero collector status.'
578+
);
579+
};
580+
setInterval(logStatus, STATUS_LOG_INTERVAL);
581+
}
545582
}
546583

547584
/**

src/collector/mock/mock.worker.ts

+60-28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MockWorkerData } from './mock';
1414
import { IncentivizedMockEscrowInterface, MessageEvent } from 'src/contracts/IncentivizedMockEscrow';
1515
import { MonitorInterface, MonitorStatus } from 'src/monitor/monitor.interface';
1616
import { Resolver, loadResolver } from 'src/resolvers/resolver';
17+
import { STATUS_LOG_INTERVAL } from 'src/logger/logger.service';
1718

1819

1920
/**
@@ -53,6 +54,8 @@ class MockCollectorWorker {
5354
private currentStatus: MonitorStatus | null = null;
5455
private monitor: MonitorInterface;
5556

57+
private fromBlock: number = 0;
58+
5659

5760
constructor() {
5861
this.config = workerData as MockWorkerData;
@@ -87,6 +90,8 @@ class MockCollectorWorker {
8790

8891
// Start listening to the monitor service (get the latest block data).
8992
this.monitor = this.startListeningToMonitor(this.config.monitorPort);
93+
94+
this.initiateIntervalStatusLog();
9095
}
9196

9297

@@ -142,34 +147,14 @@ class MockCollectorWorker {
142147
);
143148

144149
// Get the effective starting and stopping blocks.
145-
let fromBlock = null;
146-
while (fromBlock == null) {
147-
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
148-
// 'startingBlock' is specified.
149-
if (this.currentStatus != null) {
150-
if (this.config.startingBlock != null) {
151-
if (this.config.startingBlock < 0) {
152-
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
153-
if (fromBlock < 0) {
154-
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
155-
}
156-
} else {
157-
fromBlock = this.config.startingBlock;
158-
}
159-
} else {
160-
fromBlock = this.currentStatus.blockNumber;
161-
}
162-
}
163-
164-
await wait(this.config.processingInterval);
165-
}
150+
this.fromBlock = await this.getStartingBlock();
166151

167152
const stopBlock = this.config.stoppingBlock ?? Infinity;
168153

169154
while (true) {
170155
try {
171156
let toBlock = this.currentStatus?.blockNumber;
172-
if (!toBlock || fromBlock > toBlock) {
157+
if (!toBlock || this.fromBlock > toBlock) {
173158
await wait(this.config.processingInterval);
174159
continue;
175160
}
@@ -180,20 +165,20 @@ class MockCollectorWorker {
180165
}
181166

182167
// Do not process more than 'maxBlocks' within a single rpc call.
183-
const blocksToProcess = toBlock - fromBlock;
168+
const blocksToProcess = toBlock - this.fromBlock;
184169
if (this.config.maxBlocks != null && blocksToProcess > this.config.maxBlocks) {
185-
toBlock = fromBlock + this.config.maxBlocks;
170+
toBlock = this.fromBlock + this.config.maxBlocks;
186171
}
187172

188-
this.logger.info(
173+
this.logger.debug(
189174
{
190-
fromBlock,
175+
fromBlock: this.fromBlock,
191176
toBlock,
192177
},
193178
`Scanning mock messages.`,
194179
);
195180

196-
await this.queryAndProcessEvents(fromBlock, toBlock);
181+
await this.queryAndProcessEvents(this.fromBlock, toBlock);
197182

198183
if (toBlock >= stopBlock) {
199184
this.logger.info(
@@ -203,7 +188,7 @@ class MockCollectorWorker {
203188
break;
204189
}
205190

206-
fromBlock = toBlock + 1;
191+
this.fromBlock = toBlock + 1;
207192
}
208193
catch (error) {
209194
this.logger.error(error, `Error on mock.worker`);
@@ -218,6 +203,35 @@ class MockCollectorWorker {
218203
await this.store.quit();
219204
}
220205

206+
private async getStartingBlock(): Promise<number> {
207+
let fromBlock: number | null = null;
208+
while (fromBlock == null) {
209+
210+
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
211+
// 'startingBlock' is specified.
212+
if (this.currentStatus == null) {
213+
await wait(this.config.processingInterval);
214+
continue;
215+
}
216+
217+
if (this.config.startingBlock == null) {
218+
fromBlock = this.currentStatus.blockNumber;
219+
break;
220+
}
221+
222+
if (this.config.startingBlock < 0) {
223+
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
224+
if (fromBlock < 0) {
225+
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
226+
}
227+
} else {
228+
fromBlock = this.config.startingBlock;
229+
}
230+
}
231+
232+
return fromBlock;
233+
}
234+
221235
private async queryAndProcessEvents(
222236
fromBlock: number,
223237
toBlock: number
@@ -357,6 +371,24 @@ class MockCollectorWorker {
357371
await this.store.submitProof(destinationChainId, ambPayload);
358372
}
359373

374+
375+
376+
// Misc Helpers
377+
// ********************************************************************************************
378+
379+
private initiateIntervalStatusLog(): void {
380+
const logStatus = () => {
381+
this.logger.info(
382+
{
383+
latestBlock: this.currentStatus?.blockNumber,
384+
currentBlock: this.fromBlock,
385+
},
386+
'Mock collector status.'
387+
);
388+
};
389+
setInterval(logStatus, STATUS_LOG_INTERVAL);
390+
}
391+
360392
}
361393

362394
void new MockCollectorWorker().run();

0 commit comments

Comments
 (0)