Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miner/worker: broadcast block immediately once sealed #2576

Merged
merged 1 commit into from
Jul 16, 2024

Conversation

buddh0
Copy link
Collaborator

@buddh0 buddh0 commented Jul 12, 2024

Description

miner/worker: broadcast block immediately once sealed

Rationale

state.commit and write down some block may cost more than 700+ms
this PR is to reduce the unnecessary delay

Example

add an example CLI or API response...

Changes

Notable changes:

  • add each change in a bullet point here
  • ...

@@ -946,8 +946,9 @@ func (h *handler) minedBroadcastLoop() {
if obj == nil {
continue
}
if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok {
h.BroadcastBlock(ev.Block, true) // First propagate block to peers
if ev, ok := obj.Data.(core.NewSealedBlockEvent); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure how exactly this improves things? Could you clarify bit more?
Now we are waiting for a sealing to happen before broadcasting rather than mining to happen before broadcasting it.
But in (w *worker) resultLoop() function, there are several checks made before a mining event is emitted which could fail e.g. double sign. Wouldn't that be a problem?

@@ -665,7 +665,7 @@ func (w *worker) resultLoop() {
// Commit block and state to database.
task.state.SetExpectedStateRoot(block.Root())
start := time.Now()
status, err := w.chain.WriteBlockAndSetHead(block, receipts, logs, task.state, true)
status, err := w.chain.WriteBlockAndSetHead(block, receipts, logs, task.state, true, w.mux)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@emailtovamos this happens after double sign
maybe NewSealedBlockEvent is not exactly accurate, but I have not got a better word

mux.Post(NewSealedBlockEvent{Block: block})
}

if err := bc.writeBlockWithState(block, receipts, state); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

writeBlockWithState cost much, may be hundreds of milliseconds

we can post a NewSealedBlockEvent before it, so broadcast it early @emailtovamos

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, understood.
But if writeBlockWithState() fails for some reason then it is still okay to post a NewSealedBlockEvent ? This is my only doubt.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, understood. But if writeBlockWithState() fails for some reason then it is still okay to post a NewSealedBlockEvent ? This is my only doubt.

every verification has been passed when do writeBlockWithState.
if a miner has a bad db, cause failed to writeBlockWithState. It's ok, others will write the mined block into db.
if the block is bad, cause failed to writeBlockWithState. It's ok, others will reject the mined block into db.

@buddh0 buddh0 requested review from brilliant-lx and galaio July 15, 2024 09:57
core/events.go Show resolved Hide resolved
@zzzckck zzzckck merged commit 6d5b4ad into bnb-chain:develop Jul 16, 2024
7 checks passed
jingjunLi added a commit to jingjunLi/bsc that referenced this pull request Jul 22, 2024
NathanBSC added a commit to NathanBSC/bsc that referenced this pull request Jul 22, 2024
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.

6 participants