Skip to content

Commit 716f565

Browse files
Fix constants. Set effectiveGasPrice to 0 if null (#581)
* Fix constants. Set effectiveGasPrice to 0 if null Combined two small fixes into a single PR: Fixes ETH_GET_LOGS_BLOCK_RANGE_LIMIT and FEE_HISTORY_MAX_RESULTS env variables being undefined at module initialization Fixes effectiveGasPrice having 0xNaN as value for non-eth transactions in eth_getTransactionReceipt responce Signed-off-by: Maksim Dimitrov <[email protected]>
1 parent c9cd5a7 commit 716f565

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

packages/relay/src/lib/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export default {
3838

3939
DEFAULT_TINY_BAR_GAS: 72, // (853454 / 1000) * (1 / 12)
4040
ETH_FUNCTIONALITY_CODE: 84,
41-
ETH_GET_LOGS_BLOCK_RANGE_LIMIT: Number(process.env.ETH_GET_LOGS_BLOCK_RANGE_LIMIT || 500),
41+
DEFAULT_ETH_GET_LOGS_BLOCK_RANGE_LIMIT: 500,
4242
EXCHANGE_RATE_FILE_ID: "0.0.112",
4343
FEE_SCHEDULE_FILE_ID: '0.0.111',
4444

4545
TYPE_CONTRACT: 'contract',
4646
TYPE_ACCOUNT: 'account',
4747

48-
FEE_HISTORY_MAX_RESULTS: Number(process.env.FEE_HISTORY_MAX_RESULTS || 10),
48+
DEFAULT_FEE_HISTORY_MAX_RESULTS: 10,
4949
ORDER,
5050

5151
BLOCK_GAS_LIMIT: 15_000_000,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*
1919
*/
2020

21-
import constants from "../constants";
22-
2321
const REQUEST_ID_STRING = `Request ID: `;
2422
export class JsonRpcError {
2523
public code: number;
@@ -119,10 +117,10 @@ export const predefined = {
119117
code: -32001,
120118
message: `Requested resource not found. ${message}`
121119
}),
122-
'RANGE_TOO_LARGE': new JsonRpcError({
120+
'RANGE_TOO_LARGE': (blockRange: number) => new JsonRpcError({
123121
name: 'Block range too large',
124122
code: -32000,
125-
message: `Exceeded maximum block range: ${constants.ETH_GET_LOGS_BLOCK_RANGE_LIMIT}`
123+
message: `Exceeded maximum block range: ${blockRange}`
126124
}),
127125
'IP_RATE_LIMIT_EXCEEDED': new JsonRpcError({
128126
name: 'IP Rate limit exceeded',

packages/relay/src/lib/eth.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ export class EthImpl implements Eth {
140140
* Gets the fee history.
141141
*/
142142
async feeHistory(blockCount: number, newestBlock: string, rewardPercentiles: Array<number> | null, requestId?: string) {
143+
const maxResults = Number(process.env.FEE_HISTORY_MAX_RESULTS) || constants.DEFAULT_FEE_HISTORY_MAX_RESULTS;
144+
143145
const requestIdPrefix = formatRequestIdMessage(requestId);
146+
144147
this.logger.trace(`${requestIdPrefix} feeHistory(blockCount=${blockCount}, newestBlock=${newestBlock}, rewardPercentiles=${rewardPercentiles})`);
148+
145149
try {
146150
const latestBlockNumber = await this.translateBlockTag(EthImpl.blockLatest, requestId);
147151
const newestBlockNumber = await this.translateBlockTag(newestBlock, requestId);
@@ -150,7 +154,7 @@ export class EthImpl implements Eth {
150154
return predefined.REQUEST_BEYOND_HEAD_BLOCK(newestBlockNumber, latestBlockNumber);
151155
}
152156

153-
blockCount = blockCount > constants.FEE_HISTORY_MAX_RESULTS ? constants.FEE_HISTORY_MAX_RESULTS : blockCount;
157+
blockCount = blockCount > maxResults ? maxResults : blockCount;
154158

155159
if (blockCount <= 0) {
156160
return EthImpl.feeHistoryZeroBlockCountResponse;
@@ -752,7 +756,7 @@ export class EthImpl implements Eth {
752756
return e;
753757
}
754758
return predefined.INTERNAL_ERROR;
755-
}
759+
}
756760
}
757761

758762
/**
@@ -901,7 +905,7 @@ export class EthImpl implements Eth {
901905
input: contractResult.function_parameters,
902906
maxPriorityFeePerGas: maxPriorityFee,
903907
maxFeePerGas: maxFee,
904-
nonce: EthImpl.nonceNumberTo0x(contractResult.nonce),
908+
nonce: EthImpl.nanOrNumberTo0x(contractResult.nonce),
905909
r: rSig,
906910
s: sSig,
907911
to: contractResult.to?.substring(0, 42),
@@ -963,7 +967,7 @@ export class EthImpl implements Eth {
963967
logsBloom: receiptResponse.bloom,
964968
transactionHash: EthImpl.toHash32(receiptResponse.hash),
965969
transactionIndex: EthImpl.numberTo0x(receiptResponse.transaction_index),
966-
effectiveGasPrice: EthImpl.numberTo0x(Number.parseInt(effectiveGas) * 10_000_000_000),
970+
effectiveGasPrice: EthImpl.nanOrNumberTo0x(Number.parseInt(effectiveGas) * 10_000_000_000),
967971
root: receiptResponse.root,
968972
status: receiptResponse.status,
969973
};
@@ -991,8 +995,11 @@ export class EthImpl implements Eth {
991995
return input === null ? null : EthImpl.numberTo0x(input);
992996
}
993997

994-
static nonceNumberTo0x(input: number | BigNumber): string {
995-
return input === null ? EthImpl.numberTo0x(0) : EthImpl.numberTo0x(input);
998+
static nanOrNumberTo0x(input: number | BigNumber): string {
999+
// input == null assures to check against both null and undefined.
1000+
// A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X.
1001+
// The result will be true if and only if X is a NaN.
1002+
return input == null || input !== input ? EthImpl.numberTo0x(0) : EthImpl.numberTo0x(input);
9961003
}
9971004

9981005
static toHash32(value: string): string {
@@ -1180,7 +1187,7 @@ export class EthImpl implements Eth {
11801187
input: contractResultDetails.function_parameters,
11811188
maxPriorityFeePerGas: EthImpl.toNullIfEmptyHex(contractResultDetails.max_priority_fee_per_gas),
11821189
maxFeePerGas: EthImpl.toNullIfEmptyHex(contractResultDetails.max_fee_per_gas),
1183-
nonce: EthImpl.nonceNumberTo0x(contractResultDetails.nonce),
1190+
nonce: EthImpl.nanOrNumberTo0x(contractResultDetails.nonce),
11841191
r: rSig,
11851192
s: sSig,
11861193
to: contractResultDetails.to.substring(0, 42),
@@ -1219,6 +1226,7 @@ export class EthImpl implements Eth {
12191226
throw e;
12201227
}
12211228
} else if (fromBlock || toBlock) {
1229+
const blockRangeLimit = Number(process.env.ETH_GET_LOGS_BLOCK_RANGE_LIMIT) || constants.DEFAULT_ETH_GET_LOGS_BLOCK_RANGE_LIMIT;
12221230
let fromBlockTimestamp;
12231231
let toBlockTimestamp;
12241232
let fromBlockNum = 0;
@@ -1272,8 +1280,8 @@ export class EthImpl implements Eth {
12721280

12731281
if (fromBlockNum > toBlockNum) {
12741282
return [];
1275-
} else if((toBlockNum - fromBlockNum) > constants.ETH_GET_LOGS_BLOCK_RANGE_LIMIT) {
1276-
throw predefined.RANGE_TOO_LARGE;
1283+
} else if((toBlockNum - fromBlockNum) > blockRangeLimit) {
1284+
throw predefined.RANGE_TOO_LARGE(blockRangeLimit);
12771285
}
12781286
}
12791287

packages/relay/tests/lib/eth.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { predefined } from '../../src/lib/errors/JsonRpcError';
3232
import { EthImpl } from '../../src/lib/eth';
3333
import { MirrorNodeClient } from '../../src/lib/clients/mirrorNodeClient';
3434
import {
35+
defaultContractResults,
3536
defaultEvmAddress,
3637
defaultFromLongZeroAddress,
3738
expectUnsupportedMethod,
@@ -42,6 +43,7 @@ import { Block, Transaction } from '../../src/lib/model';
4243
import constants from '../../src/lib/constants';
4344
import { SDKClient } from '../../src/lib/clients';
4445
import { SDKClientError } from '../../src/lib/errors/SDKClientError';
46+
import { isTypedArray } from 'util/types';
4547

4648
const logger = pino();
4749
const registry = new Registry();
@@ -995,7 +997,7 @@ describe('Eth calls using MirrorNode', async function () {
995997
messages: [{ message: 'Not found' }]
996998
}
997999
});
998-
1000+
9991001
const resCached = await ethImpl.getBalance(contractAddress1, null);
10001002
expect(resNoCache).to.equal(defHexBalance);
10011003
expect(resCached).to.equal(EthImpl.zeroHex);
@@ -1459,7 +1461,7 @@ describe('Eth calls using MirrorNode', async function () {
14591461
});
14601462

14611463
it('eth_feeHistory with max results', async function () {
1462-
const maxResultsCap = Number(constants.FEE_HISTORY_MAX_RESULTS);
1464+
const maxResultsCap = Number(constants.DEFAULT_FEE_HISTORY_MAX_RESULTS);
14631465

14641466
mock.onGet('blocks?limit=1&order=desc').reply(200, {blocks: [{...defaultBlock, number: 10}]});
14651467
mock.onGet(`network/fees?timestamp=lte:${defaultBlock.timestamp.to}`).reply(200, defaultNetworkFees);
@@ -2070,6 +2072,22 @@ describe('Eth', async function () {
20702072
expect(receipt.status).to.eq(defaultReceipt.status);
20712073
expect(receipt.effectiveGasPrice).to.eq(defaultReceipt.effectiveGasPrice);
20722074
});
2075+
2076+
it("Handles null effectiveGasPrice", async function() {
2077+
const contractResult = {
2078+
...defaultDetailedContractResultByHash,
2079+
gas_price: null,
2080+
max_fee_per_gas: null
2081+
};
2082+
2083+
mock.onGet(`contracts/results/${defaultTxHash}`).reply(200, contractResult);
2084+
const receipt = await ethImpl.getTransactionReceipt(defaultTxHash);
2085+
2086+
expect(receipt).to.exist;
2087+
if (receipt == null) return;
2088+
2089+
expect(receipt.effectiveGasPrice).to.eq('0x0');
2090+
});
20732091
});
20742092

20752093
describe('eth_getTransactionByHash', async function () {

0 commit comments

Comments
 (0)