@@ -1280,41 +1280,13 @@ func (d *Downloader) fetchReceipts(from uint64, beaconMode bool) error {
12801280// keeps processing and scheduling them into the header chain and downloader's
12811281// queue until the stream ends or a failure occurs.
12821282func (d * Downloader ) processHeaders (origin uint64 , td , ttd * big.Int , beaconMode bool ) error {
1283- // Keep a count of uncertain headers to roll back
12841283 var (
1285- rollback uint64 // Zero means no rollback (fine as you can't unroll the genesis)
1286- rollbackErr error
1287- mode = d .getMode ()
1284+ mode = d .getMode ()
1285+ gotHeaders = false // Wait for batches of headers to process
12881286 )
1289- defer func () {
1290- if rollback > 0 {
1291- lastHeader , lastFastBlock , lastBlock := d .lightchain .CurrentHeader ().Number , common .Big0 , common .Big0
1292- if mode != LightSync {
1293- lastFastBlock = d .blockchain .CurrentSnapBlock ().Number
1294- lastBlock = d .blockchain .CurrentBlock ().Number
1295- }
1296- if err := d .lightchain .SetHead (rollback - 1 ); err != nil { // -1 to target the parent of the first uncertain block
1297- // We're already unwinding the stack, only print the error to make it more visible
1298- log .Error ("Failed to roll back chain segment" , "head" , rollback - 1 , "err" , err )
1299- }
1300- curFastBlock , curBlock := common .Big0 , common .Big0
1301- if mode != LightSync {
1302- curFastBlock = d .blockchain .CurrentSnapBlock ().Number
1303- curBlock = d .blockchain .CurrentBlock ().Number
1304- }
1305- log .Warn ("Rolled back chain segment" ,
1306- "header" , fmt .Sprintf ("%d->%d" , lastHeader , d .lightchain .CurrentHeader ().Number ),
1307- "snap" , fmt .Sprintf ("%d->%d" , lastFastBlock , curFastBlock ),
1308- "block" , fmt .Sprintf ("%d->%d" , lastBlock , curBlock ), "reason" , rollbackErr )
1309- }
1310- }()
1311- // Wait for batches of headers to process
1312- gotHeaders := false
1313-
13141287 for {
13151288 select {
13161289 case <- d .cancelCh :
1317- rollbackErr = errCanceled
13181290 return errCanceled
13191291
13201292 case task := <- d .headerProcCh :
@@ -1363,8 +1335,6 @@ func (d *Downloader) processHeaders(origin uint64, td, ttd *big.Int, beaconMode
13631335 }
13641336 }
13651337 }
1366- // Disable any rollback and return
1367- rollback = 0
13681338 return nil
13691339 }
13701340 // Otherwise split the chunk of headers into batches and process them
@@ -1375,7 +1345,6 @@ func (d *Downloader) processHeaders(origin uint64, td, ttd *big.Int, beaconMode
13751345 // Terminate if something failed in between processing chunks
13761346 select {
13771347 case <- d .cancelCh :
1378- rollbackErr = errCanceled
13791348 return errCanceled
13801349 default :
13811350 }
@@ -1422,29 +1391,11 @@ func (d *Downloader) processHeaders(origin uint64, td, ttd *big.Int, beaconMode
14221391 }
14231392 if len (chunkHeaders ) > 0 {
14241393 if n , err := d .lightchain .InsertHeaderChain (chunkHeaders ); err != nil {
1425- rollbackErr = err
1426-
1427- // If some headers were inserted, track them as uncertain
1428- if mode == SnapSync && n > 0 && rollback == 0 {
1429- rollback = chunkHeaders [0 ].Number .Uint64 ()
1430- }
14311394 log .Warn ("Invalid header encountered" , "number" , chunkHeaders [n ].Number , "hash" , chunkHashes [n ], "parent" , chunkHeaders [n ].ParentHash , "err" , err )
14321395 return fmt .Errorf ("%w: %v" , errInvalidChain , err )
14331396 }
1434- // All verifications passed, track all headers within the allowed limits
1435- if mode == SnapSync {
1436- head := chunkHeaders [len (chunkHeaders )- 1 ].Number .Uint64 ()
1437- if head - rollback > uint64 (fsHeaderSafetyNet ) {
1438- rollback = head - uint64 (fsHeaderSafetyNet )
1439- } else {
1440- rollback = 1
1441- }
1442- }
14431397 }
14441398 if len (rejected ) != 0 {
1445- // Merge threshold reached, stop importing, but don't roll back
1446- rollback = 0
1447-
14481399 log .Info ("Legacy sync reached merge threshold" , "number" , rejected [0 ].Number , "hash" , rejected [0 ].Hash (), "td" , td , "ttd" , ttd )
14491400 return ErrMergeTransition
14501401 }
@@ -1455,15 +1406,13 @@ func (d *Downloader) processHeaders(origin uint64, td, ttd *big.Int, beaconMode
14551406 for d .queue .PendingBodies () >= maxQueuedHeaders || d .queue .PendingReceipts () >= maxQueuedHeaders {
14561407 select {
14571408 case <- d .cancelCh :
1458- rollbackErr = errCanceled
14591409 return errCanceled
14601410 case <- time .After (time .Second ):
14611411 }
14621412 }
14631413 // Otherwise insert the headers for content retrieval
14641414 inserts := d .queue .Schedule (chunkHeaders , chunkHashes , origin )
14651415 if len (inserts ) != len (chunkHeaders ) {
1466- rollbackErr = fmt .Errorf ("stale headers: len inserts %v len(chunk) %v" , len (inserts ), len (chunkHeaders ))
14671416 return fmt .Errorf ("%w: stale headers" , errBadPeer )
14681417 }
14691418 }
0 commit comments