Skip to content

Commit

Permalink
perf(internal/blocksync): avoid double-calling types.BlockFromProto
Browse files Browse the repository at this point in the history
… (backport cometbft#2016) (cometbft#2145)

This is an automatic backport of pull request cometbft#2016 done by
[Mergify](https://mergify.com).
Cherry-pick of f4d73cd has failed:
```
On branch mergify/bp/v0.38.x/pr-2016
Your branch is up to date with 'origin/v0.38.x'.

You are currently cherry-picking commit f4d73cd.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   blocksync/msgs.go
	modified:   blocksync/reactor.go

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	added by them:   .changelog/v0.38.3/improvements/2016-blocksync-avoid-double-calling-block-from-proto.md

```


To fix up this pull request, you can check it out locally. See
documentation:
https://docs.github.com/en/github/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the
[documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on
`<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com) you
can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>

---------

Co-authored-by: Anton Kaliaev <[email protected]>
  • Loading branch information
2 people authored and roy-dydx committed Feb 3, 2025
1 parent 69cdde9 commit 75a7861
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[blocksync]` Avoid double-calling `types.BlockFromProto` for performance
reasons ([\#2016](https://github.com/cometbft/cometbft/pull/2016))
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ proto/spec/**/*.pb.go
*.dvi
# Python virtual environments
.venv
go.work.sum
7 changes: 3 additions & 4 deletions blocksync/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ func ValidateMsg(pb proto.Message) error {
return ErrInvalidHeight{Height: msg.Height, Reason: "negative height"}
}
case *bcproto.BlockResponse:
_, err := types.BlockFromProto(msg.Block)
if err != nil {
return err
}
// Avoid double-calling `types.BlockFromProto` for performance reasons.
// See https://github.com/cometbft/cometbft/issues/1964
return nil
case *bcproto.NoBlockResponse:
if msg.Height < 0 {
return ErrInvalidHeight{Height: msg.Height, Reason: "negative height"}
Expand Down
4 changes: 3 additions & 1 deletion blocksync/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ func (bcR *Reactor) Receive(e p2p.Envelope) {
case *bcproto.BlockResponse:
bi, err := types.BlockFromProto(msg.Block)
if err != nil {
bcR.Logger.Error("Block content is invalid", "err", err)
bcR.Logger.Error("Peer sent us invalid block", "peer", e.Src, "msg", e.Message, "err", err)
bcR.Switch.StopPeerForError(e.Src, err)
return
}
var extCommit *types.ExtendedCommit
Expand All @@ -262,6 +263,7 @@ func (bcR *Reactor) Receive(e p2p.Envelope) {
bcR.Logger.Error("failed to convert extended commit from proto",
"peer", e.Src,
"err", err)
bcR.Switch.StopPeerForError(e.Src, err)
return
}
}
Expand Down

0 comments on commit 75a7861

Please sign in to comment.