Skip to content

Commit

Permalink
Remove transactionChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
samholmes committed Jan 15, 2025
1 parent 9ffc683 commit 60d373c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 62 deletions.
38 changes: 16 additions & 22 deletions src/common/utxobased/engine/UtxoEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -133,10 +130,10 @@ export async function makeUtxoEngine(

const engineProcessor = makeUtxoEngineProcessor({
...config,
walletTools,
walletInfo,
dataLayer,
pluginState
pluginState,
walletTools,
walletInfo
})

const engine: EdgeCurrencyEngine = {
Expand Down Expand Up @@ -733,31 +730,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.
*/
Expand Down Expand Up @@ -978,6 +972,7 @@ export async function makeUtxoEngine(

const tmpEngineProcessor = makeUtxoEngineProcessor({
...config,
dataLayer: tmpDataLayer,
emitter: tmpEmitter,
engineOptions: {
...config.engineOptions,
Expand All @@ -992,7 +987,6 @@ export async function makeUtxoEngine(
gapLimit: 0
}
},
dataLayer: tmpDataLayer,
walletTools: tmpWalletTools,
walletInfo: tmpWalletInfo
})
Expand Down
68 changes: 28 additions & 40 deletions src/common/utxobased/engine/UtxoEngineProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -212,12 +212,16 @@ export function makeUtxoEngineProcessor(
emitter.on(
EngineEvent.BLOCK_HEIGHT_CHANGED,
async (_uri: string, _blockHeight: number): Promise<void> => {
// 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
}
}
}
)
Expand Down Expand Up @@ -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<void> => {
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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 60d373c

Please sign in to comment.