Skip to content

Conversation

@codaMW
Copy link

@codaMW codaMW commented Jan 4, 2026

Summary

This PR refactors how MOSTRO_PUBKEY is resolved in the CLI to avoid
panic-based startup failures and follow idiomatic Rust error handling.

Changes

  • Resolve MOSTRO_PUBKEY from:
    • --mostropubkey CLI flag, or
    • MOSTRO_PUBKEY environment variable
  • Replace expect() with proper Option -> Result conversion
  • Provide a clear, actionable error message when the pubkey is missing
  • Remove unused imports after refactor

Motivation

The previous implementation panicked if MOSTRO_PUBKEY was not set,
causing abrupt CLI termination. This change improves user experience,
maintainability, and aligns with Rust best practices.

Testing

  • cargo fmt
  • cargo check
  • cargo test

Notes

This change is backward compatible and does not alter runtime behavior
when MOSTRO_PUBKEY is correctly provided.

Summary by CodeRabbit

  • Refactor
    • Improved configuration handling for MOSTRO_PUBKEY with enhanced error messaging. The key can now be provided via CLI argument or environment variable.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 4, 2026

Walkthrough

A resolver function resolve_mostro_pubkey was introduced to consolidate MOSTRO_PUBKEY retrieval from CLI arguments or environment variables. The initialization logic was updated to use this resolver instead of accessing environment variables directly, streamlining key resolution and improving error handling.

Changes

Cohort / File(s) Summary
MOSTRO_PUBKEY resolver refactoring
src/cli.rs
Added resolve_mostro_pubkey(cli: &Cli) -> Result<String> function to centralize MOSTRO_PUBKEY resolution with CLI precedence over environment variables; updated init_context to delegate key resolution to this function; removed redundant direct environment variable access patterns.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 With whiskers twitching, I refactor with glee,
A resolver now channels the key, wild and free!
From CLI or env, one path they all take,
No tangled vines now—just clean code we make! 🌿✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ 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 directly summarizes the main change: refactoring MOSTRO_PUBKEY resolution logic and removing startup panics through improved error handling.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d520e38 and abeaef7.

📒 Files selected for processing (1)
  • src/cli.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/cli.rs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
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.

Actionable comments posted: 1

Caution

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

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

62-1476: Fix critical syntax errors preventing compilation.

The function has two blocking issues:

  1. Type mismatch: Line 62 declares return type FiatList, but line 1467 uses the ? operator which requires Result<FiatList>.
  2. Missing closing brace: The for loop starting at line 1469 is never closed. Lines 1473-1475 (sort and return) appear outside the loop scope.
🔎 Proposed fix
-pub fn load_fiat_values() -> FiatList {
+pub fn load_fiat_values() -> Result<FiatList> {
     let fiat_names = r#"
     {
       ...
     }"#;
 
     // Parse fiat names
     let fiat_json = serde_json::from_str(fiat_names).map_err(|e| anyhow::anyhow!("Failed to parse fiat names: {}", e))?;
     let mut fiatlist = FiatList::new();
     for elem in fiat_json.iter() {
         fiatlist.push((elem.0.to_string(), elem.1.name.clone()));
+    }
     
     //Return list
     fiatlist.sort_by(|a, b| a.0.cmp(&b.0));
 
-    fiatlist
+    Ok(fiatlist)
 }

Note: Callers of this function will need updating to handle the Result.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a2f0716 and d520e38.

📒 Files selected for processing (4)
  • src/.fiat.rs.un~
  • src/cli.rs
  • src/fiat.rs
  • src/fiat.rs~
🧰 Additional context used
🧠 Learnings (6)
📓 Common learnings
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/cli.rs:301-306
Timestamp: 2025-09-12T20:02:14.291Z
Learning: The mostro-cli is designed as a test client that allows users to test both regular user operations and admin operations from the terminal. Therefore, MOSTRO_PUBKEY should always be required regardless of whether NSEC_PRIVKEY is present, as both user and admin operations need to know which Mostro instance to interact with.
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/cli/add_invoice.rs:80-82
Timestamp: 2025-09-09T19:58:58.506Z
Learning: In the mostro-cli codebase, when the trade_index parameter isn't needed inside a function like wait_for_dm, it should be made Optional<i64> instead of requiring callers to pass hardcoded values like 0. This makes the API clearer and avoids unnecessary computation for resolving indices when they're not used.
📚 Learning: 2025-09-12T20:02:14.291Z
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/cli.rs:301-306
Timestamp: 2025-09-12T20:02:14.291Z
Learning: The mostro-cli is designed as a test client that allows users to test both regular user operations and admin operations from the terminal. Therefore, MOSTRO_PUBKEY should always be required regardless of whether NSEC_PRIVKEY is present, as both user and admin operations need to know which Mostro instance to interact with.

Applied to files:

  • src/cli.rs
📚 Learning: 2025-09-09T19:58:58.506Z
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/cli/add_invoice.rs:80-82
Timestamp: 2025-09-09T19:58:58.506Z
Learning: In the mostro-cli codebase, when the trade_index parameter isn't needed inside a function like wait_for_dm, it should be made Optional<i64> instead of requiring callers to pass hardcoded values like 0. This makes the API clearer and avoids unnecessary computation for resolving indices when they're not used.

Applied to files:

  • src/cli.rs
📚 Learning: 2025-09-14T14:01:01.728Z
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/util.rs:614-619
Timestamp: 2025-09-14T14:01:01.728Z
Learning: In the mostro-cli codebase, the user arkanoider indicated that accessing `public_key` without parentheses (as a field) is valid, even though most of the codebase uses `public_key()` method calls. This suggests there may be `Deref` implementation or other Rust features that allow both syntaxes to work.

Applied to files:

  • src/cli.rs
📚 Learning: 2025-09-13T20:48:31.581Z
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/cli/send_msg.rs:89-110
Timestamp: 2025-09-13T20:48:31.581Z
Learning: In mostro-cli's send_msg.rs, arkanoider confirmed that tokio::spawn with async move is correct and compiles without errors when using PublicKey (which implements Copy). The spawning approach is acceptable for DM sending in this context.

Applied to files:

  • src/cli.rs
📚 Learning: 2025-09-13T20:48:31.581Z
Learnt from: arkanoider
Repo: MostroP2P/mostro-cli PR: 135
File: src/cli/send_msg.rs:89-110
Timestamp: 2025-09-13T20:48:31.581Z
Learning: In mostro-cli's send_msg.rs, tokio::spawn with async move is valid for sending DMs when PublicKey implements Copy, and arkanoider confirmed this approach works without memory issues or compilation errors.

Applied to files:

  • src/cli.rs
🧬 Code graph analysis (1)
src/cli.rs (1)
src/util/messaging.rs (1)
  • var (214-216)
🔇 Additional comments (4)
src/fiat.rs (1)

906-915: LGTM! MWK currency entry is well-formed.

The Malawian Kwacha entry follows the correct structure and is properly positioned alphabetically after MUR.

src/cli.rs (3)

43-43: LGTM! Import simplification is appropriate.

The import correctly reflects that set_var is used via this path while var is now fully qualified at its call site (line 376).


373-385: Excellent error handling improvement!

The resolver properly prioritizes CLI flag over environment variable and replaces panic-prone expect() with idiomatic Result-based error handling. The error message clearly guides users on how to provide the required value.

Based on learnings, MOSTRO_PUBKEY is correctly enforced as required for all operations.


419-421: LGTM! Proper integration of the resolver.

The resolver is correctly invoked with error propagation, and PublicKey parsing errors are also properly handled.

Copy link
Member

@Catrya Catrya left a comment

Choose a reason for hiding this comment

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

Hi @codaMW, this commit 454030c is not related to this PR, please fix it. Commits should not be mixed between PRs

@codaMW
Copy link
Author

codaMW commented Jan 5, 2026 via email

@codaMW codaMW force-pushed the fix-graceful-mostro-pubkey branch from d520e38 to abeaef7 Compare January 6, 2026 17:23
@codaMW
Copy link
Author

codaMW commented Jan 6, 2026

Thanks @Catrya for the quick feedback! I've force-pushed a cleaned-up version:

  • Removed all unrelated fiat commits and backup files
  • Now contains only the MOSTRO_PUBKEY resolution improvement
  • Replaces panic with proper error handling and prioritizes CLI flag over env var

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.

2 participants