Skip to content

feat(katana): add fact registry arg for init - #3158

Merged
kariy merged 7 commits into
dojoengine:mainfrom
cwkang1998:feat/pass-facts-registry
Apr 21, 2025
Merged

feat(katana): add fact registry arg for init#3158
kariy merged 7 commits into
dojoengine:mainfrom
cwkang1998:feat/pass-facts-registry

Conversation

@cwkang1998

@cwkang1998 cwkang1998 commented Apr 11, 2025

Copy link
Copy Markdown
Contributor

Description

This PR adds a new optional argument --settlement-facts-registry-contract to katana init which allows passing a custom fact registry contract.

Related issue

Fixes #3034

Tests

  • Yes
  • No, because they aren't needed
  • No, because I need help

Added to documentation?

Checklist

  • I've formatted my code (scripts/rust_fmt.sh, scripts/cairo_fmt.sh)
  • I've linted my code (scripts/clippy.sh, scripts/docs.sh)
  • I've commented my code
  • I've requested a review after addressing the comments

Summary by CodeRabbit

  • New Features
    • Added support for specifying a custom facts registry contract address when initializing settlement chains via the CLI.
    • Enabled updating the facts registry contract address in settlement chain settings.
  • Bug Fixes
    • Improved error handling to ensure required arguments are provided when using custom settlement chains.
  • Tests
    • Introduced new tests to verify correct parsing of custom facts registry arguments and enforcement of required parameters.
  • Documentation
    • Updated comment formatting for improved clarity in explorer port configuration.
    • Revised development setup instructions for generating database artifacts before running tests.

@coderabbitai

coderabbitai Bot commented Apr 11, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Ohayo sensei! This update introduces a new optional argument, settlement_facts_registry_contract, to the katana init CLI, allowing users to specify a custom facts registry contract address for the settlement chain. The initialization logic is updated to use this custom address when provided, with additional validation ensuring required arguments are present. For custom settlement chains, the custom facts registry address is now mandatory. The update also includes new tests to verify argument parsing and error handling. Separately, a minor change reformats comments in the explorer options without affecting functionality. Additionally, a new setter method was added to update the facts registry contract in the settlement provider.

Changes

File(s) Change Summary
bin/katana/src/cli/init/mod.rs Added settlement_facts_registry_contract field to InitArgs, updated initialization logic, added tests for argument parsing and error handling related to the facts registry contract.
bin/katana/src/cli/init/settlement.rs Added set_fact_registry method to SettlementChainProvider to update the facts registry contract address.
crates/katana/cli/src/options.rs Reformatted comments above explorer_port in ExplorerOptions struct; no functional code changes.
DEVELOPMENT.md Updated development instructions to use scripts/extract_test_db.sh instead of scripts/rebuild_test_artifacts.sh for generating DB artifacts before tests.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant InitArgs
    participant SettlementProvider

    User->>CLI: Run `katana init` with/without --settlement-facts-registry-contract
    CLI->>InitArgs: Parse arguments
    InitArgs->>CLI: Return parsed args (with optional facts registry contract)
    CLI->>SettlementProvider: Initialize with args
    alt Custom settlement chain
        SettlementProvider->>CLI: Require facts registry contract, error if missing
    else Known chain (Mainnet/Sepolia)
        alt Custom facts registry provided
            SettlementProvider->>CLI: Use custom facts registry contract
        else
            SettlementProvider->>CLI: Use default facts registry contract
        end
    end
    CLI->>User: Success or error message
Loading

Assessment against linked issues

Objective Addressed Explanation
Add support for passing a facts registry contract address to katana init (#3034)
Ensure katana init enforces/validates required arguments with custom facts registry (#3034)
Pre-register or allow mock facts registry on known networks (#3034)

Possibly related PRs

Suggested labels

katana


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 400cdaf and 4daf421.

📒 Files selected for processing (1)
  • DEVELOPMENT.md (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (1)
DEVELOPMENT.md (1)

31-31: Verify new test DB script reference
Ohayo sensei! The documentation now points to scripts/extract_test_db.sh for generating test DB artifacts, which matches the updated workflow. Please confirm that extract_test_db.sh exists in the scripts/ folder and is marked executable (chmod +x).


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

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.

@codecov

codecov Bot commented Apr 11, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 82.79570% with 16 lines in your changes missing coverage. Please review.

Project coverage is 55.65%. Comparing base (4ef0e2d) to head (4daf421).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
bin/katana/src/cli/init/mod.rs 85.55% 13 Missing ⚠️
bin/katana/src/cli/init/settlement.rs 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3158      +/-   ##
==========================================
+ Coverage   55.59%   55.65%   +0.05%     
==========================================
  Files         443      443              
  Lines       62964    63053      +89     
==========================================
+ Hits        35006    35089      +83     
- Misses      27958    27964       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

cwkang1998 added a commit to cwkang1998/book that referenced this pull request Apr 11, 2025
cwkang1998 added a commit to cwkang1998/book that referenced this pull request Apr 11, 2025
@kariy
kariy self-requested a review April 11, 2025 17:30

@glihm glihm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the contribution here @cwkang1998!

Even if piltover's facts registry address can be changed easily there: https://github.com/keep-starknet-strange/piltover/blob/161cb3f66d256e4d1211c6b50e5d353afb713a3e/src/config/interface.cairo#L77 this addition could make the deployment for testing easier using mocked facts registry straight away from Katana.

Any additional thoughts @kariy?

Comment thread bin/katana/src/cli/init/mod.rs Outdated
Comment thread bin/katana/src/cli/init/mod.rs Outdated
Comment thread bin/katana/src/cli/init/settlement.rs Outdated
Comment thread bin/katana/src/cli/init/settlement.rs
@cwkang1998
cwkang1998 force-pushed the feat/pass-facts-registry branch from 2a9723d to 5404e0b Compare April 17, 2025 05:34

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
bin/katana/src/cli/init/mod.rs (1)

159-185: Clean implementation of the facts registry options, sensei!

The code elegantly handles different settlement chains with proper conditional logic for the facts registry. For predefined chains (Mainnet and Sepolia), it uses the provided registry if available, otherwise falls back to defaults. For custom chains, it correctly requires the registry address, as agreed in previous discussions.

However, there's some code duplication between the Mainnet and Sepolia cases that could be refactored.

 let settlement_provider_result = match settlement_chain {
     SettlementChain::Mainnet => {
-        let provider = SettlementChainProvider::sn_mainnet();
-        Ok(match self.settlement_facts_registry_contract {
-            Some(fact_registry) => {
-                SettlementChainProvider::new(provider.url().clone(), *fact_registry)
-            }
-            None => provider,
-        })
+        create_settlement_provider(SettlementChainProvider::sn_mainnet(), self.settlement_facts_registry_contract)
     }
     SettlementChain::Sepolia => {
-        let provider = SettlementChainProvider::sn_sepolia();
-        Ok(match self.settlement_facts_registry_contract {
-            Some(fact_registry) => {
-                SettlementChainProvider::new(provider.url().clone(), *fact_registry)
-            }
-            None => provider,
-        })
+        create_settlement_provider(SettlementChainProvider::sn_sepolia(), self.settlement_facts_registry_contract)
     }
     #[cfg(feature = "init-custom-settlement-chain")]
     SettlementChain::Custom(url) => match self.settlement_facts_registry_contract {
         Some(fact_registry) => Ok(SettlementChainProvider::new(url, *fact_registry)),
         None => Err(anyhow::anyhow!(
             "Fact registry contract address is required for custom settlement chain"
         )),
     },
 };

And add this helper function:

fn create_settlement_provider(
    provider: SettlementChainProvider,
    facts_registry: Option<ContractAddress>,
) -> Result<SettlementChainProvider, anyhow::Error> {
    Ok(match facts_registry {
        Some(fact_registry) => SettlementChainProvider::new(provider.url().clone(), *fact_registry),
        None => provider,
    })
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 2a9723d and 5404e0b.

⛔ Files ignored due to path filters (6)
  • crates/dojo/core-cairo-test/Scarb.lock is excluded by !**/*.lock
  • crates/torii/types-test/Scarb.lock is excluded by !**/*.lock
  • examples/simple/Scarb.lock is excluded by !**/*.lock
  • examples/spawn-and-move/Scarb.lock is excluded by !**/*.lock
  • spawn-and-move-db.tar.gz is excluded by !**/*.gz
  • types-test-db.tar.gz is excluded by !**/*.gz
📒 Files selected for processing (2)
  • bin/katana/src/cli/init/mod.rs (3 hunks)
  • crates/katana/cli/src/options.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • crates/katana/cli/src/options.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (5)
bin/katana/src/cli/init/mod.rs (5)

71-75: Ohayo! New CLI argument for facts registry looks great!

This CLI argument addition allows specifying a custom facts registry contract, enhancing flexibility for different settlement configurations. The documentation is clear, and the arg dependencies are properly set up.


187-190: Nice error handling pattern, sensei!

The error handling is clean and propagates the errors properly in the context of the CLI arguments. Using the early return pattern with Some(Err(e)) ensures that configuration errors don't get lost.


428-454: Excellent test for CLI argument parsing!

This test ensures the new facts registry CLI argument is correctly parsed and stored in the InitArgs struct. The test provides good coverage for the happy path with all required arguments.


456-498: Good negative test case for required settlement arguments!

This test verifies that the CLI correctly enforces the required settlement arguments when using the facts registry argument. The test is comprehensive and checks for all the expected error messages.


500-529: Perfect error validation for custom settlement chains!

The test properly verifies that custom settlement chains require a facts registry contract address to be provided, validating both the error condition and the exact error message. Using an async test is appropriate since configure_from_args is an async method.

This test aligns with the discussion in the previous review comments about requiring the facts registry for custom settlement chains.

@cwkang1998
cwkang1998 requested review from glihm and kariy April 17, 2025 05:41

@kariy kariy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm retracting what I said in my last review. But I guess I wasn't clear enough anyway.

My assumption was that we don't want to allow setting arbitrary facts registry contract when we're settling on 'known' chain (i.e., mainnet and sepolia) because saya (at least for now) have a dependency on the Atlantic service. Hence, why I hardcoded the facts registries that Atlantic settles the proves to:

pub fn sn_mainnet() -> Self {
let url = Url::parse(CARTRIDGE_SN_MAINNET_PROVIDER).expect("valid url");
Self::new(url, ATLANTIC_FACT_REGISTRY_MAINNET)
}
pub fn sn_sepolia() -> Self {
let url = Url::parse(CARTRIDGE_SN_SEPOLIA_PROVIDER).expect("valid url");
Self::new(url, ATLANTIC_FACT_REGISTRY_SEPOLIA)
}

So, the latest changes you've made isn't exactly different from the last one in terms of the semantics.

But from I've seen during the development of saya, there were some cases where we were mocking some parts of the whole katana -> saya proving pipeline (iirc we're also mocking the facts registry at some point). So, it'd be convenient to set a different facts registry in which case having a setter function for it make sense.

So, I took the liberty to add back the setter function.

@cwkang1998

cwkang1998 commented Apr 19, 2025

Copy link
Copy Markdown
Contributor Author

I'm retracting what I said in my last review. But I guess I wasn't clear enough anyway.

My assumption was that we don't want to allow setting arbitrary facts registry contract when we're settling on 'known' chain (i.e., mainnet and sepolia) because saya (at least for now) have a dependency on the Atlantic service. Hence, why I hardcoded the facts registries that Atlantic settles the proves to:

pub fn sn_mainnet() -> Self {
let url = Url::parse(CARTRIDGE_SN_MAINNET_PROVIDER).expect("valid url");
Self::new(url, ATLANTIC_FACT_REGISTRY_MAINNET)
}
pub fn sn_sepolia() -> Self {
let url = Url::parse(CARTRIDGE_SN_SEPOLIA_PROVIDER).expect("valid url");
Self::new(url, ATLANTIC_FACT_REGISTRY_SEPOLIA)
}

So, the latest changes you've made isn't exactly different from the last one in terms of the semantics.

But from I've seen during the development of saya, there were some cases where we were mocking some parts of the whole katana -> saya proving pipeline (iirc we're also mocking the facts registry at some point). So, it'd be convenient to set a different facts registry in which case having a setter function for it make sense.

So, I took the liberty to add back the setter function.

Got it, sorry for the misunderstanding. Will do a rebase and see if there's anything else I am missing.

cwkang1998 and others added 6 commits April 20, 2025 02:13
@cwkang1998
cwkang1998 force-pushed the feat/pass-facts-registry branch from 136256a to 400cdaf Compare April 19, 2025 18:13
@cwkang1998
cwkang1998 requested a review from kariy April 19, 2025 18:27

@kariy kariy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@kariy
kariy merged commit 0a7895f into dojoengine:main Apr 21, 2025
@cwkang1998
cwkang1998 deleted the feat/pass-facts-registry branch April 21, 2025 13:25
kariy added a commit that referenced this pull request Apr 21, 2025
* feat(katana): add fact registry arg for `init`

This PR adds a new optional argument
`--settlement-facts-registry-contract` to `katana init`
which allows passing a custom fact registry contract.

Closes #3034

* fix: remove fact registry setter

* feat: error in custom init w/ no fact registry

* chore: refactor according to ai reviews

* use setter function

* fix test

* chore: remove unneccessary changes

---------

Co-authored-by: Ammar Arif <evergreenkary@gmail.com>
cwkang1998 added a commit to cwkang1998/book that referenced this pull request Apr 22, 2025
kariy added a commit that referenced this pull request Apr 28, 2025
* feat(katana): add fact registry arg for `init`

This PR adds a new optional argument
`--settlement-facts-registry-contract` to `katana init`
which allows passing a custom fact registry contract.

Closes #3034

* fix: remove fact registry setter

* feat: error in custom init w/ no fact registry

* chore: refactor according to ai reviews

* use setter function

* fix test

* chore: remove unneccessary changes

---------

Co-authored-by: Ammar Arif <evergreenkary@gmail.com>
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.

[katana] pass facts registry address to katana init

3 participants