Replace unsafe unwrap/expect calls with proper error handling in Phase 2 modules#393
Open
Rustix69 wants to merge 12 commits into
Open
Replace unsafe unwrap/expect calls with proper error handling in Phase 2 modules#393Rustix69 wants to merge 12 commits into
Rustix69 wants to merge 12 commits into
Conversation
….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>
…-unsafe-unwraps-phase1
- 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
mcelrath
requested changes
Apr 9, 2026
mcelrath
left a comment
Collaborator
There was a problem hiding this comment.
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
- 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
- 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
- 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 - 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, _>
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes : #302
Summary
Phase 2 of the unwrap/expect cleanup: replaces unsafe
unwrap()andexpect()calls with properResulthandling in production code.Files Changed
node/src/db/db_handlers.rs- DB row parsing, index lookups,MedianTimePast,Signature, extranoncenode/src/braid/mod.rs-reverse()HashMap lookup,highest_work_pathbead selectionnode/src/rpc_server.rs- HTTP client build,ArrayParams::insert, server build/local_addr,get_beadserializationnode/src/behaviour/mod.rs- Response channel sends (respond_with_beads,respond_with_beadhashes, etc.)node/src/template_creator.rs-encode_varint(replacedexpectwith manual varint encoding)