chainntnfs/bitcoind+btcd+neutrino: let tcn query for height hint

This commit is contained in:
Conner Fromknecht 2018-08-24 20:09:53 -07:00
parent 8b8007bb5a
commit cf7700e6cb
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
3 changed files with 25 additions and 72 deletions

@ -254,7 +254,7 @@ out:
} }
b.spendNotifications[op][msg.spendID] = msg b.spendNotifications[op][msg.spendID] = msg
case *confirmationNotification: case *chainntnfs.ConfNtfn:
chainntnfs.Log.Infof("New confirmation "+ chainntnfs.Log.Infof("New confirmation "+
"subscription: txid=%v, numconfs=%v", "subscription: txid=%v, numconfs=%v",
msg.TxID, msg.NumConfirmations) msg.TxID, msg.NumConfirmations)
@ -270,7 +270,7 @@ out:
defer b.wg.Done() defer b.wg.Done()
confDetails, _, err := b.historicalConfDetails( confDetails, _, err := b.historicalConfDetails(
msg.TxID, msg.heightHint, msg.TxID, msg.HeightHint,
currentHeight, currentHeight,
) )
if err != nil { if err != nil {
@ -948,42 +948,23 @@ func (b *BitcoindNotifier) dispatchSpendDetailsManually(op wire.OutPoint,
return ErrTransactionNotFound return ErrTransactionNotFound
} }
// confirmationNotification represents a client's intent to receive a
// notification once the target txid reaches numConfirmations confirmations.
type confirmationNotification struct {
chainntnfs.ConfNtfn
heightHint uint32
}
// RegisterConfirmationsNtfn registers a notification with BitcoindNotifier // RegisterConfirmationsNtfn registers a notification with BitcoindNotifier
// which will be triggered once the txid reaches numConfs number of // which will be triggered once the txid reaches numConfs number of
// confirmations. // confirmations.
func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
_ []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) { _ []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
// Before proceeding to register the notification, we'll query our
// height hint cache to determine whether a better one exists.
if hint, err := b.confirmHintCache.QueryConfirmHint(*txid); err == nil {
if hint > heightHint {
chainntnfs.Log.Debugf("Using height hint %d retrieved "+
"from cache for %v", hint, txid)
heightHint = hint
}
}
// Construct a notification request for the transaction and send it to // Construct a notification request for the transaction and send it to
// the main event loop. // the main event loop.
ntfn := &confirmationNotification{ ntfn := &chainntnfs.ConfNtfn{
ConfNtfn: chainntnfs.ConfNtfn{ ConfID: atomic.AddUint64(&b.confClientCounter, 1),
ConfID: atomic.AddUint64(&b.confClientCounter, 1), TxID: txid,
TxID: txid, NumConfirmations: numConfs,
NumConfirmations: numConfs, Event: chainntnfs.NewConfirmationEvent(numConfs),
Event: chainntnfs.NewConfirmationEvent(numConfs), HeightHint: heightHint,
},
heightHint: heightHint,
} }
if err := b.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil { if err := b.txConfNotifier.Register(ntfn); err != nil {
return nil, err return nil, err
} }

@ -324,7 +324,7 @@ out:
} }
b.spendNotifications[op][msg.spendID] = msg b.spendNotifications[op][msg.spendID] = msg
case *confirmationNotification: case *chainntnfs.ConfNtfn:
chainntnfs.Log.Infof("New confirmation "+ chainntnfs.Log.Infof("New confirmation "+
"subscription: txid=%v, numconfs=%v", "subscription: txid=%v, numconfs=%v",
msg.TxID, msg.NumConfirmations) msg.TxID, msg.NumConfirmations)
@ -340,7 +340,7 @@ out:
defer b.wg.Done() defer b.wg.Done()
confDetails, _, err := b.historicalConfDetails( confDetails, _, err := b.historicalConfDetails(
msg.TxID, msg.heightHint, msg.TxID, msg.HeightHint,
bestHeight, bestHeight,
) )
if err != nil { if err != nil {
@ -1008,42 +1008,23 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
}, nil }, nil
} }
// confirmationNotification represents a client's intent to receive a
// notification once the target txid reaches numConfirmations confirmations.
type confirmationNotification struct {
chainntnfs.ConfNtfn
heightHint uint32
}
// RegisterConfirmationsNtfn registers a notification with BtcdNotifier // RegisterConfirmationsNtfn registers a notification with BtcdNotifier
// which will be triggered once the txid reaches numConfs number of // which will be triggered once the txid reaches numConfs number of
// confirmations. // confirmations.
func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte, func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte,
numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) { numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
// Before proceeding to register the notification, we'll query our
// height hint cache to determine whether a better one exists.
if hint, err := b.confirmHintCache.QueryConfirmHint(*txid); err == nil {
if hint > heightHint {
chainntnfs.Log.Debugf("Using height hint %d retrieved "+
"from cache for %v", hint, txid)
heightHint = hint
}
}
// Construct a notification request for the transaction and send it to // Construct a notification request for the transaction and send it to
// the main event loop. // the main event loop.
ntfn := &confirmationNotification{ ntfn := &chainntnfs.ConfNtfn{
ConfNtfn: chainntnfs.ConfNtfn{ ConfID: atomic.AddUint64(&b.confClientCounter, 1),
ConfID: atomic.AddUint64(&b.confClientCounter, 1), TxID: txid,
TxID: txid, NumConfirmations: numConfs,
NumConfirmations: numConfs, Event: chainntnfs.NewConfirmationEvent(numConfs),
Event: chainntnfs.NewConfirmationEvent(numConfs), HeightHint: heightHint,
},
heightHint: heightHint,
} }
if err := b.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil { if err := b.txConfNotifier.Register(ntfn); err != nil {
return nil, err return nil, err
} }

@ -317,7 +317,8 @@ out:
case *confirmationsNotification: case *confirmationsNotification:
chainntnfs.Log.Infof("New confirmations subscription: "+ chainntnfs.Log.Infof("New confirmations subscription: "+
"txid=%v, numconfs=%v, height_hint=%v", "txid=%v, numconfs=%v, height_hint=%v",
msg.TxID, msg.NumConfirmations, msg.heightHint) msg.TxID, msg.NumConfirmations,
msg.ConfNtfn.HeightHint)
// If the notification can be partially or // If the notification can be partially or
// fully dispatched, then we can skip the first // fully dispatched, then we can skip the first
@ -335,7 +336,8 @@ out:
defer n.wg.Done() defer n.wg.Done()
confDetails, err := n.historicalConfDetails( confDetails, err := n.historicalConfDetails(
msg.TxID, msg.pkScript, currentHeight, msg.heightHint, msg.TxID, msg.pkScript, currentHeight,
msg.ConfNtfn.HeightHint,
) )
if err != nil { if err != nil {
chainntnfs.Log.Error(err) chainntnfs.Log.Error(err)
@ -924,8 +926,7 @@ func (n *NeutrinoNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
// notification once the target txid reaches numConfirmations confirmations. // notification once the target txid reaches numConfirmations confirmations.
type confirmationsNotification struct { type confirmationsNotification struct {
chainntnfs.ConfNtfn chainntnfs.ConfNtfn
heightHint uint32 pkScript []byte
pkScript []byte
} }
// RegisterConfirmationsNtfn registers a notification with NeutrinoNotifier // RegisterConfirmationsNtfn registers a notification with NeutrinoNotifier
@ -935,16 +936,6 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
pkScript []byte, pkScript []byte,
numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) { numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
// Before proceeding to register the notification, we'll query our
// height hint cache to determine whether a better one exists.
if hint, err := n.confirmHintCache.QueryConfirmHint(*txid); err == nil {
if hint > heightHint {
chainntnfs.Log.Debugf("Using height hint %d retrieved "+
"from cache for %v", hint, txid)
heightHint = hint
}
}
// Construct a notification request for the transaction and send it to // Construct a notification request for the transaction and send it to
// the main event loop. // the main event loop.
ntfn := &confirmationsNotification{ ntfn := &confirmationsNotification{
@ -953,9 +944,9 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
TxID: txid, TxID: txid,
NumConfirmations: numConfs, NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs), Event: chainntnfs.NewConfirmationEvent(numConfs),
HeightHint: heightHint,
}, },
heightHint: heightHint, pkScript: pkScript,
pkScript: pkScript,
} }
if err := n.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil { if err := n.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil {