diff --git a/src/common/utxobased/engine/UtxoEngine.ts b/src/common/utxobased/engine/UtxoEngine.ts index c5057923..4f0d6941 100644 --- a/src/common/utxobased/engine/UtxoEngine.ts +++ b/src/common/utxobased/engine/UtxoEngine.ts @@ -61,10 +61,7 @@ import { pathToPurposeType, sumUtxos } from './utils' -import { - makeUtxoEngineProcessor, - transactionChanged -} from './UtxoEngineProcessor' +import { makeUtxoEngineProcessor } from './UtxoEngineProcessor' import { makeUtxoWalletTools } from './UtxoWalletTools' export async function makeUtxoEngine( @@ -133,10 +130,10 @@ export async function makeUtxoEngine( const engineProcessor = makeUtxoEngineProcessor({ ...config, - walletTools, - walletInfo, dataLayer, - pluginState + pluginState, + walletTools, + walletInfo }) const engine: EdgeCurrencyEngine = { @@ -734,31 +731,28 @@ export async function makeUtxoEngine( }) if (rbfTx != null) { rbfTx.blockHeight = -1 - await transactionChanged({ - walletId: walletInfo.id, - tx: rbfTx, + await dataLayer.saveTransaction({ + tx: rbfTx + }) + const rbfEdgeTx = await toEdgeTransaction({ + dataLayer, pluginInfo, - emitter, - walletTools, - dataLayer + tx: rbfTx, + walletId: walletInfo.id, + walletTools }) + emitter.emit(EngineEvent.TRANSACTIONS_CHANGED, [rbfEdgeTx]) } } const tx = fromEdgeTransaction(edgeTx) - await transactionChanged({ - walletId: walletInfo.id, - tx, - pluginInfo, - emitter, - walletTools, - dataLayer - }) await dataLayer.saveTransaction({ tx, scriptPubkeys: edgeTx.otherParams?.ourScriptPubkeys }) + emitter.emit(EngineEvent.TRANSACTIONS_CHANGED, [edgeTx]) + /* Get the wallet's UTXOs from the new transaction and save them to the processsor. */ @@ -979,6 +973,7 @@ export async function makeUtxoEngine( const tmpEngineProcessor = makeUtxoEngineProcessor({ ...config, + dataLayer: tmpDataLayer, emitter: tmpEmitter, engineOptions: { ...config.engineOptions, @@ -993,7 +988,6 @@ export async function makeUtxoEngine( gapLimit: 0 } }, - dataLayer: tmpDataLayer, walletTools: tmpWalletTools, walletInfo: tmpWalletInfo }) diff --git a/src/common/utxobased/engine/UtxoEngineProcessor.ts b/src/common/utxobased/engine/UtxoEngineProcessor.ts index 82f51dad..13c4b307 100644 --- a/src/common/utxobased/engine/UtxoEngineProcessor.ts +++ b/src/common/utxobased/engine/UtxoEngineProcessor.ts @@ -80,22 +80,22 @@ export interface UtxoEngineProcessor { } export interface UtxoEngineProcessorConfig extends EngineConfig { + dataLayer: DataLayer walletTools: UtxoWalletTools walletInfo: SafeWalletInfo - dataLayer: DataLayer } export function makeUtxoEngineProcessor( config: UtxoEngineProcessorConfig ): UtxoEngineProcessor { const { + dataLayer, initOptions, io, emitter, engineOptions, pluginState, pluginInfo, - dataLayer, walletInfo, walletTools } = config @@ -212,12 +212,16 @@ export function makeUtxoEngineProcessor( emitter.on( EngineEvent.BLOCK_HEIGHT_CHANGED, async (_uri: string, _blockHeight: number): Promise => { + // Add all unconfirmed transactions to the cache to check if these + // transactions have been confirmed: const txs = await dataLayer.fetchTransactions({ blockHeight: 0 }) for (const tx of txs) { if (tx == null) continue - taskCache.transactionUpdateCache[tx.txid] = { processing: false } + taskCache.transactionUpdateCache[tx.txid] = { + processing: false + } } } ) @@ -765,26 +769,6 @@ const addToAddressForTransactionsCache = async ( } } -export const transactionChanged = async (args: { - dataLayer: DataLayer - emitter: EngineEmitter - pluginInfo: PluginInfo - tx: TransactionData - walletTools: UtxoWalletTools - walletId: string -}): Promise => { - const { dataLayer, emitter, pluginInfo, tx, walletTools, walletId } = args - emitter.emit(EngineEvent.TRANSACTIONS_CHANGED, [ - await toEdgeTransaction({ - walletId, - tx, - walletTools, - dataLayer, - pluginInfo - }) - ]) -} - /** * Some currencies require an additional blockbook payload 'getTransactionSpecific' in order * to provide all relevant transaction data. Since this is currency-specific, we can limit @@ -1047,14 +1031,14 @@ async function* processTransactionsSpecificUpdate( tx }) - await transactionChanged({ - walletId: common.walletInfo.id, - emitter: common.emitter, - walletTools: common.walletTools, + const edgeTx = await toEdgeTransaction({ dataLayer: common.dataLayer, pluginInfo: common.pluginInfo, - tx: processedTx + tx: processedTx, + walletId: common.walletInfo.id, + walletTools: common.walletTools }) + common.emitter.emit(EngineEvent.TRANSACTIONS_CHANGED, [edgeTx]) // Add the txid to the server cache serverState.txids.add(txId) @@ -1071,8 +1055,10 @@ async function* processTransactionsSpecificUpdate( } /** - * Processes a transaction update from the TransactionUpdateCache by querying + * Processes an item from transactionUpdateCache by querying * the network for the transaction data and processing it into the DataLayer. + * It updates the transaction and all of the transaction's UTXO with the + * blockHeight received from the network. */ async function* processTransactionUpdate( common: CommonParams, @@ -1112,14 +1098,14 @@ async function* processTransactionUpdate( tx }) - await transactionChanged({ - walletId: common.walletInfo.id, - emitter: common.emitter, - walletTools: common.walletTools, + const edgeTx = await toEdgeTransaction({ dataLayer: common.dataLayer, pluginInfo: common.pluginInfo, - tx: processedTx + tx: processedTx, + walletId: common.walletInfo.id, + walletTools: common.walletTools }) + common.emitter.emit(EngineEvent.TRANSACTIONS_CHANGED, [edgeTx]) if (needsTxSpecific(common)) { // Add task to grab transactionSpecific payload @@ -1135,7 +1121,9 @@ async function* processTransactionUpdate( } catch (err) { console.error(err) common.log('error while processing transaction update:', err) - common.taskCache.transactionUpdateCache[txId] = { processing: false } + common.taskCache.transactionUpdateCache[txId] = { + processing: false + } return false } } @@ -1193,14 +1181,14 @@ async function* processAddressForTransactions( tx, scriptPubkeys: [scriptPubkey] }) - await transactionChanged({ - walletId: common.walletInfo.id, - emitter: common.emitter, - walletTools: common.walletTools, + const edgeTx = await toEdgeTransaction({ dataLayer: common.dataLayer, pluginInfo: common.pluginInfo, - tx: processedTx + tx: processedTx, + walletId: common.walletInfo.id, + walletTools: common.walletTools }) + common.emitter.emit(EngineEvent.TRANSACTIONS_CHANGED, [edgeTx]) if (needsTxSpecific(common)) { // Add task to grab transactionSpecific payload