From dfe24ed9d79deda0d0b354c77915f7236377738d Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 24 Jun 2023 16:41:53 -0700 Subject: [PATCH] Remove all unwrap() calls --- backend/rust-gbt/src/audit_transaction.rs | 2 +- backend/rust-gbt/src/gbt.rs | 19 +++++++++++++------ backend/rust-gbt/src/lib.rs | 8 ++++++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/backend/rust-gbt/src/audit_transaction.rs b/backend/rust-gbt/src/audit_transaction.rs index 90fe275ae..438058887 100644 --- a/backend/rust-gbt/src/audit_transaction.rs +++ b/backend/rust-gbt/src/audit_transaction.rs @@ -53,7 +53,7 @@ impl PartialOrd for AuditTransaction { impl Ord for AuditTransaction { fn cmp(&self, other: &AuditTransaction) -> Ordering { - self.partial_cmp(other).unwrap() + self.partial_cmp(other).expect("score will never be NaN") } } diff --git a/backend/rust-gbt/src/gbt.rs b/backend/rust-gbt/src/gbt.rs index b9dab0fd3..e78f81604 100644 --- a/backend/rust-gbt/src/gbt.rs +++ b/backend/rust-gbt/src/gbt.rs @@ -34,7 +34,7 @@ impl PartialOrd for TxPriority { } impl Ord for TxPriority { fn cmp(&self, other: &Self) -> Ordering { - self.partial_cmp(other).unwrap() + self.partial_cmp(other).expect("score will never be NaN") } } @@ -51,6 +51,7 @@ pub fn gbt(mempool: &mut HashMap) -> Option { // Initialize working structs for (uid, tx) in mempool { let audit_tx = AuditTransaction::from_thread_transaction(tx); + // Safety: audit_pool and mempool_array must always contain the same transactions audit_pool.insert(audit_tx.uid, audit_tx); mempool_array.push_back(*uid); } @@ -62,8 +63,12 @@ pub fn gbt(mempool: &mut HashMap) -> Option { // Sort by descending ancestor score mempool_array.make_contiguous().sort_unstable_by(|a, b| { - let a_tx = audit_pool.get(a).unwrap(); - let b_tx = audit_pool.get(b).unwrap(); + let a_tx = audit_pool + .get(a) + .expect("audit_pool contains exact same txes as mempool_array"); + let b_tx = audit_pool + .get(b) + .expect("audit_pool contains exact same txes as mempool_array"); b_tx.cmp(a_tx) }); @@ -224,6 +229,7 @@ fn set_relatives(txid: u32, audit_pool: &mut HashMap) { set_relatives(*parent_id, audit_pool); if let Some(parent) = audit_pool.get_mut(parent_id) { + // Safety: ancestors must always contain only txes in audit_pool ancestors.insert(*parent_id); parent.children.insert(txid); for ancestor in &parent.ancestors { @@ -237,7 +243,9 @@ fn set_relatives(txid: u32, audit_pool: &mut HashMap) { let mut total_sigops: u32 = 0; for ancestor_id in &ancestors { - let ancestor = audit_pool.get(ancestor_id).unwrap(); + let ancestor = audit_pool + .get(ancestor_id) + .expect("audit_pool contains all ancestors"); total_fee += ancestor.fee; total_weight += ancestor.weight; total_sigops += ancestor.sigops; @@ -283,8 +291,7 @@ fn update_descendants( } else { return; } - while !descendant_stack.is_empty() { - let next_txid: u32 = descendant_stack.pop().unwrap(); + while let Some(next_txid) = descendant_stack.pop() { if let Some(descendant) = audit_pool.get_mut(&next_txid) { // remove root tx as ancestor descendant.ancestors.remove(&root_txid); diff --git a/backend/rust-gbt/src/lib.rs b/backend/rust-gbt/src/lib.rs index b59c11bc9..6767a8f64 100644 --- a/backend/rust-gbt/src/lib.rs +++ b/backend/rust-gbt/src/lib.rs @@ -27,7 +27,9 @@ pub fn make(mempool_buffer: Uint8Array, callback: JsFunction) -> Result<()> { map.insert(tx.uid, tx); } - let mut global_map = THREAD_TRANSACTIONS.lock().unwrap(); + let mut global_map = THREAD_TRANSACTIONS + .lock() + .map_err(|_| napi::Error::from_reason("THREAD_TRANSACTIONS Mutex poisoned"))?; *global_map = map; run_in_thread(callback) @@ -37,7 +39,9 @@ pub fn make(mempool_buffer: Uint8Array, callback: JsFunction) -> Result<()> { ts_args_type = "newTxs: Uint8Array, removeTxs: Uint8Array, callback: (result: GbtResult) => void" )] pub fn update(new_txs: Uint8Array, remove_txs: Uint8Array, callback: JsFunction) -> Result<()> { - let mut map = THREAD_TRANSACTIONS.lock().unwrap(); + let mut map = THREAD_TRANSACTIONS + .lock() + .map_err(|_| napi::Error::from_reason("THREAD_TRANSACTIONS Mutex poisoned"))?; for tx in ThreadTransaction::batch_from_buffer(&new_txs) { map.insert(tx.uid, tx); }