@subql/node-ethereum 1.10.1-3 → 2.0.1-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +3 -2
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/configure/SubqueryProject.d.ts +9 -11
  5. package/dist/configure/SubqueryProject.js +19 -9
  6. package/dist/configure/SubqueryProject.js.map +1 -1
  7. package/dist/configure/configure.module.js +5 -7
  8. package/dist/configure/configure.module.js.map +1 -1
  9. package/dist/ethereum/api.connection.d.ts +11 -0
  10. package/dist/ethereum/api.connection.js +27 -0
  11. package/dist/ethereum/api.connection.js.map +1 -0
  12. package/dist/ethereum/api.ethereum.d.ts +18 -5
  13. package/dist/ethereum/api.ethereum.js +138 -43
  14. package/dist/ethereum/api.ethereum.js.map +1 -1
  15. package/dist/ethereum/api.service.ethereum.d.ts +11 -3
  16. package/dist/ethereum/api.service.ethereum.js +68 -18
  17. package/dist/ethereum/api.service.ethereum.js.map +1 -1
  18. package/dist/ethereum/api.service.ethereum.test.d.ts +1 -0
  19. package/dist/ethereum/api.service.ethereum.test.js +73 -0
  20. package/dist/ethereum/api.service.ethereum.test.js.map +1 -0
  21. package/dist/ethereum/block.ethereum.d.ts +1 -1
  22. package/dist/ethereum/block.ethereum.js +10 -12
  23. package/dist/ethereum/block.ethereum.js.map +1 -1
  24. package/dist/ethereum/ethers/json-rpc-batch-provider.d.ts +21 -0
  25. package/dist/ethereum/ethers/json-rpc-batch-provider.js +114 -0
  26. package/dist/ethereum/ethers/json-rpc-batch-provider.js.map +1 -0
  27. package/dist/ethereum/ethers/json-rpc-provider.d.ts +7 -0
  28. package/dist/ethereum/ethers/json-rpc-provider.js +68 -0
  29. package/dist/ethereum/ethers/json-rpc-provider.js.map +1 -0
  30. package/dist/ethereum/ethers/web/_version.d.ts +1 -0
  31. package/dist/ethereum/ethers/web/_version.js +6 -0
  32. package/dist/ethereum/ethers/web/_version.js.map +1 -0
  33. package/dist/ethereum/ethers/web/geturl.d.ts +3 -0
  34. package/dist/ethereum/ethers/web/geturl.js +116 -0
  35. package/dist/ethereum/ethers/web/geturl.js.map +1 -0
  36. package/dist/ethereum/ethers/web/index.d.ts +49 -0
  37. package/dist/ethereum/ethers/web/index.js +437 -0
  38. package/dist/ethereum/ethers/web/index.js.map +1 -0
  39. package/dist/ethereum/ethers/web/types.d.ts +26 -0
  40. package/dist/ethereum/ethers/web/types.js +4 -0
  41. package/dist/ethereum/ethers/web/types.js.map +1 -0
  42. package/dist/ethereum/safe-api.d.ts +34 -0
  43. package/dist/ethereum/safe-api.js +114 -0
  44. package/dist/ethereum/safe-api.js.map +1 -0
  45. package/dist/ethereum/utils.ethereum.d.ts +1 -1
  46. package/dist/ethereum/utils.ethereum.js +22 -87
  47. package/dist/ethereum/utils.ethereum.js.map +1 -1
  48. package/dist/indexer/blockDispatcher/block-dispatcher.service.d.ts +17 -0
  49. package/dist/indexer/blockDispatcher/block-dispatcher.service.js +55 -0
  50. package/dist/indexer/blockDispatcher/block-dispatcher.service.js.map +1 -0
  51. package/dist/indexer/blockDispatcher/ethereum-block-dispatcher.d.ts +4 -0
  52. package/dist/indexer/blockDispatcher/ethereum-block-dispatcher.js +5 -0
  53. package/dist/indexer/blockDispatcher/ethereum-block-dispatcher.js.map +1 -0
  54. package/dist/indexer/blockDispatcher/index.d.ts +4 -0
  55. package/dist/indexer/blockDispatcher/index.js +10 -0
  56. package/dist/indexer/blockDispatcher/index.js.map +1 -0
  57. package/dist/indexer/blockDispatcher/worker-block-dispatcher.service.d.ts +15 -0
  58. package/dist/indexer/blockDispatcher/worker-block-dispatcher.service.js +88 -0
  59. package/dist/indexer/blockDispatcher/worker-block-dispatcher.service.js.map +1 -0
  60. package/dist/indexer/dictionary.service.d.ts +1 -0
  61. package/dist/indexer/dictionary.service.js +22 -2
  62. package/dist/indexer/dictionary.service.js.map +1 -1
  63. package/dist/indexer/ds-processor.service.d.ts +1 -1
  64. package/dist/indexer/ds-processor.service.js +4 -0
  65. package/dist/indexer/ds-processor.service.js.map +1 -1
  66. package/dist/indexer/dynamic-ds.service.d.ts +3 -19
  67. package/dist/indexer/dynamic-ds.service.js +20 -72
  68. package/dist/indexer/dynamic-ds.service.js.map +1 -1
  69. package/dist/indexer/fetch.module.js +40 -12
  70. package/dist/indexer/fetch.module.js.map +1 -1
  71. package/dist/indexer/fetch.service.d.ts +16 -7
  72. package/dist/indexer/fetch.service.js +188 -78
  73. package/dist/indexer/fetch.service.js.map +1 -1
  74. package/dist/indexer/indexer.manager.d.ts +11 -17
  75. package/dist/indexer/indexer.manager.js +68 -71
  76. package/dist/indexer/indexer.manager.js.map +1 -1
  77. package/dist/indexer/indexer.module.js +32 -8
  78. package/dist/indexer/indexer.module.js.map +1 -1
  79. package/dist/indexer/project.service.d.ts +11 -8
  80. package/dist/indexer/project.service.js +76 -82
  81. package/dist/indexer/project.service.js.map +1 -1
  82. package/dist/indexer/sandbox.service.js +11 -2
  83. package/dist/indexer/sandbox.service.js.map +1 -1
  84. package/dist/indexer/types.d.ts +2 -1
  85. package/dist/indexer/types.js.map +1 -1
  86. package/dist/indexer/unfinalizedBlocks.service.d.ts +36 -0
  87. package/dist/indexer/unfinalizedBlocks.service.js +197 -0
  88. package/dist/indexer/unfinalizedBlocks.service.js.map +1 -0
  89. package/dist/indexer/unfinalizedBlocks.spec.d.ts +1 -0
  90. package/dist/indexer/unfinalizedBlocks.spec.js +195 -0
  91. package/dist/indexer/unfinalizedBlocks.spec.js.map +1 -0
  92. package/dist/indexer/worker/worker.d.ts +22 -6
  93. package/dist/indexer/worker/worker.js +16 -3
  94. package/dist/indexer/worker/worker.js.map +1 -1
  95. package/dist/indexer/worker/worker.service.d.ts +10 -6
  96. package/dist/indexer/worker/worker.service.js +23 -10
  97. package/dist/indexer/worker/worker.service.js.map +1 -1
  98. package/dist/indexer/worker/worker.unfinalizedBlocks.service.d.ts +11 -0
  99. package/dist/indexer/worker/worker.unfinalizedBlocks.service.js +32 -0
  100. package/dist/indexer/worker/worker.unfinalizedBlocks.service.js.map +1 -0
  101. package/dist/init.js +2 -2
  102. package/dist/init.js.map +1 -1
  103. package/dist/main.js +6 -1
  104. package/dist/main.js.map +1 -1
  105. package/dist/meta/meta.module.js +8 -0
  106. package/dist/meta/meta.module.js.map +1 -1
  107. package/dist/meta/meta.service.d.ts +18 -3
  108. package/dist/meta/meta.service.js +89 -5
  109. package/dist/meta/meta.service.js.map +1 -1
  110. package/dist/subcommands/forceClean.service.d.ts +3 -1
  111. package/dist/subcommands/forceClean.service.js +30 -6
  112. package/dist/subcommands/forceClean.service.js.map +1 -1
  113. package/dist/subcommands/reindex.init.js +6 -1
  114. package/dist/subcommands/reindex.init.js.map +1 -1
  115. package/dist/subcommands/reindex.module.js +20 -1
  116. package/dist/subcommands/reindex.module.js.map +1 -1
  117. package/dist/subcommands/reindex.service.d.ts +7 -3
  118. package/dist/subcommands/reindex.service.js +44 -50
  119. package/dist/subcommands/reindex.service.js.map +1 -1
  120. package/dist/subcommands/testing.init.d.ts +1 -0
  121. package/dist/subcommands/testing.init.js +34 -0
  122. package/dist/subcommands/testing.init.js.map +1 -0
  123. package/dist/subcommands/testing.module.d.ts +4 -0
  124. package/dist/subcommands/testing.module.js +77 -0
  125. package/dist/subcommands/testing.module.js.map +1 -0
  126. package/dist/subcommands/testing.service.d.ts +9 -0
  127. package/dist/subcommands/testing.service.js +43 -0
  128. package/dist/subcommands/testing.service.js.map +1 -0
  129. package/dist/utils/project.d.ts +3 -0
  130. package/dist/utils/project.js +45 -1
  131. package/dist/utils/project.js.map +1 -1
  132. package/dist/utils/reindex.d.ts +6 -0
  133. package/dist/utils/reindex.js +48 -0
  134. package/dist/utils/reindex.js.map +1 -0
  135. package/dist/utils/string.js +10 -2
  136. package/dist/utils/string.js.map +1 -1
  137. package/dist/yargs.d.ts +97 -49
  138. package/dist/yargs.js +130 -69
  139. package/dist/yargs.js.map +1 -1
  140. package/package.json +13 -11
  141. package/dist/indexer/worker/block-dispatcher.service.d.ts +0 -69
  142. package/dist/indexer/worker/block-dispatcher.service.js +0 -356
  143. package/dist/indexer/worker/block-dispatcher.service.js.map +0 -1
@@ -1,356 +0,0 @@
1
- "use strict";
2
- // Copyright 2020-2021 OnFinality Limited authors & contributors
3
- // SPDX-License-Identifier: Apache-2.0
4
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
5
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
7
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
8
- return c > 3 && r && Object.defineProperty(target, key, r), r;
9
- };
10
- var __metadata = (this && this.__metadata) || function (k, v) {
11
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.WorkerBlockDispatcherService = exports.BlockDispatcherService = void 0;
18
- const assert_1 = __importDefault(require("assert"));
19
- const path_1 = __importDefault(require("path"));
20
- const common_1 = require("@nestjs/common");
21
- const event_emitter_1 = require("@nestjs/event-emitter");
22
- const schedule_1 = require("@nestjs/schedule");
23
- const util_1 = require("@polkadot/util");
24
- const node_core_1 = require("@subql/node-core");
25
- const chalk_1 = __importDefault(require("chalk"));
26
- const lodash_1 = require("lodash");
27
- const indexer_manager_1 = require("../indexer.manager");
28
- const project_service_1 = require("../project.service");
29
- const NULL_MERKEL_ROOT = (0, util_1.hexToU8a)('0x00');
30
- function isNullMerkelRoot(operationHash) {
31
- return (0, util_1.u8aEq)(operationHash, NULL_MERKEL_ROOT);
32
- }
33
- async function createIndexerWorker() {
34
- const indexerWorker = node_core_1.Worker.create(path_1.default.resolve(__dirname, '../../../dist/indexer/worker/worker.js'), [
35
- 'initWorker',
36
- 'processBlock',
37
- 'fetchBlock',
38
- 'numFetchedBlocks',
39
- 'numFetchingBlocks',
40
- 'getStatus',
41
- ]);
42
- await indexerWorker.initWorker();
43
- return indexerWorker;
44
- }
45
- const logger = (0, node_core_1.getLogger)('BlockDispatcherService');
46
- // TODO move to another file
47
- /**
48
- * @description Intended to behave the same as WorkerBlockDispatcherService but doesn't use worker threads or any parallel processing
49
- */
50
- let BlockDispatcherService = class BlockDispatcherService {
51
- constructor(apiService, nodeConfig, indexerManager, eventEmitter, projectService) {
52
- this.apiService = apiService;
53
- this.nodeConfig = nodeConfig;
54
- this.indexerManager = indexerManager;
55
- this.eventEmitter = eventEmitter;
56
- this.projectService = projectService;
57
- this.fetching = false;
58
- this.isShutdown = false;
59
- this.fetchQueue = new node_core_1.Queue(nodeConfig.batchSize * 3);
60
- this.processQueue = new node_core_1.AutoQueue(nodeConfig.batchSize * 3);
61
- const fetchBlocks = this.apiService.api.fetchBlocks.bind(this.apiService.api);
62
- if (this.nodeConfig.profiler) {
63
- this.fetchBlocksBatches = (0, node_core_1.profilerWrap)(fetchBlocks, 'EthereumUtil', 'fetchBlocksBatches');
64
- }
65
- else {
66
- this.fetchBlocksBatches = fetchBlocks;
67
- }
68
- }
69
- // eslint-disable-next-line @typescript-eslint/require-await
70
- async init(onDynamicDsCreated) {
71
- this.onDynamicDsCreated = onDynamicDsCreated;
72
- const blockAmount = await this.projectService.getProcessedBlockCount();
73
- this.setProcessedBlockCount(blockAmount !== null && blockAmount !== void 0 ? blockAmount : 0);
74
- }
75
- onApplicationShutdown() {
76
- this.isShutdown = true;
77
- this.processQueue.abort();
78
- }
79
- enqueueBlocks(heights) {
80
- if (!heights.length)
81
- return;
82
- logger.info(`Enqueing blocks ${heights[0]}...${(0, lodash_1.last)(heights)}`);
83
- this.fetchQueue.putMany(heights);
84
- this.latestBufferedHeight = (0, lodash_1.last)(heights);
85
- void this.fetchBlocksFromQueue().catch((e) => {
86
- logger.error(e, 'Failed to fetch blocks from queue');
87
- if (!this.isShutdown) {
88
- throw e;
89
- }
90
- });
91
- }
92
- flushQueue(height) {
93
- this.latestBufferedHeight = height;
94
- this.fetchQueue.flush(); // Empty
95
- this.processQueue.flush();
96
- }
97
- setProcessedBlockCount(processedBlockCount) {
98
- this._processedBlockCount = processedBlockCount;
99
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlockProcessedCount, {
100
- processedBlockCount,
101
- timestamp: Date.now(),
102
- });
103
- }
104
- async fetchBlocksFromQueue() {
105
- if (this.fetching || this.isShutdown)
106
- return;
107
- // Process queue is full, no point in fetching more blocks
108
- // if (this.processQueue.freeSpace < this.nodeConfig.batchSize) return;
109
- this.fetching = true;
110
- try {
111
- while (!this.isShutdown) {
112
- const blockNums = this.fetchQueue.takeMany(Math.min(this.nodeConfig.batchSize, this.processQueue.freeSpace));
113
- // Used to compare before and after as a way to check if queue was flushed
114
- const bufferedHeight = this._latestBufferedHeight;
115
- // Queue is empty
116
- if (!blockNums.length) {
117
- // The process queue might be full so no block nums were taken, wait and try again
118
- if (this.fetchQueue.size) {
119
- await (0, node_core_1.delay)(1);
120
- continue;
121
- }
122
- break;
123
- }
124
- logger.info(`fetch block [${blockNums[0]},${blockNums[blockNums.length - 1]}], total ${blockNums.length} blocks`);
125
- const blocks = await this.fetchBlocksBatches(blockNums);
126
- if (bufferedHeight > this._latestBufferedHeight) {
127
- logger.debug(`Queue was reset for new DS, discarding fetched blocks`);
128
- continue;
129
- }
130
- const blockTasks = blocks.map((block) => async () => {
131
- var _a;
132
- const height = block.blockHeight;
133
- try {
134
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlockProcessing, {
135
- height,
136
- timestamp: Date.now(),
137
- });
138
- const { dynamicDsCreated, operationHash } = await this.indexerManager.indexBlock(block);
139
- // In memory _processedBlockCount increase, db metadata increase BlockCount in indexer.manager
140
- this.setProcessedBlockCount(this._processedBlockCount + 1);
141
- if (this.nodeConfig.proofOfIndex &&
142
- !isNullMerkelRoot(operationHash)) {
143
- if (!this.projectService.blockOffset) {
144
- // Which means during project init, it has not found offset and set value
145
- await this.projectService.upsertMetadataBlockOffset(height - 1);
146
- }
147
- void this.projectService.setBlockOffset(height - 1);
148
- }
149
- if (dynamicDsCreated) {
150
- await this.onDynamicDsCreated(height);
151
- }
152
- (0, assert_1.default)(!this.latestProcessedHeight ||
153
- height > this.latestProcessedHeight, `Block processed out of order. Height: ${height}. Latest: ${this.latestProcessedHeight}`);
154
- this.latestProcessedHeight = height;
155
- }
156
- catch (e) {
157
- if (this.isShutdown) {
158
- return;
159
- }
160
- logger.error(e, `failed to index block at height ${height} ${e.handler ? `${e.handler}(${(_a = e.stack) !== null && _a !== void 0 ? _a : ''})` : ''}`);
161
- throw e;
162
- }
163
- });
164
- // There can be enough of a delay after fetching blocks that shutdown could now be true
165
- if (this.isShutdown)
166
- break;
167
- this.processQueue.putMany(blockTasks);
168
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlockQueueSize, {
169
- value: this.processQueue.size,
170
- });
171
- }
172
- }
173
- finally {
174
- this.fetching = false;
175
- }
176
- }
177
- get queueSize() {
178
- return this.fetchQueue.size;
179
- }
180
- get freeSize() {
181
- return this.fetchQueue.freeSpace;
182
- }
183
- get latestBufferedHeight() {
184
- return this._latestBufferedHeight;
185
- }
186
- set latestBufferedHeight(height) {
187
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlocknumberQueueSize, {
188
- value: this.queueSize,
189
- });
190
- this._latestBufferedHeight = height;
191
- }
192
- };
193
- BlockDispatcherService = __decorate([
194
- (0, common_1.Injectable)(),
195
- __metadata("design:paramtypes", [node_core_1.ApiService,
196
- node_core_1.NodeConfig,
197
- indexer_manager_1.IndexerManager,
198
- event_emitter_1.EventEmitter2,
199
- project_service_1.ProjectService])
200
- ], BlockDispatcherService);
201
- exports.BlockDispatcherService = BlockDispatcherService;
202
- let WorkerBlockDispatcherService = class WorkerBlockDispatcherService {
203
- constructor(nodeConfig, eventEmitter, projectService) {
204
- this.nodeConfig = nodeConfig;
205
- this.eventEmitter = eventEmitter;
206
- this.projectService = projectService;
207
- this.taskCounter = 0;
208
- this.isShutdown = false;
209
- this.numWorkers = nodeConfig.workers;
210
- this.queue = new node_core_1.AutoQueue(this.numWorkers * nodeConfig.batchSize * 2);
211
- }
212
- async init(onDynamicDsCreated) {
213
- this.workers = await Promise.all(new Array(this.numWorkers).fill(0).map(() => createIndexerWorker()));
214
- this.onDynamicDsCreated = onDynamicDsCreated;
215
- const blockAmount = await this.projectService.getProcessedBlockCount();
216
- this.setProcessedBlockCount(blockAmount !== null && blockAmount !== void 0 ? blockAmount : 0);
217
- }
218
- setProcessedBlockCount(processedBlockCount) {
219
- this._processedBlockCount = processedBlockCount;
220
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlockProcessedCount, {
221
- processedBlockCount,
222
- timestamp: Date.now(),
223
- });
224
- }
225
- async onApplicationShutdown() {
226
- this.isShutdown = true;
227
- // Stop processing blocks
228
- this.queue.abort();
229
- // Stop all workers
230
- if (this.workers) {
231
- await Promise.all(this.workers.map((w) => w.terminate()));
232
- }
233
- }
234
- enqueueBlocks(heights) {
235
- if (!heights.length)
236
- return;
237
- logger.info(`Enqueing blocks [${heights[0]}...${(0, lodash_1.last)(heights)}], total ${heights.length} blocks`);
238
- // eslint-disable-next-line no-constant-condition
239
- if (true) {
240
- /*
241
- * Load balancing:
242
- * worker1: 1,2,3
243
- * worker2: 4,5,6
244
- */
245
- const workerIdx = this.getNextWorkerIndex();
246
- heights.map((height) => this.enqueueBlock(height, workerIdx));
247
- }
248
- else {
249
- /*
250
- * Load balancing:
251
- * worker1: 1,3,5
252
- * worker2: 2,4,6
253
- */
254
- heights.map((height) => this.enqueueBlock(height, this.getNextWorkerIndex()));
255
- }
256
- this.latestBufferedHeight = (0, lodash_1.last)(heights);
257
- }
258
- flushQueue(height) {
259
- this.latestBufferedHeight = height;
260
- this.queue.flush();
261
- }
262
- enqueueBlock(height, workerIdx) {
263
- if (this.isShutdown)
264
- return;
265
- const worker = this.workers[workerIdx];
266
- (0, assert_1.default)(worker, `Worker ${workerIdx} not found`);
267
- // Used to compare before and after as a way to check if queue was flushed
268
- const bufferedHeight = this._latestBufferedHeight;
269
- const pendingBlock = worker.fetchBlock(height);
270
- const processBlock = async () => {
271
- var _a;
272
- try {
273
- const start = new Date();
274
- const result = await pendingBlock;
275
- const end = new Date();
276
- if (bufferedHeight > this._latestBufferedHeight) {
277
- logger.debug(`Queue was reset for new DS, discarding fetched blocks`);
278
- return;
279
- }
280
- const waitTime = end.getTime() - start.getTime();
281
- if (waitTime > 1000) {
282
- logger.info(`Waiting to fetch block ${height}: ${chalk_1.default.red(`${waitTime}ms`)}`);
283
- }
284
- else if (waitTime > 200) {
285
- logger.info(`Waiting to fetch block ${height}: ${chalk_1.default.yellow(`${waitTime}ms`)}`);
286
- }
287
- // logger.info(
288
- // `worker ${workerIdx} processing block ${height}, fetched blocks: ${await worker.numFetchedBlocks()}, fetching blocks: ${await worker.numFetchingBlocks()}`,
289
- // );
290
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlockProcessing, {
291
- height,
292
- timestamp: Date.now(),
293
- });
294
- const { dynamicDsCreated, operationHash } = await worker.processBlock(height);
295
- // In memory _processedBlockCount increase, db metadata increase BlockCount in indexer.manager
296
- this.setProcessedBlockCount(this._processedBlockCount + 1);
297
- if (this.nodeConfig.proofOfIndex &&
298
- !isNullMerkelRoot(Buffer.from(operationHash, 'base64'))) {
299
- if (!this.projectService.blockOffset) {
300
- // Which means during project init, it has not found offset and set value
301
- await this.projectService.upsertMetadataBlockOffset(height - 1);
302
- }
303
- void this.projectService.setBlockOffset(height - 1);
304
- }
305
- if (dynamicDsCreated) {
306
- await this.onDynamicDsCreated(height);
307
- }
308
- }
309
- catch (e) {
310
- logger.error(e, `failed to index block at height ${height} ${e.handler ? `${e.handler}(${(_a = e.stack) !== null && _a !== void 0 ? _a : ''})` : ''}`);
311
- throw e;
312
- }
313
- };
314
- void this.queue.put(processBlock);
315
- }
316
- async sampleWorkerStatus() {
317
- for (const worker of this.workers) {
318
- const status = await worker.getStatus();
319
- logger.info(JSON.stringify(status));
320
- }
321
- }
322
- get queueSize() {
323
- return this.queue.size;
324
- }
325
- get freeSize() {
326
- return this.queue.freeSpace;
327
- }
328
- get latestBufferedHeight() {
329
- return this._latestBufferedHeight;
330
- }
331
- set latestBufferedHeight(height) {
332
- this.eventEmitter.emit(node_core_1.IndexerEvent.BlocknumberQueueSize, {
333
- value: this.queueSize,
334
- });
335
- this._latestBufferedHeight = height;
336
- }
337
- getNextWorkerIndex() {
338
- const index = this.taskCounter % this.numWorkers;
339
- this.taskCounter++;
340
- return index;
341
- }
342
- };
343
- __decorate([
344
- (0, schedule_1.Interval)(15000),
345
- __metadata("design:type", Function),
346
- __metadata("design:paramtypes", []),
347
- __metadata("design:returntype", Promise)
348
- ], WorkerBlockDispatcherService.prototype, "sampleWorkerStatus", null);
349
- WorkerBlockDispatcherService = __decorate([
350
- (0, common_1.Injectable)(),
351
- __metadata("design:paramtypes", [node_core_1.NodeConfig,
352
- event_emitter_1.EventEmitter2,
353
- project_service_1.ProjectService])
354
- ], WorkerBlockDispatcherService);
355
- exports.WorkerBlockDispatcherService = WorkerBlockDispatcherService;
356
- //# sourceMappingURL=block-dispatcher.service.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"block-dispatcher.service.js","sourceRoot":"","sources":["../../../src/indexer/worker/block-dispatcher.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,oDAA4B;AAC5B,gDAAwB;AACxB,2CAAmE;AACnE,yDAAsD;AACtD,+CAA4C;AAC5C,yCAAiD;AACjD,gDAU0B;AAE1B,kDAA0B;AAC1B,mCAA8B;AAC9B,wDAAoD;AACpD,wDAAoD;AAUpD,MAAM,gBAAgB,GAAG,IAAA,eAAQ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I,CAAC,KAAK,CAAC,IAAI,CAAC;IACzB,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;IAC9B,CAAC;IAED,IAAI,oBAAoB;QACtB,OAAO,IAAI,CAAC,qBAAqB,CAAC;IACpC,CAAC;IAED,IAAI,oBAAoB,CAAC,MAAc;QACrC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,wBAAY,CAAC,oBAAoB,EAAE;YACxD,KAAK,EAAE,IAAI,CAAC,SAAS;SACtB,CAAC,CAAC;QACH,IAAI,CAAC,qBAAqB,GAAG,MAAM,CAAC;IACtC,CAAC;IAEO,kBAAkB;QACxB,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,UAAU,CAAC;QAEjD,IAAI,CAAC,WAAW,EAAE,CAAC;QAEnB,OAAO,KAAK,CAAC;IACf,CAAC;CACF,CAAA;AAjCO;IADL,IAAA,mBAAQ,EAAC,KAAK,CAAC;;;;sEAMf;AA7KU,4BAA4B;IADxC,IAAA,mBAAU,GAAE;qCAeW,sBAAU;QACR,6BAAa;QACX,gCAAc;GAhB7B,4BAA4B,CAyMxC;AAzMY,oEAA4B","sourcesContent":["// Copyright 2020-2021 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport path from 'path';\nimport { Injectable, OnApplicationShutdown } from '@nestjs/common';\nimport { EventEmitter2 } from '@nestjs/event-emitter';\nimport { Interval } from '@nestjs/schedule';\nimport { hexToU8a, u8aEq } from '@polkadot/util';\nimport {\n ApiService,\n getLogger,\n NodeConfig,\n IndexerEvent,\n AutoQueue,\n Queue,\n Worker,\n delay,\n profilerWrap,\n} from '@subql/node-core';\nimport { EthereumBlockWrapper } from '@subql/types-ethereum';\nimport chalk from 'chalk';\nimport { last } from 'lodash';\nimport { IndexerManager } from '../indexer.manager';\nimport { ProjectService } from '../project.service';\nimport {\n FetchBlock,\n ProcessBlock,\n InitWorker,\n NumFetchedBlocks,\n NumFetchingBlocks,\n GetWorkerStatus,\n} from './worker';\n\nconst NULL_MERKEL_ROOT = hexToU8a('0x00');\n\nfunction isNullMerkelRoot(operationHash: Uint8Array): boolean {\n return u8aEq(operationHash, NULL_MERKEL_ROOT);\n}\n\ntype IIndexerWorker = {\n processBlock: ProcessBlock;\n fetchBlock: FetchBlock;\n numFetchedBlocks: NumFetchedBlocks;\n numFetchingBlocks: NumFetchingBlocks;\n getStatus: GetWorkerStatus;\n};\n\ntype IInitIndexerWorker = IIndexerWorker & {\n initWorker: InitWorker;\n};\n\ntype IndexerWorker = IIndexerWorker & {\n terminate: () => Promise<number>;\n};\n\nasync function createIndexerWorker(): Promise<IndexerWorker> {\n const indexerWorker = Worker.create<IInitIndexerWorker>(\n path.resolve(__dirname, '../../../dist/indexer/worker/worker.js'),\n [\n 'initWorker',\n 'processBlock',\n 'fetchBlock',\n 'numFetchedBlocks',\n 'numFetchingBlocks',\n 'getStatus',\n ],\n );\n\n await indexerWorker.initWorker();\n\n return indexerWorker;\n}\n\nexport interface IBlockDispatcher {\n init(onDynamicDsCreated: (height: number) => Promise<void>): Promise<void>;\n\n enqueueBlocks(heights: number[]): void;\n\n queueSize: number;\n freeSize: number;\n latestBufferedHeight: number | undefined;\n\n // Remove all enqueued blocks, used when a dynamic ds is created\n flushQueue(height: number): void;\n}\n\nconst logger = getLogger('BlockDispatcherService');\n\n// TODO move to another file\n/**\n * @description Intended to behave the same as WorkerBlockDispatcherService but doesn't use worker threads or any parallel processing\n */\n@Injectable()\nexport class BlockDispatcherService\n implements IBlockDispatcher, OnApplicationShutdown\n{\n private fetchQueue: Queue<number>;\n private processQueue: AutoQueue<void>;\n\n private fetching = false;\n private isShutdown = false;\n private onDynamicDsCreated: (height: number) => Promise<void>;\n private _latestBufferedHeight: number;\n private _processedBlockCount: number;\n\n private fetchBlocksBatches: ApiService['api']['fetchBlocks'];\n private latestProcessedHeight: number;\n\n constructor(\n private apiService: ApiService,\n private nodeConfig: NodeConfig,\n private indexerManager: IndexerManager,\n private eventEmitter: EventEmitter2,\n private projectService: ProjectService,\n ) {\n this.fetchQueue = new Queue(nodeConfig.batchSize * 3);\n this.processQueue = new AutoQueue(nodeConfig.batchSize * 3);\n\n const fetchBlocks = this.apiService.api.fetchBlocks.bind(\n this.apiService.api,\n );\n if (this.nodeConfig.profiler) {\n this.fetchBlocksBatches = profilerWrap(\n fetchBlocks,\n 'EthereumUtil',\n 'fetchBlocksBatches',\n );\n } else {\n this.fetchBlocksBatches = fetchBlocks;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n async init(\n onDynamicDsCreated: (height: number) => Promise<void>,\n ): Promise<void> {\n this.onDynamicDsCreated = onDynamicDsCreated;\n const blockAmount = await this.projectService.getProcessedBlockCount();\n this.setProcessedBlockCount(blockAmount ?? 0);\n }\n\n onApplicationShutdown(): void {\n this.isShutdown = true;\n this.processQueue.abort();\n }\n\n enqueueBlocks(heights: number[]): void {\n if (!heights.length) return;\n\n logger.info(`Enqueing blocks ${heights[0]}...${last(heights)}`);\n\n this.fetchQueue.putMany(heights);\n this.latestBufferedHeight = last(heights);\n\n void this.fetchBlocksFromQueue().catch((e) => {\n logger.error(e, 'Failed to fetch blocks from queue');\n if (!this.isShutdown) {\n throw e;\n }\n });\n }\n\n flushQueue(height: number): void {\n this.latestBufferedHeight = height;\n this.fetchQueue.flush(); // Empty\n this.processQueue.flush();\n }\n\n private setProcessedBlockCount(processedBlockCount: number) {\n this._processedBlockCount = processedBlockCount;\n this.eventEmitter.emit(IndexerEvent.BlockProcessedCount, {\n processedBlockCount,\n timestamp: Date.now(),\n });\n }\n\n private async fetchBlocksFromQueue(): Promise<void> {\n if (this.fetching || this.isShutdown) return;\n // Process queue is full, no point in fetching more blocks\n // if (this.processQueue.freeSpace < this.nodeConfig.batchSize) return;\n\n this.fetching = true;\n\n try {\n while (!this.isShutdown) {\n const blockNums = this.fetchQueue.takeMany(\n Math.min(this.nodeConfig.batchSize, this.processQueue.freeSpace),\n );\n // Used to compare before and after as a way to check if queue was flushed\n const bufferedHeight = this._latestBufferedHeight;\n\n // Queue is empty\n if (!blockNums.length) {\n // The process queue might be full so no block nums were taken, wait and try again\n if (this.fetchQueue.size) {\n await delay(1);\n continue;\n }\n break;\n }\n\n logger.info(\n `fetch block [${blockNums[0]},${\n blockNums[blockNums.length - 1]\n }], total ${blockNums.length} blocks`,\n );\n\n const blocks = await this.fetchBlocksBatches(blockNums);\n\n if (bufferedHeight > this._latestBufferedHeight) {\n logger.debug(`Queue was reset for new DS, discarding fetched blocks`);\n continue;\n }\n\n const blockTasks = blocks.map((block) => async () => {\n const height = block.blockHeight;\n try {\n this.eventEmitter.emit(IndexerEvent.BlockProcessing, {\n height,\n timestamp: Date.now(),\n });\n\n const { dynamicDsCreated, operationHash } =\n await this.indexerManager.indexBlock(\n block as EthereumBlockWrapper,\n );\n\n // In memory _processedBlockCount increase, db metadata increase BlockCount in indexer.manager\n this.setProcessedBlockCount(this._processedBlockCount + 1);\n if (\n this.nodeConfig.proofOfIndex &&\n !isNullMerkelRoot(operationHash)\n ) {\n if (!this.projectService.blockOffset) {\n // Which means during project init, it has not found offset and set value\n await this.projectService.upsertMetadataBlockOffset(height - 1);\n }\n void this.projectService.setBlockOffset(height - 1);\n }\n\n if (dynamicDsCreated) {\n await this.onDynamicDsCreated(height);\n }\n\n assert(\n !this.latestProcessedHeight ||\n height > this.latestProcessedHeight,\n `Block processed out of order. Height: ${height}. Latest: ${this.latestProcessedHeight}`,\n );\n this.latestProcessedHeight = height;\n } catch (e) {\n if (this.isShutdown) {\n return;\n }\n logger.error(\n e,\n `failed to index block at height ${height} ${\n e.handler ? `${e.handler}(${e.stack ?? ''})` : ''\n }`,\n );\n throw e;\n }\n });\n\n // There can be enough of a delay after fetching blocks that shutdown could now be true\n if (this.isShutdown) break;\n\n this.processQueue.putMany(blockTasks);\n\n this.eventEmitter.emit(IndexerEvent.BlockQueueSize, {\n value: this.processQueue.size,\n });\n }\n } finally {\n this.fetching = false;\n }\n }\n\n get queueSize(): number {\n return this.fetchQueue.size;\n }\n\n get freeSize(): number {\n return this.fetchQueue.freeSpace;\n }\n\n get latestBufferedHeight(): number {\n return this._latestBufferedHeight;\n }\n\n set latestBufferedHeight(height: number) {\n this.eventEmitter.emit(IndexerEvent.BlocknumberQueueSize, {\n value: this.queueSize,\n });\n this._latestBufferedHeight = height;\n }\n}\n\n@Injectable()\nexport class WorkerBlockDispatcherService\n implements IBlockDispatcher, OnApplicationShutdown\n{\n private workers: IndexerWorker[];\n private numWorkers: number;\n private onDynamicDsCreated: (height: number) => Promise<void>;\n\n private taskCounter = 0;\n private isShutdown = false;\n private queue: AutoQueue<void>;\n private _latestBufferedHeight: number;\n private _processedBlockCount: number;\n\n constructor(\n private nodeConfig: NodeConfig,\n private eventEmitter: EventEmitter2,\n private projectService: ProjectService,\n ) {\n this.numWorkers = nodeConfig.workers;\n this.queue = new AutoQueue(this.numWorkers * nodeConfig.batchSize * 2);\n }\n\n async init(\n onDynamicDsCreated: (height: number) => Promise<void>,\n ): Promise<void> {\n this.workers = await Promise.all(\n new Array(this.numWorkers).fill(0).map(() => createIndexerWorker()),\n );\n\n this.onDynamicDsCreated = onDynamicDsCreated;\n\n const blockAmount = await this.projectService.getProcessedBlockCount();\n this.setProcessedBlockCount(blockAmount ?? 0);\n }\n\n private setProcessedBlockCount(processedBlockCount: number) {\n this._processedBlockCount = processedBlockCount;\n this.eventEmitter.emit(IndexerEvent.BlockProcessedCount, {\n processedBlockCount,\n timestamp: Date.now(),\n });\n }\n\n async onApplicationShutdown(): Promise<void> {\n this.isShutdown = true;\n // Stop processing blocks\n this.queue.abort();\n\n // Stop all workers\n if (this.workers) {\n await Promise.all(this.workers.map((w) => w.terminate()));\n }\n }\n\n enqueueBlocks(heights: number[]): void {\n if (!heights.length) return;\n logger.info(\n `Enqueing blocks [${heights[0]}...${last(heights)}], total ${\n heights.length\n } blocks`,\n );\n\n // eslint-disable-next-line no-constant-condition\n if (true) {\n /*\n * Load balancing:\n * worker1: 1,2,3\n * worker2: 4,5,6\n */\n const workerIdx = this.getNextWorkerIndex();\n heights.map((height) => this.enqueueBlock(height, workerIdx));\n } else {\n /*\n * Load balancing:\n * worker1: 1,3,5\n * worker2: 2,4,6\n */\n heights.map((height) =>\n this.enqueueBlock(height, this.getNextWorkerIndex()),\n );\n }\n\n this.latestBufferedHeight = last(heights);\n }\n\n flushQueue(height: number): void {\n this.latestBufferedHeight = height;\n this.queue.flush();\n }\n\n private enqueueBlock(height: number, workerIdx: number) {\n if (this.isShutdown) return;\n const worker = this.workers[workerIdx];\n\n assert(worker, `Worker ${workerIdx} not found`);\n\n // Used to compare before and after as a way to check if queue was flushed\n const bufferedHeight = this._latestBufferedHeight;\n const pendingBlock = worker.fetchBlock(height);\n\n const processBlock = async () => {\n try {\n const start = new Date();\n const result = await pendingBlock;\n const end = new Date();\n\n if (bufferedHeight > this._latestBufferedHeight) {\n logger.debug(`Queue was reset for new DS, discarding fetched blocks`);\n return;\n }\n\n const waitTime = end.getTime() - start.getTime();\n if (waitTime > 1000) {\n logger.info(\n `Waiting to fetch block ${height}: ${chalk.red(`${waitTime}ms`)}`,\n );\n } else if (waitTime > 200) {\n logger.info(\n `Waiting to fetch block ${height}: ${chalk.yellow(\n `${waitTime}ms`,\n )}`,\n );\n }\n\n // logger.info(\n // `worker ${workerIdx} processing block ${height}, fetched blocks: ${await worker.numFetchedBlocks()}, fetching blocks: ${await worker.numFetchingBlocks()}`,\n // );\n\n this.eventEmitter.emit(IndexerEvent.BlockProcessing, {\n height,\n timestamp: Date.now(),\n });\n\n const { dynamicDsCreated, operationHash } = await worker.processBlock(\n height,\n );\n // In memory _processedBlockCount increase, db metadata increase BlockCount in indexer.manager\n this.setProcessedBlockCount(this._processedBlockCount + 1);\n\n if (\n this.nodeConfig.proofOfIndex &&\n !isNullMerkelRoot(Buffer.from(operationHash, 'base64'))\n ) {\n if (!this.projectService.blockOffset) {\n // Which means during project init, it has not found offset and set value\n await this.projectService.upsertMetadataBlockOffset(height - 1);\n }\n void this.projectService.setBlockOffset(height - 1);\n }\n\n if (dynamicDsCreated) {\n await this.onDynamicDsCreated(height);\n }\n } catch (e) {\n logger.error(\n e,\n `failed to index block at height ${height} ${\n e.handler ? `${e.handler}(${e.stack ?? ''})` : ''\n }`,\n );\n throw e;\n }\n };\n\n void this.queue.put(processBlock);\n }\n\n @Interval(15000)\n async sampleWorkerStatus(): Promise<void> {\n for (const worker of this.workers) {\n const status = await worker.getStatus();\n logger.info(JSON.stringify(status));\n }\n }\n\n get queueSize(): number {\n return this.queue.size;\n }\n\n get freeSize(): number {\n return this.queue.freeSpace;\n }\n\n get latestBufferedHeight(): number {\n return this._latestBufferedHeight;\n }\n\n set latestBufferedHeight(height: number) {\n this.eventEmitter.emit(IndexerEvent.BlocknumberQueueSize, {\n value: this.queueSize,\n });\n this._latestBufferedHeight = height;\n }\n\n private getNextWorkerIndex(): number {\n const index = this.taskCounter % this.numWorkers;\n\n this.taskCounter++;\n\n return index;\n }\n}\n"]}