Skip to content

fix: validate tipsets in chain_exchange_full_tipset#6828

Open
hanabi1224 wants to merge 1 commit intomainfrom
hm/validate-chain_exchange_full_tipset
Open

fix: validate tipsets in chain_exchange_full_tipset#6828
hanabi1224 wants to merge 1 commit intomainfrom
hm/validate-chain_exchange_full_tipset

Conversation

@hanabi1224
Copy link
Copy Markdown
Contributor

@hanabi1224 hanabi1224 commented Mar 31, 2026

Summary of changes

Changes introduced in this pull request:

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Outside contributions

  • I have read and agree to the CONTRIBUTING document.
  • I have read and agree to the AI Policy document. I understand that failure to comply with the guidelines will lead to rejection of the pull request.

Summary by CodeRabbit

  • Refactor
    • Updated tipset type handling in validation and network processing functions to enhance internal consistency across the codebase.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 31, 2026

Walkthrough

A new TipsetLike trait is introduced that defines a common read-only interface with methods for epoch(), key(), parents(), and parent_state(). The trait is implemented for both Tipset and FullTipset, exported from the blocks module, and used to make validation logic in chain_sync generic over tipset-like types.

Changes

Cohort / File(s) Summary
Trait Definition and Implementation
src/blocks/tipset.rs
Added new public TipsetLike trait with four read-only methods. Implemented trait for Tipset and FullTipset by delegating to their existing methods.
Module Exports
src/blocks/mod.rs
Added TipsetLike to the public re-exports from the tipset module.
Validation Refactoring
src/chain_sync/network_context.rs
Updated imports to include TipsetLike. Refactored validate_network_tipsets helper to accept generic &[T] where T: TipsetLike instead of concrete &[Tipset]. Modified callers to use the updated validation function.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • LesnyRumcajs
  • sudo-shashank
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 76.92% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: validate tipsets in chain_exchange_full_tipset' accurately describes a key change in the pull request—making chain_exchange_full_tipset validate returned tipsets. While the PR also adds a TipsetLike trait for code abstraction, the title focuses on the main fix objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch hm/validate-chain_exchange_full_tipset
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch hm/validate-chain_exchange_full_tipset

Comment @coderabbitai help to get the list of available commands and usage tips.

@hanabi1224 hanabi1224 marked this pull request as ready for review March 31, 2026 13:14
@hanabi1224 hanabi1224 requested a review from a team as a code owner March 31, 2026 13:14
@hanabi1224 hanabi1224 requested review from LesnyRumcajs and sudo-shashank and removed request for a team March 31, 2026 13:14
Copy link
Copy Markdown
Contributor

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/chain_sync/network_context.rs (1)

205-218: ⚠️ Potential issue | 🟡 Minor

Apply validation to chain_exchange_full_tipsets matching chain_exchange_full_tipset.

The batch method uses |_| true while the singular method validates chain integrity via validate_network_tipsets(tipsets, tsk). This validation ensures:

  1. The first tipset matches the requested key
  2. All tipsets form a proper chain by parent references

Without validation, a malicious peer can return unchained or mismatched tipsets that get persisted directly to the blockstore. Both methods fetch and persist similar data; the singular method's validation should apply to the batch variant as well.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/chain_sync/network_context.rs` around lines 205 - 218, The batch method
chain_exchange_full_tipsets currently passes |_| true to
handle_chain_exchange_request allowing unvalidated tipsets; update it to perform
the same validation used by chain_exchange_full_tipset by replacing the
predicate closure with one that calls validate_network_tipsets(tipsets, tsk) and
returns its boolean result. Locate chain_exchange_full_tipsets, change the
predicate passed into handle_chain_exchange_request to run
validate_network_tipsets on the fetched tipsets and the requested tsk (mirroring
chain_exchange_full_tipset), and ensure any necessary imports or visibility for
validate_network_tipsets are present so invalid or unchained tipsets are
rejected before persisting.
🧹 Nitpick comments (1)
src/blocks/tipset.rs (1)

272-279: Consider adding documentation for the public TipsetLike trait.

As a public trait that abstracts the common interface between Tipset and FullTipset, a brief doc comment explaining its purpose would help consumers understand when to use it versus concrete types.

📝 Suggested documentation
+/// A common read-only interface for tipset-like types.
+///
+/// This trait abstracts over [`Tipset`] and [`FullTipset`], enabling generic
+/// validation and processing of tipsets from network peers.
 pub trait TipsetLike {
+    /// Returns the epoch (height) of this tipset.
     fn epoch(&self) -> ChainEpoch;
+    /// Returns the key that uniquely identifies this tipset.
     fn key(&self) -> &TipsetKey;
+    /// Returns the key of the parent tipset.
     fn parents(&self) -> &TipsetKey;
+    /// Returns the parent state root CID.
     #[allow(dead_code)]
     fn parent_state(&self) -> &Cid;
 }

As per coding guidelines: "Document public functions and structs with doc comments".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/blocks/tipset.rs` around lines 272 - 279, Add a doc comment for the
public trait TipsetLike explaining its purpose as an abstraction over Tipset and
FullTipset, when to use it, and what each method represents; update the trait
definition for TipsetLike to include a short summary line and brief descriptions
for the methods epoch, key, parents, and parent_state so consumers know this
trait exposes the canonical epoch, tipset key, parent key, and parent state CID
for types like Tipset and FullTipset.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/chain_sync/network_context.rs`:
- Around line 205-218: The batch method chain_exchange_full_tipsets currently
passes |_| true to handle_chain_exchange_request allowing unvalidated tipsets;
update it to perform the same validation used by chain_exchange_full_tipset by
replacing the predicate closure with one that calls
validate_network_tipsets(tipsets, tsk) and returns its boolean result. Locate
chain_exchange_full_tipsets, change the predicate passed into
handle_chain_exchange_request to run validate_network_tipsets on the fetched
tipsets and the requested tsk (mirroring chain_exchange_full_tipset), and ensure
any necessary imports or visibility for validate_network_tipsets are present so
invalid or unchained tipsets are rejected before persisting.

---

Nitpick comments:
In `@src/blocks/tipset.rs`:
- Around line 272-279: Add a doc comment for the public trait TipsetLike
explaining its purpose as an abstraction over Tipset and FullTipset, when to use
it, and what each method represents; update the trait definition for TipsetLike
to include a short summary line and brief descriptions for the methods epoch,
key, parents, and parent_state so consumers know this trait exposes the
canonical epoch, tipset key, parent key, and parent state CID for types like
Tipset and FullTipset.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f3c6ad36-0e45-467a-a450-d345d171e177

📥 Commits

Reviewing files that changed from the base of the PR and between 0b7f2b4 and 19346b5.

📒 Files selected for processing (3)
  • src/blocks/mod.rs
  • src/blocks/tipset.rs
  • src/chain_sync/network_context.rs

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