Skip to content

Commit af6d48f

Browse files
chore: cherry picked #3254 to release/0.56 (#3255)
fix: `eth_getBlockByHash` is using invalid cache record (#3254) * chore: fix mirror node client * chore: remove .only --------- Signed-off-by: nikolay <n.atanasow94@gmail.com> Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com> Co-authored-by: Nikolay Atanasow <n.atanasow94@gmail.com>
1 parent 5155a8e commit af6d48f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
MirrorNodeTransactionRecord,
4646
RequestDetails,
4747
} from '../types';
48+
import { EthImpl } from '../eth';
4849

4950
type REQUEST_METHODS = 'GET' | 'POST';
5051

@@ -733,6 +734,7 @@ export class MirrorNodeClient {
733734
response != undefined &&
734735
response.transaction_index != undefined &&
735736
response.block_number != undefined &&
737+
response.block_hash != EthImpl.emptyHex &&
736738
response.result === 'SUCCESS'
737739
) {
738740
await this.cacheService.set(
@@ -749,14 +751,21 @@ export class MirrorNodeClient {
749751

750752
/**
751753
* In some very rare cases the /contracts/results api is called before all the data is saved in
752-
* the mirror node DB and `transaction_index` or `block_number` is returned as `undefined`. A single re-fetch is sufficient to
753-
* resolve this problem.
754+
* the mirror node DB and `transaction_index` or `block_number` is returned as `undefined` or `block_hash` as `0x`.
755+
* A single re-fetch is sufficient to resolve this problem.
754756
* @param {string} transactionIdOrHash - The transaction ID or hash
755757
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
756758
*/
757759
public async getContractResultWithRetry(transactionIdOrHash: string, requestDetails: RequestDetails) {
758760
const contractResult = await this.getContractResult(transactionIdOrHash, requestDetails);
759-
if (contractResult && !(contractResult.transaction_index && contractResult.block_number)) {
761+
if (
762+
contractResult &&
763+
!(
764+
contractResult.transaction_index &&
765+
contractResult.block_number &&
766+
contractResult.block_hash != EthImpl.emptyHex
767+
)
768+
) {
760769
return this.getContractResult(transactionIdOrHash, requestDetails);
761770
}
762771
return contractResult;

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,35 @@ describe('MirrorNodeClient', async function () {
625625
expect(mock.history.get.length).to.eq(2); // is called twice
626626
});
627627

628+
it('`getContractResultsWithRetry` by hash retries once because of block_hash equals 0x', async () => {
629+
const hash = '0x2a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb3391';
630+
mock.onGet(`contracts/results/${hash}`).replyOnce(200, { ...detailedContractResult, block_hash: '0x' });
631+
mock.onGet(`contracts/results/${hash}`).reply(200, detailedContractResult);
632+
633+
const result = await mirrorNodeInstance.getContractResultWithRetry(hash, requestDetails);
634+
expect(result).to.exist;
635+
expect(result.block_hash).equal(detailedContractResult.block_hash);
636+
expect(mock.history.get.length).to.eq(2);
637+
});
638+
639+
it('`getContractResultsWithRetry` by hash retries once because of missing transaction_index, block_number and block_hash equals 0x', async () => {
640+
const hash = '0x2a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6393';
641+
mock.onGet(`contracts/results/${hash}`).replyOnce(200, {
642+
...detailedContractResult,
643+
transaction_index: undefined,
644+
block_number: undefined,
645+
block_hash: '0x',
646+
});
647+
mock.onGet(`contracts/results/${hash}`).reply(200, detailedContractResult);
648+
649+
const result = await mirrorNodeInstance.getContractResultWithRetry(hash, requestDetails);
650+
expect(result).to.exist;
651+
expect(result.transaction_index).equal(detailedContractResult.transaction_index);
652+
expect(result.block_number).equal(detailedContractResult.block_number);
653+
expect(result.block_hash).equal(detailedContractResult.block_hash);
654+
expect(mock.history.get.length).to.eq(2);
655+
});
656+
628657
it('`getContractResults` detailed', async () => {
629658
mock
630659
.onGet(`contracts/results?limit=100&order=asc`)

0 commit comments

Comments
 (0)