bsc icon indicating copy to clipboard operation
bsc copied to clipboard

Fix duplicate quick block fetching for the same hash

Open Copilot opened this issue 3 months ago • 0 comments

Quick block fetching triggers multiple times for the same block hash when a block is imported via direct broadcast while an async quick fetch is pending. The forgetHash call clears f.announced, causing subsequent announcements to pass the len == 1 check and trigger redundant fetches.

Changes

  • Add quickFetching map[common.Hash]struct{} to track in-flight async quick fetches
  • Check quickFetching before triggering new quick fetch in notification handler
  • Clear quickFetching entry only when async operation completes (success or error), not in forgetHash
// Before: only checked announced length
if f.fetchRangeBlocks != nil && len(f.announced[notification.hash]) == 1 {
    f.asyncFetchRangeBlocks(notification)
}

// After: also check if quick fetch already in progress
if f.fetchRangeBlocks != nil && len(f.announced[notification.hash]) == 1 {
    if _, ok := f.quickFetching[notification.hash]; !ok {
        f.quickFetching[notification.hash] = struct{}{}
        f.asyncFetchRangeBlocks(notification)
    }
}

Test

Added TestQuickBlockFetchingNoDuplicates that verifies fetchRangeBlocks is called only once when the same block is announced by multiple peers while an async fetch is pending.

Original prompt

This section details on the original issue you should resolve

<issue_title>Quick block fetching more than once for the same hash</issue_title> <issue_description>#### System information Geth version: geth version OS & Version: Windows/Linux/OSX Commit hash : (735f8ea4b70f52ad3da7282447a099d66083a569) Arguments:

Description

Image
  • Expected behaviour ...

  • Actual behaviour ...

Backtrace

[backtrace]

When submitting logs: please submit them as text and not screenshots. </issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes bnb-chain/bsc#3405

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Dec 05 '25 09:12 Copilot