Skip to content

Replace unsafe unwrap/expect calls with proper error handling in Phase 2 modules#393

Open
Rustix69 wants to merge 12 commits into
braidpool:devfrom
Rustix69:fix/replace-unsafe-unwraps-phase2
Open

Replace unsafe unwrap/expect calls with proper error handling in Phase 2 modules#393
Rustix69 wants to merge 12 commits into
braidpool:devfrom
Rustix69:fix/replace-unsafe-unwraps-phase2

Conversation

@Rustix69

Copy link
Copy Markdown

Fixes : #302

Summary

Phase 2 of the unwrap/expect cleanup: replaces unsafe unwrap() and expect() calls with proper Result handling in production code.

Files Changed

  • node/src/db/db_handlers.rs - DB row parsing, index lookups, MedianTimePast, Signature, extranonce
  • node/src/braid/mod.rs - reverse() HashMap lookup, highest_work_path bead selection
  • node/src/rpc_server.rs - HTTP client build, ArrayParams::insert, server build/local_addr, get_bead serialization
  • node/src/behaviour/mod.rs - Response channel sends (respond_with_beads, respond_with_beadhashes, etc.)
  • node/src/template_creator.rs - encode_varint (replaced expect with manual varint encoding)

Rustix69 and others added 6 commits January 30, 2026 23:16
….rs and stratum.rs

- main.rs: Fix 20 unsafe unwraps with proper Result handling
  - Database initialization with map_err
  - Shell expansion for datadir path
  - RPC server startup error propagation
  - Boot peer and multiaddr parsing with graceful skip
  - Bead ID lookups with continue on error
  - Timestamp and IBD handling with fallbacks
  - Fixed silent failure in initial bead fetch by propagating errors

- stratum.rs: Fix 26 unsafe unwraps in production code
  - JSON serialization with error propagation
  - Coinbase/transaction decoding with StratumErrors
  - Version rolling mask and ntime/nonce parsing
  - Witness commitment and extranonce handling
  - Job notification serialization

All 72 tests pass. Code formatted with cargo fmt.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- node/src/db/db_handlers.rs
- node/src/braid/mod.rs
- node/src/rpc_server.rs
- node/src/behaviour/mod.rs
- node/src/template_creator.rs

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

t-ACK .

@Rustix69 Rustix69 requested a review from Sansh2356 March 6, 2026 20:17
@mcelrath mcelrath self-requested a review April 9, 2026 11:30

@mcelrath mcelrath left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hey @Rustix69 do you think you could fix the merge conflicts and address the following issues?

● Rust Review: Replace unsafe unwrap/expect calls with proper error handling in Phase 2 modules (PR #393)

Summary

Review complete. There’s a build‑blocking edit in rpc_server.rs, plus a new silent‑failure path in DB fetch logic.

Findings

🔴 Critical

  1. parse_arguments is syntactically incomplete (won’t compile).
    The new function’s match never closes, and the function never closes before the next #[derive] block, so rpc_server.rs
    doesn’t parse.
    File: node/src/rpc_server.rs (around lines ~112–145)

🟠 High

  1. fetch_bead_by_bead_hash now silently drops row‑parse errors.
    The map(|row| { ... Ok(()) }) produces Option<Result<_, DBErrors>>, but the inner Err is never checked. This converts parse
    failures into “success” with partially populated fetched_bead.
    File: node/src/db/db_handlers.rs

🟡 Medium

  1. get_bead added unused JSON serialization with wrong return type.
    The method returns Bead, but now constructs String via serde_json::to_string and discards the Result, adding a must_use
    warning and wasted work.
    File: node/src/rpc_server.rs
  2. Missing parent mapping now silently drops edges.
    Replacing unwrap() with if let Some avoids panic but hides invariant violations; you likely want to surface/log and abort
    to avoid corrupting HWP calculations.
    File: node/src/braid/mod.rs

Code Samples

   // rpc_server.rs (parse_arguments is incomplete)
   pub async fn parse_arguments(...) -> () {
       ...
       let (rpc_method, method_params) = match cli_command {
           RpcCommand::AddBead { ... } => { ... }
           RpcCommand::GetBead { ... } => { ... }
   // <-- missing closing braces for match + function

   // db_handlers.rs: inner Result is ignored
   match sqlx::query("SELECT * FROM bead WHERE hash = ?")
       .bind(...)
       .map(|row| { ... Ok(()) })
       .fetch_optional(...)
       .await
   {
       Ok(_rows) => { /* _rows: Option<Result<_, DBErrors>> ignored */ }
       Err(e) => return Err(DBErrors::TupleNotFetched { ... }),
   }

   // rpc_server.rs: unused JSON serialization with wrong return type
   match bead {
       Some(bead) => {
           let json = serde_json::to_string(&bead)?; // String
           Ok(json) // Result<String, _> (unused)
       }
       None => Err(...)
   }
   bead.ok_or_else(...) // returns Result<Bead, _>

@Rustix69

Rustix69 commented Apr 9, 2026

Copy link
Copy Markdown
Author

Hey @mcelrath I need 2 days i will push the fixes. Thanks

…rs instead of silently succeeding, and restore strict parent-mapping invariant handling in braid reverse traversal.
@Rustix69 Rustix69 requested a review from mcelrath April 9, 2026 21:03
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.

3 participants