Remove lib

Removed lib and moved the tests into a src folder. Moved the types
in the lib to appropriate modules.
This commit is contained in:
rishflab 2021-01-08 11:30:33 +11:00
parent 485220929e
commit 7d8fe742e8
24 changed files with 172 additions and 218 deletions

View File

@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
use sha2::Sha256;
use std::str::FromStr;
use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks};
use crate::{bitcoin::timelocks::BlockHeight, config::Config, protocol::ExpiredTimelocks};
pub use crate::bitcoin::{
timelocks::Timelock,

View File

@ -36,6 +36,7 @@ impl Display for Swap {
}
}
#[derive(Debug)]
pub struct Database(sled::Db);
impl Database {

View File

@ -5,8 +5,7 @@ use crate::{
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
monero,
monero::monero_private_key,
protocol::{alice, alice::AliceState},
SwapAmounts,
protocol::{alice, alice::AliceState, SwapAmounts},
};
// Large enum variant is fine because this is only used for database

View File

@ -1,10 +1,7 @@
use ::bitcoin::hashes::core::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::{
protocol::{bob, bob::BobState},
SwapAmounts,
};
use crate::protocol::{bob, bob::BobState, SwapAmounts};
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Bob {

View File

@ -1,77 +0,0 @@
#![warn(
unused_extern_crates,
rust_2018_idioms,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::fallible_impl_from,
clippy::cast_precision_loss,
clippy::cast_possible_wrap,
clippy::dbg_macro
)]
#![cfg_attr(not(test), warn(clippy::unwrap_used))]
#![forbid(unsafe_code)]
#![allow(
non_snake_case,
missing_debug_implementations,
missing_copy_implementations
)]
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
pub mod bitcoin;
pub mod cli;
pub mod config;
pub mod database;
pub mod fs;
pub mod monero;
pub mod network;
pub mod protocol;
pub mod seed;
pub mod trace;
pub type Never = std::convert::Infallible;
/// Commands sent from Bob to the main task.
#[derive(Clone, Copy, Debug)]
pub enum Cmd {
VerifyAmounts(SwapAmounts),
}
/// Responses sent from the main task back to Bob.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Rsp {
VerifiedAmounts,
Abort,
}
/// XMR/BTC swap amounts.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
// TODO(Franck): review necessity of this struct
pub struct SwapAmounts {
/// Amount of BTC to swap.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
/// Amount of XMR to swap.
#[serde(with = "monero::monero_amount")]
pub xmr: monero::Amount,
}
// TODO: Display in XMR and BTC (not picos and sats).
impl Display for SwapAmounts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} sats for {} piconeros",
self.btc.as_sat(),
self.xmr.as_piconero()
)
}
}
#[derive(Debug, Clone, Copy)]
pub enum ExpiredTimelocks {
None,
Cancel,
Punish,
}

View File

@ -14,26 +14,31 @@
#![allow(non_snake_case)]
use anyhow::{bail, Context, Result};
use cli::{Command, Options, Resume};
use config::Config;
use database::{Database, Swap};
use libp2p::{core::Multiaddr, PeerId};
use network::transport::build;
use prettytable::{row, Table};
use protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts};
use rand::rngs::OsRng;
use std::sync::Arc;
use structopt::StructOpt;
use swap::{
bitcoin,
cli::{Command, Options, Resume},
config::Config,
database::{Database, Swap},
monero, network,
network::transport::build,
protocol::{alice, alice::AliceState, bob, bob::BobState},
seed::Seed,
trace::init_tracing,
SwapAmounts,
};
use trace::init_tracing;
use tracing::{info, log::LevelFilter};
use uuid::Uuid;
pub mod bitcoin;
pub mod cli;
pub mod config;
pub mod database;
pub mod monero;
pub mod network;
pub mod protocol;
#[cfg(test)]
mod tests;
pub mod trace;
#[macro_use]
extern crate prettytable;
@ -258,10 +263,9 @@ async fn setup_wallets(
bitcoin_wallet_name: &str,
monero_wallet_rpc_url: url::Url,
config: Config,
) -> Result<(Arc<swap::bitcoin::Wallet>, Arc<swap::monero::Wallet>)> {
) -> Result<(Arc<bitcoin::Wallet>, Arc<monero::Wallet>)> {
let bitcoin_wallet =
swap::bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network)
.await?;
bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network).await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
info!(
"Connection to Bitcoin wallet succeeded, balance: {}",
@ -284,8 +288,8 @@ async fn alice_swap(
swap_id: Uuid,
state: AliceState,
listen_addr: Multiaddr,
bitcoin_wallet: Arc<swap::bitcoin::Wallet>,
monero_wallet: Arc<swap::monero::Wallet>,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
config: Config,
db: Database,
seed: Seed,
@ -316,8 +320,8 @@ async fn alice_swap(
async fn bob_swap(
swap_id: Uuid,
state: BobState,
bitcoin_wallet: Arc<swap::bitcoin::Wallet>,
monero_wallet: Arc<swap::monero::Wallet>,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
db: Database,
alice_peer_id: PeerId,
alice_addr: Multiaddr,

View File

@ -9,10 +9,7 @@ use serde::{Deserialize, Serialize};
use std::{fmt::Debug, io, marker::PhantomData};
use tracing::debug;
use crate::{
protocol::{alice, bob},
SwapAmounts,
};
use crate::protocol::{alice, bob, SwapAmounts};
/// Time to wait for a response back once we send a request.
pub const TIMEOUT: u64 = 3600; // One hour.

View File

@ -1,2 +1,37 @@
use crate::monero;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
pub mod alice;
pub mod bob;
#[derive(Debug, Clone, Copy)]
pub enum ExpiredTimelocks {
None,
Cancel,
Punish,
}
/// XMR/BTC swap amounts.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
// TODO(Franck): review necessity of this struct
pub struct SwapAmounts {
/// Amount of BTC to swap.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
/// Amount of XMR to swap.
#[serde(with = "monero::monero_amount")]
pub xmr: monero::Amount,
}
// TODO: Display in XMR and BTC (not picos and sats).
impl Display for SwapAmounts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} sats for {} piconeros",
self.btc.as_sat(),
self.xmr.as_piconero()
)
}
}

View File

@ -15,8 +15,7 @@ use crate::{
transport::SwapTransport,
Seed, TokioExecutor,
},
protocol::bob,
SwapAmounts,
protocol::{bob, SwapAmounts},
};
pub use self::{amounts::*, message0::Message0, message1::Message1, message2::Message2, state::*};

View File

@ -10,9 +10,8 @@ use crate::{
protocol::{
alice,
alice::{Behaviour, OutEvent},
bob,
bob, SwapAmounts,
},
SwapAmounts,
};
#[allow(missing_debug_implementations)]

View File

@ -19,8 +19,7 @@ use crate::{
monero,
monero::CreateWalletForOutput,
network::request_response::AliceToBob,
protocol::{alice, bob},
ExpiredTimelocks, SwapAmounts,
protocol::{alice, bob, ExpiredTimelocks, SwapAmounts},
};
#[derive(Debug)]

View File

@ -24,8 +24,7 @@ use crate::{
monero,
monero::Transfer,
network::request_response::AliceToBob,
protocol::{alice, alice::event_loop::EventLoopHandle},
SwapAmounts,
protocol::{alice, alice::event_loop::EventLoopHandle, SwapAmounts},
};
pub async fn negotiate(

View File

@ -18,17 +18,20 @@ use crate::{
database::{Database, Swap},
monero,
monero::CreateWalletForOutput,
protocol::alice::{
event_loop::EventLoopHandle,
steps::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
publish_bitcoin_redeem_transaction, publish_cancel_transaction,
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin,
protocol::{
alice::{
event_loop::EventLoopHandle,
steps::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate,
publish_bitcoin_punish_transaction, publish_bitcoin_redeem_transaction,
publish_cancel_transaction, wait_for_bitcoin_encrypted_signature,
wait_for_bitcoin_refund, wait_for_locked_bitcoin,
},
AliceState,
},
AliceState,
ExpiredTimelocks,
},
ExpiredTimelocks,
};
trait Rng: RngCore + CryptoRng + Send {}

View File

@ -14,8 +14,7 @@ use crate::{
transport::SwapTransport,
Seed, TokioExecutor,
},
protocol::{alice, bob},
SwapAmounts,
protocol::{alice, bob, SwapAmounts},
};
pub use self::{

View File

@ -16,7 +16,7 @@ use tracing::{debug, error};
use crate::{
network::request_response::{AliceToBob, AmountsProtocol, BobToAlice, Codec, TIMEOUT},
SwapAmounts,
protocol::SwapAmounts,
};
#[derive(Copy, Clone, Debug)]

View File

@ -17,8 +17,7 @@ use crate::{
},
monero,
monero::monero_private_key,
protocol::{alice, bob},
ExpiredTimelocks, SwapAmounts,
protocol::{alice, bob, ExpiredTimelocks, SwapAmounts},
};
#[derive(Debug, Clone)]

View File

@ -8,8 +8,10 @@ use uuid::Uuid;
use crate::{
database::{Database, Swap},
protocol::bob::{self, event_loop::EventLoopHandle, state::*},
ExpiredTimelocks, SwapAmounts,
protocol::{
bob::{self, event_loop::EventLoopHandle, state::*},
ExpiredTimelocks, SwapAmounts,
},
};
// TODO(Franck): Make this a method on a struct

View File

@ -3,20 +3,27 @@ use libp2p::{core::Multiaddr, PeerId};
use monero_harness::{image, Monero};
use rand::rngs::OsRng;
use std::sync::Arc;
use swap::{
use tempfile::tempdir;
use testcontainers::{clients::Cli, Container};
use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer;
use crate::{
bitcoin,
config::Config,
database::Database,
monero, network,
network::transport::build,
protocol::{alice, alice::AliceState, bob, bob::BobState},
seed::Seed,
SwapAmounts,
protocol::{alice, alice::AliceState, bob, bob::BobState,seed::Seed,
SwapAmounts},
};
use tempfile::tempdir;
use testcontainers::{clients::Cli, Container};
use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer;
mod happy_path;
mod happy_path_restart_alice;
mod happy_path_restart_bob_after_comm;
mod happy_path_restart_bob_before_comm;
mod punish;
mod refund_restart_alice;
pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
@ -51,13 +58,13 @@ pub async fn init_wallets(
}
};
let xmr_wallet = Arc::new(swap::monero::Wallet {
let xmr_wallet = Arc::new(monero::Wallet {
inner: monero.wallet(name).unwrap().client(),
network: config.monero_network,
});
let btc_wallet = Arc::new(
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network)
bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network)
.await
.unwrap(),
);
@ -131,8 +138,8 @@ pub async fn init_alice(
AliceState,
alice::event_loop::EventLoop,
alice::event_loop::EventLoopHandle,
Arc<swap::bitcoin::Wallet>,
Arc<swap::monero::Wallet>,
Arc<bitcoin::Wallet>,
Arc<monero::Wallet>,
Database,
) {
let (alice_btc_wallet, alice_xmr_wallet) = init_wallets(
@ -213,8 +220,8 @@ pub async fn init_bob(
BobState,
bob::event_loop::EventLoop,
bob::event_loop::EventLoopHandle,
Arc<swap::bitcoin::Wallet>,
Arc<swap::monero::Wallet>,
Arc<bitcoin::Wallet>,
Arc<monero::Wallet>,
Database,
) {
let (bob_btc_wallet, bob_xmr_wallet) = init_wallets(

View File

@ -1,4 +1,3 @@
use crate::testutils::{init_alice, init_bob};
use futures::{
future::{join, select},
FutureExt,
@ -6,18 +5,17 @@ use futures::{
use get_port::get_port;
use libp2p::Multiaddr;
use rand::rngs::OsRng;
use swap::{
use testcontainers::clients::Cli;
use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
monero,
protocol::{alice, bob},
seed::Seed,
};
use testcontainers::clients::Cli;
use testutils::init_tracing;
use uuid::Uuid;
pub mod testutils;
/// Run the following tests with RUST_MIN_STACK=10000000
@ -28,11 +26,11 @@ async fn happy_path() {
let cli = Cli::default();
let (
monero,
testutils::Containers {
tests::Containers {
bitcoind,
monerods: _monerods,
},
) = testutils::init_containers(&cli).await;
) = tests::init_containers(&cli).await;
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
let btc_alice = bitcoin::Amount::ZERO;

View File

@ -1,21 +1,21 @@
use crate::testutils::{init_alice, init_bob};
use get_port::get_port;
use libp2p::Multiaddr;
use rand::rngs::OsRng;
use swap::{
use tempfile::tempdir;
use testcontainers::clients::Cli;
use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
database,
database::Database,
monero,
protocol::{alice, alice::AliceState, bob},
seed::Seed,
tests,
tests::{init_alice, init_bob, init_tracing},
};
use tempfile::tempdir;
use testcontainers::clients::Cli;
use testutils::init_tracing;
use uuid::Uuid;
pub mod testutils;
#[tokio::test]
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
@ -24,11 +24,11 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
let cli = Cli::default();
let (
monero,
testutils::Containers {
tests::Containers {
bitcoind,
monerods: _monerods,
},
) = testutils::init_containers(&cli).await;
) = tests::init_containers(&cli).await;
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
@ -120,15 +120,15 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
let resume_state =
if let swap::database::Swap::Alice(state) = alice_db.get_state(alice_swap_id).unwrap() {
assert!(matches!(state, swap::database::Alice::EncSigLearned {..}));
if let database::Swap::Alice(state) = alice_db.get_state(alice_swap_id).unwrap() {
assert!(matches!(state, database::Alice::EncSigLearned {..}));
state.into()
} else {
unreachable!()
};
let (mut event_loop_after_restart, event_loop_handle_after_restart) =
testutils::init_alice_event_loop(alice_multiaddr, alice_seed);
tests::init_alice_event_loop(alice_multiaddr, alice_seed);
tokio::spawn(async move { event_loop_after_restart.run().await });
let alice_state = alice::swap::swap(

View File

@ -1,21 +1,20 @@
use crate::testutils::{init_alice, init_bob};
use get_port::get_port;
use libp2p::Multiaddr;
use rand::rngs::OsRng;
use swap::{
use tempfile::tempdir;
use testcontainers::clients::Cli;
use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
database,
database::Database,
monero,
protocol::{alice, bob, bob::BobState},
seed::Seed,
tests::{init_alice, init_bob, init_bob_event_loop, init_containers, init_tracing, Containers},
};
use tempfile::tempdir;
use testcontainers::clients::Cli;
use testutils::init_tracing;
use uuid::Uuid;
pub mod testutils;
#[tokio::test]
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
@ -24,11 +23,11 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
let cli = Cli::default();
let (
monero,
testutils::Containers {
Containers {
bitcoind,
monerods: _monerods,
},
) = testutils::init_containers(&cli).await;
) = init_containers(&cli).await;
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
@ -121,16 +120,15 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
let bob_db = Database::open(bob_db_datadir.path()).unwrap();
let resume_state =
if let swap::database::Swap::Bob(state) = bob_db.get_state(bob_swap_id).unwrap() {
assert!(matches!(state, swap::database::Bob::EncSigSent {..}));
state.into()
} else {
unreachable!()
};
let resume_state = if let database::Swap::Bob(state) = bob_db.get_state(bob_swap_id).unwrap() {
assert!(matches!(state, database::Bob::EncSigSent {..}));
state.into()
} else {
unreachable!()
};
let (event_loop_after_restart, event_loop_handle_after_restart) =
testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr);
init_bob_event_loop(alice_peer_id, alice_multiaddr);
tokio::spawn(event_loop_after_restart.run());
let bob_state = bob::swap::swap(

View File

@ -1,22 +1,20 @@
use crate::testutils::{init_alice, init_bob};
use get_port::get_port;
use libp2p::Multiaddr;
use rand::rngs::OsRng;
use swap::{
use tempfile::tempdir;
use testcontainers::clients::Cli;
use tokio::select;
use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
database::Database,
monero,
protocol::{alice, alice::AliceState, bob, bob::BobState},
seed::Seed,
tests::{init_alice, init_bob, init_bob_event_loop, init_containers, init_tracing, Containers},
};
use tempfile::tempdir;
use testcontainers::clients::Cli;
use testutils::init_tracing;
use tokio::select;
use uuid::Uuid;
pub mod testutils;
#[tokio::test]
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
@ -25,11 +23,11 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
let cli = Cli::default();
let (
monero,
testutils::Containers {
Containers {
bitcoind,
monerods: _monerods,
},
) = testutils::init_containers(&cli).await;
) = init_containers(&cli).await;
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
@ -117,7 +115,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
};
let (bob_event_loop_2, bob_event_loop_handle_2) =
testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr);
init_bob_event_loop(alice_peer_id, alice_multiaddr);
let bob_fut = bob::swap::swap(
bob_restart_state,

View File

@ -1,4 +1,3 @@
use crate::testutils::{init_alice, init_bob};
use futures::{
future::{join, select, Either},
FutureExt,
@ -6,18 +5,17 @@ use futures::{
use get_port::get_port;
use libp2p::Multiaddr;
use rand::rngs::OsRng;
use swap::{
use testcontainers::clients::Cli;
use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
monero,
protocol::{alice, alice::AliceState, bob, bob::BobState},
seed::Seed,
tests::{init_alice, init_bob, init_containers, init_tracing, Containers},
};
use testcontainers::clients::Cli;
use testutils::init_tracing;
use uuid::Uuid;
pub mod testutils;
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
/// the encsig and fail to refund or redeem. Alice punishes.
@ -28,11 +26,11 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
let cli = Cli::default();
let (
monero,
testutils::Containers {
Containers {
bitcoind,
monerods: _monerods,
},
) = testutils::init_containers(&cli).await;
) = init_containers(&cli).await;
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);

View File

@ -1,23 +1,23 @@
use crate::testutils::{init_alice, init_bob};
use futures::future::try_join;
use get_port::get_port;
use libp2p::Multiaddr;
use rand::rngs::OsRng;
use swap::{
use tempfile::tempdir;
use testcontainers::clients::Cli;
use tokio::select;
use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
database::Database,
monero,
protocol::{alice, alice::AliceState, bob, bob::BobState},
seed::Seed,
tests::{
init_alice, init_alice_event_loop, init_bob, init_containers, init_tracing, Containers,
},
};
use tempfile::tempdir;
use testcontainers::clients::Cli;
use testutils::init_tracing;
use tokio::select;
use uuid::Uuid;
pub mod testutils;
// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
// then also refunds.
@ -28,11 +28,11 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
let cli = Cli::default();
let (
monero,
testutils::Containers {
Containers {
bitcoind,
monerods: _monerods,
},
) = testutils::init_containers(&cli).await;
) = init_containers(&cli).await;
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
@ -124,7 +124,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
};
let (mut alice_event_loop_2, alice_event_loop_handle_2) =
testutils::init_alice_event_loop(alice_multiaddr, alice_seed);
init_alice_event_loop(alice_multiaddr, alice_seed);
let alice_final_state = {
let alice_db = Database::open(alice_db_datadir.path()).unwrap();