2017-05-20 01:45:38 +02:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-06-13 11:15:50 +02:00
|
|
|
"github.com/lightninglabs/neutrino"
|
2017-06-06 02:54:35 +02:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2017-08-25 02:30:43 +02:00
|
|
|
"github.com/roasbeef/btcd/rpcclient"
|
2017-06-06 02:54:35 +02:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
"github.com/roasbeef/btcwallet/waddrmgr"
|
|
|
|
"github.com/roasbeef/btcwallet/wtxmgr"
|
2017-05-20 01:45:38 +02:00
|
|
|
)
|
|
|
|
|
2017-05-25 02:52:46 +02:00
|
|
|
// NeutrinoClient is an implementation of the btcwalet chain.Interface interface.
|
|
|
|
type NeutrinoClient struct {
|
2017-05-24 18:46:38 +02:00
|
|
|
CS *neutrino.ChainService
|
2017-05-20 01:45:38 +02:00
|
|
|
|
|
|
|
// We currently support one rescan/notifiction goroutine per client
|
2017-05-21 04:36:40 +02:00
|
|
|
rescan neutrino.Rescan
|
2017-05-20 01:45:38 +02:00
|
|
|
|
|
|
|
enqueueNotification chan interface{}
|
|
|
|
dequeueNotification chan interface{}
|
2017-09-20 00:53:23 +02:00
|
|
|
startTime time.Time
|
|
|
|
lastProgressSent bool
|
2017-05-20 01:45:38 +02:00
|
|
|
currentBlock chan *waddrmgr.BlockStamp
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
rescanQuit chan struct{}
|
2017-05-24 07:03:58 +02:00
|
|
|
rescanErr <-chan error
|
2017-05-20 01:45:38 +02:00
|
|
|
wg sync.WaitGroup
|
|
|
|
started bool
|
|
|
|
scanning bool
|
2017-05-21 04:36:40 +02:00
|
|
|
finished bool
|
2017-09-20 00:53:23 +02:00
|
|
|
isRescan bool
|
2017-05-20 01:45:38 +02:00
|
|
|
|
|
|
|
clientMtx sync.Mutex
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:52:46 +02:00
|
|
|
// NewNeutrinoClient creates a new NeutrinoClient struct with a backing
|
|
|
|
// ChainService.
|
|
|
|
func NewNeutrinoClient(chainService *neutrino.ChainService) *NeutrinoClient {
|
|
|
|
return &NeutrinoClient{CS: chainService}
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start replicates the RPC client's Start method.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) Start() error {
|
2017-05-24 18:46:38 +02:00
|
|
|
s.CS.Start()
|
2017-05-20 01:45:38 +02:00
|
|
|
s.clientMtx.Lock()
|
|
|
|
defer s.clientMtx.Unlock()
|
|
|
|
if !s.started {
|
|
|
|
s.enqueueNotification = make(chan interface{})
|
|
|
|
s.dequeueNotification = make(chan interface{})
|
|
|
|
s.currentBlock = make(chan *waddrmgr.BlockStamp)
|
|
|
|
s.quit = make(chan struct{})
|
|
|
|
s.started = true
|
|
|
|
s.wg.Add(1)
|
2017-05-24 07:03:58 +02:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- ClientConnected{}:
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2017-05-20 01:45:38 +02:00
|
|
|
go s.notificationHandler()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop replicates the RPC client's Stop method.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) Stop() {
|
2017-05-20 01:45:38 +02:00
|
|
|
s.clientMtx.Lock()
|
|
|
|
defer s.clientMtx.Unlock()
|
|
|
|
if !s.started {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
close(s.quit)
|
|
|
|
s.started = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForShutdown replicates the RPC client's WaitForShutdown method.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) WaitForShutdown() {
|
2017-05-20 01:45:38 +02:00
|
|
|
s.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlock replicates the RPC client's GetBlock command.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock, error) {
|
2017-05-20 01:45:38 +02:00
|
|
|
// TODO(roasbeef): add a block cache?
|
|
|
|
// * which evication strategy? depends on use case
|
2017-05-24 03:55:57 +02:00
|
|
|
// Should the block cache be INSIDE neutrino instead of in btcwallet?
|
2017-05-24 18:46:38 +02:00
|
|
|
block, err := s.CS.GetBlockFromNetwork(*hash)
|
2017-05-20 01:45:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return block.MsgBlock(), nil
|
|
|
|
}
|
|
|
|
|
2017-05-24 03:55:57 +02:00
|
|
|
// GetBlockHeight gets the height of a block by its hash. It serves as a
|
|
|
|
// replacement for the use of GetBlockVerboseTxAsync for the wallet package
|
|
|
|
// since we can't actually return a FutureGetBlockVerboseResult because the
|
2017-08-25 02:30:43 +02:00
|
|
|
// underlying type is private to rpcclient.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) GetBlockHeight(hash *chainhash.Hash) (int32, error) {
|
2017-06-04 23:40:00 +02:00
|
|
|
_, height, err := s.CS.BlockHeaders.FetchHeader(hash)
|
2017-05-24 03:55:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int32(height), nil
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:45:38 +02:00
|
|
|
// GetBestBlock replicates the RPC client's GetBestBlock command.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) GetBestBlock() (*chainhash.Hash, int32, error) {
|
2017-06-04 23:40:00 +02:00
|
|
|
chainTip, err := s.CS.BestSnapshot()
|
2017-05-20 01:45:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2017-06-04 23:40:00 +02:00
|
|
|
|
|
|
|
return &chainTip.Hash, chainTip.Height, nil
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockStamp returns the latest block notified by the client, or an error
|
|
|
|
// if the client has been shut down.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) BlockStamp() (*waddrmgr.BlockStamp, error) {
|
2017-05-20 01:45:38 +02:00
|
|
|
select {
|
|
|
|
case bs := <-s.currentBlock:
|
|
|
|
return bs, nil
|
|
|
|
case <-s.quit:
|
|
|
|
return nil, errors.New("disconnected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 01:13:10 +02:00
|
|
|
// GetBlockHash returns the block hash for the given height, or an error if the
|
|
|
|
// client has been shut down or the hash at the block height doesn't exist or
|
|
|
|
// is unknown.
|
|
|
|
func (s *NeutrinoClient) GetBlockHash(height int64) (*chainhash.Hash, error) {
|
|
|
|
header, err := s.CS.BlockHeaders.FetchHeaderByHeight(uint32(height))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hash := header.BlockHash()
|
|
|
|
return &hash, nil
|
|
|
|
}
|
|
|
|
|
2017-09-20 22:33:12 +02:00
|
|
|
// GetBlockHeader returns the block header for the given block hash, or an error
|
|
|
|
// if the client has been shut down or the hash doesn't exist or is unknown.
|
|
|
|
func (s *NeutrinoClient) GetBlockHeader(
|
|
|
|
blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
|
|
|
|
header, _, err := s.CS.BlockHeaders.FetchHeader(blockHash)
|
|
|
|
return header, err
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:45:38 +02:00
|
|
|
// SendRawTransaction replicates the RPC client's SendRawTransaction command.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (
|
2017-05-20 01:45:38 +02:00
|
|
|
*chainhash.Hash, error) {
|
2017-05-24 18:46:38 +02:00
|
|
|
err := s.CS.SendTransaction(tx)
|
2017-05-20 01:45:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hash := tx.TxHash()
|
|
|
|
return &hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rescan replicates the RPC client's Rescan command.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) Rescan(startHash *chainhash.Hash, addrs []btcutil.Address,
|
2017-05-20 01:45:38 +02:00
|
|
|
outPoints []*wire.OutPoint) error {
|
|
|
|
s.clientMtx.Lock()
|
2017-05-24 21:31:05 +02:00
|
|
|
defer s.clientMtx.Unlock()
|
2017-05-20 01:45:38 +02:00
|
|
|
if !s.started {
|
|
|
|
return fmt.Errorf("can't do a rescan when the chain client " +
|
|
|
|
"is not started")
|
|
|
|
}
|
|
|
|
if s.scanning {
|
|
|
|
// Restart the rescan by killing the existing rescan.
|
|
|
|
close(s.rescanQuit)
|
2017-06-13 11:15:50 +02:00
|
|
|
s.rescan.WaitForShutdown()
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
|
|
|
s.rescanQuit = make(chan struct{})
|
|
|
|
s.scanning = true
|
2017-05-21 04:36:40 +02:00
|
|
|
s.finished = false
|
2017-09-20 00:53:23 +02:00
|
|
|
s.lastProgressSent = false
|
|
|
|
s.isRescan = true
|
2017-05-21 04:36:40 +02:00
|
|
|
watchOutPoints := make([]wire.OutPoint, 0, len(outPoints))
|
|
|
|
for _, op := range outPoints {
|
|
|
|
watchOutPoints = append(watchOutPoints, *op)
|
|
|
|
}
|
2017-06-04 23:40:00 +02:00
|
|
|
header, height, err := s.CS.BlockHeaders.ChainTip()
|
2017-05-24 21:31:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't get chain service's best block: %s", err)
|
|
|
|
}
|
2017-06-05 20:10:13 +02:00
|
|
|
|
|
|
|
// If the wallet is already fully caught up, or the rescan has started
|
|
|
|
// with state that indicates a "fresh" wallet, we'll send a
|
|
|
|
// notification indicating the rescan has "finished".
|
2017-05-24 21:31:05 +02:00
|
|
|
if header.BlockHash() == *startHash {
|
|
|
|
s.finished = true
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- &RescanFinished{
|
|
|
|
Hash: startHash,
|
|
|
|
Height: int32(height),
|
|
|
|
Time: header.Timestamp,
|
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
return nil
|
|
|
|
case <-s.rescanQuit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 20:10:13 +02:00
|
|
|
|
2017-05-24 18:46:38 +02:00
|
|
|
s.rescan = s.CS.NewRescan(
|
2017-08-25 02:30:43 +02:00
|
|
|
neutrino.NotificationHandlers(rpcclient.NotificationHandlers{
|
2017-06-05 20:10:13 +02:00
|
|
|
OnBlockConnected: s.onBlockConnected,
|
2017-05-20 01:45:38 +02:00
|
|
|
OnFilteredBlockConnected: s.onFilteredBlockConnected,
|
|
|
|
OnBlockDisconnected: s.onBlockDisconnected,
|
|
|
|
}),
|
2017-05-24 07:03:58 +02:00
|
|
|
neutrino.StartBlock(&waddrmgr.BlockStamp{Hash: *startHash}),
|
2017-09-20 00:53:23 +02:00
|
|
|
neutrino.StartTime(s.startTime),
|
2017-05-20 01:45:38 +02:00
|
|
|
neutrino.QuitChan(s.rescanQuit),
|
2017-05-21 04:36:40 +02:00
|
|
|
neutrino.WatchAddrs(addrs...),
|
|
|
|
neutrino.WatchOutPoints(watchOutPoints...),
|
2017-05-20 01:45:38 +02:00
|
|
|
)
|
2017-05-24 07:03:58 +02:00
|
|
|
s.rescanErr = s.rescan.Start()
|
2017-06-05 20:10:13 +02:00
|
|
|
|
2017-05-21 04:36:40 +02:00
|
|
|
return nil
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBlocks replicates the RPC client's NotifyBlocks command.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) NotifyBlocks() error {
|
2017-05-21 04:36:40 +02:00
|
|
|
s.clientMtx.Lock()
|
|
|
|
// If we're scanning, we're already notifying on blocks. Otherwise,
|
|
|
|
// start a rescan without watching any addresses.
|
|
|
|
if !s.scanning {
|
2017-05-24 07:03:58 +02:00
|
|
|
s.clientMtx.Unlock()
|
2017-05-21 04:36:40 +02:00
|
|
|
return s.NotifyReceived([]btcutil.Address{})
|
|
|
|
}
|
2017-05-24 07:03:58 +02:00
|
|
|
s.clientMtx.Unlock()
|
2017-05-20 01:45:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyReceived replicates the RPC client's NotifyReceived command.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) NotifyReceived(addrs []btcutil.Address) error {
|
2017-06-13 11:15:50 +02:00
|
|
|
s.clientMtx.Lock()
|
|
|
|
defer s.clientMtx.Unlock()
|
|
|
|
|
2017-05-21 04:36:40 +02:00
|
|
|
// If we have a rescan running, we just need to add the appropriate
|
|
|
|
// addresses to the watch list.
|
|
|
|
if s.scanning {
|
|
|
|
return s.rescan.Update(neutrino.AddAddrs(addrs...))
|
|
|
|
}
|
2017-06-13 11:15:50 +02:00
|
|
|
|
2017-05-21 04:36:40 +02:00
|
|
|
s.rescanQuit = make(chan struct{})
|
|
|
|
s.scanning = true
|
2017-06-13 11:15:50 +02:00
|
|
|
|
2017-09-20 00:53:23 +02:00
|
|
|
// Don't need RescanFinished or RescanProgress notifications.
|
2017-05-21 04:36:40 +02:00
|
|
|
s.finished = true
|
2017-09-20 00:53:23 +02:00
|
|
|
s.lastProgressSent = true
|
2017-06-13 11:15:50 +02:00
|
|
|
|
2017-05-21 04:36:40 +02:00
|
|
|
// Rescan with just the specified addresses.
|
2017-05-24 18:46:38 +02:00
|
|
|
s.rescan = s.CS.NewRescan(
|
2017-08-25 02:30:43 +02:00
|
|
|
neutrino.NotificationHandlers(rpcclient.NotificationHandlers{
|
2017-06-05 21:22:09 +02:00
|
|
|
OnBlockConnected: s.onBlockConnected,
|
2017-05-21 04:36:40 +02:00
|
|
|
OnFilteredBlockConnected: s.onFilteredBlockConnected,
|
|
|
|
OnBlockDisconnected: s.onBlockDisconnected,
|
|
|
|
}),
|
2017-09-20 00:53:23 +02:00
|
|
|
neutrino.StartTime(s.startTime),
|
2017-05-21 04:36:40 +02:00
|
|
|
neutrino.QuitChan(s.rescanQuit),
|
|
|
|
neutrino.WatchAddrs(addrs...),
|
|
|
|
)
|
2017-05-24 07:03:58 +02:00
|
|
|
s.rescanErr = s.rescan.Start()
|
2017-05-20 01:45:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notifications replicates the RPC client's Notifications method.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) Notifications() <-chan interface{} {
|
2017-05-20 01:45:38 +02:00
|
|
|
return s.dequeueNotification
|
|
|
|
}
|
|
|
|
|
2017-09-20 00:53:23 +02:00
|
|
|
// SetStartTime is a non-interface method to set the birthday of the wallet
|
|
|
|
// using this object. Since only a single rescan at a time is currently
|
|
|
|
// supported, only one birthday needs to be set. This does not fully restart a
|
|
|
|
// running rescan, so should not be used to update a rescan while it is running.
|
|
|
|
// TODO: When factoring out to multiple rescans per Neutrino client, add a
|
|
|
|
// birthday per client.
|
|
|
|
func (s *NeutrinoClient) SetStartTime(startTime time.Time) {
|
|
|
|
s.clientMtx.Lock()
|
|
|
|
defer s.clientMtx.Unlock()
|
|
|
|
|
|
|
|
s.startTime = startTime
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:45:38 +02:00
|
|
|
// onFilteredBlockConnected sends appropriate notifications to the notification
|
|
|
|
// channel.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) onFilteredBlockConnected(height int32,
|
2017-05-20 01:45:38 +02:00
|
|
|
header *wire.BlockHeader, relevantTxs []*btcutil.Tx) {
|
2017-05-21 04:36:40 +02:00
|
|
|
ntfn := FilteredBlockConnected{
|
|
|
|
Block: &wtxmgr.BlockMeta{
|
|
|
|
Block: wtxmgr.Block{
|
|
|
|
Hash: header.BlockHash(),
|
|
|
|
Height: height,
|
|
|
|
},
|
|
|
|
Time: header.Timestamp,
|
2017-05-20 01:45:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tx := range relevantTxs {
|
|
|
|
rec, err := wtxmgr.NewTxRecordFromMsgTx(tx.MsgTx(),
|
2017-05-21 04:36:40 +02:00
|
|
|
header.Timestamp)
|
2017-05-20 01:45:38 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot create transaction record for "+
|
|
|
|
"relevant tx: %s", err)
|
2017-05-21 04:36:40 +02:00
|
|
|
// TODO(aakselrod): Return?
|
|
|
|
continue
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
2017-05-21 04:36:40 +02:00
|
|
|
ntfn.RelevantTxs = append(ntfn.RelevantTxs, rec)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- ntfn:
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
case <-s.rescanQuit:
|
|
|
|
return
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
2017-09-20 00:53:23 +02:00
|
|
|
|
|
|
|
// Handle RescanFinished notification if required.
|
2017-06-04 23:40:00 +02:00
|
|
|
bs, err := s.CS.BestSnapshot()
|
2017-05-20 01:45:38 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Can't get chain service's best block: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2017-09-20 00:53:23 +02:00
|
|
|
|
2017-05-20 01:45:38 +02:00
|
|
|
if bs.Hash == header.BlockHash() {
|
2017-05-21 04:36:40 +02:00
|
|
|
// Only send the RescanFinished notification once.
|
|
|
|
s.clientMtx.Lock()
|
|
|
|
if s.finished {
|
|
|
|
s.clientMtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2017-09-20 00:53:23 +02:00
|
|
|
// Only send the RescanFinished notification once the
|
|
|
|
// underlying chain service sees itself as current.
|
|
|
|
current := s.CS.IsCurrent() && s.lastProgressSent
|
|
|
|
if current {
|
|
|
|
s.finished = true
|
|
|
|
}
|
2017-05-21 04:36:40 +02:00
|
|
|
s.clientMtx.Unlock()
|
2017-09-20 00:53:23 +02:00
|
|
|
if current {
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- &RescanFinished{
|
|
|
|
Hash: &bs.Hash,
|
|
|
|
Height: bs.Height,
|
|
|
|
Time: header.Timestamp,
|
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
case <-s.rescanQuit:
|
|
|
|
return
|
|
|
|
}
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// onBlockDisconnected sends appropriate notifications to the notification
|
|
|
|
// channel.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) onBlockDisconnected(hash *chainhash.Hash, height int32,
|
2017-05-20 01:45:38 +02:00
|
|
|
t time.Time) {
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- BlockDisconnected{
|
|
|
|
Block: wtxmgr.Block{
|
|
|
|
Hash: *hash,
|
|
|
|
Height: height,
|
|
|
|
},
|
|
|
|
Time: t,
|
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
case <-s.rescanQuit:
|
2017-06-05 20:10:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NeutrinoClient) onBlockConnected(hash *chainhash.Hash, height int32,
|
|
|
|
time time.Time) {
|
2017-09-20 00:53:23 +02:00
|
|
|
// TODO: Move this closure out and parameterize it? Is it useful
|
|
|
|
// outside here?
|
|
|
|
sendRescanProgress := func() {
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- &RescanProgress{
|
|
|
|
Hash: hash,
|
2017-06-05 20:10:13 +02:00
|
|
|
Height: height,
|
2017-09-20 00:53:23 +02:00
|
|
|
Time: time,
|
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
case <-s.rescanQuit:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Only send BlockConnected notification if we're processing blocks
|
|
|
|
// before the birthday. Otherwise, we can just update using
|
|
|
|
// RescanProgress notifications.
|
|
|
|
if time.Before(s.startTime) {
|
|
|
|
// Send a RescanProgress notification every 10K blocks.
|
|
|
|
if height%10000 == 0 {
|
|
|
|
s.clientMtx.Lock()
|
|
|
|
shouldSend := s.isRescan && !s.finished
|
|
|
|
s.clientMtx.Unlock()
|
|
|
|
if shouldSend {
|
|
|
|
sendRescanProgress()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Send a RescanProgress notification if we're just going over
|
|
|
|
// the boundary between pre-birthday and post-birthday blocks,
|
|
|
|
// and note that we've sent it.
|
|
|
|
s.clientMtx.Lock()
|
|
|
|
if !s.lastProgressSent {
|
|
|
|
shouldSend := s.isRescan && !s.finished
|
|
|
|
if shouldSend {
|
|
|
|
s.clientMtx.Unlock()
|
|
|
|
sendRescanProgress()
|
|
|
|
s.clientMtx.Lock()
|
|
|
|
s.lastProgressSent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.clientMtx.Unlock()
|
|
|
|
select {
|
|
|
|
case s.enqueueNotification <- BlockConnected{
|
|
|
|
Block: wtxmgr.Block{
|
|
|
|
Hash: *hash,
|
|
|
|
Height: height,
|
|
|
|
},
|
|
|
|
Time: time,
|
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
case <-s.rescanQuit:
|
|
|
|
}
|
2017-05-20 01:45:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notificationHandler queues and dequeues notifications. There are currently
|
|
|
|
// no bounds on the queue, so the dequeue channel should be read continually to
|
|
|
|
// avoid running out of memory.
|
2017-05-25 02:52:46 +02:00
|
|
|
func (s *NeutrinoClient) notificationHandler() {
|
2017-05-20 01:45:38 +02:00
|
|
|
hash, height, err := s.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get best block from chain service: %s",
|
|
|
|
err)
|
|
|
|
s.Stop()
|
|
|
|
s.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := &waddrmgr.BlockStamp{Hash: *hash, Height: height}
|
|
|
|
|
|
|
|
// TODO: Rather than leaving this as an unbounded queue for all types of
|
|
|
|
// notifications, try dropping ones where a later enqueued notification
|
|
|
|
// can fully invalidate one waiting to be processed. For example,
|
|
|
|
// blockconnected notifications for greater block heights can remove the
|
|
|
|
// need to process earlier blockconnected notifications still waiting
|
|
|
|
// here.
|
|
|
|
|
|
|
|
var notifications []interface{}
|
|
|
|
enqueue := s.enqueueNotification
|
|
|
|
var dequeue chan interface{}
|
|
|
|
var next interface{}
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n, ok := <-enqueue:
|
|
|
|
if !ok {
|
|
|
|
// If no notifications are queued for handling,
|
|
|
|
// the queue is finished.
|
|
|
|
if len(notifications) == 0 {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
// nil channel so no more reads can occur.
|
|
|
|
enqueue = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(notifications) == 0 {
|
|
|
|
next = n
|
|
|
|
dequeue = s.dequeueNotification
|
|
|
|
}
|
|
|
|
notifications = append(notifications, n)
|
|
|
|
|
|
|
|
case dequeue <- next:
|
|
|
|
if n, ok := next.(BlockConnected); ok {
|
|
|
|
bs = &waddrmgr.BlockStamp{
|
|
|
|
Height: n.Height,
|
|
|
|
Hash: n.Hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifications[0] = nil
|
|
|
|
notifications = notifications[1:]
|
|
|
|
if len(notifications) != 0 {
|
|
|
|
next = notifications[0]
|
|
|
|
} else {
|
|
|
|
// If no more notifications can be enqueued, the
|
|
|
|
// queue is finished.
|
|
|
|
if enqueue == nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
dequeue = nil
|
|
|
|
}
|
|
|
|
|
2017-05-24 07:03:58 +02:00
|
|
|
case err := <-s.rescanErr:
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Neutrino rescan ended with error: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:45:38 +02:00
|
|
|
case s.currentBlock <- bs:
|
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Stop()
|
|
|
|
close(s.dequeueNotification)
|
|
|
|
s.wg.Done()
|
|
|
|
}
|