chainntnfs/bitcoindnotify: remove all mempool spend clients

This commit is contained in:
Johan T. Halseth 2018-07-17 11:18:18 +02:00
parent 3808105dcd
commit 181014c363
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -331,6 +331,14 @@ out:
// handleRelevantTx notifies any clients of a relevant transaction. // handleRelevantTx notifies any clients of a relevant transaction.
func (b *BitcoindNotifier) handleRelevantTx(tx chain.RelevantTx, bestHeight int32) { func (b *BitcoindNotifier) handleRelevantTx(tx chain.RelevantTx, bestHeight int32) {
msgTx := tx.TxRecord.MsgTx msgTx := tx.TxRecord.MsgTx
// We only care about notifying on confirmed spends, so in case this is
// a mempool spend, we can continue, and wait for the spend to appear
// in chain.
if tx.Block == nil {
return
}
// First, check if this transaction spends an output // First, check if this transaction spends an output
// that has an existing spend notification for it. // that has an existing spend notification for it.
for i, txIn := range msgTx.TxIn { for i, txIn := range msgTx.TxIn {
@ -349,55 +357,22 @@ func (b *BitcoindNotifier) handleRelevantTx(tx chain.RelevantTx, bestHeight int3
SpendingTx: &msgTx, SpendingTx: &msgTx,
SpenderInputIndex: uint32(i), SpenderInputIndex: uint32(i),
} }
// TODO(roasbeef): after change to spendDetails.SpendingHeight = tx.Block.Height
// loadfilter, only notify on block
// inclusion?
confirmedSpend := false for _, ntfn := range clients {
if tx.Block != nil { chainntnfs.Log.Infof("Dispatching confirmed "+
confirmedSpend = true "spend notification for outpoint=%v "+
spendDetails.SpendingHeight = tx.Block.Height "at height %v", ntfn.targetOutpoint,
} else {
spendDetails.SpendingHeight = bestHeight + 1
}
// Keep spendNotifications that are
// waiting for a confirmation around.
// They will be notified when we find
// the spend within a block.
rem := make(map[uint64]*spendNotification)
for c, ntfn := range clients {
// If this is a mempool spend, store the client
// to wait for a confirmed spend.
if !confirmedSpend {
rem[c] = ntfn
continue
}
confStr := "unconfirmed"
if confirmedSpend {
confStr = "confirmed"
}
chainntnfs.Log.Infof("Dispatching %s "+
"spend notification for "+
"outpoint=%v at height %v",
confStr, ntfn.targetOutpoint,
spendDetails.SpendingHeight) spendDetails.SpendingHeight)
ntfn.spendChan <- spendDetails ntfn.spendChan <- spendDetails
// Close spendChan to ensure that any calls to Cancel will not // Close spendChan to ensure that any calls to
// block. This is safe to do since the channel is buffered, and the // Cancel will not block. This is safe to do
// since the channel is buffered, and the
// message can still be read by the receiver. // message can still be read by the receiver.
close(ntfn.spendChan) close(ntfn.spendChan)
} }
delete(b.spendNotifications, prevOut) delete(b.spendNotifications, prevOut)
// If we had any clients left, add them
// back to the map.
if len(rem) > 0 {
b.spendNotifications[prevOut] = rem
}
} }
} }
} }