Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replaced getRecord network calls with calls to mirror node to avoid SDK costs #2737

Conversation

quiet-node
Copy link
Member

Description:

  • Added an instance of Mirror Node client to the SDKClient class.

  • Added the retrieveAndCaptureChargedTxFee() method to retrieve charged transaction fees by making repeated calls to the Mirror Node using the appropriate transactionId. This method is intended to replace the getRecord() methods called by the SDK to reduce costs.

  • Refactored the createFile() and deleteFile() method to implement the new retrieveAndCaptureChargedTxFee logic.

Related issue(s):

Fixes #2706

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

@quiet-node quiet-node added the enhancement New feature or request label Jul 22, 2024
@quiet-node quiet-node added this to the 0.53.0 milestone Jul 22, 2024
@quiet-node quiet-node self-assigned this Jul 22, 2024
Copy link

sonarcloud bot commented Jul 22, 2024

Copy link

github-actions bot commented Jul 22, 2024

Acceptance Tests

     25 files     285 suites   51m 36s ⏱️
   609 tests    599 ✔️ 4 💤   6
1 212 runs  1 192 ✔️ 4 💤 16

Results for commit e8b58f6.

♻️ This comment has been updated with latest results.

Comment on lines +137 to +144
this.mirrorNodeClient = new MirrorNodeClient(
process.env.MIRROR_NODE_URL || '',
logger.child({ name: `mirror-node` }),
register,
this.cacheService,
undefined,
process.env.MIRROR_NODE_URL_WEB3 || process.env.MIRROR_NODE_URL || '',
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, let's inject this MirrorNodeClient instead of instantiating it here, I see no point in having multiple copies of this client in the heap space. Same for other places where MirrorNodeClient is used, we should inject the same object instance for those classes.

In general, it's a good practice to apply dependency injection to make each class independent of its dependencies and have a loosely coupled program.

@@ -98,9 +100,22 @@ export class SDKClient {
private operatorAccountGauge;
private maxChunks;
private fileAppendChunkSize;
private mirrorNodeClient;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private mirrorNodeClient;
private readonly mirrorNodeClient: MirrorNodeClient;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for other lines above (98-102):

  • please add types to those variables
  • change them to readonly if we never reassign them anywhere in the class

}

// Ensure that the calldata file is not empty
if (fileId) {
const fileSize = await (await new FileInfoQuery().setFileId(fileId).execute(client)).size;
// @todo: figure out the relationship between query.getCost() with the actual HBAR operator get charged as they are different => capture in metrics
const fileSize = (await new FileInfoQuery().setFileId(fileId).execute(client)).size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: Why not use await this.executeQuery here?

.setFileId(fileId)
.setMaxTransactionFee(new Hbar(2))
.freezeWith(this.clientMain);

// execute fileDeleteTx
const fileDeleteTxResponse = await fileDeleteTx.execute(this.clientMain);
await fileDeleteTx.execute(this.clientMain);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - don't we want to use executeTransaction here and also for all other transactions executed inside SdkClient?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also add a method executeAllTransactions and reuse it for cases where we want to call executeAll.

In this way we can only define this try/catch/finally logic only in one place and we don't have to duplicate it everywhere, just have to make sure all transactions and queries are using executeTransaction/executeAllTransactions and executeQuery.

Comment on lines +105 to +108
private readonly MirrorNodeRetries = parseNumericEnvVar(
'MIRROR_NODE_GET_CONTRACT_RESULTS_RETRIES',
'MIRROR_NODE_GET_CONTRACT_RESULTS_DEFAULT_RETRIES',
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like something that's a responsibility of the MirrorNodeClient, let's move it there.

@quiet-node quiet-node marked this pull request as draft July 23, 2024 14:17
@quiet-node quiet-node closed this Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace getRecord network calls with calls to Mirror node
2 participants