Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

return cached signature on regression request#1

Open
sh-cha wants to merge 2 commits into
mainfrom
feat/return-existing-sign
Open

return cached signature on regression request#1
sh-cha wants to merge 2 commits into
mainfrom
feat/return-existing-sign

Conversation

@sh-cha
Copy link
Copy Markdown

@sh-cha sh-cha commented May 27, 2025

Summary by CodeRabbit

  • New Features
    • Introduced signature caching to prevent redundant signing operations, improving efficiency and helping to prevent double signing.
  • Bug Fixes
    • Ensured that old signature records are pruned automatically, retaining only recent entries for optimal performance.
  • Tests
    • Added tests to verify correct addition and pruning of signature records.

@sh-cha sh-cha requested a review from beer-1 May 27, 2025 06:28
@sh-cha sh-cha self-assigned this May 27, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 27, 2025

Walkthrough

A new mechanism for caching and pruning recently signed messages was added to the consensus state, using a BTreeMap to track signatures and prevent double-signing. The SignableMsg enum now supports cloning, and the signing process in sessions checks for and reuses cached signatures, storing new ones after signing.

Changes

File(s) Change Summary
src/chain/state.rs Added last_signs field to State for tracking signed messages; new methods for storing, retrieving, and pruning signatures; added tests for this logic.
src/privval.rs Made SignableMsg derive the Clone trait.
src/session.rs Modified signing logic to handle regression errors by checking for existing cached signatures before signing; saves new signatures after signing; added private helper methods for these operations.

Poem

In the ledger's gentle thrum and hum,
A rabbit caches signs—no need to rerun!
With pruning paws it sweeps the past,
Ensuring double-signs are never cast.
Now signatures hop, safe and sound,
In a BTreeMap, neatly bound.
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 75f259c and d82ea05.

📒 Files selected for processing (1)
  • src/session.rs (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/session.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-push-image
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa7059c and 75f259c.

📒 Files selected for processing (3)
  • src/chain/state.rs (8 hunks)
  • src/privval.rs (1 hunks)
  • src/session.rs (4 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
src/chain/state.rs

[error] 1-350: cargo fmt check failed due to code formatting differences. Please run 'cargo fmt' to fix formatting issues.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-push-image
🔇 Additional comments (7)
src/privval.rs (1)

24-24: LGTM! Clone derivation enables signature caching.

The addition of the Clone trait to SignableMsg is necessary to support the new signature caching mechanism. This allows signable messages to be stored and retrieved from the cache without ownership issues.

src/session.rs (3)

131-133: LGTM! Early return optimization for cached signatures.

The early return when a cached signature is found is an effective optimization that prevents redundant signing operations. The logic properly checks for existing signatures before proceeding with the signing process.


166-166: LGTM! Proper signature caching after successful signing.

The signature is correctly cached after successful signing and before returning the response. The use of clone() is appropriate here since the signable message needs to be stored in the cache while also being returned to the caller.


231-242: LGTM! Well-structured caching helper methods.

The private helper methods are well-designed:

  • check_existing_sign: Properly extracts consensus state and queries the cache
  • save_sign: Correctly stores the signature with appropriate locking

Both methods handle the mutex locking appropriately and extract the necessary state information for cache operations.

src/chain/state.rs (3)

30-30: LGTM! Appropriate data structure for signature caching.

The BTreeMap<(Height, Round, i8), SignableMsg> is a good choice for caching signatures:

  • The key tuple (Height, Round, i8) uniquely identifies consensus states
  • BTreeMap provides efficient lookups and ordered iteration for pruning
  • SignableMsg values can now be cloned thanks to the trait derivation

53-53: LGTM! Proper field initialization.

The last_signs field is correctly initialized as an empty BTreeMap in both constructor methods (load_state and write_initial_state).

Also applies to: 180-180


214-221: LGTM! Efficient cache operations with pruning.

The cache methods are well-implemented:

  • get_last_vote: Uses cloned() to return an owned copy without affecting the cache
  • save_sign: Implements intelligent pruning by retaining only entries within 100 heights of the current height

The pruning logic height.value() < 100 + key.0.value() effectively keeps recent signatures while preventing unbounded memory growth.

Comment thread src/chain/state.rs
Comment on lines +333 to +373
#[test]
fn test_save_sign() {
let mut state = State {
consensus_state: state!(1, 1, 0, None),
state_file_path: EXAMPLE_PATH.into(),
last_signs: BTreeMap::new(),
};

state.save_sign(Height::from(1 as u32), Round::from(1 as u16), 0, SignableMsg::Proposal(Proposal{
msg_type: Type::Proposal,
pol_round: None,
block_id: None,
timestamp: None,
signature: None,
height: Height::from(1 as u32),
round: Round::from(1 as u16),
}));

assert_eq!(state.last_signs.len(), 1);
state.save_sign(Height::from(100 as u32), Round::from(1 as u16), 0, SignableMsg::Proposal(Proposal{
msg_type: Type::Proposal,
pol_round: None,
block_id: None,
timestamp: None,
signature: None,
height: Height::from(100 as u32),
round: Round::from(1 as u16),
}));
assert_eq!(state.last_signs.len(), 2);

state.save_sign(Height::from(101 as u32), Round::from(1 as u16), 0, SignableMsg::Proposal(Proposal{
msg_type: Type::Proposal,
pol_round: None,
block_id: None,
timestamp: None,
signature: None,
height: Height::from(101 as u32),
round: Round::from(1 as u16),
}));
assert_eq!(state.last_signs.len(), 2);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix code formatting issues.

The test code demonstrates the pruning functionality well, but there are formatting issues that need to be addressed.

Based on the pipeline failure, please run cargo fmt to fix the formatting issues:

cargo fmt

The test logic itself is sound - it verifies that:

  1. Signatures are added to the cache
  2. Pruning occurs when height advances beyond the 100-height window
🤖 Prompt for AI Agents
In src/chain/state.rs around lines 333 to 373, the test_save_sign function has
formatting issues causing pipeline failures. Run the Rust formatter by executing
`cargo fmt` in the project root to automatically fix indentation, spacing, and
line breaks according to Rust style guidelines. This will resolve the formatting
problems without altering the test logic.

@beer-1
Copy link
Copy Markdown
Member

beer-1 commented May 28, 2025

I think this should be done with more cares like the signing block is exactly same or not

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants