From c781ee949db110166bc83bc9e05bb6b20e3a7162 Mon Sep 17 00:00:00 2001 From: rishflab Date: Fri, 23 Oct 2020 13:13:17 +1100 Subject: [PATCH] Move storage into application crate --- swap/Cargo.toml | 3 + swap/src/lib.rs | 1 + .../tests/harness => swap/src}/storage.rs | 17 +-- xmr-btc/Cargo.toml | 1 - xmr-btc/src/lib.rs | 3 + xmr-btc/tests/e2e.rs | 116 ------------------ xmr-btc/tests/harness/mod.rs | 1 - 7 files changed, 17 insertions(+), 125 deletions(-) rename {xmr-btc/tests/harness => swap/src}/storage.rs (91%) diff --git a/swap/Cargo.toml b/swap/Cargo.toml index 8a2bcb6c..de572e12 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -22,9 +22,12 @@ monero = "0.9" rand = "0.7" reqwest = { version = "0.10", default-features = false, features = ["socks"] } serde = { version = "1", features = ["derive"] } +serde_cbor = "0.11" serde_derive = "1.0" serde_json = "1" +sled = "0.34" structopt = "0.3" +tempfile = "3" time = "0.2" tokio = { version = "0.2", features = ["rt-threaded", "time", "macros", "sync"] } torut = { version = "0.1", optional = true } diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 76b92e67..7a85eeed 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -5,6 +5,7 @@ pub mod alice; pub mod bitcoin; pub mod bob; pub mod network; +pub mod storage; #[cfg(feature = "tor")] pub mod tor; diff --git a/xmr-btc/tests/harness/storage.rs b/swap/src/storage.rs similarity index 91% rename from xmr-btc/tests/harness/storage.rs rename to swap/src/storage.rs index 5fbcc40f..86581fc0 100644 --- a/xmr-btc/tests/harness/storage.rs +++ b/swap/src/storage.rs @@ -1,5 +1,5 @@ use anyhow::{anyhow, Context, Result}; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use serde::{de::DeserializeOwned, Serialize}; use std::path::Path; pub struct Database @@ -78,16 +78,19 @@ mod tests { #![allow(non_snake_case)] use super::*; use bitcoin::SigHash; - use curve25519_dalek::scalar::Scalar; - use ecdsa_fun::fun::rand_core::OsRng; + use rand::rngs::OsRng; + use serde::{Deserialize, Serialize}; use std::str::FromStr; - use xmr_btc::serde::{bitcoin_amount, monero_private_key}; + use xmr_btc::{ + serde::{bitcoin_amount, monero_private_key}, + CrossCurveScalar, Curve25519Scalar, + }; #[derive(Debug, Serialize, Deserialize, PartialEq)] pub struct TestState { A: xmr_btc::bitcoin::PublicKey, a: xmr_btc::bitcoin::SecretKey, - s_a: ::cross_curve_dleq::Scalar, + s_a: CrossCurveScalar, #[serde(with = "monero_private_key")] s_b: monero::PrivateKey, S_a_monero: ::monero::PublicKey, @@ -108,8 +111,8 @@ mod tests { let db = Database::open(db_dir.path()).unwrap(); let a = xmr_btc::bitcoin::SecretKey::new_random(&mut OsRng); - let s_a = cross_curve_dleq::Scalar::random(&mut OsRng); - let s_b = monero::PrivateKey::from_scalar(Scalar::random(&mut OsRng)); + let s_a = CrossCurveScalar::random(&mut OsRng); + let s_b = monero::PrivateKey::from_scalar(Curve25519Scalar::random(&mut OsRng)); let v_a = xmr_btc::monero::PrivateViewKey::new_random(&mut OsRng); let S_a_monero = monero::PublicKey::from_private_key(&monero::PrivateKey { scalar: s_a.into_ed25519(), diff --git a/xmr-btc/Cargo.toml b/xmr-btc/Cargo.toml index 143a1a67..22dfc30b 100644 --- a/xmr-btc/Cargo.toml +++ b/xmr-btc/Cargo.toml @@ -33,7 +33,6 @@ futures = "0.3" monero-harness = { path = "../monero-harness" } reqwest = { version = "0.10", default-features = false } serde_cbor = "0.11" -sled = "0.34" tempfile = "3" testcontainers = "0.10" tracing = "0.1" diff --git a/xmr-btc/src/lib.rs b/xmr-btc/src/lib.rs index 4af89d72..6a14fce6 100644 --- a/xmr-btc/src/lib.rs +++ b/xmr-btc/src/lib.rs @@ -52,6 +52,9 @@ pub mod monero; pub mod serde; pub mod transport; +pub use cross_curve_dleq::Scalar as CrossCurveScalar; +pub use curve25519_dalek::scalar::Scalar as Curve25519Scalar; + use async_trait::async_trait; use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic}; use futures::{ diff --git a/xmr-btc/tests/e2e.rs b/xmr-btc/tests/e2e.rs index 386e7605..72795fb7 100644 --- a/xmr-btc/tests/e2e.rs +++ b/xmr-btc/tests/e2e.rs @@ -16,8 +16,6 @@ mod tests { use futures::future; use monero_harness::Monero; use rand::rngs::OsRng; - - use crate::harness::storage::Database; use std::convert::TryInto; use testcontainers::clients::Cli; use tracing_subscriber::util::SubscriberInitExt; @@ -241,118 +239,4 @@ mod tests { initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee ); } - - #[tokio::test] - async fn recover_protocol_state_from_db() { - let _guard = tracing_subscriber::fmt() - .with_env_filter("info") - .set_default(); - - let cli = Cli::default(); - let (monero, _container) = Monero::new(&cli).unwrap(); - let bitcoind = init_bitcoind(&cli).await; - - let alice_db_dir = tempfile::tempdir().unwrap(); - let alice_db: Database = - harness::storage::Database::open(alice_db_dir.path()).unwrap(); - let bob_db_dir = tempfile::tempdir().unwrap(); - let bob_db: Database = - harness::storage::Database::open(bob_db_dir.path()).unwrap(); - - let ( - alice_state0, - bob_state0, - mut alice_node, - mut bob_node, - initial_balances, - swap_amounts, - ) = init_test(&monero, &bitcoind, None, None).await; - - { - let (alice_state, bob_state) = future::try_join( - run_alice_until( - &mut alice_node, - alice_state0.into(), - harness::alice::is_state5, - &mut OsRng, - ), - run_bob_until( - &mut bob_node, - bob_state0.into(), - harness::bob::is_state3, - &mut OsRng, - ), - ) - .await - .unwrap(); - - // save state to db - alice_db.insert_latest_state(&alice_state).await.unwrap(); - bob_db.insert_latest_state(&bob_state).await.unwrap(); - }; - - let (alice_state6, bob_state5) = { - // recover state from db - let alice_state = alice_db.get_latest_state().unwrap(); - let bob_state = bob_db.get_latest_state().unwrap(); - - let (alice_state, bob_state) = future::try_join( - run_alice_until( - &mut alice_node, - alice_state, - harness::alice::is_state6, - &mut OsRng, - ), - run_bob_until( - &mut bob_node, - bob_state, - harness::bob::is_state5, - &mut OsRng, - ), - ) - .await - .unwrap(); - - let alice_state: alice::State6 = alice_state.try_into().unwrap(); - let bob_state: bob::State5 = bob_state.try_into().unwrap(); - - (alice_state, bob_state) - }; - - let alice_final_btc_balance = alice_node.bitcoin_wallet.balance().await.unwrap(); - let bob_final_btc_balance = bob_node.bitcoin_wallet.balance().await.unwrap(); - - let lock_tx_bitcoin_fee = bob_node - .bitcoin_wallet - .transaction_fee(bob_state5.tx_lock_id()) - .await - .unwrap(); - - let alice_final_xmr_balance = alice_node.monero_wallet.0.get_balance(0).await.unwrap(); - - monero.wait_for_bob_wallet_block_height().await.unwrap(); - - let bob_final_xmr_balance = bob_node.monero_wallet.0.get_balance(0).await.unwrap(); - - assert_eq!( - alice_final_btc_balance, - initial_balances.alice_btc + swap_amounts.btc - - bitcoin::Amount::from_sat(bitcoin::TX_FEE) - ); - assert_eq!( - bob_final_btc_balance, - initial_balances.bob_btc - swap_amounts.btc - lock_tx_bitcoin_fee - ); - - assert_eq!( - alice_final_xmr_balance, - initial_balances.alice_xmr.as_piconero() - - swap_amounts.xmr.as_piconero() - - alice_state6.lock_xmr_fee().as_piconero() - ); - assert_eq!( - bob_final_xmr_balance, - initial_balances.bob_xmr.as_piconero() + swap_amounts.xmr.as_piconero() - ); - } } diff --git a/xmr-btc/tests/harness/mod.rs b/xmr-btc/tests/harness/mod.rs index 022e8715..3d189243 100644 --- a/xmr-btc/tests/harness/mod.rs +++ b/xmr-btc/tests/harness/mod.rs @@ -1,5 +1,4 @@ pub mod node; -pub mod storage; pub mod transport; pub mod wallet;