chain: fix Neutrino driver bugs

This commit is contained in:
Alex 2017-05-23 23:03:58 -06:00 committed by Olaoluwa Osuntokun
parent 4a25ae194a
commit 0e7e8404e5

View file

@ -28,6 +28,7 @@ type SPVChain struct {
quit chan struct{} quit chan struct{}
rescanQuit chan struct{} rescanQuit chan struct{}
rescanErr <-chan error
wg sync.WaitGroup wg sync.WaitGroup
started bool started bool
scanning bool scanning bool
@ -53,6 +54,13 @@ func (s *SPVChain) Start() error {
s.quit = make(chan struct{}) s.quit = make(chan struct{})
s.started = true s.started = true
s.wg.Add(1) s.wg.Add(1)
go func() {
select {
case s.enqueueNotification <- ClientConnected{}:
case <-s.quit:
return
}
}()
go s.notificationHandler() go s.notificationHandler()
} }
return nil return nil
@ -156,22 +164,25 @@ func (s *SPVChain) Rescan(startHash *chainhash.Hash, addrs []btcutil.Address,
OnFilteredBlockConnected: s.onFilteredBlockConnected, OnFilteredBlockConnected: s.onFilteredBlockConnected,
OnBlockDisconnected: s.onBlockDisconnected, OnBlockDisconnected: s.onBlockDisconnected,
}), }),
neutrino.StartBlock(&waddrmgr.BlockStamp{Hash: *startHash}),
neutrino.QuitChan(s.rescanQuit), neutrino.QuitChan(s.rescanQuit),
neutrino.WatchAddrs(addrs...), neutrino.WatchAddrs(addrs...),
neutrino.WatchOutPoints(watchOutPoints...), neutrino.WatchOutPoints(watchOutPoints...),
) )
s.rescanErr = s.rescan.Start()
return nil return nil
} }
// NotifyBlocks replicates the RPC client's NotifyBlocks command. // NotifyBlocks replicates the RPC client's NotifyBlocks command.
func (s *SPVChain) NotifyBlocks() error { func (s *SPVChain) NotifyBlocks() error {
s.clientMtx.Lock() s.clientMtx.Lock()
defer s.clientMtx.Unlock()
// If we're scanning, we're already notifying on blocks. Otherwise, // If we're scanning, we're already notifying on blocks. Otherwise,
// start a rescan without watching any addresses. // start a rescan without watching any addresses.
if !s.scanning { if !s.scanning {
s.clientMtx.Unlock()
return s.NotifyReceived([]btcutil.Address{}) return s.NotifyReceived([]btcutil.Address{})
} }
s.clientMtx.Unlock()
return nil return nil
} }
@ -198,6 +209,7 @@ func (s *SPVChain) NotifyReceived(addrs []btcutil.Address) error {
neutrino.QuitChan(s.rescanQuit), neutrino.QuitChan(s.rescanQuit),
neutrino.WatchAddrs(addrs...), neutrino.WatchAddrs(addrs...),
) )
s.rescanErr = s.rescan.Start()
return nil return nil
} }
@ -350,6 +362,11 @@ out:
dequeue = nil dequeue = nil
} }
case err := <-s.rescanErr:
if err != nil {
log.Errorf("Neutrino rescan ended with error: %s", err)
}
case s.currentBlock <- bs: case s.currentBlock <- bs:
case <-s.quit: case <-s.quit: