Skip to content

Commit b3de537

Browse files
committed
eth/downloader: flush state data before exit
1 parent 3ec1b9a commit b3de537

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

eth/downloader/statesync.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,19 @@ func (s *stateSync) Cancel() error {
274274
// receive data from peers, rather those are buffered up in the downloader and
275275
// pushed here async. The reason is to decouple processing from data receipt
276276
// and timeouts.
277-
func (s *stateSync) loop() error {
277+
func (s *stateSync) loop() (err error) {
278278
// Listen for new peer events to assign tasks to them
279279
newPeer := make(chan *peerConnection, 1024)
280280
peerSub := s.d.peers.SubscribeNewPeers(newPeer)
281281
defer peerSub.Unsubscribe()
282+
defer func() {
283+
err = s.commit(true)
284+
}()
282285

283286
// Keep assigning new tasks until the sync completes or aborts
284287
for s.sched.Pending() > 0 {
285-
if err := s.commit(false); err != nil {
286-
return err
288+
if err = s.commit(false); err != nil {
289+
return
287290
}
288291
s.assignTasks()
289292
// Tasks assigned, wait for something to happen
@@ -307,14 +310,14 @@ func (s *stateSync) loop() error {
307310
s.d.dropPeer(req.peer.id)
308311
}
309312
// Process all the received blobs and check for stale delivery
310-
if err := s.process(req); err != nil {
313+
if err = s.process(req); err != nil {
311314
log.Warn("Node data write error", "err", err)
312-
return err
315+
return
313316
}
314317
req.peer.SetNodeDataIdle(len(req.response))
315318
}
316319
}
317-
return s.commit(true)
320+
return
318321
}
319322

320323
func (s *stateSync) commit(force bool) error {
@@ -323,7 +326,10 @@ func (s *stateSync) commit(force bool) error {
323326
}
324327
start := time.Now()
325328
b := s.d.stateDB.NewBatch()
326-
s.sched.Commit(b)
329+
// Ignore empty write.
330+
if written, err := s.sched.Commit(b); written == 0 || err != nil {
331+
return err
332+
}
327333
if err := b.Write(); err != nil {
328334
return fmt.Errorf("DB write error: %v", err)
329335
}

trie/sync.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
212212
}
213213

214214
// Commit flushes the data stored in the internal membatch out to persistent
215-
// storage, returning th enumber of items written and any occurred error.
215+
// storage, returning th number of items written and any occurred error.
216216
func (s *TrieSync) Commit(dbw ethdb.Putter) (int, error) {
217217
// Dump the membatch into a database dbw
218218
for i, key := range s.membatch.order {

0 commit comments

Comments
 (0)