Skip to content

Commit da04575

Browse files
Nana-ECnatanasowAlfredoG87Ivo-Yankovebadiere
authored
Cherry pick 0.18 commits since 0.18.0-alpha1 (#901)
Cherry pick PRs that went into main branch since cutting a 0.18.0-alpha1. PRs map to individual commits - [PR 882 - eth_feeHistory invalid cached response](https://github.com/hashgraph/hedera-json-rpc-relay/pull/882) - [PR 887 - Needed changes for metrics improvements](https://github.com/hashgraph/hedera-json-rpc-relay/pull/887) - [PR 883 - Handle GRPC timeout error](https://github.com/hashgraph/hedera-json-rpc-relay/pull/883) - PR 898 - update-logs-and-metrics Added the requestId to the server context](https://github.com/hashgraph/hedera-json-rpc-relay/pull/898) --------- Signed-off-by: ebadiere <[email protected]> Signed-off-by: Nana Essilfie-Conduah <[email protected]> --------- Signed-off-by: nikolay <[email protected]> Signed-off-by: Nana Essilfie-Conduah <[email protected]> Signed-off-by: Alfredo Gutierrez <[email protected]> Signed-off-by: Ivo Yankov <[email protected]> Signed-off-by: ebadiere <[email protected]> Co-authored-by: Nikolay Atanasow <[email protected]> Co-authored-by: Alfredo Gutierrez <[email protected]> Co-authored-by: Ivo Yankov <[email protected]> Co-authored-by: Eric Badiere <[email protected]>
1 parent 777ee61 commit da04575

File tree

21 files changed

+451
-131
lines changed

21 files changed

+451
-131
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ MIRROR_NODE_LIMIT_PARAM =
2525
CLIENT_TRANSPORT_SECURITY=
2626
INPUT_SIZE_LIMIT=
2727
ETH_CALL_CONSENSUS=
28+
CONSENSUS_MAX_EXECUTION_TIME=
2829
ETH_CALL_CACHE_TTL=

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ MIRROR_NODE_URL=http://127.0.0.1:5551
8181
LOCAL_NODE=true
8282
SERVER_PORT=7546
8383
E2E_RELAY_HOST=http://127.0.0.1:7546
84-
DEFAULT_RATE_LIMIT = 200
85-
TIER_1_RATE_LIMIT = 100
86-
TIER_2_RATE_LIMIT = 200
87-
TIER_3_RATE_LIMIT = 400
84+
DEFAULT_RATE_LIMIT: 200
85+
TIER_1_RATE_LIMIT: 100
86+
TIER_2_RATE_LIMIT: 800
87+
TIER_3_RATE_LIMIT: 1600
8888
LIMIT_DURATION = 60000
8989
HBAR_RATE_LIMIT_TINYBAR = 6000000000
9090
HBAR_RATE_LIMIT_DURATION = 60000
@@ -96,6 +96,7 @@ MIRROR_NODE_RETRY_DELAY = 500
9696
MIRROR_NODE_LIMIT_PARAM = 100
9797
INPUT_SIZE_LIMIT = 1
9898
ETH_CALL_CACHE_TTL = 200
99+
CONSENSUS_MAX_EXECUTION_TIME = 10000
99100
````
100101

101102
Note: Read more about `DEV_MODE` [here](docs/dev-mode.md)

helm-chart/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ data:
3232
MIRROR_NODE_LIMIT_PARAM: {{ .Values.config.MIRROR_NODE_LIMIT_PARAM | quote }}
3333
INPUT_SIZE_LIMIT: {{ .Values.config.INPUT_SIZE_LIMIT | quote }}
3434
ETH_CALL_CACHE_TTL: {{ .Values.config.ETH_CALL_CACHE_TTL | quote }}
35+
CONSENSUS_MAX_EXECUTION_TIME: {{ .Values.config.CONSENSUS_MAX_EXECUTION_TIME | quote }}

helm-chart/value-test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ config:
121121
MIRROR_NODE_LIMIT_PARAM: 100
122122
INPUT_SIZE_LIMIT: 1
123123
ETH_CALL_CACHE_TTL: 200
124+
CONSENSUS_MAX_EXECUTION_TIME: 15000
124125

125126
# Enable rolling_restarts if SDK calls fail this is usually due to stale connections that get cycled on restart
126127
rolling_restart:

helm-chart/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ config:
122122
CLIENT_TRANSPORT_SECURITY: false
123123
INPUT_SIZE_LIMIT: 1
124124
ETH_CALL_CACHE_TTL: 200
125+
CONSENSUS_MAX_EXECUTION_TIME: 15000
125126

126127
# Enable rolling_restarts if SDK calls fail this is usually due to stale connections that get cycled on restart
127128
rolling_restart:

package-lock.json

Lines changed: 158 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export class MirrorNodeClient {
167167
name: metricHistogramName,
168168
help: 'Mirror node response method statusCode latency histogram',
169169
labelNames: ['method', 'statusCode'],
170-
registers: [register]
170+
registers: [register],
171+
buckets: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000] // ms (milliseconds)
171172
});
172173

173174
this.logger.info(`Mirror Node client successfully configured to REST url: ${this.restUrl} and Web3 url: ${this.web3Url} `);
@@ -331,6 +332,21 @@ export class MirrorNodeClient {
331332
requestId);
332333
}
333334

335+
/**
336+
* In some very rare cases the /contracts/results api is called before all the data is saved in
337+
* the mirror node DB and `transaction_index` is returned as `undefined`. A single re-fetch is sufficient to
338+
* resolve this problem.
339+
* @param transactionIdOrHash
340+
* @param requestId
341+
*/
342+
public async getContractResultWithRetry(transactionIdOrHash: string, requestId?: string) {
343+
let contractResult = await this.getContractResult(transactionIdOrHash, requestId);
344+
if (contractResult && typeof contractResult.transaction_index === 'undefined') {
345+
return this.getContractResult(transactionIdOrHash, requestId);
346+
}
347+
return contractResult;
348+
}
349+
334350
public async getContractResults(contractResultsParams?: IContractResultsParams, limitOrderParams?: ILimitOrderParams, requestId?: string) {
335351
const queryParamObject = {};
336352
this.setContractResultsParams(queryParamObject, contractResultsParams);

packages/relay/src/lib/clients/sdkClient.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const _ = require('lodash');
6262
export class SDKClient {
6363
static transactionMode = 'TRANSACTION';
6464
static queryMode = 'QUERY';
65+
static recordMode = 'RECORD';
6566
/**
6667
* The client to use for connecting to the main consensus network. The account
6768
* associated with this client will pay for all operations on the main network.
@@ -95,6 +96,13 @@ export class SDKClient {
9596
// populate with consensusnode requests via SDK
9697
constructor(clientMain: Client, logger: Logger, register: Registry) {
9798
this.clientMain = clientMain;
99+
100+
if (process.env.CONSENSUS_MAX_EXECUTION_TIME) {
101+
// sets the maximum time in ms for the SDK to wait when submitting
102+
// a transaction/query before throwing a TIMEOUT error
103+
this.clientMain = clientMain.setMaxExecutionTime(Number(process.env.CONSENSUS_MAX_EXECUTION_TIME));
104+
}
105+
98106
this.logger = logger;
99107
this.register = register;
100108
this.operatorAccountId = clientMain.operatorAccountId ? clientMain.operatorAccountId.toString() : 'UNKNOWN';
@@ -133,7 +141,7 @@ export class SDKClient {
133141

134142
const duration = parseInt(process.env.HBAR_RATE_LIMIT_DURATION!);
135143
const total = parseInt(process.env.HBAR_RATE_LIMIT_TINYBAR!);
136-
this.hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration);
144+
this.hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, register);
137145
}
138146

139147
async getAccountBalance(account: string, callerName: string, requestId?: string): Promise<AccountBalance> {
@@ -300,7 +308,7 @@ export class SDKClient {
300308
const requestIdPrefix = formatRequestIdMessage(requestId);
301309
const currentDateNow = Date.now();
302310
try {
303-
const shouldLimit = this.hbarLimiter.shouldLimit(currentDateNow);
311+
const shouldLimit = this.hbarLimiter.shouldLimit(currentDateNow, SDKClient.queryMode, callerName);
304312
if (shouldLimit) {
305313
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
306314
}
@@ -348,6 +356,11 @@ export class SDKClient {
348356
if (e instanceof JsonRpcError){
349357
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
350358
}
359+
360+
if (sdkClientError.isGrpcTimeout()) {
361+
throw predefined.REQUEST_TIMEOUT;
362+
}
363+
351364
throw sdkClientError;
352365
}
353366
};
@@ -357,7 +370,7 @@ export class SDKClient {
357370
const requestIdPrefix = formatRequestIdMessage(requestId);
358371
const currentDateNow = Date.now();
359372
try {
360-
const shouldLimit = this.hbarLimiter.shouldLimit(currentDateNow);
373+
const shouldLimit = this.hbarLimiter.shouldLimit(currentDateNow, SDKClient.transactionMode, callerName);
361374
if (shouldLimit) {
362375
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
363376
}
@@ -411,7 +424,7 @@ export class SDKClient {
411424
if (!resp.getRecord) {
412425
throw new SDKClientError({}, `${requestIdPrefix} Invalid response format, expected record availability: ${JSON.stringify(resp)}`);
413426
}
414-
const shouldLimit = this.hbarLimiter.shouldLimit(currentDateNow);
427+
const shouldLimit = this.hbarLimiter.shouldLimit(currentDateNow, SDKClient.recordMode, transactionName);
415428
if (shouldLimit) {
416429
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
417430
}
@@ -476,7 +489,6 @@ export class SDKClient {
476489
status,
477490
caller)
478491
.observe(resolvedCost);
479-
this.operatorAccountGauge.labels(mode, type, this.operatorAccountId).dec(resolvedCost);
480492
};
481493

482494
/**

packages/relay/src/lib/errors/JsonRpcError.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,25 @@ export const predefined = {
162162
message: 'Value below 10_000_000_000 wei which is 1 tinybar'
163163
}),
164164
'INVALID_CONTRACT_ADDRESS': (address) => {
165-
const message = address && address.length ? ` Expected length of 42 chars but was ${address.length}.` : ''
165+
let message = `Invalid Contract Address: ${address}.`;
166+
if (address && address.length) {
167+
message = `${message} Expected length of 42 chars but was ${address.length}.`;
168+
}
169+
166170
return new JsonRpcError({
167171
name: 'Invalid Contract Address',
168172
code: -32012,
169-
message: `Invalid Contract Address: ${address}.${message}`
173+
message: message
170174
})
171-
}
175+
},
176+
'COULD_NOT_ESTIMATE_GAS_PRICE': new JsonRpcError({
177+
name: 'Could not estimate gas price',
178+
code: -32604,
179+
message: 'Error encountered estimating the gas price'
180+
}),
181+
'COULD_NOT_RETRIEVE_LATEST_BLOCK': new JsonRpcError({
182+
name: 'Could not retrieve latest block',
183+
code: -32607,
184+
message: 'Error encountered retrieving latest block'
185+
})
172186
};

packages/relay/src/lib/errors/SDKClientError.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,10 @@ export class SDKClientError extends Error {
5555
public isInsufficientTxFee(): boolean {
5656
return this.statusCode === Status.InsufficientTxFee._code;
5757
}
58+
59+
public isGrpcTimeout(): boolean {
60+
// The SDK uses the same code for Grpc Timeout as INVALID_TRANSACTION_ID
61+
return this.statusCode === Status.InvalidTransactionId._code;
62+
}
5863
}
5964

0 commit comments

Comments
 (0)