From cb3dd6a406e6fd99e4da54c21d919e563d78efdf Mon Sep 17 00:00:00 2001 From: argonmining Date: Fri, 16 Aug 2024 00:35:48 -0500 Subject: [PATCH] Enhance Transaction Handling with Improved Logging, Error Handling, and Retry Logic Commit Description: This commit introduces several important enhancements to the trxManager class in the katpool-payment repository, focusing on improving the robustness and clarity of the transaction processing workflow. Key changes include: Key Changes: Enhanced Logging: Added detailed logging around the createTransactions process to capture more information about the outputs and transactions being processed. Improved logs during the transaction processing loop to provide better insight into potential mismatches and skipped transactions. Improved Error Handling: Updated error handling within the transaction processing loop to detect and log mismatches between the number of transactions created and the number of outputs provided. Replaced the warn method with log in the Monitoring class to maintain consistency in log levels. Validation Checks: Introduced a validation check to ensure that the number of transactions matches the number of outputs before proceeding with processing, preventing potential issues from incomplete or mismatched transactions. Retry Logic: Implemented a retry mechanism to handle temporary mismatches between transactions and outputs, attempting up to three retries before aborting the process. Impact: These changes aim to improve the reliability and transparency of the transaction processing system within katpool-payment, ensuring that potential issues are caught early and logged clearly. The retry mechanism adds robustness to the system by mitigating transient issues that may arise during transaction creation. --- src/trxs/index.ts | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/trxs/index.ts b/src/trxs/index.ts index 0be6baa..6fbb9b3 100644 --- a/src/trxs/index.ts +++ b/src/trxs/index.ts @@ -66,20 +66,46 @@ export default class trxManager { } private async enqueueTransactions(outputs: IPaymentOutput[]) { - const { transactions } = await createTransactions({ - entries: this.context, - outputs, - changeAddress: this.address, - priorityFee: 0n + this.monitoring.log(`TrxManager: Preparing to create transactions for ${outputs.length} outputs.`); + + // Log the outputs being passed to createTransactions + outputs.forEach((output, index) => { + this.monitoring.debug(`Output ${index}: Address = ${output.address}, Amount = ${output.amount}`); }); - // Log the lengths to debug any potential mismatch + let transactions: PendingTransaction[]; + let attempts = 0; + const maxRetries = 3; + + do { + transactions = (await createTransactions({ + entries: this.context, + outputs, + changeAddress: this.address, + priorityFee: 0n + })).transactions; + + if (transactions.length === outputs.length) { + break; + } + + this.monitoring.log(`TrxManager: Mismatch detected on attempt ${attempts + 1}. Retrying...`); + attempts++; + + } while (attempts < maxRetries); + + if (transactions.length !== outputs.length) { + this.monitoring.error(`TrxManager: Failed to match transactions and outputs after ${maxRetries} attempts. Aborting.`); + return; + } + this.monitoring.log(`TrxManager: Created ${transactions.length} transactions for ${outputs.length} outputs.`); // Process each transaction sequentially with its associated address for (let i = 0; i < transactions.length; i++) { if (!outputs[i]) { - this.monitoring.error(`TrxManager: Missing output for transaction at index ${i}`); + this.monitoring.error(`TrxManager: Missing output for transaction at index ${i}. Transactions length: ${transactions.length}, Outputs length: ${outputs.length}`); + this.monitoring.debug(`Skipping transaction ID: ${transactions[i].id}`); continue; } @@ -88,11 +114,11 @@ export default class trxManager { ? outputs[i].address : (outputs[i].address as any).toString(); // Explicitly cast Address to string - await this.processTransaction(transaction, address as string); // Explicitly cast to string here too + this.monitoring.debug(`Processing transaction ID: ${transaction.id} for address: ${address}`); + await this.processTransaction(transaction, address as string); } } - private async processTransaction(transaction: PendingTransaction, address: string) { if (DEBUG) this.monitoring.debug(`TrxManager: Signing transaction ID: ${transaction.id}`); await transaction.sign([this.privateKey]);