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 libp2p::Multiaddr;
|
||||||
use monero_harness::Monero;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{alice, alice::swap::AliceState, bitcoin, bob, storage::Database};
|
use swap::{alice, alice::swap::AliceState, bitcoin, bob, storage::Database};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
@ -18,19 +16,18 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
|||||||
let _guard = init_tracing();
|
let _guard = init_tracing();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
let (
|
||||||
let _ = bitcoind.init(5).await;
|
monero,
|
||||||
let (monero, _container) =
|
testutils::Containers {
|
||||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
bitcoind,
|
||||||
.await
|
monerods: _monerods,
|
||||||
.unwrap();
|
},
|
||||||
|
) = testutils::init_containers(&cli).await;
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
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 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_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_xmr_starting_balance = xmr_to_swap * 10;
|
||||||
|
|
||||||
let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9877"
|
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,
|
btc_to_swap,
|
||||||
bob_btc_starting_balance,
|
bob_btc_starting_balance,
|
||||||
xmr_to_swap,
|
xmr_to_swap,
|
||||||
bob_xmr_starting_balance,
|
|
||||||
config,
|
config,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -113,15 +109,8 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
|||||||
assert!(matches!(state, swap::state::Alice::EncSignLearned {..}));
|
assert!(matches!(state, swap::state::Alice::EncSignLearned {..}));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (_, mut event_loop_after_restart, event_loop_handle_after_restart) =
|
let (mut event_loop_after_restart, event_loop_handle_after_restart) =
|
||||||
testutils::init_alice_eventloop(
|
testutils::init_alice_eventloop(alice_multiaddr);
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_multiaddr,
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
let _alice_swarm_fut = tokio::spawn(async move { event_loop_after_restart.run().await });
|
let _alice_swarm_fut = tokio::spawn(async move { event_loop_after_restart.run().await });
|
||||||
|
|
||||||
let alice_state = alice::swap::recover(
|
let alice_state = alice::swap::recover(
|
||||||
@ -136,4 +125,6 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::BtcRedeemed {..}));
|
assert!(matches!(alice_state, AliceState::BtcRedeemed {..}));
|
||||||
|
|
||||||
|
// TODO: Additionally assert balances
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
use crate::testutils::{init_alice, init_bob};
|
||||||
use bitcoin_harness::Bitcoind;
|
|
||||||
use futures::future::try_join;
|
use futures::future::try_join;
|
||||||
use libp2p::Multiaddr;
|
use libp2p::Multiaddr;
|
||||||
use monero_harness::Monero;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{alice, bob, storage::Database};
|
use swap::{alice, bob, storage::Database};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
@ -20,12 +18,13 @@ async fn happy_path() {
|
|||||||
let _guard = init_tracing();
|
let _guard = init_tracing();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
let (
|
||||||
let _ = bitcoind.init(5).await;
|
monero,
|
||||||
let (monero, _container) =
|
testutils::Containers {
|
||||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
bitcoind,
|
||||||
.await
|
monerods: _monerods,
|
||||||
.unwrap();
|
},
|
||||||
|
) = testutils::init_containers(&cli).await;
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
||||||
let btc_alice = bitcoin::Amount::ZERO;
|
let btc_alice = bitcoin::Amount::ZERO;
|
||||||
@ -69,7 +68,6 @@ async fn happy_path() {
|
|||||||
btc_to_swap,
|
btc_to_swap,
|
||||||
btc_bob,
|
btc_bob,
|
||||||
xmr_to_swap,
|
xmr_to_swap,
|
||||||
xmr_bob,
|
|
||||||
config,
|
config,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
use crate::testutils::{init_alice, init_bob};
|
||||||
use bitcoin_harness::Bitcoind;
|
|
||||||
use futures::future::try_join;
|
use futures::future::try_join;
|
||||||
use libp2p::Multiaddr;
|
use libp2p::Multiaddr;
|
||||||
use monero_harness::Monero;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database};
|
use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
@ -20,18 +18,18 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
|
|||||||
let _guard = init_tracing();
|
let _guard = init_tracing();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
let (
|
||||||
let _ = bitcoind.init(5).await;
|
monero,
|
||||||
let (monero, _container) =
|
testutils::Containers {
|
||||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
bitcoind,
|
||||||
.await
|
monerods: _monerods,
|
||||||
.unwrap();
|
},
|
||||||
|
) = testutils::init_containers(&cli).await;
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
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 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_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_btc_starting_balance = bitcoin::Amount::ZERO;
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
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,
|
btc_to_swap,
|
||||||
bob_btc_starting_balance,
|
bob_btc_starting_balance,
|
||||||
xmr_to_swap,
|
xmr_to_swap,
|
||||||
bob_xmr_starting_balance,
|
|
||||||
config,
|
config,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
use crate::testutils::{init_alice, init_bob};
|
||||||
use bitcoin_harness::Bitcoind;
|
|
||||||
use futures::future::try_join;
|
use futures::future::try_join;
|
||||||
use libp2p::Multiaddr;
|
use libp2p::Multiaddr;
|
||||||
use monero_harness::Monero;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database};
|
use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
@ -19,19 +17,14 @@ pub mod testutils;
|
|||||||
async fn both_refund() {
|
async fn both_refund() {
|
||||||
let _guard = init_tracing();
|
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 cli = Cli::default();
|
||||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
let (
|
||||||
let _ = bitcoind.init(5).await;
|
monero,
|
||||||
let (monero, _container) =
|
testutils::Containers {
|
||||||
Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
bitcoind,
|
||||||
.await
|
monerods: _monerods,
|
||||||
.unwrap();
|
},
|
||||||
|
) = testutils::init_containers(&cli).await;
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
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 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,
|
btc_to_swap,
|
||||||
bob_btc_starting_balance,
|
bob_btc_starting_balance,
|
||||||
xmr_to_swap,
|
xmr_to_swap,
|
||||||
bob_xmr_starting_balance,
|
|
||||||
Config::regtest(),
|
Config::regtest(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
@ -21,7 +21,6 @@ pub async fn init_bob(
|
|||||||
btc_to_swap: bitcoin::Amount,
|
btc_to_swap: bitcoin::Amount,
|
||||||
btc_starting_balance: bitcoin::Amount,
|
btc_starting_balance: bitcoin::Amount,
|
||||||
xmr_to_swap: xmr_btc::monero::Amount,
|
xmr_to_swap: xmr_btc::monero::Amount,
|
||||||
xmr_stating_balance: xmr_btc::monero::Amount,
|
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> (
|
) -> (
|
||||||
BobState,
|
BobState,
|
||||||
@ -31,25 +30,15 @@ pub async fn init_bob(
|
|||||||
Arc<swap::monero::Wallet>,
|
Arc<swap::monero::Wallet>,
|
||||||
Database,
|
Database,
|
||||||
) {
|
) {
|
||||||
let bob_btc_wallet = Arc::new(
|
let (bob_btc_wallet, bob_xmr_wallet) = init_wallets(
|
||||||
swap::bitcoin::Wallet::new("bob", bitcoind.node_url.clone(), config.bitcoin_network)
|
"bob",
|
||||||
.await
|
bitcoind,
|
||||||
.unwrap(),
|
monero,
|
||||||
);
|
Some(btc_starting_balance),
|
||||||
bitcoind
|
None,
|
||||||
.mint(
|
config,
|
||||||
bob_btc_wallet.inner.new_address().await.unwrap(),
|
|
||||||
btc_starting_balance,
|
|
||||||
)
|
)
|
||||||
.await
|
.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 amounts = SwapAmounts {
|
let amounts = SwapAmounts {
|
||||||
btc: btc_to_swap,
|
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,
|
btc_to_swap: bitcoin::Amount,
|
||||||
xmr_to_swap: monero::Amount,
|
xmr_to_swap: monero::Amount,
|
||||||
alice_btc_wallet: Arc<bitcoin::Wallet>,
|
alice_btc_wallet: Arc<bitcoin::Wallet>,
|
||||||
listen: Multiaddr,
|
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> (
|
) -> AliceState {
|
||||||
AliceState,
|
|
||||||
alice::event_loop::EventLoop,
|
|
||||||
alice::event_loop::EventLoopHandle,
|
|
||||||
) {
|
|
||||||
let rng = &mut OsRng;
|
let rng = &mut OsRng;
|
||||||
|
|
||||||
let amounts = SwapAmounts {
|
let amounts = SwapAmounts {
|
||||||
@ -123,39 +107,74 @@ pub async fn init_alice_eventloop(
|
|||||||
redeem_address,
|
redeem_address,
|
||||||
punish_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_behaviour = alice::Behaviour::default();
|
||||||
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
||||||
|
|
||||||
let (swarm_driver, handle) =
|
let (swarm_driver, handle) =
|
||||||
alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap();
|
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(
|
pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
||||||
bitcoind: &Bitcoind<'_>,
|
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||||
monero: &Monero,
|
let _ = bitcoind.init(5).await;
|
||||||
xmr_starting_balance: xmr_btc::monero::Amount,
|
let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()])
|
||||||
config: Config,
|
|
||||||
) -> (Arc<monero::Wallet>, Arc<bitcoin::Wallet>) {
|
|
||||||
monero
|
|
||||||
.init(vec![("alice", xmr_starting_balance.as_piconero())])
|
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let alice_xmr_wallet = Arc::new(swap::monero::Wallet(
|
(monero, Containers { bitcoind, monerods })
|
||||||
monero.wallet("alice").unwrap().client(),
|
}
|
||||||
));
|
|
||||||
|
|
||||||
let alice_btc_wallet = Arc::new(
|
pub async fn init_wallets(
|
||||||
swap::bitcoin::Wallet::new("alice", bitcoind.node_url.clone(), config.bitcoin_network)
|
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
|
.await
|
||||||
.unwrap(),
|
.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)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
@ -174,17 +193,21 @@ pub async fn init_alice(
|
|||||||
Arc<swap::bitcoin::Wallet>,
|
Arc<swap::bitcoin::Wallet>,
|
||||||
Arc<swap::monero::Wallet>,
|
Arc<swap::monero::Wallet>,
|
||||||
) {
|
) {
|
||||||
let (alice_xmr_wallet, alice_btc_wallet) =
|
let (alice_btc_wallet, alice_xmr_wallet) = init_wallets(
|
||||||
init_alice_wallets(bitcoind, monero, xmr_starting_balance, config).await;
|
"alice",
|
||||||
|
bitcoind,
|
||||||
let (alice_start_state, swarm_driver, handle) = init_alice_eventloop(
|
monero,
|
||||||
btc_to_swap,
|
None,
|
||||||
xmr_to_swap,
|
Some(xmr_starting_balance),
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
listen,
|
|
||||||
config,
|
config,
|
||||||
)
|
)
|
||||||
.await;
|
.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,
|
alice_start_state,
|
||||||
swarm_driver,
|
swarm_driver,
|
||||||
@ -197,8 +220,8 @@ pub async fn init_alice(
|
|||||||
// This is just to keep the containers alive
|
// This is just to keep the containers alive
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Containers<'a> {
|
pub struct Containers<'a> {
|
||||||
bitcoind: Bitcoind<'a>,
|
pub bitcoind: Bitcoind<'a>,
|
||||||
monerods: Vec<Container<'a, Cli, image::Monero>>,
|
pub monerods: Vec<Container<'a, Cli, image::Monero>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Utility function to initialize logging in the test environment.
|
/// Utility function to initialize logging in the test environment.
|
||||||
|
Loading…
Reference in New Issue
Block a user