From e0e8bf31c5d44f7de33ce774b221debf2c42256c Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 23 Mar 2022 01:58:05 +0800 Subject: [PATCH] eth/downloader: ignore zero size header batch for importing (#24569) * eth/downloader: ignore zero size header batch for importing * core, light: reject empty header batch for importing --- core/blockchain.go | 3 +++ eth/downloader/downloader.go | 32 +++++++++++++++++--------------- light/lightchain.go | 3 +++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index f272522624..c6c8453503 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2309,6 +2309,9 @@ Error: %v // of the header retrieval mechanisms already need to verify nonces, as well as // because nonces can be verified sparsely, not needing to check each. func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { + if len(chain) == 0 { + return 0, nil + } start := time.Now() if i, err := bc.hc.ValidateHeaderChain(chain, checkFreq); err != nil { return i, err diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index c1b1fdf79d..ebd414105f 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1377,23 +1377,25 @@ func (d *Downloader) processHeaders(origin uint64, td, ttd *big.Int, beaconMode } } } - if n, err := d.lightchain.InsertHeaderChain(chunkHeaders, frequency); err != nil { - rollbackErr = err + if len(chunkHeaders) > 0 { + if n, err := d.lightchain.InsertHeaderChain(chunkHeaders, frequency); err != nil { + rollbackErr = err - // If some headers were inserted, track them as uncertain - if (mode == SnapSync || frequency > 1) && n > 0 && rollback == 0 { - rollback = chunkHeaders[0].Number.Uint64() + // If some headers were inserted, track them as uncertain + if (mode == SnapSync || frequency > 1) && n > 0 && rollback == 0 { + rollback = chunkHeaders[0].Number.Uint64() + } + log.Warn("Invalid header encountered", "number", chunkHeaders[n].Number, "hash", chunkHashes[n], "parent", chunkHeaders[n].ParentHash, "err", err) + return fmt.Errorf("%w: %v", errInvalidChain, err) } - log.Warn("Invalid header encountered", "number", chunkHeaders[n].Number, "hash", chunkHashes[n], "parent", chunkHeaders[n].ParentHash, "err", err) - return fmt.Errorf("%w: %v", errInvalidChain, err) - } - // All verifications passed, track all headers within the alloted limits - if mode == SnapSync { - head := chunkHeaders[len(chunkHeaders)-1].Number.Uint64() - if head-rollback > uint64(fsHeaderSafetyNet) { - rollback = head - uint64(fsHeaderSafetyNet) - } else { - rollback = 1 + // All verifications passed, track all headers within the allowed limits + if mode == SnapSync { + head := chunkHeaders[len(chunkHeaders)-1].Number.Uint64() + if head-rollback > uint64(fsHeaderSafetyNet) { + rollback = head - uint64(fsHeaderSafetyNet) + } else { + rollback = 1 + } } } if len(rejected) != 0 { diff --git a/light/lightchain.go b/light/lightchain.go index 61309ce356..0cc88b46e7 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -419,6 +419,9 @@ func (lc *LightChain) SetChainHead(header *types.Header) error { // In the case of a light chain, InsertHeaderChain also creates and posts light // chain events when necessary. func (lc *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { + if len(chain) == 0 { + return 0, nil + } if atomic.LoadInt32(&lc.disableCheckFreq) == 1 { checkFreq = 0 }