Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 9be4f1f

Browse files
committed
wait for confirmation
1 parent 91a3d42 commit 9be4f1f

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

romeo/tests/tests/bitcoin_client.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use reqwest::blocking::Client;
2-
use serde_json::json;
1+
use std::{str::FromStr, thread::sleep, time::Duration};
2+
3+
use bdk::{
4+
bitcoin::{hash_types::Txid, Address, BlockHash},
5+
bitcoincore_rpc::{Auth, Client as BClient, RpcApi},
6+
};
37
use url::Url;
48

59
pub fn bitcoin_url() -> Url {
@@ -10,38 +14,38 @@ pub fn electrs_url() -> Url {
1014
Url::parse("tcp://electrs:60401").unwrap()
1115
}
1216

13-
pub fn generate_blocks(
17+
pub fn client_new(url: &str, user: &str, pass: &str) -> BClient {
18+
BClient::new(url, Auth::UserPass(user.into(), pass.into())).unwrap()
19+
}
20+
21+
pub fn mine_blocks(
22+
client: &BClient,
1423
blocks: u64,
15-
ctx: &Client,
1624
address: &str,
17-
) -> Vec<String> {
18-
let endpoint = bitcoin_url();
19-
let user = "devnet";
20-
let password = "devnet";
21-
let body = json!({
22-
"jsonrpc": "1.0",
23-
"id": "1",
24-
"method": "generatetoaddress",
25-
//developer's
26-
"params": [blocks,address]
27-
});
28-
29-
let response_json: serde_json::Value = ctx
30-
.post(endpoint)
31-
.basic_auth(user, Some(password))
32-
.header(reqwest::header::CONTENT_TYPE, "application/json")
33-
.json(&body)
34-
.send()
25+
) -> Vec<BlockHash> {
26+
client
27+
.generate_to_address(blocks, &Address::from_str(address).unwrap())
3528
.unwrap()
36-
.json()
37-
.unwrap();
38-
39-
assert_eq!(response_json["error"], serde_json::Value::Null);
40-
serde_json::from_value(response_json["result"].clone()).expect("block_ids")
4129
}
4230

43-
#[test]
44-
fn mine_empty_block() {
45-
let client = Client::new();
46-
generate_blocks(10, &client, "mqVnk6NPRdhntvfm4hh9vvjiRkFDUuSYsH");
31+
pub fn wait_for_tx_confirmation(
32+
b_client: &BClient,
33+
txid: &Txid,
34+
confirmations: i32,
35+
) {
36+
loop {
37+
match b_client.get_transaction(txid, None) {
38+
Ok(tx) if tx.info.confirmations >= confirmations => {
39+
break;
40+
}
41+
Ok(ok) => {
42+
println!("Waiting confirmation on {txid}:{ok:?}")
43+
}
44+
Err(e) => {
45+
println!("Waiting confirmation on {txid}:{e:?}")
46+
}
47+
}
48+
49+
sleep(Duration::from_secs(1));
50+
}
4751
}

romeo/tests/tests/deposit.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
1-
use std::{thread::sleep, time::Duration};
1+
use std::{str::FromStr, thread::sleep, time::Duration};
22

33
use anyhow::Result;
44
use bdk::{
5-
bitcoin::{psbt::serialize::Serialize, PrivateKey},
5+
bitcoin::{psbt::serialize::Serialize, Address, PrivateKey},
6+
bitcoincore_rpc::RpcApi,
67
blockchain::{
78
ConfigurableBlockchain, ElectrumBlockchain, ElectrumBlockchainConfig,
89
},
910
database::MemoryDatabase,
1011
template::P2Wpkh,
1112
SyncOptions, Wallet,
1213
};
13-
use reqwest::blocking::Client;
1414
use sbtc_cli::commands::{
1515
broadcast::{broadcast_tx, BroadcastArgs},
1616
deposit::{build_deposit_tx, DepositArgs},
1717
};
1818

1919
use super::{
20-
bitcoin_client::{electrs_url, generate_blocks},
20+
bitcoin_client::{
21+
bitcoin_url, client_new, electrs_url, mine_blocks,
22+
wait_for_tx_confirmation,
23+
},
2124
KeyType::*,
2225
WALLETS,
2326
};
2427

2528
#[test]
2629
fn broadcast_deposit() -> Result<()> {
27-
let client = Client::new();
30+
let b_client = client_new(bitcoin_url().as_str(), "devnet", "devnet");
31+
32+
b_client
33+
.import_address(
34+
&Address::from_str(WALLETS[1][P2wpkh].address).unwrap(),
35+
None,
36+
Some(false),
37+
)
38+
.unwrap();
39+
2840
{
29-
generate_blocks(1, &client, WALLETS[0][P2wpkh].address);
30-
generate_blocks(1, &client, WALLETS[1][P2wpkh].address);
41+
mine_blocks(&b_client, 1, WALLETS[0][P2wpkh].address);
42+
mine_blocks(&b_client, 1, WALLETS[1][P2wpkh].address);
3143
// pads blocks to get rewards.
32-
generate_blocks(100, &client, WALLETS[0][P2wpkh].address);
44+
mine_blocks(&b_client, 100, WALLETS[0][P2wpkh].address);
3345
};
3446

3547
let electrum_url = electrs_url();
3648

3749
// suboptimal, replace once we have better events.
50+
// replace with b_client balance?
3851
{
3952
let blockchain =
4053
ElectrumBlockchain::from_config(&ElectrumBlockchainConfig {
@@ -88,5 +101,9 @@ fn broadcast_deposit() -> Result<()> {
88101
})
89102
.unwrap();
90103

104+
let txid = tx.txid();
105+
106+
wait_for_tx_confirmation(&b_client, &txid, 1);
107+
91108
Ok(())
92109
}

0 commit comments

Comments
 (0)