Skip to content

Commit c57c54c

Browse files
committed
eth, les: defer starting LES service until ETH initial sync is finished
1 parent c8130df commit c57c54c

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

eth/backend.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type Config struct {
104104

105105
type LesServer interface {
106106
Start(srvr *p2p.Server)
107+
Synced()
107108
Stop()
108109
Protocols() []p2p.Protocol
109110
}
@@ -145,6 +146,7 @@ type Ethereum struct {
145146

146147
func (s *Ethereum) AddLesServer(ls LesServer) {
147148
s.lesServer = ls
149+
s.protocolManager.lesServer = ls
148150
}
149151

150152
// New creates a new Ethereum object (including the

eth/handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ type ProtocolManager struct {
8787
quitSync chan struct{}
8888
noMorePeers chan struct{}
8989

90+
lesServer LesServer
91+
9092
// wait group is used for graceful shutdowns during downloading
9193
// and processing
9294
wg sync.WaitGroup
@@ -171,7 +173,7 @@ func NewProtocolManager(config *params.ChainConfig, fastSync bool, networkId int
171173
return blockchain.CurrentBlock().NumberU64()
172174
}
173175
inserter := func(blocks types.Blocks) (int, error) {
174-
atomic.StoreUint32(&manager.synced, 1) // Mark initial sync done on any fetcher import
176+
manager.setSynced() // Mark initial sync done on any fetcher import
175177
return manager.insertChain(blocks)
176178
}
177179
manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)

eth/sync.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
180180
if err := pm.downloader.Synchronise(peer.id, pHead, pTd, mode); err != nil {
181181
return
182182
}
183-
atomic.StoreUint32(&pm.synced, 1) // Mark initial sync done
183+
pm.setSynced() // Mark initial sync done
184184

185185
// If fast sync was enabled, and we synced up, disable it
186186
if atomic.LoadUint32(&pm.fastSync) == 1 {
@@ -191,3 +191,10 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
191191
}
192192
}
193193
}
194+
195+
// setSynced sets the synced flag and notifies the light server if present
196+
func (pm *ProtocolManager) setSynced() {
197+
if atomic.SwapUint32(&pm.synced, 1) == 0 && pm.lesServer != nil {
198+
pm.lesServer.Synced()
199+
}
200+
}

les/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ func (pm *ProtocolManager) Start(srvr *p2p.Server) {
267267
} else {
268268
if topicDisc != nil {
269269
go func() {
270-
glog.V(logger.Debug).Infoln("Starting registering topic", string(lesTopic))
270+
glog.V(logger.Info).Infoln("Starting registering topic", string(lesTopic))
271271
topicDisc.RegisterTopic(lesTopic, pm.quitSync)
272-
glog.V(logger.Debug).Infoln("Stopped registering topic", string(lesTopic))
272+
glog.V(logger.Info).Infoln("Stopped registering topic", string(lesTopic))
273273
}()
274274
}
275275
go func() {

les/server.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type LesServer struct {
4242
fcManager *flowcontrol.ClientManager // nil if our node is client only
4343
fcCostStats *requestCostStats
4444
defParams *flowcontrol.ServerParams
45+
srvr *p2p.Server
46+
synced, stopped bool
47+
lock sync.Mutex
4548
}
4649

4750
func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
@@ -67,12 +70,35 @@ func (s *LesServer) Protocols() []p2p.Protocol {
6770
return s.protocolManager.SubProtocols
6871
}
6972

73+
// Start only starts the actual service if the ETH protocol has already been synced,
74+
// otherwise it will be started by Synced()
7075
func (s *LesServer) Start(srvr *p2p.Server) {
71-
s.protocolManager.Start(srvr)
76+
s.lock.Lock()
77+
defer s.lock.Unlock()
78+
79+
s.srvr = srvr
80+
if s.synced {
81+
s.protocolManager.Start(s.srvr)
82+
}
83+
}
84+
85+
// Synced notifies the server that the ETH protocol has been synced and LES service can be started
86+
func (s *LesServer) Synced() {
87+
s.lock.Lock()
88+
defer s.lock.Unlock()
7289

90+
s.synced = true
91+
if s.srvr != nil && !s.stopped {
92+
s.protocolManager.Start(s.srvr)
93+
}
7394
}
7495

96+
// Stop stops the LES service
7597
func (s *LesServer) Stop() {
98+
s.lock.Lock()
99+
defer s.lock.Unlock()
100+
101+
s.stopped = true
76102
s.fcCostStats.store()
77103
s.fcManager.Stop()
78104
go func() {

0 commit comments

Comments
 (0)