Skip to content

feat: optimise tx processing#240

Open
losh11 wants to merge 1 commit intomasterfrom
losh11/txoptimisation
Open

feat: optimise tx processing#240
losh11 wants to merge 1 commit intomasterfrom
losh11/txoptimisation

Conversation

@losh11
Copy link
Copy Markdown
Member

@losh11 losh11 commented Sep 17, 2025

No description provided.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @losh11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant optimization to the transaction processing logic, specifically for 'convert' transactions. It refines how these transactions are handled by implementing a pre-filtering mechanism that identifies and leverages already cached transaction data. This change aims to reduce redundant processing, improve performance, and ensure that only truly new or uncached convert transactions undergo the full processing pipeline, ultimately leading to a more responsive user experience.

Highlights

  • Optimized Transaction Processing: Implemented a significant optimization for processing 'convert' transactions by introducing a pre-filtering mechanism to identify and skip already cached transactions, thereby reducing redundant computations.
  • Enhanced Transaction Matching: Improved the logic for matching 'convert' transactions with existing LND transactions by incorporating multiple validation criteria, including destination address, selected outpoints, amount proximity, and timestamp proximity.
  • Reduced processConvertTransactions Calls: The processConvertTransactions function is now called only for uncached 'convert' transactions, leading to more efficient resource utilization and faster transaction retrieval.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable optimization for transaction processing by pre-filtering converted transactions that are already cached, which avoids redundant processing and API calls. The implementation is sound, but I've identified a few areas for improvement related to code duplication, performance, and code style that would enhance maintainability and readability.

Comment on lines +520 to +548
const mainTx = lndTransactions.transactions.find(tx => {
const hasDestinationAddress = tx.outputDetails?.some(
output => output.address === convertTx.destinationAddress,
);

if (!hasDestinationAddress) {
return false;
}

// Additional validation: check if transaction uses selected outpoints
const selectedOutpointSet = new Set(
convertTx.selectedOutpoints || [],
);
const usesSelectedOutpoints = tx.previousOutpoints?.some(
prevOutpoint =>
selectedOutpointSet.has(prevOutpoint.outpoint || ''),
);

// Additional validation: check amount proximity to target amount
const amountMatches =
Math.abs(Number(tx.amount) - Number(convertTx.targetAmount)) <
Math.max(1000, Number(convertTx.targetAmount) * 0.01);

// Add processed convert transactions to the txs array
processedTransactions.forEach(processedTx => {
// NOTE: skip processing if tx was cached before
// TODO: move the skipping to the processConvertTransactions
// when it's finished and stable to avoid getPriceOnDate calls
// Additional validation: check timestamp proximity (within 1 hour)
const timestampMatches =
Math.abs(Number(tx.timeStamp) - convertTx.timestamp) < 3600;

return usesSelectedOutpoints || (amountMatches && timestampMatches);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The logic to find the mainTx is nearly identical to the logic within processConvertTransactions. This code duplication can lead to maintenance issues, as changes would need to be made in two places. It would be best to extract this matching logic into a shared utility function that can be called from both getTransactions and processConvertTransactions.

Comment on lines +530 to +532
const selectedOutpointSet = new Set(
convertTx.selectedOutpoints || [],
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The selectedOutpointSet is being re-created on every iteration of the lndTransactions.transactions.find() callback. Since this set is constant for a given convertTx, it's inefficient to rebuild it repeatedly. For better performance, you should create this Set once, before the .find() method is called.

Comment on lines +512 to +516
const cachedConvertTransactions: {
convertTx: IConvertedTx;
cachedTx: IDecodedTx;
mainTx: any;
}[] = [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The mainTx property is typed as any, which undermines TypeScript's type safety. You can provide a more specific type by inferring it from lndTransactions.transactions. This improves code clarity and allows for better static analysis.

Suggested change
const cachedConvertTransactions: {
convertTx: IConvertedTx;
cachedTx: IDecodedTx;
mainTx: any;
}[] = [];
const cachedConvertTransactions: {
convertTx: IConvertedTx;
cachedTx: IDecodedTx;
mainTx: (typeof lndTransactions.transactions)[0];
}[] = [];

Comment on lines +539 to +545
const amountMatches =
Math.abs(Number(tx.amount) - Number(convertTx.targetAmount)) <
Math.max(1000, Number(convertTx.targetAmount) * 0.01);

// Add processed convert transactions to the txs array
processedTransactions.forEach(processedTx => {
// NOTE: skip processing if tx was cached before
// TODO: move the skipping to the processConvertTransactions
// when it's finished and stable to avoid getPriceOnDate calls
// Additional validation: check timestamp proximity (within 1 hour)
const timestampMatches =
Math.abs(Number(tx.timeStamp) - convertTx.timestamp) < 3600;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This code uses several magic numbers (1000, 0.01, 3600) for proximity checks. This makes the logic less clear. It's a good practice to extract these values into named constants (e.g., AMOUNT_TOLERANCE_SATS, AMOUNT_TOLERANCE_PERCENTAGE, TIMESTAMP_TOLERANCE_SECONDS) to improve readability and maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant