Skip to content

Commit dc96b57

Browse files
committed
add rpc to run_blockchain_tests
1 parent bcecca0 commit dc96b57

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

.github/workflows/cont_integration.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ jobs:
8686
- name: esplora
8787
container: bitcoindevkit/esplora
8888
start: /root/electrs --network regtest -vvv --cookie admin:passw --jsonrpc-import --electrum-rpc-addr=0.0.0.0:60401 --http-addr 0.0.0.0:3002
89+
- name: rpc
90+
container: bitcoindevkit/electrs
91+
start: /root/electrs --network regtest --jsonrpc-import
8992
container: ${{ matrix.blockchain.container }}
9093
env:
9194
BDK_RPC_AUTH: USER_PASS

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ env_logger = "0.7"
7171
base64 = "^0.11"
7272
clap = "2.33"
7373
serial_test = "0.4"
74-
bitcoind = "0.4.0"
74+
bitcoind = "0.5.0"
7575

7676
[[example]]
7777
name = "address_validator"

run_blockchain_tests.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ usage() {
44
cat <<'EOF'
55
Script for running the bdk blockchain tests for a specific blockchain by starting up the backend in docker.
66
7-
Usage: ./run_blockchain_tests.sh [esplora|electrum] [test name].
7+
Usage: ./run_blockchain_tests.sh [esplora|electrum|rpc] [test name].
88
99
EOF
1010
}
@@ -37,6 +37,10 @@ case "$blockchain" in
3737
id="$(docker run -d -p 127.0.0.1:18443-18444:18443-18444/tcp -p 127.0.0.1:60401:60401/tcp -p 127.0.0.1:3002:3002/tcp bitcoindevkit/esplora)"
3838
export BDK_ESPLORA_URL=http://127.0.0.1:3002
3939
;;
40+
rpc)
41+
eprintln "starting electrs docker container"
42+
id="$(docker run -d -p 127.0.0.1:18443-18444:18443-18444/tcp -p 127.0.0.1:60401:60401/tcp bitcoindevkit/electrs)"
43+
;;
4044
*)
4145
usage;
4246
exit 1;

src/blockchain/rpc.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,33 @@ fn list_wallet_dir(client: &Client) -> Result<Vec<String>, Error> {
363363
Ok(result.wallets.into_iter().map(|n| n.name).collect())
364364
}
365365

366-
#[cfg(test)]
367-
#[cfg(feature = "test-rpc")]
368-
#[bdk_blockchain_tests(crate)]
369-
fn local_rpc() -> RpcBlockchain {
370-
let exe = std::env::var("BITCOIND_EXE").unwrap();
371-
let bitcoind = bitcoind::BitcoinD::new(exe).unwrap();
372-
let config = RpcConfig {
373-
url: bitcoind.url.clone(),
374-
auth: Auth::CookieFile(bitcoind.cookie_file.clone()),
375-
network: Network::Regtest,
376-
wallet_name: "bdk-test".to_string(),
377-
skip_blocks: None,
378-
};
379-
RpcBlockchain::from_config(&config).unwrap()
366+
#[cfg(feature = "test-blockchains")]
367+
crate::bdk_blockchain_tests! {
368+
369+
fn test_instance() -> RpcBlockchain {
370+
let url = std::env::var("BDK_RPC_URL").unwrap_or_else(|_| "127.0.0.1:18443".to_string());
371+
let url = format!("http://{}", url);
372+
373+
// TODO same code in `fn get_auth` in testutils, make it public there
374+
let auth = match std::env::var("BDK_RPC_AUTH").as_ref().map(String::as_ref) {
375+
Ok("USER_PASS") => Auth::UserPass(
376+
std::env::var("BDK_RPC_USER").unwrap(),
377+
std::env::var("BDK_RPC_PASS").unwrap(),
378+
),
379+
_ => Auth::CookieFile(std::path::PathBuf::from(
380+
std::env::var("BDK_RPC_COOKIEFILE")
381+
.unwrap_or_else(|_| "/home/user/.bitcoin/regtest/.cookie".to_string()),
382+
)),
383+
};
384+
let config = RpcConfig {
385+
url,
386+
auth,
387+
network: Network::Regtest,
388+
wallet_name: "client-wallet-test".to_string(),
389+
skip_blocks: None,
390+
};
391+
RpcBlockchain::from_config(&config).unwrap()
392+
}
380393
}
381394

382395
#[cfg(feature = "test-rpc")]
@@ -417,14 +430,15 @@ mod test {
417430
}
418431
fn create_bitcoind(args: Vec<String>) -> BitcoinD {
419432
let exe = std::env::var("BITCOIND_EXE").unwrap();
420-
bitcoind::BitcoinD::with_args(exe, args, false).unwrap()
433+
bitcoind::BitcoinD::with_args(exe, args, false, bitcoind::P2P::No).unwrap()
421434
}
422435

423436
const DESCRIPTOR_PUB: &'static str = "wpkh(tpubD6NzVbkrYhZ4X2yy78HWrr1M9NT8dKeWfzNiQqDdMqqa9UmmGztGGz6TaLFGsLfdft5iu32gxq1T4eMNxExNNWzVCpf9Y6JZi5TnqoC9wJq/*)";
424437
const DESCRIPTOR_PRIV: &'static str = "wpkh(tprv8ZgxMBicQKsPdZxBDUcvTSMEaLwCTzTc6gmw8KBKwa3BJzWzec4g6VUbQBHJcutDH6mMEmBeVyN27H1NF3Nu8isZ1Sts4SufWyfLE6Mf1MB/*)";
425438

426439
#[test]
427440
fn test_rpc_wallet_setup() {
441+
env_logger::try_init().unwrap();
428442
let bitcoind = create_bitcoind(vec![]);
429443
let node_address = bitcoind.client.get_new_address(None, None).unwrap();
430444
let blockchain = create_rpc(&bitcoind, DESCRIPTOR_PUB, Network::Regtest).unwrap();
@@ -435,6 +449,8 @@ mod test {
435449
generate(&bitcoind, 101);
436450
wallet.sync(noop_progress(), None).unwrap();
437451
let address = wallet.get_address(AddressIndex::New).unwrap();
452+
let expected_address = "bcrt1q8dyvgt4vhr8ald4xuwewcxhdjha9a5k78wxm5t";
453+
assert_eq!(expected_address, address.to_string());
438454
send_to_address(&bitcoind, &address, 100_000);
439455
wallet.sync(noop_progress(), None).unwrap();
440456
assert_eq!(wallet.get_balance().unwrap(), 100_000);
@@ -467,11 +483,6 @@ mod test {
467483
let wallet_skip =
468484
Wallet::new(DESCRIPTOR_PRIV, None, Network::Regtest, db, blockchain_skip).unwrap();
469485
send_to_address(&bitcoind, &address, 100_000);
470-
assert_eq!(
471-
"bcrt1q8dyvgt4vhr8ald4xuwewcxhdjha9a5k78wxm5t",
472-
address.to_string()
473-
);
474-
println!("address:{}", &address);
475486
generate(&bitcoind, 1); // TODO why this is needed even if list_unspent should include zero conf and unsafe?
476487
wallet_skip.sync(noop_progress(), None).unwrap();
477488
assert_eq!(wallet_skip.get_balance().unwrap(), 100_000);

0 commit comments

Comments
 (0)