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
// We'll check if this transaction spends an output that has an existing details *SpendDetail
// spend notification for it.
for i, txIn := range tx.TxIn {
// If this input doesn't spend an existing registered outpoint,
// we'll go on to the next.
prevOut := txIn.PreviousOutPoint
if _, ok := spendNtfns[prevOut]; !ok {
continue
} }
// Otherwise, we'll create a spend summary and send off the // We'll set up the onSpend filter callback to gather all the fulfilled
// details to the notification subscribers. // spends requests within this transaction.
txHash := tx.TxHash() var spends []spend
details := &SpendDetail{ onSpend := func(request SpendRequest, details *SpendDetail) {
SpentOutPoint: &prevOut, spends = append(spends, spend{&request, details})
SpenderTxHash: &txHash,
SpendingTx: tx,
SpenderInputIndex: uint32(i),
SpendingHeight: txHeight,
} }
if err := n.updateSpendDetails(prevOut, details); err != nil { n.filterTx(tx, nil, blockHeight, nil, onSpend)
// After the transaction has been filtered, we can finally dispatch
// notifications for each request.
for _, spend := range spends {
err := n.updateSpendDetails(*spend.request, spend.details)
if err != nil {
return err return err
} }
} }