Skip to content

Comments

bsc: add new block fetching mechanism;#3040

Merged
zzzckck merged 7 commits intobnb-chain:developfrom
galaio:new-block-fetching
Apr 29, 2025
Merged

bsc: add new block fetching mechanism;#3040
zzzckck merged 7 commits intobnb-chain:developfrom
galaio:new-block-fetching

Conversation

@galaio
Copy link
Contributor

@galaio galaio commented Apr 21, 2025

Description

This PR will implement bnb-chain/BEPs#564.

It supports fast fetching of newly generated blocks, avoids validators and full nodes from lagging behind in the subsecond block interval, and especially improves the validator consensus efficiency, which can produce the next block or send vote faster.
image

Example

If you want to use new block fetching mechanism, you need modify the config:

[Node]
EnableQuickBlockFetching = true

Changes

Notable changes:

  • bsc: add BlocksByRange msgs;
  • block_fetcher: support quick block fetching;

@galaio galaio requested review from MatusKysel, buddh0 and zzzckck April 21, 2025 08:41
@MatusKysel MatusKysel requested a review from Copilot April 22, 2025 06:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a new block fetching mechanism for the BSC protocol to support fast retrieval of newly generated blocks and improve validator consensus efficiency. Key changes include adding two new messages (GetBlocksByRange and BlocksByRange) in the BSC protocol, updating the peer and dispatcher logic to support these messages, and integrating a quick block fetching feature into the block fetcher along with corresponding configuration and tests.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
eth/protocols/bsc/protocol.go Added new protocol version, message constants, and packet types
eth/protocols/bsc/peer.go Introduced dispatcher and a new RequestBlocksByRange method
eth/protocols/bsc/handler.go Added handlers for GetBlocksByRange and BlocksByRange messages
eth/protocols/bsc/dispatcher.go Implemented dispatcher logic for request/response messaging
eth/handler.go Integrated quick block fetching callback into block fetcher initialization
eth/fetcher/block_fetcher.go Introduced quick block fetching channel and scheduling logic
eth/ethconfig/* and eth/backend.go Updated configuration and backend wiring to enable quick block fetching
eth/fetcher/block_fetcher_test.go Added tests covering multiple peers and the quick block fetching feature

@galaio galaio marked this pull request as ready for review April 22, 2025 08:49
@galaio galaio force-pushed the new-block-fetching branch 2 times, most recently from cd7d144 to d25d1e1 Compare April 24, 2025 03:08
buddh0
buddh0 previously approved these changes Apr 27, 2025
Copy link
Collaborator

@zzzckck zzzckck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this PR does not change the current eth block fetch logic, right?
with EnableQuickBlockFetching enabled, the eth blcok fetch would still work?

@galaio
Copy link
Contributor Author

galaio commented Apr 28, 2025

seems like this PR does not change the current eth block fetch logic, right? with EnableQuickBlockFetching enabled, the eth blcok fetch would still work?

Yes, after enabling EnableQuickBlockFetching, BlocksByRange will usually fetch the block first. If it times out or fails, the previous fetchHeader and fetchBody will still work.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a new block fetching mechanism that accelerates the retrieval of freshly generated blocks to improve validator consensus efficiency. Key changes include:

  • Adding a new configuration flag (EnableQuickBlockFetching) in node/config.go.
  • Extending the BSC protocol (in protocol.go, peer.go, handler.go) with new messages (GetBlocksByRangeMsg and BlocksByRangeMsg) and corresponding dispatcher and handler logic.
  • Integrating the quick block fetching mechanism into the block fetcher (and related tests) to support fast block imports.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
node/config.go Added the EnableQuickBlockFetching flag to control the quick block fetching mechanism.
eth/protocols/bsc/protocol.go Introduced Bsc2 protocol version and new messages for block range fetching.
eth/protocols/bsc/peer.go Implemented RequestBlocksByRange method using the dispatcher.
eth/protocols/bsc/handler.go Added handlers for GetBlocksByRange and BlocksByRange messages.
eth/handler.go Integrated quick block fetching with an inline fetchRangeBlocks function and its usage.
eth/fetcher/block_fetcher.go Extended the block fetcher with a quick fetching channel and logic to handle range calls.
eth/fetcher/block_fetcher_test.go Added tests for the new quick block fetching feature.
eth/downloader/downloader.go Minor logging updates for chain synchronization.
eth/backend.go Passed the EnableQuickBlockFetching flag to the handler for proper integration.

Comment on lines +160 to +176
if req.StartBlockHash != (common.Hash{}) {
block = backend.Chain().GetBlockByHash(req.StartBlockHash)
} else {
block = backend.Chain().GetBlockByNumber(req.StartBlockHeight)
}

if block == nil {
return fmt.Errorf("msg %v, cannot get start block: %v, %v", GetBlocksByRangeMsg, req.StartBlockHeight, req.StartBlockHash)
}
blocks = append(blocks, NewBlockData(block))
for i := uint64(1); i < req.Count; i++ {
block = backend.Chain().GetBlockByHash(block.ParentHash())
if block == nil {
break
}
blocks = append(blocks, NewBlockData(block))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait so StartBlockHeight it's from but it's means we going backwards as you putting there parents right ? so let say from is 1000 then I will get like 1000, 999, 998 .. not 1000, 1001 as I would be expecting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is the reverse block order fetching.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it stated like this in BEP ? Because I don't remember

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

galaio added 6 commits April 29, 2025 14:02
block_fetcher: add some UTs;

block_fetcher: add some UTs;

bsc: fix some failed UTs;

bsc: fix some failed UTs;

bsc: fix some failed UTs;

bsc: fix some failed UTs;

bsc: fix some failed UTs;

bsc: fix some comments;
chore: fix some comments;
@galaio galaio force-pushed the new-block-fetching branch from d9988f8 to d516dc3 Compare April 29, 2025 06:02
MatusKysel
MatusKysel previously approved these changes Apr 29, 2025
@zzzckck zzzckck merged commit 61cb07c into bnb-chain:develop Apr 29, 2025
7 checks passed
galaio added a commit to galaio/bsc that referenced this pull request May 29, 2025
galaio added a commit to galaio/bsc that referenced this pull request May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants