-
Notifications
You must be signed in to change notification settings - Fork 5
feat: optimise tx processing #240
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -507,29 +507,80 @@ export const getTransactions = (): AppThunk => async (dispatch, getState) => { | |
|
|
||
| // Process convert transactions using the extracted utility function | ||
| if (convertedTransactions && convertedTransactions.length > 0) { | ||
| const {processedTransactions, processedTxHashes} = | ||
| await processConvertTransactions( | ||
| convertedTransactions, | ||
| lndTransactions, | ||
| (timestamp: number) => getPriceOnDate(timestamp, torEnabled), | ||
| ); | ||
| // Pre-filter to only process uncached converted transactions | ||
| const uncachedConvertedTransactions: IConvertedTx[] = []; | ||
| const cachedConvertTransactions: { | ||
| convertTx: IConvertedTx; | ||
| cachedTx: IDecodedTx; | ||
| mainTx: any; | ||
| }[] = []; | ||
|
|
||
| for (const convertTx of convertedTransactions) { | ||
| // Find the main transaction for this convert operation | ||
| 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 || [], | ||
| ); | ||
|
Comment on lines
+530
to
+532
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| 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; | ||
|
Comment on lines
+539
to
+545
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code uses several magic numbers ( |
||
|
|
||
| return usesSelectedOutpoints || (amountMatches && timestampMatches); | ||
| }); | ||
|
Comment on lines
+520
to
+548
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic to find the |
||
|
|
||
| if (!mainTx?.txHash) { | ||
| // Can't find main transaction, include for processing | ||
| uncachedConvertedTransactions.push(convertTx); | ||
| continue; | ||
| } | ||
|
|
||
| // Check if already cached | ||
| const cachedTx = checkTxCache( | ||
| cachedTxHashSet, | ||
| transactionsByHash, | ||
| processedTx.txHash, | ||
| mainTx.txHash, | ||
| ); | ||
|
|
||
| if (cachedTx) { | ||
| txs.push({ | ||
| ...cachedTx, | ||
| numConfirmations: processedTx.numConfirmations, | ||
| }); | ||
| // Transaction is cached, store for later processing | ||
| cachedConvertTransactions.push({convertTx, cachedTx, mainTx}); | ||
| processedConvertTxHashes.add(mainTx.txHash); | ||
| } else { | ||
| // Not cached, needs processing | ||
| uncachedConvertedTransactions.push(convertTx); | ||
| } | ||
| } | ||
|
|
||
| // Process only uncached converted transactions | ||
| if (uncachedConvertedTransactions.length > 0) { | ||
| const {processedTransactions, processedTxHashes} = | ||
| await processConvertTransactions( | ||
| uncachedConvertedTransactions, | ||
| lndTransactions, | ||
| (timestamp: number) => getPriceOnDate(timestamp, torEnabled), | ||
| ); | ||
|
|
||
| // Add newly processed convert transactions to the txs array | ||
| processedTransactions.forEach(processedTx => { | ||
| const decodedTx: IDecodedTx = { | ||
| txHash: processedTx.txHash, | ||
| blockHash: processedTx.blockHash, | ||
|
|
@@ -547,10 +598,19 @@ export const getTransactions = (): AppThunk => async (dispatch, getState) => { | |
| }; | ||
| txs.push(decodedTx); | ||
| cachedTxHashesBuf.push(decodedTx.txHash); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Merge the processed transaction hashes | ||
| processedTxHashes.forEach(hash => processedConvertTxHashes.add(hash)); | ||
| } | ||
|
|
||
| processedConvertTxHashes = processedTxHashes; | ||
| // Add cached convert transactions to the txs array | ||
| cachedConvertTransactions.forEach(({cachedTx, mainTx}) => { | ||
| txs.push({ | ||
| ...cachedTx, | ||
| numConfirmations: mainTx.numConfirmations, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // Compare nexus-api txs with lnd txs to append missing ones in lnd | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
mainTxproperty is typed asany, which undermines TypeScript's type safety. You can provide a more specific type by inferring it fromlndTransactions.transactions. This improves code clarity and allows for better static analysis.