mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-10-21 18:18:04 -04:00
Move storage into application crate
This commit is contained in:
parent
11a4a7563b
commit
c781ee949d
7 changed files with 17 additions and 125 deletions
|
@ -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"
|
||||
|
|
|
@ -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::{
|
||||
|
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
pub mod node;
|
||||
pub mod storage;
|
||||
pub mod transport;
|
||||
pub mod wallet;
|
||||
|
||||
|
|
|
@ -1,160 +0,0 @@
|
|||
use anyhow::{anyhow, Context, Result};
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
|
||||
pub struct Database<T>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
db: sled::Db,
|
||||
_marker: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> Database<T>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
// TODO: serialize using lazy/one-time initlisation
|
||||
const LAST_STATE_KEY: &'static str = "latest_state";
|
||||
|
||||
pub fn open(path: &Path) -> Result<Self> {
|
||||
let db =
|
||||
sled::open(path).with_context(|| format!("Could not open the DB at {:?}", path))?;
|
||||
|
||||
Ok(Database {
|
||||
db,
|
||||
_marker: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn insert_latest_state(&self, state: &T) -> Result<()> {
|
||||
let key = serialize(&Self::LAST_STATE_KEY)?;
|
||||
let new_value = serialize(&state).context("Could not serialize new state value")?;
|
||||
|
||||
let old_value = self.db.get(&key)?;
|
||||
|
||||
self.db
|
||||
.compare_and_swap(key, old_value, Some(new_value))
|
||||
.context("Could not write in the DB")?
|
||||
.context("Stored swap somehow changed, aborting saving")?;
|
||||
|
||||
// TODO: see if this can be done through sled config
|
||||
self.db
|
||||
.flush_async()
|
||||
.await
|
||||
.map(|_| ())
|
||||
.context("Could not flush db")
|
||||
}
|
||||
|
||||
pub fn get_latest_state(&self) -> anyhow::Result<T> {
|
||||
let key = serialize(&Self::LAST_STATE_KEY)?;
|
||||
|
||||
let encoded = self
|
||||
.db
|
||||
.get(&key)?
|
||||
.ok_or_else(|| anyhow!("State does not exist {:?}", key))?;
|
||||
|
||||
let state = deserialize(&encoded).context("Could not deserialize state")?;
|
||||
Ok(state)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize<T>(t: &T) -> anyhow::Result<Vec<u8>>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
Ok(serde_cbor::to_vec(t)?)
|
||||
}
|
||||
|
||||
pub fn deserialize<T>(v: &[u8]) -> anyhow::Result<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
Ok(serde_cbor::from_slice(&v)?)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(non_snake_case)]
|
||||
use super::*;
|
||||
use bitcoin::SigHash;
|
||||
use curve25519_dalek::scalar::Scalar;
|
||||
use ecdsa_fun::fun::rand_core::OsRng;
|
||||
use std::str::FromStr;
|
||||
use xmr_btc::serde::{bitcoin_amount, monero_private_key};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
pub struct TestState {
|
||||
A: xmr_btc::bitcoin::PublicKey,
|
||||
a: xmr_btc::bitcoin::SecretKey,
|
||||
s_a: ::cross_curve_dleq::Scalar,
|
||||
#[serde(with = "monero_private_key")]
|
||||
s_b: monero::PrivateKey,
|
||||
S_a_monero: ::monero::PublicKey,
|
||||
S_a_bitcoin: xmr_btc::bitcoin::PublicKey,
|
||||
v: xmr_btc::monero::PrivateViewKey,
|
||||
#[serde(with = "bitcoin_amount")]
|
||||
btc: ::bitcoin::Amount,
|
||||
xmr: xmr_btc::monero::Amount,
|
||||
refund_timelock: u32,
|
||||
refund_address: ::bitcoin::Address,
|
||||
transaction: ::bitcoin::Transaction,
|
||||
tx_punish_sig: xmr_btc::bitcoin::Signature,
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recover_state_from_db() {
|
||||
let db_dir = tempfile::tempdir().unwrap();
|
||||
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 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(),
|
||||
});
|
||||
let S_a_bitcoin = s_a.into_secp256k1().into();
|
||||
let tx_punish_sig = a.sign(SigHash::default());
|
||||
|
||||
let state = TestState {
|
||||
A: a.public(),
|
||||
a,
|
||||
s_b,
|
||||
s_a,
|
||||
S_a_monero,
|
||||
S_a_bitcoin,
|
||||
v: v_a,
|
||||
btc: ::bitcoin::Amount::from_sat(100),
|
||||
xmr: xmr_btc::monero::Amount::from_piconero(1000),
|
||||
refund_timelock: 0,
|
||||
refund_address: ::bitcoin::Address::from_str("1L5wSMgerhHg8GZGcsNmAx5EXMRXSKR3He")
|
||||
.unwrap(),
|
||||
transaction: ::bitcoin::Transaction {
|
||||
version: 0,
|
||||
lock_time: 0,
|
||||
input: vec![::bitcoin::TxIn::default()],
|
||||
output: vec![::bitcoin::TxOut::default()],
|
||||
},
|
||||
tx_punish_sig,
|
||||
};
|
||||
|
||||
db.insert_latest_state(&state)
|
||||
.await
|
||||
.expect("Failed to save state the first time");
|
||||
let recovered: TestState = db
|
||||
.get_latest_state()
|
||||
.expect("Failed to recover state the first time");
|
||||
|
||||
// We insert and recover twice to ensure database implementation allows the
|
||||
// caller to write to an existing key
|
||||
db.insert_latest_state(&recovered)
|
||||
.await
|
||||
.expect("Failed to save state the second time");
|
||||
let recovered: TestState = db
|
||||
.get_latest_state()
|
||||
.expect("Failed to recover state the second time");
|
||||
|
||||
assert_eq!(state, recovered);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue