Skip to content

Commit

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

for performance reasons.
Closes #1964

---

#### PR checklist

- [ ] ~~Tests written/updated~~
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
  • Loading branch information
melekes authored Jan 26, 2024
1 parent 28ad4d2 commit f4d73cd
Show file tree
Hide file tree
Showing 3 changed files with 8 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))
7 changes: 3 additions & 4 deletions internal/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 internal/blocksync/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,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 @@ -266,6 +267,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 f4d73cd

Please sign in to comment.