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

Minor refactoring for simulator's sake #143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

abailly
Copy link
Contributor

@abailly abailly commented Mar 13, 2025

This small PR introduces 2 changes in order to ease full consensus testing with "simulator". Arguably, those changes are useful and make sense in and of themselves.

Summary by CodeRabbit

  • New Features

    • Improved chain state management now clearly differentiates between an initial (genesis) state and an active header. This change enhances system safety and operational clarity.
    • Refined error handling now automatically falls back to a secure default when a required reference is missing, reducing potential disruptions.
  • Refactor

    • Updated simulation logic optimises state loading and management, ensuring more consistent and efficient behaviour during execution.

Copy link

coderabbitai bot commented Mar 13, 2025

Walkthrough

The changes introduce a new Tip<H: IsHeader> enum to clearly differentiate between a genesis state and a header state for chain selection. Several structs and methods are updated to adopt this type, including modifications in the consensus, store, and simulation modules. Additionally, error handling in the nonce evolution logic now uses a default value rather than an explicit error, and lifetime annotations are added to a simulation function to improve correctness in control flow.

Changes

File Change Summary
crates/amaru-consensus/.../chain_selection.rs Introduced a new Tip<H: IsHeader> enum; updated Fragment and ChainSelector struct signatures and method signatures (e.g., tip, start_from, select_roll_forward, select_rollback); added new trait implementations for encoding and header functionality.
crates/amaru-consensus/.../store.rs Modified error handling in the evolve_nonce method: replaced ok_or_else with unwrap_or to use a default header (Point::Origin) when missing; updated import statement to include Point.
simulation/amaru-sim/.../simulator.rs Updated the load_tip_from_store function by adding a lifetime parameter to the builder argument; altered control flow to return the builder immediately if the tip equals Origin, otherwise load and apply the header.

Sequence Diagram(s)

sequenceDiagram
    participant S as Simulator
    participant CS as ChainStore
    participant B as Builder

    S->>S: Check tip type
    alt Tip is Origin
        S->>B: Return builder unchanged
    else Tip is Specific
        S->>CS: Request header for tip
        CS-->>S: Return header (or panic if missing)
        S->>B: Update builder with the retrieved header
    end
Loading

Poem

In our code’s domain, a new tip now takes flight,
A genesis or header shining ever so bright.
With tweaks here and there, our chain stands secure,
Each function and flow is cleaner and pure.
Cheers to our devs—coding magic outright! 🎉

(Keep calm and code on!)


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c32e7da and 2bf1415.

📒 Files selected for processing (3)
  • crates/amaru-consensus/src/consensus/chain_selection.rs (10 hunks)
  • crates/amaru-consensus/src/consensus/store.rs (2 hunks)
  • simulation/amaru-sim/src/bin/amaru-sim/simulator.rs (1 hunks)

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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

@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: 0

🧹 Nitpick comments (1)
simulation/amaru-sim/src/bin/amaru-sim/simulator.rs (1)

166-172: Watch out for the panic!.
If you’re feeling generous, you might bubble up an error rather than scaring the entire program stiff with a panic. Otherwise, she’ll be right for internal testing.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c32e7da and 1acd5ec.

📒 Files selected for processing (3)
  • crates/amaru-consensus/src/consensus/chain_selection.rs (10 hunks)
  • crates/amaru-consensus/src/consensus/store.rs (2 hunks)
  • simulation/amaru-sim/src/bin/amaru-sim/simulator.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build on windows-latest with target x86_64-pc-windows-msvc
  • GitHub Check: Snapshots (preprod, 10.1.4)
🔇 Additional comments (17)
crates/amaru-consensus/src/consensus/store.rs (2)

15-15: Cheerio! Importing Point is spot-on.
No concerns here, old chap. This import is crucial for referencing the origin in case we run out of parents.


115-115: Double-check the shift from error to default.
Swapping an error for Point::Origin might be all right if you want to keep the chain going when no parent is found. However, do confirm you won’t be silently glossing over genuine issues.

simulation/amaru-sim/src/bin/amaru-sim/simulator.rs (1)

161-165: Neat usage of the 'a lifetime parameter.
Blimey, that’s a tidy approach to letting the function hand back the same builder with a matching lifetime, saving you from lifetimes mischief.

crates/amaru-consensus/src/consensus/chain_selection.rs (14)

16-17: Additional imports look grand.
All good here, mate. A tidy extension to the toolbox for handling Point and HASH_SIZE.


31-31: Top-notch pivot to Tip<H> for the anchor field.
Switching the anchor to Tip<H> clarifies whether we’re anchored at Genesis or a specific header. Good on you!


40-40: start_from with Tip<H> is a sweet shift.
This function elegantly sets up a fragment from either Genesis or an actual header. G’day for flexibility!


57-59: Method tip() now returns a Tip<H>.
You’re neatly distinguishing between Genesis and a live header. That’s the bee’s knees for chain clarity.


75-79: The new Tip<H> enum is positively spiffing!
This is an excellent approach to differentiate Genesis from an actual header. Keep calm and tip on.


81-95: Encoding Tip<H> via CBOR.
Your encode logic clearly handles either Genesis or Hdr scenario. Jolly fine, ensures consistent serialisation.


97-125: Implementing IsHeader for Tip<H>.
Crikey, that’s cunning! It means anywhere a header is required, Tip<H> can fill the role. A brilliant bit of polymorphism.


128-134: Converting from Option<H> to Tip<H>.
This is a neat way to avoid unwrapping. If you lack a real header, you get Genesis. Easy as pie!


212-212: Using .into() to convert to Tip<H> in the builder.
All gravy here, mate. A direct transformation is quite tidy.


219-219: Fragment::start_from gets a Tip<H>?
Spreading the usage of Tip<H> fosters consistency. It’s marvellous to see it in the fragment’s starting point.


271-271: self.tip = Tip::Hdr(header);
Right as rain. Assigning a real header to the chain’s tip is perfectly correct for the roll-forward scenario.


309-309: Set tip to Hdr(best_tip) on rollback.
When the chain’s rolled back and you find a best chain, you properly set it. Lovely stuff, no complaints here.


320-329: Caution with that panic!("Fragment has no tip").
We see the slip: height > 0 implies there must be a header. A compile-time check would be smashing, but for now at least you know where to step on the brake if something goes awry.


549-549: Asserting the tip is indeed Hdr(*rollback_point).
This test checks that your rollback logic does what it says on the tin. Good on you!

abailly added 2 commits March 13, 2025 14:43
This is useful in testing when we start following a chain from
scratch. To work, this also requires cooperation from
epoch_from_slot() which currently hard codes epoch lengths which
obviously won't work for small slot values.

Signed-off-by: Arnaud Bailly <[email protected]>
This means the chain selector starts with tip being 'Genesis'

Signed-off-by: Arnaud Bailly <[email protected]>
@abailly abailly force-pushed the abailly/refactoring-for-simulator branch from 1acd5ec to 2bf1415 Compare March 13, 2025 13:43
@abailly abailly requested a review from KtorZ March 13, 2025 13:43
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.

1 participant