From b410de01df6469852532b107f63ff830d4ccc862 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 23 Dec 2020 15:20:24 +1100 Subject: [PATCH] Move state.rs under database module The types in state are only used for the database --- swap/src/alice/swap.rs | 3 +-- swap/src/bob/swap.rs | 6 +++++- swap/src/database.rs | 6 ++++-- swap/src/{ => database}/state.rs | 0 swap/src/lib.rs | 1 - swap/src/main.rs | 3 +-- swap/tests/happy_path_restart_alice.rs | 15 ++++++++------- swap/tests/happy_path_restart_bob_after_comm.rs | 14 +++++++------- 8 files changed, 26 insertions(+), 22 deletions(-) rename swap/src/{ => database}/state.rs (100%) diff --git a/swap/src/alice/swap.rs b/swap/src/alice/swap.rs index 3c220bf8..eb454f0e 100644 --- a/swap/src/alice/swap.rs +++ b/swap/src/alice/swap.rs @@ -11,9 +11,8 @@ use crate::{ }, }, bitcoin::EncryptedSignature, - database::Database, + database::{state::Swap, Database}, network::request_response::AliceToBob, - state::Swap, SwapAmounts, }; use anyhow::Result; diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index 7f004cea..b174cb54 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -1,4 +1,8 @@ -use crate::{bob::event_loop::EventLoopHandle, database::Database, state, SwapAmounts}; +use crate::{ + bob::event_loop::EventLoopHandle, + database::{state, Database}, + SwapAmounts, +}; use anyhow::{bail, Result}; use async_recursion::async_recursion; use rand::{CryptoRng, RngCore}; diff --git a/swap/src/database.rs b/swap/src/database.rs index d7cf3466..3034eef1 100644 --- a/swap/src/database.rs +++ b/swap/src/database.rs @@ -1,9 +1,11 @@ -use crate::state::Swap; use anyhow::{anyhow, bail, Context, Result}; use serde::{de::DeserializeOwned, Serialize}; +use state::Swap; use std::path::Path; use uuid::Uuid; +pub mod state; + #[derive(Debug)] pub struct Database(sled::Db); @@ -82,7 +84,7 @@ where #[cfg(test)] mod tests { - use crate::state::{Alice, AliceEndState, Bob, BobEndState}; + use crate::database::state::{Alice, AliceEndState, Bob, BobEndState}; use super::*; diff --git a/swap/src/state.rs b/swap/src/database/state.rs similarity index 100% rename from swap/src/state.rs rename to swap/src/database/state.rs diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 7ee2a4be..04a66898 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -23,7 +23,6 @@ pub mod cli; pub mod database; pub mod monero; pub mod network; -pub mod state; pub mod trace; pub type Never = std::convert::Infallible; diff --git a/swap/src/main.rs b/swap/src/main.rs index 5230ec1a..3534a954 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -25,10 +25,9 @@ use swap::{ bitcoin, bob, bob::swap::BobState, cli::{Command, Options, Resume}, - database::Database, + database::{state::Swap, Database}, monero, network::transport::build, - state::Swap, trace::init_tracing, SwapAmounts, }; diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index d1601e9b..9fa554ea 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -111,13 +111,14 @@ 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::state::Swap::Alice(state) = alice_db.get_state(alice_swap_id).unwrap() { - assert!(matches!(state, swap::state::Alice::EncSigLearned {..})); - state.into() - } else { - unreachable!() - }; + let resume_state = if let swap::database::state::Swap::Alice(state) = + alice_db.get_state(alice_swap_id).unwrap() + { + assert!(matches!(state, swap::database::state::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 eae3f9a0..1086bb97 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -114,13 +114,13 @@ 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::state::Swap::Bob(state) = bob_db.get_state(bob_swap_id).unwrap() - { - assert!(matches!(state, swap::state::Bob::EncSigSent {..})); - state.into() - } else { - unreachable!() - }; + let resume_state = + if let swap::database::state::Swap::Bob(state) = bob_db.get_state(bob_swap_id).unwrap() { + assert!(matches!(state, swap::database::state::Bob::EncSigSent {..})); + state.into() + } else { + unreachable!() + }; let (event_loop_after_restart, event_loop_handle_after_restart) = testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr);