From 33a111d879abd1e3680f7a48f8175e4f96d926fc Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Tue, 3 Nov 2020 16:53:28 +1100 Subject: [PATCH] Move Swap state out of storage --- swap/src/alice.rs | 22 +++++------ swap/src/bob.rs | 17 +++++---- swap/src/lib.rs | 1 + swap/src/state.rs | 88 ++++++++++++++++++++++++++++++++++++++++++ swap/src/storage.rs | 93 +++------------------------------------------ swap/tests/e2e.rs | 3 +- 6 files changed, 114 insertions(+), 110 deletions(-) create mode 100644 swap/src/state.rs diff --git a/swap/src/alice.rs b/swap/src/alice.rs index 0b89d2b4..b419f5a9 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -32,7 +32,8 @@ use crate::{ transport::SwapTransport, TokioExecutor, }, - storage::{self, Database}, + state, + storage::Database, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, }; use xmr_btc::{ @@ -177,7 +178,7 @@ pub async fn swap( }; let swap_id = Uuid::new_v4(); - db.insert_latest_state(swap_id, storage::Alice::Handshaken(state3.clone()).into()) + db.insert_latest_state(swap_id, state::Alice::Handshaken(state3.clone()).into()) .await?; info!("Handshake complete, we now have State3 for Alice."); @@ -205,14 +206,14 @@ pub async fn swap( public_spend_key, public_view_key, }) => { - db.insert_latest_state(swap_id, storage::Alice::BtcLocked(state3.clone()).into()) + db.insert_latest_state(swap_id, state::Alice::BtcLocked(state3.clone()).into()) .await?; let (transfer_proof, _) = monero_wallet .transfer(public_spend_key, public_view_key, amount) .await?; - db.insert_latest_state(swap_id, storage::Alice::XmrLocked(state3.clone()).into()) + db.insert_latest_state(swap_id, state::Alice::XmrLocked(state3.clone()).into()) .await?; let mut guard = network.as_ref().lock().await; @@ -223,7 +224,7 @@ pub async fn swap( GeneratorState::Yielded(Action::RedeemBtc(tx)) => { db.insert_latest_state( swap_id, - storage::Alice::BtcRedeemable { + state::Alice::BtcRedeemable { state: state3.clone(), redeem_tx: tx.clone(), } @@ -237,11 +238,8 @@ pub async fn swap( let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?; } GeneratorState::Yielded(Action::PunishBtc(tx)) => { - db.insert_latest_state( - swap_id, - storage::Alice::BtcPunishable(state3.clone()).into(), - ) - .await?; + db.insert_latest_state(swap_id, state::Alice::BtcPunishable(state3.clone()).into()) + .await?; let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?; } @@ -251,7 +249,7 @@ pub async fn swap( }) => { db.insert_latest_state( swap_id, - storage::Alice::BtcRefunded { + state::Alice::BtcRefunded { state: state3.clone(), spend_key, view_key, @@ -265,7 +263,7 @@ pub async fn swap( .await?; } GeneratorState::Complete(()) => { - db.insert_latest_state(swap_id, storage::Alice::SwapComplete.into()) + db.insert_latest_state(swap_id, state::Alice::SwapComplete.into()) .await?; return Ok(()); diff --git a/swap/src/bob.rs b/swap/src/bob.rs index f9217dec..39a380b5 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -30,7 +30,8 @@ use crate::{ transport::SwapTransport, TokioExecutor, }, - storage::{self, Database}, + state, + storage::Database, Cmd, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, }; use xmr_btc::{ @@ -144,7 +145,7 @@ pub async fn swap( }; let swap_id = Uuid::new_v4(); - db.insert_latest_state(swap_id, storage::Bob::Handshaken(state2.clone()).into()) + db.insert_latest_state(swap_id, state::Bob::Handshaken(state2.clone()).into()) .await?; swarm.send_message2(alice.clone(), state2.next_message()); @@ -172,11 +173,11 @@ pub async fn swap( let _ = bitcoin_wallet .broadcast_signed_transaction(signed_tx_lock) .await?; - db.insert_latest_state(swap_id, storage::Bob::BtcLocked(state2.clone()).into()) + db.insert_latest_state(swap_id, state::Bob::BtcLocked(state2.clone()).into()) .await?; } GeneratorState::Yielded(bob::Action::SendBtcRedeemEncsig(tx_redeem_encsig)) => { - db.insert_latest_state(swap_id, storage::Bob::XmrLocked(state2.clone()).into()) + db.insert_latest_state(swap_id, state::Bob::XmrLocked(state2.clone()).into()) .await?; let mut guard = network.as_ref().lock().await; @@ -196,7 +197,7 @@ pub async fn swap( spend_key, view_key, }) => { - db.insert_latest_state(swap_id, storage::Bob::BtcRedeemed(state2.clone()).into()) + db.insert_latest_state(swap_id, state::Bob::BtcRedeemed(state2.clone()).into()) .await?; monero_wallet @@ -204,7 +205,7 @@ pub async fn swap( .await?; } GeneratorState::Yielded(bob::Action::CancelBtc(tx_cancel)) => { - db.insert_latest_state(swap_id, storage::Bob::BtcRefundable(state2.clone()).into()) + db.insert_latest_state(swap_id, state::Bob::BtcRefundable(state2.clone()).into()) .await?; let _ = bitcoin_wallet @@ -212,7 +213,7 @@ pub async fn swap( .await?; } GeneratorState::Yielded(bob::Action::RefundBtc(tx_refund)) => { - db.insert_latest_state(swap_id, storage::Bob::BtcRefundable(state2.clone()).into()) + db.insert_latest_state(swap_id, state::Bob::BtcRefundable(state2.clone()).into()) .await?; let _ = bitcoin_wallet @@ -220,7 +221,7 @@ pub async fn swap( .await?; } GeneratorState::Complete(()) => { - db.insert_latest_state(swap_id, storage::Bob::SwapComplete.into()) + db.insert_latest_state(swap_id, state::Bob::SwapComplete.into()) .await?; return Ok(()); diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 32743dd3..401184ed 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -6,6 +6,7 @@ pub mod bitcoin; pub mod bob; pub mod monero; pub mod network; +pub mod state; pub mod storage; pub mod tor; diff --git a/swap/src/state.rs b/swap/src/state.rs new file mode 100644 index 00000000..701c61e0 --- /dev/null +++ b/swap/src/state.rs @@ -0,0 +1,88 @@ +use serde::{Deserialize, Serialize}; +use std::fmt::Display; +use xmr_btc::{alice, bob, monero, serde::monero_private_key}; + +#[allow(clippy::large_enum_variant)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub enum Swap { + Alice(Alice), + Bob(Bob), +} + +#[allow(clippy::large_enum_variant)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub enum Alice { + Handshaken(alice::State3), + BtcLocked(alice::State3), + XmrLocked(alice::State3), + BtcRedeemable { + state: alice::State3, + redeem_tx: bitcoin::Transaction, + }, + BtcPunishable(alice::State3), + BtcRefunded { + state: alice::State3, + #[serde(with = "monero_private_key")] + spend_key: monero::PrivateKey, + view_key: monero::PrivateViewKey, + }, + SwapComplete, +} + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub enum Bob { + Handshaken(bob::State2), + BtcLocked(bob::State2), + XmrLocked(bob::State2), + BtcRedeemed(bob::State2), + BtcRefundable(bob::State2), + SwapComplete, +} + +impl From for Swap { + fn from(from: Alice) -> Self { + Swap::Alice(from) + } +} + +impl From for Swap { + fn from(from: Bob) -> Self { + Swap::Bob(from) + } +} + +impl Display for Swap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Swap::Alice(alice) => Display::fmt(alice, f), + Swap::Bob(bob) => Display::fmt(bob, f), + } + } +} + +impl Display for Alice { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Alice::Handshaken(_) => f.write_str("Handshake complete"), + Alice::BtcLocked(_) => f.write_str("Bitcoin locked"), + Alice::XmrLocked(_) => f.write_str("Monero locked"), + Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"), + Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"), + Alice::BtcRefunded { .. } => f.write_str("Monero refundable"), + Alice::SwapComplete => f.write_str("Swap complete"), + } + } +} + +impl Display for Bob { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Bob::Handshaken(_) => f.write_str("Handshake complete"), + Bob::BtcLocked(_) | Bob::XmrLocked(_) | Bob::BtcRefundable(_) => { + f.write_str("Bitcoin refundable") + } + Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"), + Bob::SwapComplete => f.write_str("Swap complete"), + } + } +} diff --git a/swap/src/storage.rs b/swap/src/storage.rs index 8a1fc5ed..c0a11a90 100644 --- a/swap/src/storage.rs +++ b/swap/src/storage.rs @@ -1,93 +1,8 @@ +use crate::state::Swap; use anyhow::{anyhow, bail, Context, Result}; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use std::{fmt::Display, path::Path}; +use serde::{de::DeserializeOwned, Serialize}; +use std::path::Path; use uuid::Uuid; -use xmr_btc::{alice, bob, monero, serde::monero_private_key}; - -#[allow(clippy::large_enum_variant)] -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] -pub enum Swap { - Alice(Alice), - Bob(Bob), -} - -#[allow(clippy::large_enum_variant)] -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] -pub enum Alice { - Handshaken(alice::State3), - BtcLocked(alice::State3), - XmrLocked(alice::State3), - BtcRedeemable { - state: alice::State3, - redeem_tx: bitcoin::Transaction, - }, - BtcPunishable(alice::State3), - BtcRefunded { - state: alice::State3, - #[serde(with = "monero_private_key")] - spend_key: monero::PrivateKey, - view_key: monero::PrivateViewKey, - }, - SwapComplete, -} - -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] -pub enum Bob { - Handshaken(bob::State2), - BtcLocked(bob::State2), - XmrLocked(bob::State2), - BtcRedeemed(bob::State2), - BtcRefundable(bob::State2), - SwapComplete, -} - -impl From for Swap { - fn from(from: Alice) -> Self { - Swap::Alice(from) - } -} - -impl From for Swap { - fn from(from: Bob) -> Self { - Swap::Bob(from) - } -} - -impl Display for Swap { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Swap::Alice(alice) => Display::fmt(alice, f), - Swap::Bob(bob) => Display::fmt(bob, f), - } - } -} - -impl Display for Alice { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Alice::Handshaken(_) => f.write_str("Handshake complete"), - Alice::BtcLocked(_) => f.write_str("Bitcoin locked"), - Alice::XmrLocked(_) => f.write_str("Monero locked"), - Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"), - Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"), - Alice::BtcRefunded { .. } => f.write_str("Monero refundable"), - Alice::SwapComplete => f.write_str("Swap complete"), - } - } -} - -impl Display for Bob { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Bob::Handshaken(_) => f.write_str("Handshake complete"), - Bob::BtcLocked(_) | Bob::XmrLocked(_) | Bob::BtcRefundable(_) => { - f.write_str("Bitcoin refundable") - } - Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"), - Bob::SwapComplete => f.write_str("Swap complete"), - } - } -} pub struct Database(sled::Db); @@ -168,6 +83,8 @@ where #[cfg(test)] mod tests { + use crate::state::{Alice, Bob}; + use super::*; #[tokio::test] diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs index 1f293d3c..df72ce36 100644 --- a/swap/tests/e2e.rs +++ b/swap/tests/e2e.rs @@ -68,8 +68,7 @@ mod e2e_test { let alice_transport = build(alice_behaviour.identity()).unwrap(); let db_dir = tempdir().unwrap(); - let db = Database::open(db_dir.path()).unwrap(); - + let db = Database::open(std::path::Path::new("/home/luckysori/test/xmr_btc_swap")).unwrap(); let alice_swap = alice::swap( alice_btc_wallet.clone(), alice_xmr_wallet.clone(),