Skip to content

Commit

Permalink
chore: fix mirror node client
Browse files Browse the repository at this point in the history
Signed-off-by: nikolay <[email protected]>
  • Loading branch information
natanasow committed Nov 13, 2024
1 parent 3dd36f6 commit bbbfdac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
15 changes: 12 additions & 3 deletions packages/relay/src/lib/clients/mirrorNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
MirrorNodeTransactionRecord,
RequestDetails,
} from '../types';
import { EthImpl } from '../eth';

type REQUEST_METHODS = 'GET' | 'POST';

Expand Down Expand Up @@ -733,6 +734,7 @@ export class MirrorNodeClient {
response != undefined &&
response.transaction_index != undefined &&
response.block_number != undefined &&
response.block_hash != EthImpl.emptyHex &&
response.result === 'SUCCESS'
) {
await this.cacheService.set(
Expand All @@ -749,14 +751,21 @@ export class MirrorNodeClient {

/**
* In some very rare cases the /contracts/results api is called before all the data is saved in
* the mirror node DB and `transaction_index` or `block_number` is returned as `undefined`. A single re-fetch is sufficient to
* resolve this problem.
* the mirror node DB and `transaction_index` or `block_number` is returned as `undefined` or `block_hash` as `0x`.
* A single re-fetch is sufficient to resolve this problem.
* @param {string} transactionIdOrHash - The transaction ID or hash
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
*/
public async getContractResultWithRetry(transactionIdOrHash: string, requestDetails: RequestDetails) {
const contractResult = await this.getContractResult(transactionIdOrHash, requestDetails);
if (contractResult && !(contractResult.transaction_index && contractResult.block_number)) {
if (
contractResult &&
!(
contractResult.transaction_index &&
contractResult.block_number &&
contractResult.block_hash != EthImpl.emptyHex
)
) {
return this.getContractResult(transactionIdOrHash, requestDetails);
}
return contractResult;
Expand Down
33 changes: 32 additions & 1 deletion packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const logger = pino();
const noTransactions = '?transactions=false';
const requestDetails = new RequestDetails({ requestId: getRequestId(), ipAddress: '0.0.0.0' });

describe('MirrorNodeClient', async function () {
describe.only('MirrorNodeClient', async function () {
this.timeout(20000);

const registry = new Registry();
Expand Down Expand Up @@ -625,6 +625,37 @@ describe('MirrorNodeClient', async function () {
expect(mock.history.get.length).to.eq(2); // is called twice
});

it('`getContractResultsWithRetry` by hash retries once because of block_hash equals 0x', async () => {
const hash = '0x2a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb3391';
mock.onGet(`contracts/results/${hash}`).replyOnce(200, { ...detailedContractResult, block_hash: '0x' });
mock.onGet(`contracts/results/${hash}`).reply(200, detailedContractResult);

const result = await mirrorNodeInstance.getContractResultWithRetry(hash, requestDetails);
expect(result).to.exist;
expect(result.block_hash).equal(detailedContractResult.block_hash);
expect(mock.history.get.length).to.eq(2);
});

it('`getContractResultsWithRetry` by hash retries once because of missing transaction_index, block_number and block_hash equals 0x', async () => {
const hash = '0x2a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6393';
mock
.onGet(`contracts/results/${hash}`)
.replyOnce(200, {
...detailedContractResult,
transaction_index: undefined,
block_number: undefined,
block_hash: '0x',
});
mock.onGet(`contracts/results/${hash}`).reply(200, detailedContractResult);

const result = await mirrorNodeInstance.getContractResultWithRetry(hash, requestDetails);
expect(result).to.exist;
expect(result.transaction_index).equal(detailedContractResult.transaction_index);
expect(result.block_number).equal(detailedContractResult.block_number);
expect(result.block_hash).equal(detailedContractResult.block_hash);
expect(mock.history.get.length).to.eq(2);
});

it('`getContractResults` detailed', async () => {
mock
.onGet(`contracts/results?limit=100&order=asc`)
Expand Down

0 comments on commit bbbfdac

Please sign in to comment.