2018-09-20 13:21:32 +03:00
|
|
|
// +build dev
|
2018-08-17 05:41:32 +03:00
|
|
|
|
2018-08-09 10:05:30 +03:00
|
|
|
package bitcoindnotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcwallet/chain"
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnsafeStart starts the notifier with a specified best height and optional
|
2018-10-05 12:07:55 +03:00
|
|
|
// best hash. Its bestBlock and txNotifier are initialized with bestHeight and
|
|
|
|
// optionally bestHash. The parameter generateBlocks is necessary for the
|
|
|
|
// bitcoind notifier to ensure we drain all notifications up to syncHeight,
|
|
|
|
// since if they are generated ahead of UnsafeStart the chainConn may start up
|
|
|
|
// with an outdated best block and miss sending ntfns. Used for testing.
|
2018-08-09 10:05:30 +03:00
|
|
|
func (b *BitcoindNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Hash,
|
|
|
|
syncHeight int32, generateBlocks func() error) error {
|
|
|
|
|
|
|
|
// Connect to bitcoind, and register for notifications on connected,
|
|
|
|
// and disconnected blocks.
|
|
|
|
if err := b.chainConn.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.chainConn.NotifyBlocks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
b.txNotifier = chainntnfs.NewTxNotifier(
|
2018-08-15 03:53:34 +03:00
|
|
|
uint32(bestHeight), reorgSafetyLimit, b.confirmHintCache,
|
2018-10-05 12:07:55 +03:00
|
|
|
b.spendHintCache,
|
2018-08-15 03:53:34 +03:00
|
|
|
)
|
2018-08-09 10:05:30 +03:00
|
|
|
|
|
|
|
if generateBlocks != nil {
|
|
|
|
// Ensure no block notifications are pending when we start the
|
|
|
|
// notification dispatcher goroutine.
|
|
|
|
|
|
|
|
// First generate the blocks, then drain the notifications
|
|
|
|
// for the generated blocks.
|
|
|
|
if err := generateBlocks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.After(60 * time.Second)
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ntfn := <-b.chainConn.Notifications():
|
|
|
|
switch update := ntfn.(type) {
|
|
|
|
case chain.BlockConnected:
|
|
|
|
if update.Height >= syncHeight {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return fmt.Errorf("unable to catch up to height %d",
|
|
|
|
syncHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run notificationDispatcher after setting the notifier's best block
|
|
|
|
// to avoid a race condition.
|
|
|
|
b.bestBlock = chainntnfs.BlockEpoch{Height: bestHeight, Hash: bestHash}
|
|
|
|
if bestHash == nil {
|
|
|
|
hash, err := b.chainConn.GetBlockHash(int64(bestHeight))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.bestBlock.Hash = hash
|
|
|
|
}
|
|
|
|
|
|
|
|
b.wg.Add(1)
|
|
|
|
go b.notificationDispatcher()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|