mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Refactor container initialization
This commit is contained in:
parent
143d8bc07d
commit
8c929564ef
@ -1,6 +1,4 @@
|
||||
use bitcoin_harness::Bitcoind;
|
||||
use libp2p::Multiaddr;
|
||||
use monero_harness::Monero;
|
||||
use rand::rngs::OsRng;
|
||||
use swap::{alice, alice::swap::AliceState, bitcoin, bob, storage::Database};
|
||||
use tempfile::tempdir;
|
||||
@ -18,19 +16,18 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||
let _guard = init_tracing();
|
||||
|
||||
let cli = Cli::default();
|
||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||
let _ = bitcoind.init(5).await;
|
||||
let (monero, _container) =
|
||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||
.await
|
||||
.unwrap();
|
||||
let (
|
||||
monero,
|
||||
testutils::Containers {
|
||||
bitcoind,
|
||||
monerods: _monerods,
|
||||
},
|
||||
) = testutils::init_containers(&cli).await;
|
||||
|
||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
||||
let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000);
|
||||
|
||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
||||
let bob_xmr_starting_balance = xmr_btc::monero::Amount::ZERO;
|
||||
|
||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
||||
|
||||
let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9877"
|
||||
@ -64,7 +61,6 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||
btc_to_swap,
|
||||
bob_btc_starting_balance,
|
||||
xmr_to_swap,
|
||||
bob_xmr_starting_balance,
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
@ -113,15 +109,8 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||
assert!(matches!(state, swap::state::Alice::EncSignLearned {..}));
|
||||
}
|
||||
|
||||
let (_, mut event_loop_after_restart, event_loop_handle_after_restart) =
|
||||
testutils::init_alice_eventloop(
|
||||
btc_to_swap,
|
||||
xmr_to_swap,
|
||||
alice_btc_wallet.clone(),
|
||||
alice_multiaddr,
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
let (mut event_loop_after_restart, event_loop_handle_after_restart) =
|
||||
testutils::init_alice_eventloop(alice_multiaddr);
|
||||
let _alice_swarm_fut = tokio::spawn(async move { event_loop_after_restart.run().await });
|
||||
|
||||
let alice_state = alice::swap::recover(
|
||||
@ -136,4 +125,6 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||
.unwrap();
|
||||
|
||||
assert!(matches!(alice_state, AliceState::BtcRedeemed {..}));
|
||||
|
||||
// TODO: Additionally assert balances
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
use crate::testutils::{init_alice, init_bob};
|
||||
use bitcoin_harness::Bitcoind;
|
||||
use futures::future::try_join;
|
||||
use libp2p::Multiaddr;
|
||||
use monero_harness::Monero;
|
||||
use rand::rngs::OsRng;
|
||||
use swap::{alice, bob, storage::Database};
|
||||
use tempfile::tempdir;
|
||||
@ -20,12 +18,13 @@ async fn happy_path() {
|
||||
let _guard = init_tracing();
|
||||
|
||||
let cli = Cli::default();
|
||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||
let _ = bitcoind.init(5).await;
|
||||
let (monero, _container) =
|
||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||
.await
|
||||
.unwrap();
|
||||
let (
|
||||
monero,
|
||||
testutils::Containers {
|
||||
bitcoind,
|
||||
monerods: _monerods,
|
||||
},
|
||||
) = testutils::init_containers(&cli).await;
|
||||
|
||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
||||
let btc_alice = bitcoin::Amount::ZERO;
|
||||
@ -69,7 +68,6 @@ async fn happy_path() {
|
||||
btc_to_swap,
|
||||
btc_bob,
|
||||
xmr_to_swap,
|
||||
xmr_bob,
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
|
@ -1,8 +1,6 @@
|
||||
use crate::testutils::{init_alice, init_bob};
|
||||
use bitcoin_harness::Bitcoind;
|
||||
use futures::future::try_join;
|
||||
use libp2p::Multiaddr;
|
||||
use monero_harness::Monero;
|
||||
use rand::rngs::OsRng;
|
||||
use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database};
|
||||
use tempfile::tempdir;
|
||||
@ -20,18 +18,18 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
|
||||
let _guard = init_tracing();
|
||||
|
||||
let cli = Cli::default();
|
||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||
let _ = bitcoind.init(5).await;
|
||||
let (monero, _container) =
|
||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||
.await
|
||||
.unwrap();
|
||||
let (
|
||||
monero,
|
||||
testutils::Containers {
|
||||
bitcoind,
|
||||
monerods: _monerods,
|
||||
},
|
||||
) = testutils::init_containers(&cli).await;
|
||||
|
||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
||||
let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000);
|
||||
|
||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
||||
let bob_xmr_starting_balance = xmr_btc::monero::Amount::ZERO;
|
||||
|
||||
let alice_btc_starting_balance = bitcoin::Amount::ZERO;
|
||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
||||
@ -68,7 +66,6 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
|
||||
btc_to_swap,
|
||||
bob_btc_starting_balance,
|
||||
xmr_to_swap,
|
||||
bob_xmr_starting_balance,
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
|
@ -1,8 +1,6 @@
|
||||
use crate::testutils::{init_alice, init_bob};
|
||||
use bitcoin_harness::Bitcoind;
|
||||
use futures::future::try_join;
|
||||
use libp2p::Multiaddr;
|
||||
use monero_harness::Monero;
|
||||
use rand::rngs::OsRng;
|
||||
use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database};
|
||||
use tempfile::tempdir;
|
||||
@ -19,19 +17,14 @@ pub mod testutils;
|
||||
async fn both_refund() {
|
||||
let _guard = init_tracing();
|
||||
|
||||
use tracing_subscriber::util::SubscriberInitExt as _;
|
||||
let _guard = tracing_subscriber::fmt()
|
||||
.with_env_filter("swap=info,xmr_btc=info")
|
||||
.with_ansi(false)
|
||||
.set_default();
|
||||
|
||||
let cli = Cli::default();
|
||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||
let _ = bitcoind.init(5).await;
|
||||
let (monero, _container) =
|
||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||
.await
|
||||
.unwrap();
|
||||
let (
|
||||
monero,
|
||||
testutils::Containers {
|
||||
bitcoind,
|
||||
monerods: _monerods,
|
||||
},
|
||||
) = testutils::init_containers(&cli).await;
|
||||
|
||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
||||
let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000);
|
||||
@ -72,7 +65,6 @@ async fn both_refund() {
|
||||
btc_to_swap,
|
||||
bob_btc_starting_balance,
|
||||
xmr_to_swap,
|
||||
bob_xmr_starting_balance,
|
||||
Config::regtest(),
|
||||
)
|
||||
.await;
|
||||
|
@ -21,7 +21,6 @@ pub async fn init_bob(
|
||||
btc_to_swap: bitcoin::Amount,
|
||||
btc_starting_balance: bitcoin::Amount,
|
||||
xmr_to_swap: xmr_btc::monero::Amount,
|
||||
xmr_stating_balance: xmr_btc::monero::Amount,
|
||||
config: Config,
|
||||
) -> (
|
||||
BobState,
|
||||
@ -31,25 +30,15 @@ pub async fn init_bob(
|
||||
Arc<swap::monero::Wallet>,
|
||||
Database,
|
||||
) {
|
||||
let bob_btc_wallet = Arc::new(
|
||||
swap::bitcoin::Wallet::new("bob", bitcoind.node_url.clone(), config.bitcoin_network)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
bitcoind
|
||||
.mint(
|
||||
bob_btc_wallet.inner.new_address().await.unwrap(),
|
||||
btc_starting_balance,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
monero
|
||||
.init(vec![("bob", xmr_stating_balance.as_piconero())])
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet("bob").unwrap().client()));
|
||||
let (bob_btc_wallet, bob_xmr_wallet) = init_wallets(
|
||||
"bob",
|
||||
bitcoind,
|
||||
monero,
|
||||
Some(btc_starting_balance),
|
||||
None,
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
|
||||
let amounts = SwapAmounts {
|
||||
btc: btc_to_swap,
|
||||
@ -89,17 +78,12 @@ pub async fn init_bob(
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn init_alice_eventloop(
|
||||
pub async fn init_alice_state(
|
||||
btc_to_swap: bitcoin::Amount,
|
||||
xmr_to_swap: monero::Amount,
|
||||
alice_btc_wallet: Arc<bitcoin::Wallet>,
|
||||
listen: Multiaddr,
|
||||
config: Config,
|
||||
) -> (
|
||||
AliceState,
|
||||
alice::event_loop::EventLoop,
|
||||
alice::event_loop::EventLoopHandle,
|
||||
) {
|
||||
) -> AliceState {
|
||||
let rng = &mut OsRng;
|
||||
|
||||
let amounts = SwapAmounts {
|
||||
@ -123,39 +107,74 @@ pub async fn init_alice_eventloop(
|
||||
redeem_address,
|
||||
punish_address,
|
||||
);
|
||||
let start_state = AliceState::Started { amounts, state0 };
|
||||
|
||||
AliceState::Started { amounts, state0 }
|
||||
}
|
||||
|
||||
pub fn init_alice_eventloop(
|
||||
listen: Multiaddr,
|
||||
) -> (
|
||||
alice::event_loop::EventLoop,
|
||||
alice::event_loop::EventLoopHandle,
|
||||
) {
|
||||
let alice_behaviour = alice::Behaviour::default();
|
||||
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
||||
|
||||
let (swarm_driver, handle) =
|
||||
alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap();
|
||||
|
||||
(start_state, swarm_driver, handle)
|
||||
(swarm_driver, handle)
|
||||
}
|
||||
|
||||
pub async fn init_alice_wallets(
|
||||
bitcoind: &Bitcoind<'_>,
|
||||
monero: &Monero,
|
||||
xmr_starting_balance: xmr_btc::monero::Amount,
|
||||
config: Config,
|
||||
) -> (Arc<monero::Wallet>, Arc<bitcoin::Wallet>) {
|
||||
monero
|
||||
.init(vec![("alice", xmr_starting_balance.as_piconero())])
|
||||
pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||
let _ = bitcoind.init(5).await;
|
||||
let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let alice_xmr_wallet = Arc::new(swap::monero::Wallet(
|
||||
monero.wallet("alice").unwrap().client(),
|
||||
));
|
||||
(monero, Containers { bitcoind, monerods })
|
||||
}
|
||||
|
||||
let alice_btc_wallet = Arc::new(
|
||||
swap::bitcoin::Wallet::new("alice", bitcoind.node_url.clone(), config.bitcoin_network)
|
||||
pub async fn init_wallets(
|
||||
name: &str,
|
||||
bitcoind: &Bitcoind<'_>,
|
||||
monero: &Monero,
|
||||
btc_starting_balance: Option<xmr_btc::bitcoin::Amount>,
|
||||
xmr_starting_balance: Option<xmr_btc::monero::Amount>,
|
||||
config: Config,
|
||||
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
|
||||
match xmr_starting_balance {
|
||||
Some(amount) => {
|
||||
monero
|
||||
.init(vec![(name, amount.as_piconero())])
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
None => {
|
||||
monero
|
||||
.init(vec![(name, monero::Amount::ZERO.as_piconero())])
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
let xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet(name).unwrap().client()));
|
||||
|
||||
let btc_wallet = Arc::new(
|
||||
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
(alice_xmr_wallet, alice_btc_wallet)
|
||||
if let Some(amount) = btc_starting_balance {
|
||||
bitcoind
|
||||
.mint(btc_wallet.inner.new_address().await.unwrap(), amount)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
(btc_wallet, xmr_wallet)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@ -174,17 +193,21 @@ pub async fn init_alice(
|
||||
Arc<swap::bitcoin::Wallet>,
|
||||
Arc<swap::monero::Wallet>,
|
||||
) {
|
||||
let (alice_xmr_wallet, alice_btc_wallet) =
|
||||
init_alice_wallets(bitcoind, monero, xmr_starting_balance, config).await;
|
||||
|
||||
let (alice_start_state, swarm_driver, handle) = init_alice_eventloop(
|
||||
btc_to_swap,
|
||||
xmr_to_swap,
|
||||
alice_btc_wallet.clone(),
|
||||
listen,
|
||||
let (alice_btc_wallet, alice_xmr_wallet) = init_wallets(
|
||||
"alice",
|
||||
bitcoind,
|
||||
monero,
|
||||
None,
|
||||
Some(xmr_starting_balance),
|
||||
config,
|
||||
)
|
||||
.await;
|
||||
|
||||
let alice_start_state =
|
||||
init_alice_state(btc_to_swap, xmr_to_swap, alice_btc_wallet.clone(), config).await;
|
||||
|
||||
let (swarm_driver, handle) = init_alice_eventloop(listen);
|
||||
|
||||
(
|
||||
alice_start_state,
|
||||
swarm_driver,
|
||||
@ -197,8 +220,8 @@ pub async fn init_alice(
|
||||
// This is just to keep the containers alive
|
||||
#[allow(dead_code)]
|
||||
pub struct Containers<'a> {
|
||||
bitcoind: Bitcoind<'a>,
|
||||
monerods: Vec<Container<'a, Cli, image::Monero>>,
|
||||
pub bitcoind: Bitcoind<'a>,
|
||||
pub monerods: Vec<Container<'a, Cli, image::Monero>>,
|
||||
}
|
||||
|
||||
/// Utility function to initialize logging in the test environment.
|
||||
|
Loading…
Reference in New Issue
Block a user