Move storage into application crate

This commit is contained in:
rishflab 2020-10-23 13:13:17 +11:00 committed by rishflab
parent 11a4a7563b
commit c781ee949d
7 changed files with 17 additions and 125 deletions

View File

@ -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 }

View File

@ -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;

View File

@ -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<T>
@ -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(),

View File

@ -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"

View File

@ -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::{

View File

@ -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<alice::State> =
harness::storage::Database::open(alice_db_dir.path()).unwrap();
let bob_db_dir = tempfile::tempdir().unwrap();
let bob_db: Database<bob::State> =
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()
);
}
}

View File

@ -1,5 +1,4 @@
pub mod node;
pub mod storage;
pub mod transport;
pub mod wallet;