From: 志宇 Date: Sat, 15 Feb 2025 14:47:20 +0000 (+1100) Subject: fix(electrum): Handle negative heights properly X-Git-Tag: wallet-1.2.0~26^2~1 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/enum.SelectionConstraint.html?a=commitdiff_plain;h=75020521db4e5268960d1b84fe13482f9c6c9457;p=bdk fix(electrum): Handle negative heights properly In electrum, heights returned alongside txs may be 0 or -1. 0 means the tx is unconfirmed. -1 means the tx is unconfirmed and spends an unconfirmed tx. Previously, the codebase assumed that heights cannot be negative and used a `i32 as usize` cast (which would lead to panic if the i32 is negative). --- diff --git a/crates/electrum/src/bdk_electrum_client.rs b/crates/electrum/src/bdk_electrum_client.rs index 4034a7fa..621a69e1 100644 --- a/crates/electrum/src/bdk_electrum_client.rs +++ b/crates/electrum/src/bdk_electrum_client.rs @@ -276,7 +276,9 @@ impl BdkElectrumClient { for tx_res in spk_history { tx_update.txs.push(self.fetch_tx(tx_res.tx_hash)?); - self.validate_merkle_for_anchor(tx_update, tx_res.tx_hash, tx_res.height)?; + if let Ok(height) = tx_res.height.try_into() { + self.validate_merkle_for_anchor(tx_update, tx_res.tx_hash, height)?; + } } } } @@ -312,7 +314,9 @@ impl BdkElectrumClient { if !has_residing && res.tx_hash == op_txid { has_residing = true; tx_update.txs.push(Arc::clone(&op_tx)); - self.validate_merkle_for_anchor(tx_update, res.tx_hash, res.height)?; + if let Ok(height) = res.height.try_into() { + self.validate_merkle_for_anchor(tx_update, res.tx_hash, height)?; + } } if !has_spending && res.tx_hash != op_txid { @@ -326,7 +330,9 @@ impl BdkElectrumClient { continue; } tx_update.txs.push(Arc::clone(&res_tx)); - self.validate_merkle_for_anchor(tx_update, res.tx_hash, res.height)?; + if let Ok(height) = res.height.try_into() { + self.validate_merkle_for_anchor(tx_update, res.tx_hash, height)?; + } } } } @@ -360,7 +366,9 @@ impl BdkElectrumClient { .into_iter() .find(|r| r.tx_hash == txid) { - self.validate_merkle_for_anchor(tx_update, txid, r.height)?; + if let Ok(height) = r.height.try_into() { + self.validate_merkle_for_anchor(tx_update, txid, height)?; + } } tx_update.txs.push(tx); @@ -374,11 +382,11 @@ impl BdkElectrumClient { &self, tx_update: &mut TxUpdate, txid: Txid, - confirmation_height: i32, + confirmation_height: usize, ) -> Result<(), Error> { if let Ok(merkle_res) = self .inner - .transaction_get_merkle(&txid, confirmation_height as usize) + .transaction_get_merkle(&txid, confirmation_height) { let mut header = self.fetch_header(merkle_res.block_height as u32)?; let mut is_confirmed_tx = electrum_client::utils::validate_merkle_proof(