Skip to content

Commit 5ad916c

Browse files
authored
Merge pull request #940 from benthecarman/over-underflows
Fix trivial underflow/overflow issues
2 parents 1c89247 + 4a449fc commit 5ad916c

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/chain/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) mod bitcoind;
99
mod electrum;
1010
mod esplora;
1111

12-
use std::collections::HashMap;
12+
use std::collections::{HashMap, HashSet};
1313
use std::sync::{Arc, Mutex};
1414
use std::time::Duration;
1515

@@ -84,7 +84,7 @@ impl WalletSyncStatus {
8484

8585
pub(crate) struct ChainSource {
8686
kind: ChainSourceKind,
87-
registered_txids: Mutex<Vec<Txid>>,
87+
registered_txids: Mutex<HashSet<Txid>>,
8888
tx_broadcaster: Arc<Broadcaster>,
8989
logger: Arc<Logger>,
9090
}
@@ -113,7 +113,7 @@ impl ChainSource {
113113
node_metrics,
114114
)?;
115115
let kind = ChainSourceKind::Esplora(esplora_chain_source);
116-
let registered_txids = Mutex::new(Vec::new());
116+
let registered_txids = Mutex::new(HashSet::new());
117117
Ok((Self { kind, registered_txids, tx_broadcaster, logger }, None))
118118
}
119119

@@ -133,7 +133,7 @@ impl ChainSource {
133133
node_metrics,
134134
);
135135
let kind = ChainSourceKind::Electrum(electrum_chain_source);
136-
let registered_txids = Mutex::new(Vec::new());
136+
let registered_txids = Mutex::new(HashSet::new());
137137
(Self { kind, registered_txids, tx_broadcaster, logger }, None)
138138
}
139139

@@ -156,7 +156,7 @@ impl ChainSource {
156156
);
157157
let best_block = bitcoind_chain_source.poll_best_block().await.ok();
158158
let kind = ChainSourceKind::Bitcoind(bitcoind_chain_source);
159-
let registered_txids = Mutex::new(Vec::new());
159+
let registered_txids = Mutex::new(HashSet::new());
160160
(Self { kind, registered_txids, tx_broadcaster, logger }, best_block)
161161
}
162162

@@ -180,7 +180,7 @@ impl ChainSource {
180180
);
181181
let best_block = bitcoind_chain_source.poll_best_block().await.ok();
182182
let kind = ChainSourceKind::Bitcoind(bitcoind_chain_source);
183-
let registered_txids = Mutex::new(Vec::new());
183+
let registered_txids = Mutex::new(HashSet::new());
184184
(Self { kind, registered_txids, tx_broadcaster, logger }, best_block)
185185
}
186186

@@ -214,7 +214,7 @@ impl ChainSource {
214214
}
215215
}
216216

217-
pub(crate) fn registered_txids(&self) -> Vec<Txid> {
217+
pub(crate) fn registered_txids(&self) -> HashSet<Txid> {
218218
self.registered_txids.lock().expect("lock").clone()
219219
}
220220

@@ -472,7 +472,7 @@ impl ChainSource {
472472

473473
impl Filter for ChainSource {
474474
fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
475-
self.registered_txids.lock().expect("lock").push(*txid);
475+
self.registered_txids.lock().expect("lock").insert(*txid);
476476
match &self.kind {
477477
ChainSourceKind::Esplora(esplora_chain_source) => {
478478
esplora_chain_source.register_tx(txid, script_pubkey)

src/payment/bolt11.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl Bolt11Payment {
524524
_ => 0,
525525
};
526526
if let Some(invoice_amount_msat) = details.amount_msat {
527-
if claimable_amount_msat < invoice_amount_msat - skimmed_fee_msat {
527+
if claimable_amount_msat < invoice_amount_msat.saturating_sub(skimmed_fee_msat) {
528528
log_error!(
529529
self.logger,
530530
"Failed to manually claim payment {} as the claimable amount is less than expected",

src/payment/unified.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ impl UnifiedPayment {
129129
pub fn receive(
130130
&self, amount_sats: u64, description: &str, expiry_sec: u32,
131131
) -> Result<String, Error> {
132-
let onchain_address = self.onchain_payment.new_address()?;
132+
let amount_msats = amount_sats.checked_mul(1_000).ok_or(Error::InvalidAmount)?;
133133

134-
let amount_msats = amount_sats * 1_000;
134+
let onchain_address = self.onchain_payment.new_address()?;
135135

136136
let bolt12_offer =
137137
match self.bolt12_payment.receive_inner(amount_msats, description, None, None) {

tests/integration_tests_rust.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,18 @@ async fn generate_bip21_uri() {
16801680
assert!(uni_payment.contains("lno="));
16811681
}
16821682

1683+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
1684+
async fn unified_receive_rejects_msat_overflow() {
1685+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
1686+
let chain_source = random_chain_source(&bitcoind, &electrsd);
1687+
let node = setup_node(&chain_source, random_config(true));
1688+
1689+
assert_eq!(
1690+
Err(NodeError::InvalidAmount),
1691+
node.unified_payment().receive(u64::MAX, "asdf", 4_000)
1692+
);
1693+
}
1694+
16831695
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
16841696
async fn unified_send_receive_bip21_uri() {
16851697
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();

0 commit comments

Comments
 (0)