Skip to content

Commit

Permalink
test(electrum): fetch_prev_txout does not process coinbase transact…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
LagginTimes committed Dec 4, 2024
1 parent 541abe9 commit 4cf7254
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions crates/electrum/src/bdk_electrum_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,52 @@ fn chain_update(
}
Ok(tip)
}

#[cfg(test)]
mod test {
use crate::{bdk_electrum_client::TxUpdate, BdkElectrumClient};
use bdk_chain::bitcoin::{hashes::Hash, OutPoint, Transaction, TxIn, Txid};
use bdk_core::collections::BTreeMap;
use bdk_testenv::{utils::new_tx, TestEnv};
use std::sync::Arc;

#[cfg(feature = "default")]
#[test]
fn test_fetch_prev_txout_with_coinbase() {
let env = TestEnv::new().unwrap();
let electrum_client =
electrum_client::Client::new(env.electrsd.electrum_url.as_str()).unwrap();
let client = BdkElectrumClient::new(electrum_client);

// Calling `fetch_prev_txout` on a coinbase transaction triggers a `fetch_tx` on a
// transaction with a txid of all zeros. However, since the error from Electrum's
// `transaction_get` is propagated upwards and may not interrupt the `sync`/`full_scan`
// process, we have to insert a transaction with a txid of all zeros into the `tx_cache` to
// ensure `fetch_prev_txout` will fail if it attempts to fetch this transaction.
let mut tx_cache = client.tx_cache.lock().unwrap();
tx_cache.insert(Txid::all_zeros(), new_tx(0).into());
drop(tx_cache);

// Create a coinbase transaction.
let coinbase_tx = Transaction {
input: vec![TxIn {
previous_output: OutPoint::null(),
..Default::default()
}],
..new_tx(0)
};

assert!(coinbase_tx.is_coinbase());

// Test that `fetch_prev_txout` does not process our coinbase transaction. If it attempts to
// fetch our previously inserted transaction with a txid of all zeros, this test will fail.
let mut tx_update = TxUpdate {
txs: vec![Arc::new(coinbase_tx)],
..Default::default()
};
assert!(client.fetch_prev_txout(&mut tx_update).is_ok());

// Ensure that the txouts are empty.
assert_eq!(tx_update.txouts, BTreeMap::default());
}
}

0 comments on commit 4cf7254

Please sign in to comment.