8000 cometbft-light-client-verifier: reuse sign_bytes buffer by melekes · Pull Request #57 · cometbft/cometbft-rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cometbft-light-client-verifier: reuse sign_bytes buffer #57

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

Merged
merged 5 commits into from
May 28, 2025

Conversation

melekes
Copy link
Contributor
@melekes melekes commented Apr 28, 2025

@melekes

This comment was marked as resolved.

@melekes melekes requested a review from Copilot May 5, 2025 12:03
Copy link
@Copilot 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 ports improvements from the tendermint-rs project to reuse the sign_bytes buffer and refactor commit verification logic in the light-client verifier.

  • Added a new sign_bytes_into method in the vote module to reduce memory allocations.
  • Merged and refactored commit verification methods to combine validator and signer overlap checks.
  • Updated tests and changelog entries to document both improvements and breaking changes.

Reviewed Changes

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

Show a summary per file
File Description
light-client-verifier/src/verifier.rs Refactored commit verification logic and merged overlapping checks.
light-client-verifier/src/predicates.rs Introduced has_sufficient_validators_and_signers_overlap method.
cometbft/src/vote.rs Added sign_bytes_into to reuse the existing buffer for encoding.
.changelog/unreleased/improvements/35-reuse-sign-bytes.md Documented the buffer reuse improvement.
.changelog/unreleased/breaking-changes/33-avoid-chucking-signature-multiple-times.md Documented breaking changes in signature verification rework.

@melekes melekes self-assigned this May 14, 2025
@melekes melekes force-pushed the mina86_reuse_sign_bytes_buffer branch from 0fea176 to 8e62d56 Compare May 26, 2025 13:05
@melekes melekes requested a review from Copilot May 27, 2025 05:15
Copy link
@Copilot 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 optimizes the light client verifier by reusing a buffer for canonical vote sign bytes and introduces a new helper on SignedVote to write into that buffer, reducing allocations.

  • Reuse a pre-allocated sign_bytes buffer in NonAbsentCommitVotes
  • Add sign_bytes_into on SignedVote for buffered encoding
  • Update changelog with the improvement

Reviewed Changes

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

File Description
light-client-verifier/src/operations/voting_power.rs Add reusable sign_bytes buffer with initial capacity and update vote verification to reuse it
cometbft/src/vote.rs Introduce SignedVote::sign_bytes_into to encode into an existing buffer
.changelog/unreleased/improvements/35-reuse-sign-bytes.md Document reuse of sign_bytes buffer
Comments suppressed due to low confidence (1)

cometbft/src/vote.rs:439

  • Add unit tests for sign_bytes_into to verify it writes the expected canonical vote bytes into the provided buffer under various scenarios.
pub fn sign_bytes_into(&self, buf: &mut impl BufMut) -> Result<(), ProtobufError> {

self.sign_bytes.truncate(0);
vote.signed_vote
.sign_bytes_into(&mut self.sign_bytes)
.expect("buffer is resized if needed and encoding never fails");
Copy link
Preview
Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Avoid using expect on a Protobuf encoding result to prevent unexpected panics. Consider mapping the error into VerificationError and returning it instead of panicking.

Suggested change
.expect("buffer is resized if needed and encoding never fails");
.map_err(|e| VerificationError::encoding_error(e.to_string()))?;

Copilot uses AI. Check for mistakes.

@@ -436,6 +436,10 @@ impl SignedVote {
Protobuf::<RawCanonicalVote>::encode_length_delimited_vec(self.vote.clone())
}

Copy link
Preview
Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

[nitpick] Add a Rustdoc comment for sign_bytes_into explaining what buffer format it writes and when it should be used.

Suggested change
/// Writes the length-delimited protobuf-encoded representation of the canonical vote
/// into the provided buffer.
///
/// # Arguments
///
/// * `buf` - A mutable buffer that implements the `BufMut` trait, where the encoded
/// bytes will be written.
///
/// # Returns
///
/// Returns `Ok(())` if the operation is successful, or a `ProtobufError` if encoding fails.
///
/// # Usage
///
/// Use this function when you need to write the encoded bytes directly into a buffer
/// for efficiency, such as when preparing data for network transmission or storage.

Copilot uses AI. Check for mistakes.

@melekes melekes merged commit 49de8d7 into main May 28, 2025
21 of 22 checks passed
@melekes melekes deleted the mina86_reuse_sign_bytes_buffer branch May 28, 2025 07:21
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.

Port "light-client-verifier: reuse sign_bytes buffer" (tendermint-rs#1413)
2 participants
0