Skip to content

Commit b2db191

Browse files
committed
feat(bitreq): Add tests for rpc methods
- add tests for sendrawtransaction and getblockchaininfo methods
1 parent cbc6937 commit b2db191

1 file changed

Lines changed: 86 additions & 1 deletion

File tree

tests/rpc_client.rs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
//! These tests require a running Bitcoin Core node in regtest mode. To setup, refer to [`bitcoind`].
66
77
use core::str::FromStr;
8+
use std::collections::BTreeMap;
89

910
use bdk_bitcoind_client::bitreq::{Auth, Client};
10-
use corepc_types::bitcoin::{Amount, BlockHash, Txid};
11+
use corepc_types::bitcoin::{
12+
Amount, BlockHash, Network, Transaction, Txid, absolute, transaction::Version,
13+
};
1114

1215
mod testenv;
1316

@@ -312,3 +315,85 @@ fn test_get_block_filter() {
312315

313316
assert!(!result.filter.is_empty());
314317
}
318+
319+
#[test]
320+
fn test_get_blockchain_info() {
321+
let env = TestEnv::setup().unwrap();
322+
323+
env.mine_blocks(2, None).expect("failed to mine blocks");
324+
325+
let blockchain_info = env
326+
.client
327+
.get_blockchain_info()
328+
.expect("failed to get blockchain info");
329+
330+
assert_eq!(blockchain_info.chain, Network::Regtest);
331+
assert!(blockchain_info.blocks >= 2);
332+
333+
let best_hash = env.client.get_best_block_hash().unwrap();
334+
assert_eq!(blockchain_info.best_block_hash, best_hash);
335+
}
336+
337+
#[test]
338+
fn test_send_raw_transaction() {
339+
let env = TestEnv::setup().unwrap();
340+
341+
env.mine_blocks(101, None).expect("failed to mine blocks");
342+
343+
let recipient_address = env.bitcoind.client.new_address().unwrap();
344+
345+
let mut outputs = BTreeMap::new();
346+
outputs.insert(recipient_address, Amount::from_btc(0.001).unwrap());
347+
348+
let funded_psbt = env
349+
.bitcoind
350+
.client
351+
.wallet_create_funded_psbt(vec![], vec![outputs])
352+
.unwrap()
353+
.into_model()
354+
.unwrap();
355+
356+
let signed_psbt = env
357+
.bitcoind
358+
.client
359+
.wallet_process_psbt(&funded_psbt.psbt)
360+
.unwrap()
361+
.into_model()
362+
.unwrap();
363+
364+
assert!(signed_psbt.complete, "PSBT was not completely signed");
365+
366+
let finalized = env
367+
.bitcoind
368+
.client
369+
.finalize_psbt(&signed_psbt.psbt)
370+
.unwrap()
371+
.into_model()
372+
.unwrap();
373+
374+
let raw_hex = finalized.psbt.unwrap().extract_tx().unwrap();
375+
376+
let txid = env
377+
.client
378+
.send_raw_transaction(&raw_hex)
379+
.expect("failed to broadcast transaction");
380+
381+
let mempool = env.client.get_raw_mempool().expect("failed to get mempool");
382+
assert!(mempool.contains(&txid));
383+
}
384+
385+
#[test]
386+
fn test_send_raw_transaction_invalid() {
387+
let env = TestEnv::setup().unwrap();
388+
389+
let invalid_tx = Transaction {
390+
version: Version::ONE,
391+
lock_time: absolute::LockTime::ZERO,
392+
input: vec![],
393+
output: vec![],
394+
};
395+
396+
let result = env.client.send_raw_transaction(&invalid_tx);
397+
398+
assert!(result.is_err(), "Expected transaction to be rejected");
399+
}

0 commit comments

Comments
 (0)