chain: dispatch BlockConnected notifications for NeutrinoClient

This commit is contained in:
Olaoluwa Osuntokun 2017-06-05 11:10:13 -07:00
parent dbe472edd6
commit 772fbd4726

View file

@ -163,6 +163,10 @@ func (s *NeutrinoClient) Rescan(startHash *chainhash.Hash, addrs []btcutil.Addre
if err != nil { if err != nil {
return fmt.Errorf("Can't get chain service's best block: %s", err) return fmt.Errorf("Can't get chain service's best block: %s", err)
} }
// 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".
if header.BlockHash() == *startHash { if header.BlockHash() == *startHash {
s.finished = true s.finished = true
select { select {
@ -177,8 +181,10 @@ func (s *NeutrinoClient) Rescan(startHash *chainhash.Hash, addrs []btcutil.Addre
return nil return nil
} }
} }
s.rescan = s.CS.NewRescan( s.rescan = s.CS.NewRescan(
neutrino.NotificationHandlers(btcrpcclient.NotificationHandlers{ neutrino.NotificationHandlers(btcrpcclient.NotificationHandlers{
OnBlockConnected: s.onBlockConnected,
OnFilteredBlockConnected: s.onFilteredBlockConnected, OnFilteredBlockConnected: s.onFilteredBlockConnected,
OnBlockDisconnected: s.onBlockDisconnected, OnBlockDisconnected: s.onBlockDisconnected,
}), }),
@ -188,6 +194,7 @@ func (s *NeutrinoClient) Rescan(startHash *chainhash.Hash, addrs []btcutil.Addre
neutrino.WatchOutPoints(watchOutPoints...), neutrino.WatchOutPoints(watchOutPoints...),
) )
s.rescanErr = s.rescan.Start() s.rescanErr = s.rescan.Start()
return nil return nil
} }
@ -312,6 +319,22 @@ func (s *NeutrinoClient) onBlockDisconnected(hash *chainhash.Hash, height int32,
} }
} }
func (s *NeutrinoClient) onBlockConnected(hash *chainhash.Hash, height int32,
time time.Time) {
select {
case s.enqueueNotification <- BlockConnected{
Block: wtxmgr.Block{
Hash: *hash,
Height: height,
},
Time: time,
}:
case <-s.quit:
case <-s.rescanQuit:
}
}
// notificationHandler queues and dequeues notifications. There are currently // notificationHandler queues and dequeues notifications. There are currently
// no bounds on the queue, so the dequeue channel should be read continually to // no bounds on the queue, so the dequeue channel should be read continually to
// avoid running out of memory. // avoid running out of memory.