Skip to content

Commit 3b024f4

Browse files
committed
use a different bitcoind for blockchain tests
1 parent a8a1d7c commit 3b024f4

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/blockchain/rpc.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -352,26 +352,13 @@ fn list_wallet_dir(client: &Client) -> Result<Vec<String>, Error> {
352352
#[cfg(feature = "test-rpc")]
353353
#[bdk_blockchain_tests(crate)]
354354
fn local_rpc() -> RpcBlockchain {
355-
let url = std::env::var("BDK_RPC_URL").unwrap_or_else(|_| "127.0.0.1:18443".to_string());
356-
let url = format!("http://{}", url);
357-
let wallet_name = std::env::var("BDK_RPC_WALLET").unwrap_or_else(|_| "bdk-test".to_string());
358-
359-
// TODO same code in `fn get_auth` in testutils, make it public there
360-
let auth = match std::env::var("BDK_RPC_AUTH").as_ref().map(String::as_ref) {
361-
Ok("USER_PASS") => Auth::UserPass(
362-
std::env::var("BDK_RPC_USER").unwrap(),
363-
std::env::var("BDK_RPC_PASS").unwrap(),
364-
),
365-
_ => Auth::CookieFile(std::path::PathBuf::from(
366-
std::env::var("BDK_RPC_COOKIEFILE")
367-
.unwrap_or_else(|_| "/home/user/.bitcoin/regtest/.cookie".to_string()),
368-
)),
369-
};
355+
let exe = std::env::var("BITCOIND_EXE").unwrap();
356+
let bitcoind = bitcoind::BitcoinD::new(exe).unwrap();
370357
let config = RpcConfig {
371-
url,
372-
auth,
358+
url: bitcoind.url.clone(),
359+
auth: Auth::CookieFile(bitcoind.cookie_file.clone()),
373360
network: Network::Regtest,
374-
wallet_name,
361+
wallet_name: "bdk-test".to_string(),
375362
};
376363
RpcBlockchain::from_config(&config).unwrap()
377364
}
@@ -416,15 +403,17 @@ mod test {
416403
bitcoind::BitcoinD::with_args(exe, args, false).unwrap()
417404
}
418405

419-
const EXAMPLE_DESCRIPTOR: &'static str = "wpkh(tpubD6NzVbkrYhZ4X2yy78HWrr1M9NT8dKeWfzNiQqDdMqqa9UmmGztGGz6TaLFGsLfdft5iu32gxq1T4eMNxExNNWzVCpf9Y6JZi5TnqoC9wJq/*)";
406+
const DESCRIPTOR_PUB: &'static str = "wpkh(tpubD6NzVbkrYhZ4X2yy78HWrr1M9NT8dKeWfzNiQqDdMqqa9UmmGztGGz6TaLFGsLfdft5iu32gxq1T4eMNxExNNWzVCpf9Y6JZi5TnqoC9wJq/*)";
407+
const DESCRIPTOR_PRIV: &'static str = "wpkh(tprv8ZgxMBicQKsPdZxBDUcvTSMEaLwCTzTc6gmw8KBKwa3BJzWzec4g6VUbQBHJcutDH6mMEmBeVyN27H1NF3Nu8isZ1Sts4SufWyfLE6Mf1MB/*)";
420408

421409
#[test]
422410
fn test_rpc_wallet_setup() {
423411
let bitcoind = create_bitcoind(vec![]);
424-
let blockchain = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Regtest).unwrap();
412+
let node_address = bitcoind.client.get_new_address(None, None).unwrap();
413+
let blockchain = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest).unwrap();
425414
let db = MemoryDatabase::new();
426415
let wallet =
427-
Wallet::new(EXAMPLE_DESCRIPTOR, None, Network::Regtest, db, blockchain).unwrap();
416+
Wallet::new(DESCRIPTOR_PRIV, None, Network::Regtest, db, blockchain).unwrap();
428417

429418
wallet.sync(noop_progress(), None).unwrap();
430419
generate(&bitcoind, 101);
@@ -433,26 +422,36 @@ mod test {
433422
send_to_address(&bitcoind, &address, 100_000);
434423
wallet.sync(noop_progress(), None).unwrap();
435424
assert_eq!(wallet.get_balance().unwrap(), 100_000);
425+
426+
let mut builder = wallet.build_tx();
427+
builder.add_recipient(node_address.script_pubkey(), 50_000);
428+
let (mut psbt, details) = builder.finish().unwrap();
429+
let finalized = wallet.sign(&mut psbt, Default::default()).unwrap();
430+
assert!(finalized, "Cannot finalize transaction");
431+
let tx = psbt.extract_tx();
432+
wallet.broadcast(tx).unwrap();
433+
wallet.sync(noop_progress(), None).unwrap();
434+
assert_eq!(wallet.get_balance().unwrap(), 100_000 - 50_000 - details.fees);
436435
}
437436

438437
#[test]
439438
fn test_rpc_from_config() {
440439
let bitcoind = create_bitcoind(vec![]);
441-
let blockchain = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Regtest);
440+
let blockchain = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest);
442441
assert!(blockchain.is_ok());
443-
let blockchain = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Testnet);
442+
let blockchain = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Testnet);
444443
assert!(blockchain.is_err(), "wrong network doesn't error");
445444
}
446445

447446
#[test]
448447
fn test_rpc_capabilities_get_tx() {
449448
let bitcoind = create_bitcoind(vec![]);
450-
let rpc = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Regtest).unwrap();
449+
let rpc = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest).unwrap();
451450
let capabilities = rpc.get_capabilities();
452451
assert!(capabilities.contains(&Capability::FullHistory) && capabilities.len() == 1);
453452
let bitcoind_indexed = create_bitcoind(vec!["-txindex".to_string()]);
454453
let rpc_indexed =
455-
create_rpc(&bitcoind_indexed, EXAMPLE_DESCRIPTOR, Network::Regtest).unwrap();
454+
create_rpc(&bitcoind_indexed, DESCRIPTOR_PUB, Network::Regtest).unwrap();
456455
assert_eq!(rpc_indexed.get_capabilities().len(), 3);
457456
let address = generate(&bitcoind_indexed, 101);
458457
let txid = send_to_address(&bitcoind_indexed, &address, 100_000);
@@ -463,7 +462,7 @@ mod test {
463462
#[test]
464463
fn test_rpc_estimate_fee_get_height() {
465464
let bitcoind = create_bitcoind(vec![]);
466-
let rpc = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Regtest).unwrap();
465+
let rpc = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest).unwrap();
467466
let result = rpc.estimate_fee(2);
468467
assert!(result.is_err());
469468
let address = generate(&bitcoind, 100);
@@ -482,7 +481,7 @@ mod test {
482481
#[test]
483482
fn test_rpc_node_synced_height() {
484483
let bitcoind = create_bitcoind(vec![]);
485-
let rpc = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Regtest).unwrap();
484+
let rpc = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest).unwrap();
486485
let synced_height = rpc.get_node_synced_height().unwrap();
487486

488487
assert_eq!(synced_height, 0);
@@ -495,7 +494,7 @@ mod test {
495494
#[test]
496495
fn test_rpc_broadcast() {
497496
let bitcoind = create_bitcoind(vec![]);
498-
let rpc = create_rpc(&bitcoind, EXAMPLE_DESCRIPTOR, Network::Regtest).unwrap();
497+
let rpc = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest).unwrap();
499498
let address = generate(&bitcoind, 101);
500499
let utxo = bitcoind
501500
.client
@@ -534,7 +533,7 @@ mod test {
534533
fn test_rpc_wallet_name() {
535534
let secp = Secp256k1::new();
536535
let name =
537-
wallet_name_from_descriptor(EXAMPLE_DESCRIPTOR, None, Network::Regtest, &secp).unwrap();
536+
wallet_name_from_descriptor(DESCRIPTOR_PUB, None, Network::Regtest, &secp).unwrap();
538537
assert_eq!("tmg7aqay", name);
539538
}
540539

0 commit comments

Comments
 (0)