Skip to content

Commit 51ff96b

Browse files
authored
Optimise requests to mirror node to speed up relay (#788)
* Edit mirror node client Signed-off-by: nikolay <[email protected]> * Fix promises Signed-off-by: nikolay <[email protected]> * Edit promises Signed-off-by: nikolay <[email protected]> * Fix tests Signed-off-by: nikolay <[email protected]> * Edit dapp test Signed-off-by: nikolay <[email protected]> * Bump hedera-local version * Resolve conflicts Signed-off-by: nikolay <[email protected]> * Edit default gas price Signed-off-by: nikolay <[email protected]> * Remove metamask reset Signed-off-by: nikolay <[email protected]> * Edit test Signed-off-by: nikolay <[email protected]> Signed-off-by: nikolay <[email protected]>
1 parent 32ee497 commit 51ff96b

File tree

7 files changed

+15085
-23778
lines changed

7 files changed

+15085
-23778
lines changed

dapp-example/tests/e2e/index.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ describe('Test Core Hedera User Scenarios', function() {
5858
}).timeout(180000);
5959

6060
it('Transfer HBARs', function() {
61-
cy.resetMetamaskAccount();
62-
63-
cy.get('#sendHbarsToField').type('0x54C51b7637BF6fE9709e1e0EBc8b2Ca6a24b0f0A');
61+
cy.get('#sendHbarsToField').clear().type('0x54C51b7637BF6fE9709e1e0EBc8b2Ca6a24b0f0A');
6462
cy.get('#sendHbarsAmountField').clear().type('10000000000000000').trigger('change');
6563
cy.get('#sendHbarsBtn').should('not.be.disabled').click();
6664
cy.confirmMetamaskTransaction();

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"check:node": "ts-node packages/server/tests/helpers/nodeCheck.ts"
4747
},
4848
"dependencies": {
49-
"@hashgraph/hedera-local": "^2.1.3",
49+
"@hashgraph/hedera-local": "^2.4.1",
5050
"@open-rpc/schema-utils-js": "^1.16.1",
5151
"@types/find-config": "^1.0.1",
5252
"keyv-file": "^0.2.0",

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -503,30 +503,57 @@ export class MirrorNodeClient {
503503
}
504504
}
505505

506-
public async resolveEntityType(entityIdentifier: string, requestId?: string) {
507-
const contractResult = await this.getContract(entityIdentifier, requestId);
508-
if (contractResult) {
509-
return {
510-
type: constants.TYPE_CONTRACT,
511-
entity: contractResult
512-
};
506+
public async resolveEntityType(
507+
entityIdentifier: string,
508+
requestId?: string,
509+
searchableTypes: any[] = [constants.TYPE_CONTRACT, constants.TYPE_ACCOUNT, constants.TYPE_TOKEN]
510+
) {
511+
const buildPromise = (fn) => new Promise((resolve, reject) => fn.then((values) => {
512+
if (values == null) reject();
513+
resolve(values);
514+
}));
515+
516+
if (searchableTypes.find(t => t === constants.TYPE_CONTRACT)) {
517+
const contract = await this.getContract(entityIdentifier, requestId);
518+
if (contract) {
519+
return {
520+
type: constants.TYPE_CONTRACT,
521+
entity: contract
522+
};
523+
}
513524
}
514-
const accountResult = await this.getAccount(entityIdentifier, requestId);
515-
if (accountResult) {
516-
return {
517-
type: constants.TYPE_ACCOUNT,
518-
entity: accountResult
519-
};
525+
526+
let data;
527+
try {
528+
const promises = [
529+
searchableTypes.find(t => t === constants.TYPE_ACCOUNT) ? buildPromise(this.getAccount(entityIdentifier, requestId)) : Promise.reject(),
530+
searchableTypes.find(t => t === constants.TYPE_TOKEN) ? buildPromise(this.getTokenById(`0.0.${parseInt(entityIdentifier, 16)}`, requestId)) : Promise.reject()
531+
];
532+
// maps the promises with indices of the promises array
533+
// because there is no such method as Promise.anyWithIndex in js
534+
// the index is needed afterward for detecting the resolved promise type (contract, account, or token)
535+
// @ts-ignore
536+
data = await Promise.any(promises.map((promise, index) => promise.then(value => ({ value, index }))));
537+
} catch (e) {
538+
return null;
520539
}
521-
const tokenResult = await this.getTokenById(`0.0.${parseInt(entityIdentifier, 16)}`, requestId);
522-
if (tokenResult) {
523-
return {
524-
type: constants.TYPE_TOKEN,
525-
entity: tokenResult
540+
541+
let type;
542+
switch (data.index) {
543+
case 0: {
544+
type = constants.TYPE_ACCOUNT;
545+
break;
546+
}
547+
case 1: {
548+
type = constants.TYPE_TOKEN;
549+
break;
526550
}
527551
}
528552

529-
return null;
553+
return {
554+
type,
555+
entity: data.value
556+
};
530557
}
531558

532559
//exposing mirror node instance for tests

packages/relay/src/lib/eth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ export class EthImpl implements Eth {
714714
}
715715

716716
try {
717-
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId);
717+
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId, [constants.TYPE_CONTRACT, constants.TYPE_TOKEN]);
718718
if (result) {
719719
if (result?.type === constants.TYPE_TOKEN) {
720720
this.logger.trace(`${requestIdPrefix} Token redirect case, return redirectBytecode`);
@@ -891,7 +891,7 @@ export class EthImpl implements Eth {
891891

892892
// check consensus node as back up
893893
try {
894-
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId);
894+
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId, [constants.TYPE_ACCOUNT, constants.TYPE_CONTRACT]);
895895
if (result?.type === constants.TYPE_ACCOUNT) {
896896
const accountInfo = await this.sdkClient.getAccountInfo(result?.entity.account, EthImpl.ethGetTransactionCount, requestId);
897897
return EthImpl.numberTo0x(Number(accountInfo.ethereumNonce));

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,15 @@ describe("Open RPC Specification", function () {
261261
validateResponseSchema(methodsResponseSchema.eth_getBlockTransactionCountByNumber, response);
262262
});
263263

264-
it('should execute "eth_getCode" with block tag', async function () {
264+
it('should execute "eth_getCode" with block tag', async function() {
265+
mock.onGet(`tokens/${defaultContractResults.results[0].contract_id}`).reply(404);
265266
const response = await ethImpl.getCode(contractAddress1, 'latest');
266267

267268
validateResponseSchema(methodsResponseSchema.eth_getCode, response);
268269
});
269270

270-
it('should execute "eth_getCode" with block number', async function () {
271+
it('should execute "eth_getCode" with block number', async function() {
272+
mock.onGet(`tokens/${defaultContractResults.results[0].contract_id}`).reply(404);
271273
const response = await ethImpl.getCode(contractAddress1, '0x3');
272274

273275
validateResponseSchema(methodsResponseSchema.eth_getCode, response);

packages/server/tests/helpers/assertions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class Assertions {
2929
static emptyArrayHex = '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347';
3030
static emptyBloom = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
3131
static ethEmptyTrie = '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421';
32-
static defaultGasPrice = 720_000_000_000;
32+
static defaultGasPrice = 710_000_000_000;
3333
static datedGasPrice = 570_000_000_000;
3434
static updatedGasPrice = 640_000_000_000;
3535
static maxBlockGasLimit = 15_000_000;

0 commit comments

Comments
 (0)