From e1e85338627e99ef368e6f3851f43a8c17503095 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 23 Dec 2020 15:30:23 +1100 Subject: [PATCH] Embed state mod in database mod The `Swap` struct is now database specific, no need to have a 20 lines module. --- swap/src/alice/swap.rs | 2 +- swap/src/bob/swap.rs | 22 +++++-------- swap/src/database.rs | 33 ++++++++++++++++--- swap/src/database/state.rs | 30 ----------------- swap/src/main.rs | 2 +- swap/tests/happy_path_restart_alice.rs | 15 ++++----- .../happy_path_restart_bob_after_comm.rs | 2 +- 7 files changed, 48 insertions(+), 58 deletions(-) delete mode 100644 swap/src/database/state.rs diff --git a/swap/src/alice/swap.rs b/swap/src/alice/swap.rs index eb454f0e..d77f2187 100644 --- a/swap/src/alice/swap.rs +++ b/swap/src/alice/swap.rs @@ -11,7 +11,7 @@ use crate::{ }, }, bitcoin::EncryptedSignature, - database::{state::Swap, Database}, + database::{Database, Swap}, network::request_response::AliceToBob, SwapAmounts, }; diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index b174cb54..223f29c1 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -1,8 +1,4 @@ -use crate::{ - bob::event_loop::EventLoopHandle, - database::{state, Database}, - SwapAmounts, -}; +use crate::{bob::event_loop::EventLoopHandle, database, database::Database, SwapAmounts}; use anyhow::{bail, Result}; use async_recursion::async_recursion; use rand::{CryptoRng, RngCore}; @@ -137,7 +133,7 @@ where let state = BobState::Negotiated(state2); let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, @@ -159,7 +155,7 @@ where let state = BobState::BtcLocked(state3); let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, @@ -213,7 +209,7 @@ where BobState::CancelTimelockExpired(state4) }; let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, @@ -255,7 +251,7 @@ where BobState::CancelTimelockExpired(state) }; let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, @@ -291,7 +287,7 @@ where }; let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, @@ -311,7 +307,7 @@ where let state = BobState::XmrRedeemed; let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, @@ -335,7 +331,7 @@ where } let state = BobState::BtcCancelled(state4); - db.insert_latest_state(swap_id, state::Swap::Bob(state.clone().into())) + db.insert_latest_state(swap_id, database::Swap::Bob(state.clone().into())) .await?; run_until( @@ -364,7 +360,7 @@ where }; let db_state = state.clone().into(); - db.insert_latest_state(swap_id, state::Swap::Bob(db_state)) + db.insert_latest_state(swap_id, database::Swap::Bob(db_state)) .await?; run_until( state, diff --git a/swap/src/database.rs b/swap/src/database.rs index 6c7a0459..49d61930 100644 --- a/swap/src/database.rs +++ b/swap/src/database.rs @@ -1,12 +1,10 @@ use anyhow::{anyhow, bail, Context, Result}; -use serde::{de::DeserializeOwned, Serialize}; -use state::Swap; -use std::path::Path; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use std::{fmt::Display, path::Path}; use uuid::Uuid; mod alice; mod bob; -pub mod state; pub use alice::*; pub use bob::*; @@ -87,6 +85,33 @@ where Ok(serde_cbor::from_slice(&v)?) } +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +pub enum Swap { + Alice(Alice), + Bob(Bob), +} + +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), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/swap/src/database/state.rs b/swap/src/database/state.rs deleted file mode 100644 index 63e13ce8..00000000 --- a/swap/src/database/state.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::database::{Alice, Bob}; -use serde::{Deserialize, Serialize}; -use std::fmt::Display; - -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] -pub enum Swap { - Alice(Alice), - Bob(Bob), -} - -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), - } - } -} diff --git a/swap/src/main.rs b/swap/src/main.rs index 3534a954..419b3320 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -25,7 +25,7 @@ use swap::{ bitcoin, bob, bob::swap::BobState, cli::{Command, Options, Resume}, - database::{state::Swap, Database}, + database::{Database, Swap}, monero, network::transport::build, trace::init_tracing, diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index b35d33c3..4ab738d3 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -111,14 +111,13 @@ 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::state::Swap::Alice(state) = - alice_db.get_state(alice_swap_id).unwrap() - { - assert!(matches!(state, swap::database::Alice::EncSigLearned {..})); - state.into() - } else { - unreachable!() - }; + 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 {..})); + state.into() + } else { + unreachable!() + }; let (mut event_loop_after_restart, event_loop_handle_after_restart) = testutils::init_alice_event_loop(alice_multiaddr); diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 31d60016..fb7310c8 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -115,7 +115,7 @@ 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::state::Swap::Bob(state) = bob_db.get_state(bob_swap_id).unwrap() { + 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 {