Skip to content

Commit

Permalink
call isTransactionOutputSpendable before checking hints
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Feb 23, 2021
1 parent b16fc45 commit 848ac2b
Showing 1 changed file with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,21 @@ class ZmqWatcher(chainHash: ByteVector32, blockCount: AtomicLong, client: Extend
// first let's see if the parent tx was published or not
client.getTxConfirmations(txid).collect {
case Some(_) =>
// if we know some potential spending txs, we try to fetch them directly
Future.sequence(hints.map(txid => client.getTransaction(txid).map(Some(_)).recover { case _ => None }))
.map(_
.flatten // filter out errors
.find(tx => tx.txIn.exists(i => i.outPoint.txid == txid && i.outPoint.index == outputIndex)) match {
case Some(spendingTx) =>
// there can be only one spending tx for an utxo
log.info(s"$txid:$outputIndex has already been spent by a tx provided in hints: txid=${spendingTx.txid}")
self ! NewTransaction(spendingTx)
case None =>
// no luck, we have to do it the hard way...
// parent tx was published, we need to make sure this particular output has not been spent
client.isTransactionOutputSpendable(txid, outputIndex, includeMempool = true).collect {
case false =>
// parent tx was published, we need to make sure this particular output has not been spent
client.isTransactionOutputSpendable(txid, outputIndex, includeMempool = true).collect {
case false =>
// the output has been spent, let's find the spending tx
// if we know some potential spending txs, we try to fetch them directly
Future.sequence(hints.map(txid => client.getTransaction(txid).map(Some(_)).recover { case _ => None }))
.map(_
.flatten // filter out errors
.find(tx => tx.txIn.exists(i => i.outPoint.txid == txid && i.outPoint.index == outputIndex)) match {
case Some(spendingTx) =>
// there can be only one spending tx for an utxo
log.info(s"$txid:$outputIndex has already been spent by a tx provided in hints: txid=${spendingTx.txid}")
self ! NewTransaction(spendingTx)
case None =>
// no luck, we have to do it the hard way...
log.info(s"$txid:$outputIndex has already been spent, looking for the spending tx in the mempool")
client.getMempool().map { mempoolTxs =>
mempoolTxs.filter(tx => tx.txIn.exists(i => i.outPoint.txid == txid && i.outPoint.index == outputIndex)) match {
Expand All @@ -172,9 +173,8 @@ class ZmqWatcher(chainHash: ByteVector32, blockCount: AtomicLong, client: Extend
txs.foreach(tx => self ! NewTransaction(tx))
}
}
}
}
)
})
}
}
Keep

Expand Down

0 comments on commit 848ac2b

Please sign in to comment.