chainntnfs: refactor common registration code within RegisterConfirmationsNtfn

This commit is contained in:
Wilmer Paulino
2019-08-16 19:55:26 -07:00
parent 0a5080c144
commit 5089311ca1
5 changed files with 192 additions and 295 deletions

View File

@@ -39,7 +39,6 @@ type chainUpdate struct {
// chain client. Multiple concurrent clients are supported. All notifications
// are achieved via non-blocking sends on client channels.
type BitcoindNotifier struct {
confClientCounter uint64 // To be used atomically.
spendClientCounter uint64 // To be used atomically.
epochClientCounter uint64 // To be used atomically.
@@ -827,41 +826,23 @@ func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
pkScript []byte,
numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
// Construct a notification request for the transaction and send it to
// the main event loop.
confID := atomic.AddUint64(&b.confClientCounter, 1)
confRequest, err := chainntnfs.NewConfRequest(txid, pkScript)
if err != nil {
return nil, err
}
ntfn := &chainntnfs.ConfNtfn{
ConfID: confID,
ConfRequest: confRequest,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs, func() {
b.txNotifier.CancelConf(confRequest, confID)
}),
HeightHint: heightHint,
}
chainntnfs.Log.Infof("New confirmation subscription: %v, num_confs=%v",
confRequest, numConfs)
// Register the conf notification with the TxNotifier. A non-nil value
// for `dispatch` will be returned if we are required to perform a
// manual scan for the confirmation. Otherwise the notifier will begin
// watching at tip for the transaction to confirm.
dispatch, _, err := b.txNotifier.RegisterConf(ntfn)
ntfn, err := b.txNotifier.RegisterConf(
txid, pkScript, numConfs, heightHint,
)
if err != nil {
return nil, err
}
if dispatch == nil {
if ntfn.HistoricalDispatch == nil {
return ntfn.Event, nil
}
select {
case b.notificationRegistry <- dispatch:
case b.notificationRegistry <- ntfn.HistoricalDispatch:
return ntfn.Event, nil
case <-b.quit:
return nil, chainntnfs.ErrChainNotifierShuttingDown