chainntnfs/txnotifier: refactor ProcessRelevantSpendTx to detect script spends

In this commit, we refactor the TxNotifier's ProcessRelevantSpendTx to
also detect script spends. This can easily be done as the transaction
filtering logic was refactored into its own method in a previous commit.
This commit is contained in:
Wilmer Paulino 2018-12-06 21:13:56 -08:00
parent 8042d4f1c8
commit c3d5204f09
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

@ -870,9 +870,11 @@ func (n *TxNotifier) CancelSpend(spendRequest SpendRequest, spendID uint64) {
// ProcessRelevantSpendTx processes a transaction provided externally. This will // ProcessRelevantSpendTx processes a transaction provided externally. This will
// check whether the transaction is relevant to the notifier if it spends any // check whether the transaction is relevant to the notifier if it spends any
// outputs for which we currently have registered notifications for. If it is // outpoints/output scripts for which we currently have registered notifications
// relevant, spend notifications will be dispatched to the caller. // for. If it is relevant, spend notifications will be dispatched to the caller.
func (n *TxNotifier) ProcessRelevantSpendTx(tx *wire.MsgTx, txHeight int32) error { func (n *TxNotifier) ProcessRelevantSpendTx(tx *btcutil.Tx,
blockHeight uint32) error {
select { select {
case <-n.quit: case <-n.quit:
return ErrTxNotifierExiting return ErrTxNotifierExiting
@ -884,31 +886,26 @@ func (n *TxNotifier) ProcessRelevantSpendTx(tx *wire.MsgTx, txHeight int32) erro
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
// Grab the set of active registered outpoints to determine if the // We'll use a channel to coalesce all the spend requests that this
// transaction spends any of them. // transaction fulfills.
spendNtfns := n.spendNotifications type spend struct {
request *SpendRequest
details *SpendDetail
}
// We'll check if this transaction spends an output that has an existing // We'll set up the onSpend filter callback to gather all the fulfilled
// spend notification for it. // spends requests within this transaction.
for i, txIn := range tx.TxIn { var spends []spend
// If this input doesn't spend an existing registered outpoint, onSpend := func(request SpendRequest, details *SpendDetail) {
// we'll go on to the next. spends = append(spends, spend{&request, details})
prevOut := txIn.PreviousOutPoint }
if _, ok := spendNtfns[prevOut]; !ok { n.filterTx(tx, nil, blockHeight, nil, onSpend)
continue
}
// Otherwise, we'll create a spend summary and send off the // After the transaction has been filtered, we can finally dispatch
// details to the notification subscribers. // notifications for each request.
txHash := tx.TxHash() for _, spend := range spends {
details := &SpendDetail{ err := n.updateSpendDetails(*spend.request, spend.details)
SpentOutPoint: &prevOut, if err != nil {
SpenderTxHash: &txHash,
SpendingTx: tx,
SpenderInputIndex: uint32(i),
SpendingHeight: txHeight,
}
if err := n.updateSpendDetails(prevOut, details); err != nil {
return err return err
} }
} }