chainntnfs: add height hint caches to chain notifiers

This commit is contained in:
Wilmer Paulino committed 2018-08-21 12:55:51 -04:00
1 parent 6be642a033
commit 30fd219b1c
10 files changed
+138 -30

No files matched your search

+18 -2
View File
@@ -83,6 +83,16 @@ type BtcdNotifier struct {
chainUpdates *chainntnfs.ConcurrentQueue
txUpdates *chainntnfs.ConcurrentQueue
// spendHintCache is a cache used to query and update the latest height
// hints for an outpoint. Each height hint represents the earliest
// height at which the outpoint could have been spent within the chain.
spendHintCache chainntnfs.SpendHintCache
// confirmHintCache is a cache used to query the latest height hints for
// a transaction. Each height hint represents the earliest height at
// which the transaction could have confirmed within the chain.
confirmHintCache chainntnfs.ConfirmHintCache
wg sync.WaitGroup
quit chan struct{}
}
@@ -93,7 +103,9 @@ var _ chainntnfs.ChainNotifier = (*BtcdNotifier)(nil)
// New returns a new BtcdNotifier instance. This function assumes the btcd node
// detailed in the passed configuration is already running, and willing to
// accept new websockets clients.
func New(config *rpcclient.ConnConfig) (*BtcdNotifier, error) {
func New(config *rpcclient.ConnConfig, spendHintCache chainntnfs.SpendHintCache,
confirmHintCache chainntnfs.ConfirmHintCache) (*BtcdNotifier, error) {
notifier := &BtcdNotifier{
notificationCancels: make(chan interface{}),
notificationRegistry: make(chan interface{}),
@@ -105,6 +117,9 @@ func New(config *rpcclient.ConnConfig) (*BtcdNotifier, error) {
chainUpdates: chainntnfs.NewConcurrentQueue(10),
txUpdates: chainntnfs.NewConcurrentQueue(10),
spendHintCache: spendHintCache,
confirmHintCache: confirmHintCache,
quit: make(chan struct{}),
}
@@ -150,7 +165,8 @@ func (b *BtcdNotifier) Start() error {
}
b.txConfNotifier = chainntnfs.NewTxConfNotifier(
uint32(currentHeight), reorgSafetyLimit)
uint32(currentHeight), reorgSafetyLimit, b.confirmHintCache,
)
b.bestBlock = chainntnfs.BlockEpoch{
Height: currentHeight,
+2 -1
View File
@@ -28,7 +28,8 @@ func (b *BtcdNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Hash,
}
b.txConfNotifier = chainntnfs.NewTxConfNotifier(
uint32(bestHeight), reorgSafetyLimit)
uint32(bestHeight), reorgSafetyLimit, b.confirmHintCache,
)
b.chainUpdates.Start()
b.txUpdates.Start()
+19 -6
View File
@@ -1,6 +1,7 @@
package btcdnotify
import (
"errors"
"fmt"
"github.com/btcsuite/btcd/rpcclient"
@@ -10,18 +11,30 @@ import (
// createNewNotifier creates a new instance of the ChainNotifier interface
// implemented by BtcdNotifier.
func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) {
if len(args) != 1 {
return nil, fmt.Errorf("incorrect number of arguments to .New(...), "+
"expected 1, instead passed %v", len(args))
if len(args) != 3 {
return nil, fmt.Errorf("incorrect number of arguments to "+
".New(...), expected 2, instead passed %v", len(args))
}
config, ok := args[0].(*rpcclient.ConnConfig)
if !ok {
return nil, fmt.Errorf("first argument to btcdnotifier.New is " +
"incorrect, expected a *rpcclient.ConnConfig")
return nil, errors.New("first argument to btcdnotifier.New " +
"is incorrect, expected a *rpcclient.ConnConfig")
}
return New(config)
spendHintCache, ok := args[1].(chainntnfs.SpendHintCache)
if !ok {
return nil, errors.New("second argument to btcdnotifier.New " +
"is incorrect, expected a chainntnfs.SpendHintCache")
}
confirmHintCache, ok := args[2].(chainntnfs.ConfirmHintCache)
if !ok {
return nil, errors.New("third argument to btcdnotifier.New " +
"is incorrect, expected a chainntnfs.ConfirmHintCache")
}
return New(config, spendHintCache, confirmHintCache)
}
// init registers a driver for the BtcdNotifier concrete implementation of the