Skip to content

Commit

Permalink
fix: handle log null entities more gracefully in getBlock()
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Dec 20, 2024
1 parent 13d09e9 commit aab97f3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
*
*/

import constants from '../../../constants';
import { JsonRpcError, predefined } from '../../../errors/JsonRpcError';
import { ICommonService } from './ICommonService';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import * as _ from 'lodash';
import { Logger } from 'pino';
import { MirrorNodeClient } from '../../../clients';

import { nullableNumberTo0x, numberTo0x, parseNumericEnvVar, toHash32 } from '../../../../formatters';
import { SDKClientError } from '../../../errors/SDKClientError';
import { MirrorNodeClient } from '../../../clients';
import constants from '../../../constants';
import { JsonRpcError, predefined } from '../../../errors/JsonRpcError';
import { MirrorNodeClientError } from '../../../errors/MirrorNodeClientError';
import { SDKClientError } from '../../../errors/SDKClientError';
import { Log } from '../../../model';
import * as _ from 'lodash';
import { CacheService } from '../../cacheService/cacheService';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { RequestDetails } from '../../../types';
import { CacheService } from '../../cacheService/cacheService';
import { ICommonService } from './ICommonService';

/**
* Create a new Common Service implementation.
Expand Down Expand Up @@ -334,7 +335,7 @@ export class CommonService implements ICommonService {
new Log({
address: log.address,
blockHash: toHash32(log.block_hash),
blockNumber: numberTo0x(log.block_number),
blockNumber: nullableNumberTo0x(log.block_number),
data: log.data,
logIndex: nullableNumberTo0x(log.index),
removed: false,
Expand Down
38 changes: 32 additions & 6 deletions packages/relay/tests/lib/eth/eth_getBlockByHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
*/

import { expect, use } from 'chai';
import sinon from 'sinon';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';

import { ASCIIToHex, numberTo0x, prepend0x } from '../../../dist/formatters';
import { predefined } from '../../../src';
import { SDKClient } from '../../../src/lib/clients';
import { EthImpl } from '../../../src/lib/eth';
import { RequestDetails } from '../../../src/lib/types';
import RelayAssertions from '../../assertions';
import {
blockLogsBloom,
defaultContractResults,
defaultDetailedContractResults,
overrideEnvsInMochaDescribe,
} from '../../helpers';
import { SDKClient } from '../../../src/lib/clients';
import RelayAssertions from '../../assertions';
import { ASCIIToHex, numberTo0x, prepend0x } from '../../../dist/formatters';
import {
ACCOUNT_WITHOUT_TRANSACTIONS,
BLOCK_HASH,
Expand All @@ -54,13 +55,13 @@ import {
DEFAULT_BLOCK_RECEIPTS_ROOT_HASH,
DEFAULT_CONTRACT,
DEFAULT_ETH_GET_BLOCK_BY_LOGS,
DEFAULT_LOGS,
DEFAULT_NETWORK_FEES,
LINKS_NEXT_RES,
MOCK_ACCOUNT_WITHOUT_TRANSACTIONS,
NO_SUCH_BLOCK_EXISTS_RES,
} from './eth-config';
import { generateEthTestEnv } from './eth-helpers';
import { RequestDetails } from '../../../src/lib/types';

use(chaiAsPromised);

Expand All @@ -70,7 +71,7 @@ let ethImplLowTransactionCount: EthImpl;

describe('@ethGetBlockByHash using MirrorNode', async function () {
this.timeout(10000);
let { restMock, hapiServiceInstance, ethImpl, cacheService, mirrorNodeInstance, logger, registry } =
const { restMock, hapiServiceInstance, ethImpl, cacheService, mirrorNodeInstance, logger, registry } =
generateEthTestEnv(true);
const results = defaultContractResults.results;
const TOTAL_GAS_USED = numberTo0x(results[0].gas_used + results[1].gas_used);
Expand Down Expand Up @@ -365,4 +366,29 @@ describe('@ethGetBlockByHash using MirrorNode', async function () {
);
});
});

it('eth_getBlockByHash should gracefully handle nulbale entities found in logs', async function () {
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_HASH}`).reply(200, DEFAULT_BLOCK);
restMock.onGet(CONTRACT_RESULTS_WITH_FILTER_URL).reply(200, defaultContractResults);
restMock.onGet('network/fees').reply(200, DEFAULT_NETWORK_FEES);

const nullEntitiedLogs = {
logs: [{ ...DEFAULT_LOGS.logs[0], block_number: null, transaction_index: null, logIndex: null }],
};

restMock.onGet(CONTRACT_RESULTS_LOGS_WITH_FILTER_URL).reply(200, nullEntitiedLogs);

const result = await ethImpl.getBlockByHash(BLOCK_HASH, false, requestDetails);

RelayAssertions.assertBlock(result, {
hash: BLOCK_HASH_TRIMMED,
gasUsed: TOTAL_GAS_USED,
number: BLOCK_NUMBER_HEX,
parentHash: BLOCK_HASH_PREV_TRIMMED,
timestamp: BLOCK_TIMESTAMP_HEX,
transactions: [CONTRACT_HASH_1, CONTRACT_HASH_2],
receiptsRoot: DEFAULT_BLOCK_RECEIPTS_ROOT_HASH,
});
});
});
51 changes: 39 additions & 12 deletions packages/relay/tests/lib/eth/eth_getBlockByNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,30 @@
*
*/

import { fail } from 'assert';
import MockAdapter from 'axios-mock-adapter';
import { expect, use } from 'chai';
import sinon from 'sinon';
import chaiAsPromised from 'chai-as-promised';
import { Logger } from 'pino';
import { Registry } from 'prom-client';
import sinon from 'sinon';

import { ASCIIToHex, hashNumber, numberTo0x, prepend0x } from '../../../dist/formatters';
import { predefined } from '../../../src';
import { MirrorNodeClient, SDKClient } from '../../../src/lib/clients';
import constants from '../../../src/lib/constants';
import { EthImpl } from '../../../src/lib/eth';
import { Block, Transaction } from '../../../src/lib/model';
import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
import { RequestDetails } from '../../../src/lib/types';
import RelayAssertions from '../../assertions';
import {
blockLogsBloom,
defaultContractResults,
defaultDetailedContractResults,
overrideEnvsInMochaDescribe,
} from '../../helpers';
import { Block, Transaction } from '../../../src/lib/model';
import { MirrorNodeClient, SDKClient } from '../../../src/lib/clients';
import RelayAssertions from '../../assertions';
import constants from '../../../src/lib/constants';
import { ASCIIToHex, hashNumber, numberTo0x, prepend0x } from '../../../dist/formatters';
import {
BLOCK_HASH,
BLOCK_HASH_PREV_TRIMMED,
Expand Down Expand Up @@ -63,6 +70,7 @@ import {
DEFAULT_BLOCKS_RES,
DEFAULT_CONTRACT_RES_REVERT,
DEFAULT_ETH_GET_BLOCK_BY_LOGS,
DEFAULT_LOGS,
DEFAULT_NETWORK_FEES,
GAS_USED_1,
GAS_USED_2,
Expand All @@ -76,12 +84,6 @@ import {
NOT_FOUND_RES,
} from './eth-config';
import { generateEthTestEnv } from './eth-helpers';
import { fail } from 'assert';
import { RequestDetails } from '../../../src/lib/types';
import MockAdapter from 'axios-mock-adapter';
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
import { Registry } from 'prom-client';

use(chaiAsPromised);

Expand Down Expand Up @@ -609,4 +611,29 @@ describe('@ethGetBlockByNumber using MirrorNode', async function () {
});
});
});

it('eth_getBlockByNumber should gracefully handle nulbale entities found in logs', async function () {
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_HASH}`).reply(200, DEFAULT_BLOCK);
restMock.onGet(CONTRACT_RESULTS_WITH_FILTER_URL).reply(200, defaultContractResults);
restMock.onGet('network/fees').reply(200, DEFAULT_NETWORK_FEES);

const nullEntitiedLogs = {
logs: [{ ...DEFAULT_LOGS.logs[0], block_number: null, transaction_index: null, logIndex: null }],
};

restMock.onGet(CONTRACT_RESULTS_LOGS_WITH_FILTER_URL).reply(200, nullEntitiedLogs);

const result = await ethImpl.getBlockByNumber(BLOCK_HASH, false, requestDetails);

RelayAssertions.assertBlock(result, {
hash: BLOCK_HASH_TRIMMED,
gasUsed: TOTAL_GAS_USED,
number: BLOCK_NUMBER_HEX,
parentHash: BLOCK_HASH_PREV_TRIMMED,
timestamp: BLOCK_TIMESTAMP_HEX,
transactions: [CONTRACT_HASH_1, CONTRACT_HASH_2],
receiptsRoot: DEFAULT_BLOCK_RECEIPTS_ROOT_HASH,
});
});
});

0 comments on commit aab97f3

Please sign in to comment.