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
case *confirmationNotification:
case *chainntnfs.ConfNtfn:
chainntnfs.Log.Infof("New confirmation "+
"subscription: txid=%v, numconfs=%v",
msg.TxID, msg.NumConfirmations)
@ -270,7 +270,7 @@ out:
defer b.wg.Done()
confDetails, _, err := b.historicalConfDetails(
msg.TxID, msg.heightHint,
msg.TxID, msg.HeightHint,
currentHeight,
)
if err != nil {
@ -948,42 +948,23 @@ func (b *BitcoindNotifier) dispatchSpendDetailsManually(op wire.OutPoint,
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
// which will be triggered once the txid reaches numConfs number of
// confirmations.
func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
_ []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
// the main event loop.
ntfn := &confirmationNotification{
ConfNtfn: chainntnfs.ConfNtfn{
ConfID: atomic.AddUint64(&b.confClientCounter, 1),
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs),
},
heightHint: heightHint,
ntfn := &chainntnfs.ConfNtfn{
ConfID: atomic.AddUint64(&b.confClientCounter, 1),
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs),
HeightHint: heightHint,
}
if err := b.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil {
if err := b.txConfNotifier.Register(ntfn); err != nil {
return nil, err
}

@ -324,7 +324,7 @@ out:
}
b.spendNotifications[op][msg.spendID] = msg
case *confirmationNotification:
case *chainntnfs.ConfNtfn:
chainntnfs.Log.Infof("New confirmation "+
"subscription: txid=%v, numconfs=%v",
msg.TxID, msg.NumConfirmations)
@ -340,7 +340,7 @@ out:
defer b.wg.Done()
confDetails, _, err := b.historicalConfDetails(
msg.TxID, msg.heightHint,
msg.TxID, msg.HeightHint,
bestHeight,
)
if err != nil {
@ -1008,42 +1008,23 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
}, 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
// which will be triggered once the txid reaches numConfs number of
// confirmations.
func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []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
// the main event loop.
ntfn := &confirmationNotification{
ConfNtfn: chainntnfs.ConfNtfn{
ConfID: atomic.AddUint64(&b.confClientCounter, 1),
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs),
},
heightHint: heightHint,
ntfn := &chainntnfs.ConfNtfn{
ConfID: atomic.AddUint64(&b.confClientCounter, 1),
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs),
HeightHint: heightHint,
}
if err := b.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil {
if err := b.txConfNotifier.Register(ntfn); err != nil {
return nil, err
}

@ -317,7 +317,8 @@ out:
case *confirmationsNotification:
chainntnfs.Log.Infof("New confirmations subscription: "+
"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
// fully dispatched, then we can skip the first
@ -335,7 +336,8 @@ out:
defer n.wg.Done()
confDetails, err := n.historicalConfDetails(
msg.TxID, msg.pkScript, currentHeight, msg.heightHint,
msg.TxID, msg.pkScript, currentHeight,
msg.ConfNtfn.HeightHint,
)
if err != nil {
chainntnfs.Log.Error(err)
@ -924,8 +926,7 @@ func (n *NeutrinoNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
// notification once the target txid reaches numConfirmations confirmations.
type confirmationsNotification struct {
chainntnfs.ConfNtfn
heightHint uint32
pkScript []byte
pkScript []byte
}
// RegisterConfirmationsNtfn registers a notification with NeutrinoNotifier
@ -935,16 +936,6 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
pkScript []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 := 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
// the main event loop.
ntfn := &confirmationsNotification{
@ -953,9 +944,9 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(numConfs),
HeightHint: heightHint,
},
heightHint: heightHint,
pkScript: pkScript,
pkScript: pkScript,
}
if err := n.txConfNotifier.Register(&ntfn.ConfNtfn); err != nil {