Split monero and bitcoin container initialisation

More control over the setup for the bitcoin environment to support BDK wallet.
This commit is contained in:
rishflab 2021-02-02 10:39:34 +11:00
parent 9eae0db9ac
commit 24de0dcda3

View file

@ -1,10 +1,11 @@
use crate::testutils; use crate::testutils;
use bitcoin_harness::Bitcoind; use anyhow::Result;
use bitcoin_harness::{BitcoindRpcApi, Client};
use futures::Future; use futures::Future;
use get_port::get_port; use get_port::get_port;
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
use monero_harness::{image, Monero}; use monero_harness::{image, Monero};
use std::{path::PathBuf, sync::Arc}; use std::{path::PathBuf, sync::Arc, time::Duration};
use swap::{ use swap::{
bitcoin, bitcoin,
bitcoin::Timelock, bitcoin::Timelock,
@ -19,8 +20,11 @@ use testcontainers::{clients::Cli, Container};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tracing_core::dispatcher::DefaultGuard; use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer; use tracing_log::LogTracer;
use url::Url;
use uuid::Uuid; use uuid::Uuid;
const TEST_WALLET_NAME: &str = "testwallet";
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StartingBalances { pub struct StartingBalances {
pub xmr: monero::Amount, pub xmr: monero::Amount,
@ -336,7 +340,7 @@ where
let (alice_bitcoin_wallet, alice_monero_wallet) = init_wallets( let (alice_bitcoin_wallet, alice_monero_wallet) = init_wallets(
"alice", "alice",
&containers.bitcoind, containers.bitcoind_url.clone(),
&monero, &monero,
alice_starting_balances.clone(), alice_starting_balances.clone(),
config, config,
@ -360,7 +364,7 @@ where
let (bob_bitcoin_wallet, bob_monero_wallet) = init_wallets( let (bob_bitcoin_wallet, bob_monero_wallet) = init_wallets(
"bob", "bob",
&containers.bitcoind, containers.bitcoind_url,
&monero, &monero,
bob_starting_balances.clone(), bob_starting_balances.clone(),
config, config,
@ -394,18 +398,86 @@ where
} }
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) { async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); let bitcoind_url = init_bitcoind_container().await.unwrap();
let _ = bitcoind.init(5).await; let (monero, monerods) = init_monero_container(&cli).await;
(monero, Containers {
bitcoind_url,
monerods,
})
}
async fn init_bitcoind_container() -> Result<Url> {
// let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
let node_url: Url = "".parse()?;
init_bitcoind(node_url.clone(), 5).await?;
Ok(node_url)
}
async fn mine(bitcoind_client: Client, reward_address: bitcoin::Address) -> Result<()> {
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
bitcoind_client
.generatetoaddress(1, reward_address.clone(), None)
.await?;
}
}
pub async fn init_bitcoind(node_url: Url, spendable_quantity: u32) -> Result<Client> {
let bitcoind_client = Client::new(node_url.clone());
bitcoind_client
.createwallet(TEST_WALLET_NAME, None, None, None, None)
.await?;
let reward_address = bitcoind_client
.with_wallet(TEST_WALLET_NAME)?
.getnewaddress(None, None)
.await?;
bitcoind_client
.generatetoaddress(101 + spendable_quantity, reward_address.clone(), None)
.await?;
let _ = tokio::spawn(mine(bitcoind_client.clone(), reward_address));
Ok(bitcoind_client)
}
/// Send Bitcoin to the specified address, limited to the spendable bitcoin
/// quantity.
pub async fn mint(node_url: Url, address: bitcoin::Address, amount: bitcoin::Amount) -> Result<()> {
let bitcoind_client = Client::new(node_url.clone());
bitcoind_client
.send_to_address(TEST_WALLET_NAME, address.clone(), amount)
.await?;
// Confirm the transaction
let reward_address = bitcoind_client
.with_wallet(TEST_WALLET_NAME)?
.getnewaddress(None, None)
.await?;
bitcoind_client
.generatetoaddress(1, reward_address, None)
.await?;
Ok(())
}
async fn init_monero_container(
cli: &Cli,
) -> (
Monero,
Vec<Container<'_, Cli, monero_harness::image::Monero>>,
) {
let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()]) let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
.await .await
.unwrap(); .unwrap();
(monero, Containers { bitcoind, monerods }) (monero, monerods)
} }
async fn init_wallets( async fn init_wallets(
name: &str, name: &str,
bitcoind: &Bitcoind<'_>, bitcoind_url: Url,
monero: &Monero, monero: &Monero,
starting_balances: StartingBalances, starting_balances: StartingBalances,
config: Config, config: Config,
@ -421,19 +493,19 @@ async fn init_wallets(
}); });
let btc_wallet = Arc::new( let btc_wallet = Arc::new(
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network) swap::bitcoin::Wallet::new(name, bitcoind_url.clone(), config.bitcoin_network)
.await .await
.unwrap(), .unwrap(),
); );
if starting_balances.btc != bitcoin::Amount::ZERO { if starting_balances.btc != bitcoin::Amount::ZERO {
bitcoind mint(
.mint( bitcoind_url,
btc_wallet.inner.new_address().await.unwrap(), btc_wallet.inner.new_address().await.unwrap(),
starting_balances.btc, starting_balances.btc,
) )
.await .await
.unwrap(); .unwrap();
} }
(btc_wallet, xmr_wallet) (btc_wallet, xmr_wallet)
@ -442,7 +514,7 @@ async fn init_wallets(
// This is just to keep the containers alive // This is just to keep the containers alive
#[allow(dead_code)] #[allow(dead_code)]
struct Containers<'a> { struct Containers<'a> {
bitcoind: Bitcoind<'a>, bitcoind_url: Url,
monerods: Vec<Container<'a, Cli, image::Monero>>, monerods: Vec<Container<'a, Cli, image::Monero>>,
} }