-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmock.worker.ts
394 lines (316 loc) · 13.6 KB
/
mock.worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { JsonRpcProvider, Log, LogDescription, SigningKey, Wallet, keccak256, zeroPadValue } from 'ethers6';
import pino from 'pino';
import { convertHexToDecimal, tryErrorToString, wait } from 'src/common/utils';
import { IncentivizedMockEscrow__factory } from 'src/contracts';
import { Store } from 'src/store/store.lib';
import { AmbMessage, AmbPayload } from 'src/store/types/store.types';
import { workerData, MessagePort } from 'worker_threads';
import {
decodeMockMessage,
encodeMessage,
encodeSignature,
} from './mock.utils';
import { MockWorkerData } from './mock';
import { IncentivizedMockEscrowInterface, MessageEvent } from 'src/contracts/IncentivizedMockEscrow';
import { MonitorInterface, MonitorStatus } from 'src/monitor/monitor.interface';
import { Resolver, loadResolver } from 'src/resolvers/resolver';
import { STATUS_LOG_INTERVAL } from 'src/logger/logger.service';
/**
* Example AMB implementation which uses a simple signed message to validate transactions.
* This example worker service is provided with the following parameters:
* @param workerData.chainId The id of the chain the worker runs for.
* @param workerData.rpc The RPC to use for the chain.
* @param workerData.startingBlock The block from which to start processing events (optional).
* @param workerData.stoppingBlock The block at which to stop processing events (optional).
* @param workerData.retryInterval Time to wait before retrying failed logic.
* @param processingInterval Throttle of the main 'run' loop
* @param workerData.maxBlocks Max number of blocks to scan at a time.
* @param workerData.incentivesAddress The address of the Generalised Incentive implementation for the AMB.
* @param workerData.privateKey The key used to sign the relayed messages.
* @param workerData.monitorPort Port for communication with the 'monitor' service.
* @param workerData.loggerOptions Logger related config to spawn a pino logger with.
* @dev Custom additional configuration parameters should be set on config.example.yaml for future reference.
*/
class MockCollectorWorker {
private readonly config: MockWorkerData;
private readonly chainId: string;
private readonly signingKey: SigningKey;
private readonly incentivesAddress: string;
private readonly incentivesAddressBytes32: string;
private readonly incentivesEscrowInterface: IncentivizedMockEscrowInterface;
private readonly filterTopics: string[][];
private readonly resolver: Resolver;
private readonly store: Store;
private readonly provider: JsonRpcProvider;
private readonly logger: pino.Logger;
private currentStatus: MonitorStatus | null = null;
private monitor: MonitorInterface;
private fromBlock: number = 0;
constructor() {
this.config = workerData as MockWorkerData;
this.chainId = this.config.chainId;
// Get a connection to the redis store.
// The redis store has been wrapped into a lib to make it easier to standardise
// communication between the various components.
this.store = new Store(this.chainId);
// Get an Ethers provider with which to collect the bounties information.
this.provider = this.initializeProvider(this.config.rpc);
// Create the key that will sign the cross chain messages
this.signingKey = this.initializeSigningKey(this.config.privateKey);
this.logger = this.initializeLogger(this.chainId);
// Load a helper to perform chain-type specific operations (e.g. query the l2 -> l1BlockNumber for arbitrum)
this.resolver = this.loadResolver(
this.config.resolver,
this.provider,
this.logger
);
// Define the parameters for the rpc logs queries and message signing.
this.incentivesAddress = this.config.incentivesAddress;
this.incentivesAddressBytes32 = zeroPadValue(this.incentivesAddress, 32);
this.incentivesEscrowInterface = IncentivizedMockEscrow__factory.createInterface();
this.filterTopics = [[this.incentivesEscrowInterface.getEvent('Message').topicHash]];
// Start listening to the monitor service (get the latest block data).
this.monitor = this.startListeningToMonitor(this.config.monitorPort);
this.initiateIntervalStatusLog();
}
// Initialization helpers
// ********************************************************************************************
private initializeLogger(chainId: string): pino.Logger {
return pino(this.config.loggerOptions).child({
worker: 'collector-mock',
chain: chainId,
});
}
private initializeProvider(rpc: string): JsonRpcProvider {
return new JsonRpcProvider(
rpc,
undefined,
{ staticNetwork: true }
)
}
private loadResolver(
resolver: string | null,
provider: JsonRpcProvider,
logger: pino.Logger
): Resolver {
return loadResolver(resolver, provider, logger);
}
private initializeSigningKey(privateKey: string): SigningKey {
return new Wallet(privateKey).signingKey;
}
private startListeningToMonitor(port: MessagePort): MonitorInterface {
const monitor = new MonitorInterface(port);
monitor.addListener((status: MonitorStatus) => {
this.currentStatus = status;
});
return monitor;
}
// Main handler
// ********************************************************************************************
async run(): Promise<void> {
this.logger.info(
{ incentiveAddresses: this.incentivesAddress },
`Mock collector worker started.`,
);
// Get the effective starting and stopping blocks.
this.fromBlock = await this.getStartingBlock();
const stopBlock = this.config.stoppingBlock ?? Infinity;
while (true) {
try {
let toBlock = this.currentStatus?.blockNumber;
if (!toBlock || this.fromBlock > toBlock) {
await wait(this.config.processingInterval);
continue;
}
// Stop the relayer after a certain block.
if (toBlock > stopBlock) {
toBlock = stopBlock;
}
// Do not process more than 'maxBlocks' within a single rpc call.
const blocksToProcess = toBlock - this.fromBlock;
if (this.config.maxBlocks != null && blocksToProcess > this.config.maxBlocks) {
toBlock = this.fromBlock + this.config.maxBlocks;
}
this.logger.debug(
{
fromBlock: this.fromBlock,
toBlock,
},
`Scanning mock messages.`,
);
await this.queryAndProcessEvents(this.fromBlock, toBlock);
if (toBlock >= stopBlock) {
this.logger.info(
{ stopBlock: toBlock },
`Finished processing blocks. Exiting worker.`,
);
break;
}
this.fromBlock = toBlock + 1;
}
catch (error) {
this.logger.error(error, `Error on mock.worker`);
await wait(this.config.retryInterval)
}
await wait(this.config.processingInterval);
}
// Cleanup worker
this.monitor.close();
await this.store.quit();
}
private async getStartingBlock(): Promise<number> {
let fromBlock: number | null = null;
while (fromBlock == null) {
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
// 'startingBlock' is specified.
if (this.currentStatus == null) {
await wait(this.config.processingInterval);
continue;
}
if (this.config.startingBlock == null) {
fromBlock = this.currentStatus.blockNumber;
break;
}
if (this.config.startingBlock < 0) {
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
if (fromBlock < 0) {
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
}
} else {
fromBlock = this.config.startingBlock;
}
}
return fromBlock;
}
private async queryAndProcessEvents(
fromBlock: number,
toBlock: number
): Promise<void> {
const logs = await this.queryLogs(fromBlock, toBlock);
for (const log of logs) {
try {
await this.handleEvent(log);
} catch (error) {
this.logger.error(
{ log, error },
`Failed to process event on mock collector worker.`
);
}
}
}
private async queryLogs(
fromBlock: number,
toBlock: number
): Promise<Log[]> {
const filter = {
address: this.incentivesAddress,
topics: this.filterTopics,
fromBlock,
toBlock
};
let logs: Log[] | undefined;
let i = 0;
while (logs == undefined) {
try {
logs = await this.provider.getLogs(filter);
} catch (error) {
i++;
this.logger.warn(
{ ...filter, error: tryErrorToString(error), try: i },
`Failed to 'getLogs' on mock collector. Worker blocked until successful query.`
);
await wait(this.config.retryInterval);
}
}
return logs;
}
// Event handlers
// ********************************************************************************************
private async handleEvent(log: Log): Promise<void> {
const parsedLog = this.incentivesEscrowInterface.parseLog(log);
if (parsedLog == null) {
this.logger.error(
{ topics: log.topics, data: log.data },
`Failed to parse a mock escrow contract event.`,
);
return;
}
if (parsedLog.name != 'Message') {
this.logger.warn(
{ name: parsedLog.name, topic: parsedLog.topic },
`Event with unknown name/topic received.`,
);
return;
}
await this.handleMockMessage(log, parsedLog);
}
private async handleMockMessage(
log: Log,
parsedLog: LogDescription
): Promise<void> {
const messageEvent = parsedLog.args as unknown as MessageEvent.OutputObject;
const message = messageEvent.message;
// Derive the message identifier
const decodedMessage = decodeMockMessage(message);
const transactionBlockNumber = await this.resolver.getTransactionBlockNumber(
log.blockNumber
);
const amb: AmbMessage = {
...decodedMessage,
amb: 'mock',
sourceEscrow: this.config.incentivesAddress,
blockNumber: log.blockNumber,
transactionBlockNumber,
blockHash: log.blockHash,
transactionHash: log.transactionHash
}
// Set the collect message on-chain. This is not the proof but the raw message.
// It can be used by plugins to facilitate other jobs.
await this.store.setAmb(amb, log.transactionHash);
// Set destination address for the bounty.
await this.store.registerDestinationAddress({
messageIdentifier: amb.messageIdentifier,
destinationAddress: messageEvent.recipient,
});
// Encode and sign the message for delivery.
// This is the proof which enables us to submit the transaciton later.
// For Mock, this is essentially PoA with a single key. The deployment needs to match the private key available
// to the relayer.
const encodedMessage = encodeMessage(this.incentivesAddressBytes32, message);
const signature = this.signingKey.sign(keccak256(encodedMessage));
const executionContext = encodeSignature(signature);
const destinationChainId = convertHexToDecimal(amb.destinationChain);
// Construct the payload.
const ambPayload: AmbPayload = {
messageIdentifier: amb.messageIdentifier,
amb: 'mock',
destinationChainId,
message: encodedMessage,
messageCtx: executionContext, // If the generalised incentives implementation does not use the context set it to "0x".
};
this.logger.info(
{
messageIdentifier: amb.messageIdentifier,
destinationChainId: destinationChainId,
},
`Mock message found.`,
);
// Submit the proofs to any listeners. If there is a submitter, it will process the proof and submit it.
await this.store.submitProof(destinationChainId, ambPayload);
}
// Misc Helpers
// ********************************************************************************************
private initiateIntervalStatusLog(): void {
const logStatus = () => {
this.logger.info(
{
latestBlock: this.currentStatus?.blockNumber,
currentBlock: this.fromBlock,
},
'Mock collector status.'
);
};
setInterval(logStatus, STATUS_LOG_INTERVAL);
}
}
void new MockCollectorWorker().run();