Remove all unwrap() calls

This commit is contained in:
junderw 2023-06-24 16:41:53 -07:00 committed by Mononaut
parent 4661bea2f0
commit dfe24ed9d7
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E
3 changed files with 20 additions and 9 deletions

View File

@ -53,7 +53,7 @@ impl PartialOrd for AuditTransaction {
impl Ord for AuditTransaction { impl Ord for AuditTransaction {
fn cmp(&self, other: &AuditTransaction) -> Ordering { fn cmp(&self, other: &AuditTransaction) -> Ordering {
self.partial_cmp(other).unwrap() self.partial_cmp(other).expect("score will never be NaN")
} }
} }

View File

@ -34,7 +34,7 @@ impl PartialOrd for TxPriority {
} }
impl Ord for TxPriority { impl Ord for TxPriority {
fn cmp(&self, other: &Self) -> Ordering { 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<u32, ThreadTransaction>) -> Option<GbtResult> {
// Initialize working structs // Initialize working structs
for (uid, tx) in mempool { for (uid, tx) in mempool {
let audit_tx = AuditTransaction::from_thread_transaction(tx); 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); audit_pool.insert(audit_tx.uid, audit_tx);
mempool_array.push_back(*uid); mempool_array.push_back(*uid);
} }
@ -62,8 +63,12 @@ pub fn gbt(mempool: &mut HashMap<u32, ThreadTransaction>) -> Option<GbtResult> {
// Sort by descending ancestor score // Sort by descending ancestor score
mempool_array.make_contiguous().sort_unstable_by(|a, b| { mempool_array.make_contiguous().sort_unstable_by(|a, b| {
let a_tx = audit_pool.get(a).unwrap(); let a_tx = audit_pool
let b_tx = audit_pool.get(b).unwrap(); .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) b_tx.cmp(a_tx)
}); });
@ -224,6 +229,7 @@ fn set_relatives(txid: u32, audit_pool: &mut HashMap<u32, AuditTransaction>) {
set_relatives(*parent_id, audit_pool); set_relatives(*parent_id, audit_pool);
if let Some(parent) = audit_pool.get_mut(parent_id) { if let Some(parent) = audit_pool.get_mut(parent_id) {
// Safety: ancestors must always contain only txes in audit_pool
ancestors.insert(*parent_id); ancestors.insert(*parent_id);
parent.children.insert(txid); parent.children.insert(txid);
for ancestor in &parent.ancestors { for ancestor in &parent.ancestors {
@ -237,7 +243,9 @@ fn set_relatives(txid: u32, audit_pool: &mut HashMap<u32, AuditTransaction>) {
let mut total_sigops: u32 = 0; let mut total_sigops: u32 = 0;
for ancestor_id in &ancestors { 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_fee += ancestor.fee;
total_weight += ancestor.weight; total_weight += ancestor.weight;
total_sigops += ancestor.sigops; total_sigops += ancestor.sigops;
@ -283,8 +291,7 @@ fn update_descendants(
} else { } else {
return; return;
} }
while !descendant_stack.is_empty() { while let Some(next_txid) = descendant_stack.pop() {
let next_txid: u32 = descendant_stack.pop().unwrap();
if let Some(descendant) = audit_pool.get_mut(&next_txid) { if let Some(descendant) = audit_pool.get_mut(&next_txid) {
// remove root tx as ancestor // remove root tx as ancestor
descendant.ancestors.remove(&root_txid); descendant.ancestors.remove(&root_txid);

View File

@ -27,7 +27,9 @@ pub fn make(mempool_buffer: Uint8Array, callback: JsFunction) -> Result<()> {
map.insert(tx.uid, tx); 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; *global_map = map;
run_in_thread(callback) 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" ts_args_type = "newTxs: Uint8Array, removeTxs: Uint8Array, callback: (result: GbtResult) => void"
)] )]
pub fn update(new_txs: Uint8Array, remove_txs: Uint8Array, callback: JsFunction) -> Result<()> { 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) { for tx in ThreadTransaction::batch_from_buffer(&new_txs) {
map.insert(tx.uid, tx); map.insert(tx.uid, tx);
} }