chainntnfs/btcd: move NotifySpent to after recording the outpoint

This commit moves the call to the btcd backend to start watching an
outpoint for spentness to after we have recorded the outpoint in our
list of clients. This is done to avoid a race that could occur if btcd
quicly sent a spend notification before we had been able to record it in
our map, essentially losing it.
This commit is contained in:
Johan T. Halseth 2018-03-27 13:50:24 +02:00
parent 2b00e01c53
commit b08fc05390
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -508,10 +508,6 @@ type spendCancel struct {
func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
_ uint32) (*chainntnfs.SpendEvent, error) { _ uint32) (*chainntnfs.SpendEvent, error) {
if err := b.chainConn.NotifySpent([]*wire.OutPoint{outpoint}); err != nil {
return nil, err
}
ntfn := &spendNotification{ ntfn := &spendNotification{
targetOutpoint: outpoint, targetOutpoint: outpoint,
spendChan: make(chan *chainntnfs.SpendDetail, 1), spendChan: make(chan *chainntnfs.SpendDetail, 1),
@ -524,6 +520,10 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
case b.notificationRegistry <- ntfn: case b.notificationRegistry <- ntfn:
} }
if err := b.chainConn.NotifySpent([]*wire.OutPoint{outpoint}); err != nil {
return nil, err
}
// The following conditional checks to ensure that when a spend notification // The following conditional checks to ensure that when a spend notification
// is registered, the output hasn't already been spent. If the output // is registered, the output hasn't already been spent. If the output
// is no longer in the UTXO set, the chain will be rescanned from the point // is no longer in the UTXO set, the chain will be rescanned from the point