@hiero-ledger/hiero-json-rpc-relay 0.81.0 → 0.82.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 (30) hide show
  1. package/.standalone/dist/config-service/services/globalConfig.js +26 -2
  2. package/.standalone/dist/metrics/definitions.js +28 -0
  3. package/.standalone/dist/relay/formatters.js +10 -1
  4. package/.standalone/dist/relay/lib/clients/mirrorNodeClient.js +187 -41
  5. package/.standalone/dist/relay/lib/constants.js +1 -1
  6. package/.standalone/dist/relay/lib/debug.js +108 -53
  7. package/.standalone/dist/relay/lib/errors/JsonRpcError.js +12 -0
  8. package/.standalone/dist/relay/lib/services/ethService/blockService/blockWorker.js +5 -2
  9. package/.standalone/dist/relay/lib/services/ethService/ethCommonService/CommonService.js +26 -6
  10. package/.standalone/dist/relay/lib/services/ethService/ethCommonService/commonWorker.js +54 -1
  11. package/.standalone/dist/relay/lib/services/ethService/feeService/FeeService.js +9 -0
  12. package/.standalone/dist/relay/lib/services/ethService/transactionService/TransactionService.js +13 -3
  13. package/.standalone/dist/relay/lib/services/workersService/WorkersPool.js +3 -0
  14. package/.standalone/dist/relay/lib/types/RequestDetails.js +3 -0
  15. package/.standalone/dist/relay/lib/utils/getLogsMemoryBudget.js +84 -0
  16. package/.standalone/dist/relay/lib/utils/prestate.js +115 -1
  17. package/.standalone/dist/relay/lib/utils/responseSizeBudget.js +160 -0
  18. package/.standalone/dist/relay/lib/validators/objectTypes.js +51 -15
  19. package/.standalone/dist/relay/lib/validators/types.js +11 -1
  20. package/.standalone/dist/server/koaJsonRpc/index.js +11 -8
  21. package/.standalone/dist/ws-server/service/pollerService.js +165 -64
  22. package/.standalone/dist/ws-server/utils/constants.js +4 -0
  23. package/.standalone/dist/ws-server/utils/utils.js +3 -2
  24. package/.standalone/dist/ws-server/webSocketServer.js +23 -2
  25. package/.standalone/docs/openrpc.json +1 -1
  26. package/.standalone/node_modules/follow-redirects/index.js +1 -5
  27. package/.standalone/node_modules/follow-redirects/package.json +1 -1
  28. package/.standalone/package.json +1 -1
  29. package/README.md +1 -1
  30. package/package.json +1 -1
@@ -168,6 +168,25 @@ const _CONFIG = {
168
168
  required: false,
169
169
  defaultValue: 1000,
170
170
  },
171
+ ETH_GET_LOGS_MAX_PAGE_SIZE: {
172
+ type: 'number',
173
+ required: false,
174
+ defaultValue: 8388608, // 8 MiB
175
+ validation: (value) => value >= 1 || 'ETH_GET_LOGS_MAX_PAGE_SIZE must be 1 or greater.',
176
+ },
177
+ ETH_GET_LOGS_MAX_RESPONSE_SIZE: {
178
+ type: 'number',
179
+ required: false,
180
+ defaultValue: 26214400, // 25 MiB
181
+ validation: (value) => value >= 1 || 'ETH_GET_LOGS_MAX_RESPONSE_SIZE must be 1 or greater.',
182
+ },
183
+ ETH_GET_LOGS_RELAY_MAX_RESPONSE_SIZE: {
184
+ type: 'number',
185
+ required: false,
186
+ defaultValue: 52428800, // 50 MiB
187
+ validation: (value) => (Number.isInteger(value) && value >= 1) ||
188
+ 'ETH_GET_LOGS_RELAY_MAX_RESPONSE_SIZE must be an integer of 1 or greater.',
189
+ },
171
190
  // Each sub-topic is sent to the Mirror Node as a repeated `topicN` query param, so this must not exceed the
172
191
  // Mirror Node's `hiero.mirror.rest.query.maxRepeatedQueryParameters` (default 100).
173
192
  ETH_GET_LOGS_SUB_TOPICS_LIMIT: {
@@ -413,6 +432,11 @@ const _CONFIG = {
413
432
  required: false,
414
433
  defaultValue: null,
415
434
  },
435
+ MIRROR_NODE_BLOCK_RANGE_CACHE_ENABLED: {
436
+ type: 'boolean',
437
+ required: false,
438
+ defaultValue: true,
439
+ },
416
440
  MIRROR_NODE_CONTRACT_RESULTS_LOGS_PG_MAX: {
417
441
  type: 'number',
418
442
  required: false,
@@ -802,7 +826,7 @@ const _CONFIG = {
802
826
  TXPOOL_API_ENABLED: {
803
827
  type: 'boolean',
804
828
  required: false,
805
- defaultValue: false,
829
+ defaultValue: true,
806
830
  },
807
831
  TX_STATUS_TRACING: {
808
832
  type: 'boolean',
@@ -862,7 +886,7 @@ const _CONFIG = {
862
886
  ENABLE_NONCE_ORDERING: {
863
887
  type: 'boolean',
864
888
  required: false,
865
- defaultValue: false,
889
+ defaultValue: true,
866
890
  },
867
891
  USE_MIRROR_NODE_MODULARIZED_SERVICES: {
868
892
  type: 'boolean',
@@ -107,6 +107,26 @@ exports.METRICS = {
107
107
  help: 'Count of errors returned from Mirror Node by HTTP status code and error type',
108
108
  labelNames: ['method', 'statusCode'],
109
109
  }),
110
+ requests: counter({
111
+ name: 'rpc_relay_mirror_node_request_count',
112
+ help: 'Count of Mirror Node requests, excluding axios-retry retries',
113
+ labelNames: ['method'],
114
+ }),
115
+ retries: counter({
116
+ name: 'rpc_relay_mirror_node_retry_count',
117
+ help: 'Count of Mirror Node request retries made by axios-retry, for any reason',
118
+ labelNames: ['method'],
119
+ }),
120
+ timeouts: counter({
121
+ name: 'rpc_relay_mirror_node_timeout_count',
122
+ help: 'Count of Mirror Node requests that failed because their final attempt exceeded MIRROR_NODE_TIMEOUT',
123
+ labelNames: ['method'],
124
+ }),
125
+ timeoutRetries: counter({
126
+ name: 'rpc_relay_mirror_node_timeout_retry_count',
127
+ help: 'Count of Mirror Node request retries made after an attempt exceeded MIRROR_NODE_TIMEOUT',
128
+ labelNames: ['method'],
129
+ }),
110
130
  },
111
131
  /** Operator account metrics. */
112
132
  operator: {
@@ -358,6 +378,14 @@ exports.METRICS = {
358
378
  name: 'rpc_websocket_active_newheads_polls',
359
379
  help: 'Relay websocket active newHeads polls count',
360
380
  }),
381
+ lagBlocks: gauge({
382
+ name: 'rpc_websocket_poller_lag_blocks',
383
+ help: 'Relay websocket poller blocks the latest block is ahead of the last handled one',
384
+ }),
385
+ failedPolls: counter({
386
+ name: 'rpc_websocket_poller_failed_polls_total',
387
+ help: 'Relay websocket poller failed polls count',
388
+ }),
361
389
  },
362
390
  /** Subscription metrics. */
363
391
  subscription: {
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.tinybarsToWeibars = exports.mapKeysAndValues = exports.isHex = exports.isValidEthereumAddress = exports.strip0x = exports.toHexString = exports.weibarToTinyBar = exports.weibarHexToTinyBarInt = exports.trimPrecedingZeros = exports.generateRandomHex = exports.toNullIfEmptyHex = exports.toHash32 = exports.nanOrNumberInt64To0x = exports.nanOrNumberTo0x = exports.nullableNumberTo0x = exports.numberTo0x = exports.prepend0x = exports.parseNumericEnvVar = exports.formatTransactionIdWithoutQueryParams = exports.formatTransactionId = exports.decodeErrorMessage = exports.hashNumber = void 0;
7
+ exports.tinybarsToWeibars = exports.mapKeysAndValues = exports.isEmptyAccount = exports.isHex = exports.isValidEthereumAddress = exports.strip0x = exports.toHexString = exports.weibarToTinyBar = exports.weibarHexToTinyBarInt = exports.trimPrecedingZeros = exports.generateRandomHex = exports.toNullIfEmptyHex = exports.toHash32 = exports.nanOrNumberInt64To0x = exports.nanOrNumberTo0x = exports.nullableNumberTo0x = exports.numberTo0x = exports.prepend0x = exports.parseNumericEnvVar = exports.formatTransactionIdWithoutQueryParams = exports.formatTransactionId = exports.decodeErrorMessage = exports.hashNumber = void 0;
8
8
  exports.hexToASCII = hexToASCII;
9
9
  exports.stripLeadingZeroForSignatures = stripLeadingZeroForSignatures;
10
10
  exports.ASCIIToHex = ASCIIToHex;
@@ -240,6 +240,15 @@ const isHex = (value) => {
240
240
  return hexRegex.test(value);
241
241
  };
242
242
  exports.isHex = isHex;
243
+ /**
244
+ * Whether an account holds nothing: no nonce, no code and no balance. Storage is ignored, as in geth.
245
+ * The mirror node can report a missing nonce or code as null.
246
+ */
247
+ const isEmptyAccount = (account) => {
248
+ const hasCode = !!account.code && account.code !== EMPTY_HEX;
249
+ return !account.nonce && !hasCode && BigInt(account.balance) === BigInt(0);
250
+ };
251
+ exports.isEmptyAccount = isEmptyAccount;
243
252
  const tinybarsToWeibars = (value, allowNegativeValues = false) => {
244
253
  if (value && value < 0) {
245
254
  // negative amount can be received only by CONTRACT_NEGATIVE_VALUE revert
@@ -1,5 +1,38 @@
1
1
  "use strict";
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
37
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
38
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -27,7 +60,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
27
60
  exports.MirrorNodeClient = exports.isSyntheticContractRecord = exports.isChildContractRecord = exports.isImmatureContractRecord = void 0;
28
61
  // Side-effect import: configures the json-bigint bignumber.js instance before any response is parsed.
29
62
  require("../bigNumberConfig");
30
- const axios_1 = __importDefault(require("axios"));
63
+ const axios_1 = __importStar(require("axios"));
31
64
  const axios_retry_1 = __importDefault(require("axios-retry"));
32
65
  const better_lookup_1 = require("better-lookup");
33
66
  const http_1 = __importDefault(require("http"));
@@ -45,6 +78,7 @@ const TransactionTimestampIndexFactory_1 = require("../services/transactionTimes
45
78
  const WorkersPool_1 = require("../services/workersService/WorkersPool");
46
79
  const types_1 = require("../types");
47
80
  const requestAbort_1 = require("../utils/requestAbort");
81
+ const responseSizeBudget_1 = require("../utils/responseSizeBudget");
48
82
  const constants_1 = __importDefault(require("./../constants"));
49
83
  /**
50
84
  * Whether a Mirror Node contract-result / log record is "immature" - the block linkage is not (yet)
@@ -181,7 +215,7 @@ class MirrorNodeClient {
181
215
  },
182
216
  retryCondition: (error) => {
183
217
  var _a, _b;
184
- if (axios_1.default.isCancel(error)) {
218
+ if (axios_1.default.isCancel(error) || MirrorNodeClient.isMaxContentLengthError(error)) {
185
219
  return false;
186
220
  }
187
221
  // @ts-ignore
@@ -191,6 +225,31 @@ class MirrorNodeClient {
191
225
  });
192
226
  return axiosClient;
193
227
  }
228
+ /**
229
+ * Reports whether a request failed because its response body grew past the request's `maxContentLength`.
230
+ *
231
+ * @param error - The value thrown by the failed request.
232
+ * @returns `true` for the error axios raises when it stops a response at `maxContentLength`.
233
+ */
234
+ static isMaxContentLengthError(error) {
235
+ return (axios_1.default.isAxiosError(error) &&
236
+ error.code === axios_1.default.AxiosError.ERR_BAD_RESPONSE &&
237
+ error.message.startsWith('maxContentLength size of'));
238
+ }
239
+ /**
240
+ * Reports whether fetching logs failed because a size limit was reached: the call's budget, or a single page
241
+ * stopped at its maximum size. Such failures must not be retried by another strategy, since it would only
242
+ * download the same oversized logs again.
243
+ *
244
+ * @param error - The value thrown while fetching the logs.
245
+ * @param limits - The size limits of the `eth_getLogs` call, if any.
246
+ * @returns `true` when the error is a size limit error, or the call's budget is already exceeded.
247
+ */
248
+ static isLogsSizeError(error, limits) {
249
+ return (responseSizeBudget_1.ResponseSizeBudget.isExceededError(error) ||
250
+ (limits === null || limits === void 0 ? void 0 : limits.budget.isExceeded) === true ||
251
+ (error instanceof JsonRpcError_1.JsonRpcError && error.code === JsonRpcError_1.predefined.MIRROR_NODE_RESPONSE_SIZE_EXCEEDED(0).code));
252
+ }
194
253
  constructor(restUrl, logger, register, cacheService, restClient, web3Url, web3Client, transactionTimestampIndex = new TransactionTimestampIndexFactory_1.DisabledTransactionTimestampIndex()) {
195
254
  this.MIRROR_NODE_RETRY_DELAY = services_1.ConfigService.get('MIRROR_NODE_RETRY_DELAY');
196
255
  this.MIRROR_NODE_REQUEST_RETRY_COUNT = services_1.ConfigService.get('MIRROR_NODE_REQUEST_RETRY_COUNT');
@@ -213,6 +272,12 @@ class MirrorNodeClient {
213
272
  const metricsFactory = new metrics_1.MetricsFactory(register);
214
273
  this.mirrorResponseHistogram = metricsFactory.histogram(metrics_1.METRICS.mirrorNode.responseLatency);
215
274
  this.mirrorErrorCodeCounter = metricsFactory.counter(metrics_1.METRICS.mirrorNode.httpErrorCodes);
275
+ this.mirrorCounters = {
276
+ requests: metricsFactory.counter(metrics_1.METRICS.mirrorNode.requests),
277
+ retries: metricsFactory.counter(metrics_1.METRICS.mirrorNode.retries),
278
+ timeouts: metricsFactory.counter(metrics_1.METRICS.mirrorNode.timeouts),
279
+ timeoutRetries: metricsFactory.counter(metrics_1.METRICS.mirrorNode.timeoutRetries),
280
+ };
216
281
  if (worker_threads_1.isMainThread) {
217
282
  this.logger.info(`Mirror Node client successfully configured to REST url: %s and Web3 url: %s `, this.restUrl, this.web3Url);
218
283
  }
@@ -268,10 +333,11 @@ class MirrorNodeClient {
268
333
  // return raw data so response can be processed properly by subsequent operations.
269
334
  return data;
270
335
  }
271
- request(path, pathLabel, method, requestDetails, data, retries) {
336
+ request(path, pathLabel, method, requestDetails, data, retries, maxContentLength) {
272
337
  return __awaiter(this, void 0, void 0, function* () {
273
338
  var _a, _b;
274
339
  (0, requestAbort_1.throwIfRequestAborted)(requestDetails);
340
+ this.incrementMirrorCounter('requests', pathLabel);
275
341
  const start = Date.now();
276
342
  const controller = new AbortController();
277
343
  const callerSignal = (0, requestAbort_1.getRequestAbortSignal)(requestDetails);
@@ -289,9 +355,22 @@ class MirrorNodeClient {
289
355
  const formattedClientIp = this.formatIpForForwardedHeader(requestDetails.ipAddress);
290
356
  axiosRequestConfig.headers['Forwarded'] = `for="${formattedClientIp}"`;
291
357
  }
292
- // request specific config for axios-retry
358
+ // Count retries here, since the catch below only sees the last attempt.
359
+ const retryConfig = {
360
+ onRetry: (_retryCount, retryError) => {
361
+ this.incrementMirrorCounter('retries', pathLabel);
362
+ if (MirrorNodeClient.isTimeoutError(retryError)) {
363
+ this.incrementMirrorCounter('timeoutRetries', pathLabel);
364
+ }
365
+ },
366
+ };
293
367
  if (retries != null) {
294
- axiosRequestConfig['axios-retry'] = { retries };
368
+ retryConfig.retries = retries;
369
+ }
370
+ axiosRequestConfig['axios-retry'] = retryConfig;
371
+ // stop the download as soon as the body grows past the limit, instead of buffering all of it
372
+ if (maxContentLength != null) {
373
+ axiosRequestConfig.maxContentLength = maxContentLength;
295
374
  }
296
375
  let response;
297
376
  if (method === MirrorNodeClient.HTTP_GET) {
@@ -331,8 +410,15 @@ class MirrorNodeClient {
331
410
  // Record metrics
332
411
  this.addLabelToMirrorResponseHistogram(pathLabel, effectiveStatusCode.toString(), ms);
333
412
  this.addLabelToMirrorErrorCodeCounter(pathLabel, effectiveStatusCode.toString());
413
+ if (MirrorNodeClient.isTimeoutError(axiosError)) {
414
+ this.incrementMirrorCounter('timeouts', pathLabel);
415
+ }
334
416
  // always abort the request on failure as the axios call can hang until the parent code/stack times out (might be a few minutes in a server-side applications)
335
417
  controller.abort();
418
+ if (maxContentLength != null && MirrorNodeClient.isMaxContentLengthError(error)) {
419
+ this.logger.warn(`Mirror node response stopped at the maximum size: method=%s, path=%s, maxContentLength=%s`, method, path, maxContentLength);
420
+ throw JsonRpcError_1.predefined.MIRROR_NODE_RESPONSE_SIZE_EXCEEDED(maxContentLength);
421
+ }
336
422
  // either return null for accepted error codes or throw a MirrorNodeClientError
337
423
  return this.handleError(error, path, pathLabel, effectiveStatusCode, method);
338
424
  }
@@ -370,9 +456,22 @@ class MirrorNodeClient {
370
456
  value,
371
457
  }, () => this.mirrorErrorCodeCounter.labels(pathLabel, value).inc());
372
458
  }
373
- get(path, pathLabel, requestDetails, retries) {
459
+ /**
460
+ * Increments one of the mirror request, retry or timeout counters, via the parent thread when called from a worker.
461
+ *
462
+ * @param counter - Which counter to increment.
463
+ * @param pathLabel - Label identifying the mirrored request path.
464
+ */
465
+ incrementMirrorCounter(counter, pathLabel) {
466
+ WorkersPool_1.WorkersPool.updateMetricViaWorkerOrLocal(MirrorNodeClient.INCREMENT_MIRROR_COUNTER, { counter, pathLabel }, () => this.mirrorCounters[counter].labels(pathLabel).inc());
467
+ }
468
+ /** Axios reports its own MIRROR_NODE_TIMEOUT expiry as ECONNABORTED, or ETIMEDOUT when clarifyTimeoutError is on. */
469
+ static isTimeoutError(error) {
470
+ return error.code === axios_1.AxiosError.ECONNABORTED || error.code === axios_1.AxiosError.ETIMEDOUT;
471
+ }
472
+ get(path, pathLabel, requestDetails, retries, maxContentLength) {
374
473
  return __awaiter(this, void 0, void 0, function* () {
375
- return this.request(path, pathLabel, 'GET', requestDetails, null, retries);
474
+ return this.request(path, pathLabel, 'GET', requestDetails, null, retries, maxContentLength);
376
475
  });
377
476
  }
378
477
  post(path, data, pathLabel, requestDetails, retries) {
@@ -407,10 +506,11 @@ class MirrorNodeClient {
407
506
  throw mirrorError;
408
507
  }
409
508
  getPaginatedResults(url_1, pathLabel_1, resultProperty_1, requestDetails_1) {
410
- return __awaiter(this, arguments, void 0, function* (url, pathLabel, resultProperty, requestDetails, results = [], page = 1, pageMax = services_1.ConfigService.get('MIRROR_NODE_PAGINATION_MAX')) {
411
- var _a;
412
- const result = yield this.get(url, pathLabel, requestDetails);
509
+ return __awaiter(this, arguments, void 0, function* (url, pathLabel, resultProperty, requestDetails, results = [], page = 1, pageMax = services_1.ConfigService.get('MIRROR_NODE_PAGINATION_MAX'), options) {
510
+ var _a, _b;
511
+ const result = yield this.get(url, pathLabel, requestDetails, undefined, options === null || options === void 0 ? void 0 : options.maxPageBytes);
413
512
  if (result && result[resultProperty]) {
513
+ (_a = options === null || options === void 0 ? void 0 : options.onPage) === null || _a === void 0 ? void 0 : _a.call(options, result[resultProperty]);
414
514
  results = results.concat(result[resultProperty]);
415
515
  }
416
516
  if (page === pageMax) {
@@ -418,10 +518,10 @@ class MirrorNodeClient {
418
518
  this.logger.trace(`Max page reached %s with %s results`, pageMax, results.length);
419
519
  throw JsonRpcError_1.predefined.PAGINATION_MAX(pageMax);
420
520
  }
421
- if (((_a = result === null || result === void 0 ? void 0 : result.links) === null || _a === void 0 ? void 0 : _a.next) && page < pageMax) {
521
+ if (((_b = result === null || result === void 0 ? void 0 : result.links) === null || _b === void 0 ? void 0 : _b.next) && page < pageMax) {
422
522
  page++;
423
523
  const next = result.links.next.replace(constants_1.default.NEXT_LINK_PREFIX, '');
424
- return this.getPaginatedResults(next, pathLabel, resultProperty, requestDetails, results, page, pageMax);
524
+ return this.getPaginatedResults(next, pathLabel, resultProperty, requestDetails, results, page, pageMax, options);
425
525
  }
426
526
  else {
427
527
  return results;
@@ -608,9 +708,9 @@ class MirrorNodeClient {
608
708
  this.setQueryParam(queryParamObject, 'order', MirrorNodeClient.ORDER.ASC);
609
709
  const queryParams = this.getQueryParams(queryParamObject);
610
710
  const blocks = yield this.getPaginatedResults(`${MirrorNodeClient.GET_BLOCKS_ENDPOINT}${queryParams}`, MirrorNodeClient.GET_BLOCKS_ENDPOINT, 'blocks', requestDetails, [], 1, pageMax);
611
- // Populate the per-block cache so subsequent getBlock(number) calls from any service
612
- // resolve from cache rather than issuing a new upstream request.
613
- yield Promise.all(blocks.map((block) => this.cacheService.set(`${constants_1.default.CACHE_KEY.GET_BLOCK}.${block.number}`, block, MirrorNodeClient.GET_BLOCK_ENDPOINT)));
711
+ if (services_1.ConfigService.get('MIRROR_NODE_BLOCK_RANGE_CACHE_ENABLED')) {
712
+ yield Promise.all(blocks.map((block) => this.cacheService.set(`${constants_1.default.CACHE_KEY.GET_BLOCK}.${block.number}`, block, MirrorNodeClient.GET_BLOCK_ENDPOINT)));
713
+ }
614
714
  return blocks;
615
715
  });
616
716
  }
@@ -805,11 +905,17 @@ class MirrorNodeClient {
805
905
  *
806
906
  * @param transactionIdOrHash - The transaction to trace.
807
907
  * @param requestDetails - Details for request tracking and logging.
908
+ * @param params - Whether to include each account's code and storage, both by default, and whether to return
909
+ * the diff between the state before and after the transaction.
808
910
  * @returns The prestate response, or null when the transaction has none.
809
911
  */
810
- getContractResultPrestate(transactionIdOrHash, requestDetails) {
811
- return __awaiter(this, void 0, void 0, function* () {
812
- const queryParams = this.getQueryParams({ code: true, storage: true });
912
+ getContractResultPrestate(transactionIdOrHash_1, requestDetails_1) {
913
+ return __awaiter(this, arguments, void 0, function* (transactionIdOrHash, requestDetails, params = { code: true, storage: true, diff: false }) {
914
+ const queryParams = this.getQueryParams({
915
+ code: params.code,
916
+ storage: params.storage,
917
+ diff: params.diff,
918
+ });
813
919
  return this.get(`${this.getContractResultsPrestateByTransactionIdPath(transactionIdOrHash)}${queryParams}`, MirrorNodeClient.GET_CONTRACTS_RESULTS_PRESTATE, requestDetails);
814
920
  });
815
921
  }
@@ -866,24 +972,41 @@ class MirrorNodeClient {
866
972
  * - `block_number` being null/undefined
867
973
  * - `block_hash` being '0x' (empty hex)
868
974
  *
869
- * @param fetchFn - Async function that fetches the log results
975
+ * When size limits are given, every page is charged to the call's budget as it arrives. The bytes of an attempt
976
+ * that is retried are released first, so the same logs are never counted twice.
977
+ *
978
+ * @param fetchFn - Async function that fetches the log results, reporting each page through the given options
870
979
  * @param requestDetails - Request details for logging
871
980
  * @param maxAttempts - Maximum total fetch attempts (initial + retries). For example, 10 means 1 initial + 9 retries.
981
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
872
982
  * @param attempt - Current attempt number
873
983
  * @returns Array of mature log results
874
984
  * @throws DEPENDENT_SERVICE_IMMATURE_RECORDS error if immature records persist after all attempts
985
+ * @throws LOGS_RESPONSE_SIZE_EXCEEDED once the call's budget is exceeded
875
986
  */
876
- fetchLogsWithImmatureRetry(fetchFn_1, requestDetails_1, maxAttempts_1) {
877
- return __awaiter(this, arguments, void 0, function* (fetchFn, requestDetails, maxAttempts, attempt = 0) {
878
- const logResults = yield fetchFn();
987
+ fetchLogsWithImmatureRetry(fetchFn_1, requestDetails_1, maxAttempts_1, limits_1) {
988
+ return __awaiter(this, arguments, void 0, function* (fetchFn, requestDetails, maxAttempts, limits, attempt = 0) {
989
+ let chargedBytes = 0;
990
+ const options = limits && {
991
+ maxPageBytes: limits.maxPageBytes,
992
+ onPage: (logs) => {
993
+ const bytes = (0, responseSizeBudget_1.estimateLogsResponseSize)(logs);
994
+ chargedBytes += bytes;
995
+ limits.budget.consume(bytes);
996
+ },
997
+ };
998
+ const logResults = yield fetchFn(options);
879
999
  const hasImmatureRecords = logResults.some((log) => log && ((0, exports.isImmatureContractRecord)(log, this.logger) || log.index == null));
880
1000
  if (hasImmatureRecords) {
881
1001
  const isLastAttempt = attempt >= maxAttempts - 1;
882
1002
  if (!isLastAttempt) {
1003
+ // the retry fetches the same logs again, so the bytes of this attempt must not stay charged
1004
+ limits === null || limits === void 0 ? void 0 : limits.budget.release(chargedBytes);
883
1005
  const retryDelay = this.getMirrorNodeRetryDelay();
884
1006
  this.logger.debug('Immature log records detected, retrying after %sms (attempt %s/%s)', retryDelay, attempt + 1, maxAttempts);
885
1007
  yield new Promise((r) => setTimeout(r, retryDelay));
886
- return this.fetchLogsWithImmatureRetry(fetchFn, requestDetails, maxAttempts, attempt + 1);
1008
+ limits === null || limits === void 0 ? void 0 : limits.budget.assertNotExceeded();
1009
+ return this.fetchLogsWithImmatureRetry(fetchFn, requestDetails, maxAttempts, limits, attempt + 1);
887
1010
  }
888
1011
  else {
889
1012
  throw JsonRpcError_1.predefined.DEPENDENT_SERVICE_IMMATURE_RECORDS;
@@ -902,11 +1025,12 @@ class MirrorNodeClient {
902
1025
  * @param sliceCount - Number of timestamp slices for parallel fetching. Default is 1 (sequential mode).
903
1026
  * @param contractLogsResultsParams - Query parameters for contract logs results
904
1027
  * @param limitOrderParams - Pagination limit and ordering parameters
1028
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
905
1029
  * @returns Mature contract logs results array
906
1030
  */
907
1031
  getContractResultsLogsWithRetry(requestDetails_1) {
908
- return __awaiter(this, arguments, void 0, function* (requestDetails, sliceCount = 1, contractLogsResultsParams, limitOrderParams) {
909
- return this.fetchContractLogsWithSlicingSupport(MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_ENDPOINT, MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_ENDPOINT, requestDetails, sliceCount, contractLogsResultsParams, limitOrderParams);
1032
+ return __awaiter(this, arguments, void 0, function* (requestDetails, sliceCount = 1, contractLogsResultsParams, limitOrderParams, limits) {
1033
+ return this.fetchContractLogsWithSlicingSupport(MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_ENDPOINT, MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_ENDPOINT, requestDetails, sliceCount, contractLogsResultsParams, limitOrderParams, limits);
910
1034
  });
911
1035
  }
912
1036
  /**
@@ -920,14 +1044,15 @@ class MirrorNodeClient {
920
1044
  * @param sliceCount - Number of timestamp slices for parallel fetching. Default is 1 (sequential mode).
921
1045
  * @param contractLogsResultsParams - Query parameters for contract logs results
922
1046
  * @param limitOrderParams - Pagination limit and ordering parameters
1047
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
923
1048
  * @returns Mature contract logs results array for the specified address
924
1049
  */
925
1050
  getContractResultsLogsByAddress(address_1, requestDetails_1) {
926
- return __awaiter(this, arguments, void 0, function* (address, requestDetails, sliceCount = 1, contractLogsResultsParams, limitOrderParams) {
1051
+ return __awaiter(this, arguments, void 0, function* (address, requestDetails, sliceCount = 1, contractLogsResultsParams, limitOrderParams, limits) {
927
1052
  if (address === constants_1.default.ZERO_ADDRESS_HEX)
928
1053
  return [];
929
1054
  const apiEndpoint = MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT.replace(MirrorNodeClient.ADDRESS_PLACEHOLDER, address);
930
- return this.fetchContractLogsWithSlicingSupport(apiEndpoint, MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT, requestDetails, sliceCount, contractLogsResultsParams, limitOrderParams);
1055
+ return this.fetchContractLogsWithSlicingSupport(apiEndpoint, MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT, requestDetails, sliceCount, contractLogsResultsParams, limitOrderParams, limits);
931
1056
  });
932
1057
  }
933
1058
  /**
@@ -943,15 +1068,17 @@ class MirrorNodeClient {
943
1068
  * @param sliceCount - Number of timestamp slices for parallel fetching
944
1069
  * @param contractLogsResultsParams - Query parameters for contract logs results
945
1070
  * @param limitOrderParams - Pagination limit and ordering parameters
1071
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
946
1072
  * @returns Mature contract logs results array
947
1073
  */
948
- fetchContractLogsWithSlicingSupport(apiEndpoint, pathLabel, requestDetails, sliceCount, contractLogsResultsParams, limitOrderParams) {
1074
+ fetchContractLogsWithSlicingSupport(apiEndpoint, pathLabel, requestDetails, sliceCount, contractLogsResultsParams, limitOrderParams, limits) {
949
1075
  return __awaiter(this, void 0, void 0, function* () {
950
1076
  const timestampRange = contractLogsResultsParams === null || contractLogsResultsParams === void 0 ? void 0 : contractLogsResultsParams.timestamp;
951
1077
  // Attempt parallel timestamp slicing when applicable
952
1078
  if (timestampRange && sliceCount > 1) {
1079
+ const slicesLimits = limits && Object.assign(Object.assign({}, limits), { budget: limits.budget.createChild() });
953
1080
  try {
954
- return yield this.getContractResultsLogsWithTimestampSlicing(apiEndpoint, pathLabel, requestDetails, timestampRange, sliceCount, contractLogsResultsParams, limitOrderParams);
1081
+ return yield this.getContractResultsLogsWithTimestampSlicing(apiEndpoint, pathLabel, requestDetails, timestampRange, sliceCount, contractLogsResultsParams, limitOrderParams, slicesLimits);
955
1082
  }
956
1083
  catch (error) {
957
1084
  // Determine if error is recoverable (should fallback to sequential) vs fatal (should propagate).
@@ -964,19 +1091,21 @@ class MirrorNodeClient {
964
1091
  typeof error === 'object' &&
965
1092
  'code' in error &&
966
1093
  error.code === JsonRpcError_1.predefined.DEPENDENT_SERVICE_IMMATURE_RECORDS.code;
967
- // Immature records should propagate - sequential won't resolve data maturity issues
968
- if (isImmatureRecordsError) {
1094
+ // Immature records should propagate - sequential won't resolve data maturity issues.
1095
+ // Size errors too: a sequential retry would only download the same oversized logs again.
1096
+ if (isImmatureRecordsError || MirrorNodeClient.isLogsSizeError(error, limits)) {
969
1097
  throw error;
970
1098
  }
971
1099
  // Log recoverable errors and fallback to sequential
972
1100
  if (isTimestampError || isApiError) {
973
1101
  this.logger.warn('Timestamp slicing failed (%s), falling back to sequential: %s', isTimestampError ? 'configuration error' : 'API error', error instanceof Error ? error.message : String(error));
974
1102
  }
1103
+ slicesLimits === null || slicesLimits === void 0 ? void 0 : slicesLimits.budget.release(slicesLimits.budget.usedBytes);
975
1104
  }
976
1105
  }
977
1106
  // Sequential fetching: fallback or when slicing not applicable
978
1107
  const queryParams = this.prepareLogsParams(contractLogsResultsParams, limitOrderParams);
979
- return this.fetchLogsWithImmatureRetry(() => this.getPaginatedResults(`${apiEndpoint}${queryParams}`, pathLabel, MirrorNodeClient.CONTRACT_RESULT_LOGS_PROPERTY, requestDetails, [], 1, this.calculateMaxPage(contractLogsResultsParams)), requestDetails, this.getMirrorNodeRequestRetryCount());
1108
+ return this.fetchLogsWithImmatureRetry((options) => this.getPaginatedResults(`${apiEndpoint}${queryParams}`, pathLabel, MirrorNodeClient.CONTRACT_RESULT_LOGS_PROPERTY, requestDetails, [], 1, this.calculateMaxPage(contractLogsResultsParams), options), requestDetails, this.getMirrorNodeRequestRetryCount(), limits);
980
1109
  });
981
1110
  }
982
1111
  getEarliestBlock(requestDetails) {
@@ -1313,15 +1442,16 @@ class MirrorNodeClient {
1313
1442
  * @param maxAttempts - Maximum number of retry attempts for immature records
1314
1443
  * @param contractLogsResultsParams - Base query parameters
1315
1444
  * @param limitOrderParams - Pagination limit and ordering parameters
1445
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
1316
1446
  * @returns Mature contract logs results for the slice
1317
1447
  * @throws DEPENDENT_SERVICE_IMMATURE_RECORDS if immature records persist after all attempts
1318
1448
  */
1319
- fetchLogSliceWithRetry(apiEndpoint, pathLabel, requestDetails, slice, maxAttempts, contractLogsResultsParams, limitOrderParams) {
1449
+ fetchLogSliceWithRetry(apiEndpoint, pathLabel, requestDetails, slice, maxAttempts, contractLogsResultsParams, limitOrderParams, limits) {
1320
1450
  return __awaiter(this, void 0, void 0, function* () {
1321
1451
  const _a = contractLogsResultsParams || {}, { timestamp: _originalTimestamp } = _a, baseParams = __rest(_a, ["timestamp"]);
1322
1452
  const sliceParams = Object.assign(Object.assign({}, baseParams), { timestamp: [slice.from, slice.to] });
1323
1453
  const queryParams = this.prepareLogsParams(sliceParams, limitOrderParams);
1324
- return this.fetchLogsWithImmatureRetry(() => this.getPaginatedResults(`${apiEndpoint}${queryParams}`, pathLabel, MirrorNodeClient.CONTRACT_RESULT_LOGS_PROPERTY, requestDetails), requestDetails, maxAttempts);
1454
+ return this.fetchLogsWithImmatureRetry((options) => this.getPaginatedResults(`${apiEndpoint}${queryParams}`, pathLabel, MirrorNodeClient.CONTRACT_RESULT_LOGS_PROPERTY, requestDetails, undefined, undefined, undefined, options), requestDetails, maxAttempts, limits);
1325
1455
  });
1326
1456
  }
1327
1457
  /**
@@ -1335,21 +1465,35 @@ class MirrorNodeClient {
1335
1465
  * @param slices - Array of timestamp slice boundaries for parallel fetching
1336
1466
  * @param contractLogsResultsParams - Original query parameters excluding timestamp
1337
1467
  * @param limitOrderParams - Pagination limit and ordering parameters
1468
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
1338
1469
  * @returns Deduplicated and timestamp-sorted array of contract result logs
1339
- * @throws Error if any slice fails after exhausting retries
1470
+ * @throws Error if any slice fails after exhausting retries, once the other slices have stopped
1340
1471
  */
1341
- executeLogsSlicesWithConcurrency(apiEndpoint, pathLabel, requestDetails, slices, contractLogsResultsParams, limitOrderParams) {
1472
+ executeLogsSlicesWithConcurrency(apiEndpoint, pathLabel, requestDetails, slices, contractLogsResultsParams, limitOrderParams, limits) {
1342
1473
  return __awaiter(this, void 0, void 0, function* () {
1343
1474
  const concurrency = services_1.ConfigService.get('MIRROR_NODE_TIMESTAMP_SLICING_CONCURRENCY');
1344
1475
  const maxAttempts = this.getMirrorNodeRequestRetryCount();
1345
1476
  const sliceResults = [];
1346
1477
  const activeRequestsPool = new Set();
1478
+ const slicesController = new AbortController();
1479
+ const callerSignal = (0, requestAbort_1.getRequestAbortSignal)(requestDetails);
1480
+ const slicesRequestDetails = new types_1.RequestDetails({
1481
+ requestId: requestDetails.requestId,
1482
+ ipAddress: requestDetails.ipAddress,
1483
+ connectionId: requestDetails.connectionId,
1484
+ abortSignal: callerSignal ? AbortSignal.any([callerSignal, slicesController.signal]) : slicesController.signal,
1485
+ });
1486
+ const stopSlicesAndRethrow = (error) => __awaiter(this, void 0, void 0, function* () {
1487
+ slicesController.abort();
1488
+ yield Promise.allSettled(activeRequestsPool);
1489
+ throw error;
1490
+ });
1347
1491
  for (const slice of slices) {
1348
1492
  const sliceIndex = sliceResults.length;
1349
1493
  sliceResults.push([]); // Pre-allocate slot for ordered results
1350
1494
  // Start fetch request for the particular slice
1351
1495
  const activeRequest = (() => __awaiter(this, void 0, void 0, function* () {
1352
- const data = yield this.fetchLogSliceWithRetry(apiEndpoint, pathLabel, requestDetails, slice, maxAttempts, contractLogsResultsParams, limitOrderParams);
1496
+ const data = yield this.fetchLogSliceWithRetry(apiEndpoint, pathLabel, slicesRequestDetails, slice, maxAttempts, contractLogsResultsParams, limitOrderParams, limits);
1353
1497
  sliceResults[sliceIndex] = data;
1354
1498
  }))();
1355
1499
  // Add the activeRequest to the pool and set up self-cleanup
@@ -1361,14 +1505,14 @@ class MirrorNodeClient {
1361
1505
  // Throttle: pause iteration when concurrency limit is reached
1362
1506
  // Resume and add new requests to the pool when any active request completes
1363
1507
  if (activeRequestsPool.size >= concurrency) {
1364
- yield Promise.race(activeRequestsPool);
1508
+ yield Promise.race(activeRequestsPool).catch(stopSlicesAndRethrow);
1365
1509
  }
1366
1510
  if ((0, requestAbort_1.isRequestAborted)(requestDetails)) {
1367
1511
  break;
1368
1512
  }
1369
1513
  }
1370
1514
  // Wait for all remaining requests to complete
1371
- yield Promise.all(activeRequestsPool);
1515
+ yield Promise.all(activeRequestsPool).catch(stopSlicesAndRethrow);
1372
1516
  (0, requestAbort_1.throwIfRequestAborted)(requestDetails);
1373
1517
  // Merge all slice results with deduplication (single-threaded, no race condition)
1374
1518
  const allLogs = [];
@@ -1403,9 +1547,10 @@ class MirrorNodeClient {
1403
1547
  * @param sliceCount - Number of slices to create for parallel fetching
1404
1548
  * @param contractLogsResultsParams - Original query parameters excluding timestamp
1405
1549
  * @param limitOrderParams - Pagination limit and ordering parameters
1550
+ * @param limits - Optional size limits of the `eth_getLogs` call the logs are fetched for
1406
1551
  * @returns Deduplicated and sorted array of contract result logs from all slices
1407
1552
  */
1408
- getContractResultsLogsWithTimestampSlicing(apiEndpoint, pathLabel, requestDetails, timestampRange, sliceCount, contractLogsResultsParams, limitOrderParams) {
1553
+ getContractResultsLogsWithTimestampSlicing(apiEndpoint, pathLabel, requestDetails, timestampRange, sliceCount, contractLogsResultsParams, limitOrderParams, limits) {
1409
1554
  return __awaiter(this, void 0, void 0, function* () {
1410
1555
  // Parse and validate timestamp range
1411
1556
  const { fromNanos, toNanos } = this.parseTimestampRange(timestampRange);
@@ -1413,7 +1558,7 @@ class MirrorNodeClient {
1413
1558
  const slices = this.splitTimestampRange(fromNanos, toNanos, sliceCount);
1414
1559
  this.logger.debug(`Starting executing log retrieval with concurrency: slices=%s, concurrency=%s`, slices.length, services_1.ConfigService.get('MIRROR_NODE_TIMESTAMP_SLICING_CONCURRENCY'));
1415
1560
  // Execute all slices with concurrency control and immature record retries
1416
- return yield this.executeLogsSlicesWithConcurrency(apiEndpoint, pathLabel, requestDetails, slices, contractLogsResultsParams, limitOrderParams);
1561
+ return yield this.executeLogsSlicesWithConcurrency(apiEndpoint, pathLabel, requestDetails, slices, contractLogsResultsParams, limitOrderParams, limits);
1417
1562
  });
1418
1563
  }
1419
1564
  /**
@@ -1696,6 +1841,7 @@ MirrorNodeClient.IS_MODULARIZED = 'Is-Modularized';
1696
1841
  */
1697
1842
  MirrorNodeClient.ADD_LABEL_TO_MIRROR_RESPONSE_HISTOGRAM = 'addLabelToMirrorResponseHistogram';
1698
1843
  MirrorNodeClient.ADD_LABEL_TO_MIRROR_ERROR_CODE_COUNTER = 'addLabelToMirrorErrorCodeCounter';
1844
+ MirrorNodeClient.INCREMENT_MIRROR_COUNTER = 'incrementMirrorCounter';
1699
1845
  MirrorNodeClient.EVM_ADDRESS_REGEX = /\/accounts\/([\d.]+)/;
1700
1846
  MirrorNodeClient.mirrorNodeContractResultsPageMax = services_1.ConfigService.get('MIRROR_NODE_CONTRACT_RESULTS_PG_MAX');
1701
1847
  MirrorNodeClient.mirrorNodeContractResultsLogsPageMax = services_1.ConfigService.get('MIRROR_NODE_CONTRACT_RESULTS_LOGS_PG_MAX');
@@ -28,7 +28,7 @@ var CACHE_KEY;
28
28
  CACHE_KEY["RESOLVE_ENTITY_TYPE"] = "resolveEntityType";
29
29
  CACHE_KEY["DEBUG_TRACE_BLOCK_BY_NUMBER"] = "debug_traceBlockByNumber";
30
30
  CACHE_KEY["PRESTATE_TRACER"] = "prestateTracer";
31
- CACHE_KEY["PRESTATE_TRACER_ENDPOINT"] = "prestateTracerEndpoint";
31
+ CACHE_KEY["PRESTATE_ENDPOINT_RESPONSE"] = "prestateEndpointResponse";
32
32
  CACHE_KEY["PRESTATE_ENDPOINT_UNSUPPORTED"] = "prestateEndpointUnsupported";
33
33
  })(CACHE_KEY || (CACHE_KEY = {}));
34
34
  var CACHE_TTL;