Embed state mod in database mod

The `Swap` struct is now database specific, no need to have a 20 lines
 module.
This commit is contained in:
Franck Royer 2020-12-23 15:30:23 +11:00
parent 59708c57e6
commit e1e8533862
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
7 changed files with 48 additions and 58 deletions

View File

@ -11,7 +11,7 @@ use crate::{
}, },
}, },
bitcoin::EncryptedSignature, bitcoin::EncryptedSignature,
database::{state::Swap, Database}, database::{Database, Swap},
network::request_response::AliceToBob, network::request_response::AliceToBob,
SwapAmounts, SwapAmounts,
}; };

View File

@ -1,8 +1,4 @@
use crate::{ use crate::{bob::event_loop::EventLoopHandle, database, database::Database, SwapAmounts};
bob::event_loop::EventLoopHandle,
database::{state, Database},
SwapAmounts,
};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
@ -137,7 +133,7 @@ where
let state = BobState::Negotiated(state2); let state = BobState::Negotiated(state2);
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,
@ -159,7 +155,7 @@ where
let state = BobState::BtcLocked(state3); let state = BobState::BtcLocked(state3);
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,
@ -213,7 +209,7 @@ where
BobState::CancelTimelockExpired(state4) BobState::CancelTimelockExpired(state4)
}; };
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,
@ -255,7 +251,7 @@ where
BobState::CancelTimelockExpired(state) BobState::CancelTimelockExpired(state)
}; };
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,
@ -291,7 +287,7 @@ where
}; };
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,
@ -311,7 +307,7 @@ where
let state = BobState::XmrRedeemed; let state = BobState::XmrRedeemed;
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,
@ -335,7 +331,7 @@ where
} }
let state = BobState::BtcCancelled(state4); 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?; .await?;
run_until( run_until(
@ -364,7 +360,7 @@ where
}; };
let db_state = state.clone().into(); 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?; .await?;
run_until( run_until(
state, state,

View File

@ -1,12 +1,10 @@
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use state::Swap; use std::{fmt::Display, path::Path};
use std::path::Path;
use uuid::Uuid; use uuid::Uuid;
mod alice; mod alice;
mod bob; mod bob;
pub mod state;
pub use alice::*; pub use alice::*;
pub use bob::*; pub use bob::*;
@ -87,6 +85,33 @@ where
Ok(serde_cbor::from_slice(&v)?) Ok(serde_cbor::from_slice(&v)?)
} }
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Swap {
Alice(Alice),
Bob(Bob),
}
impl From<Alice> for Swap {
fn from(from: Alice) -> Self {
Swap::Alice(from)
}
}
impl From<Bob> 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -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<Alice> for Swap {
fn from(from: Alice) -> Self {
Swap::Alice(from)
}
}
impl From<Bob> 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),
}
}
}

View File

@ -25,7 +25,7 @@ use swap::{
bitcoin, bob, bitcoin, bob,
bob::swap::BobState, bob::swap::BobState,
cli::{Command, Options, Resume}, cli::{Command, Options, Resume},
database::{state::Swap, Database}, database::{Database, Swap},
monero, monero,
network::transport::build, network::transport::build,
trace::init_tracing, trace::init_tracing,

View File

@ -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 alice_db = Database::open(alice_db_datadir.path()).unwrap();
let resume_state = if let swap::database::state::Swap::Alice(state) = let resume_state =
alice_db.get_state(alice_swap_id).unwrap() if let swap::database::Swap::Alice(state) = alice_db.get_state(alice_swap_id).unwrap() {
{ assert!(matches!(state, swap::database::Alice::EncSigLearned {..}));
assert!(matches!(state, swap::database::Alice::EncSigLearned {..})); state.into()
state.into() } else {
} else { unreachable!()
unreachable!() };
};
let (mut event_loop_after_restart, event_loop_handle_after_restart) = let (mut event_loop_after_restart, event_loop_handle_after_restart) =
testutils::init_alice_event_loop(alice_multiaddr); testutils::init_alice_event_loop(alice_multiaddr);

View File

@ -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 bob_db = Database::open(bob_db_datadir.path()).unwrap();
let resume_state = 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 {..})); assert!(matches!(state, swap::database::Bob::EncSigSent {..}));
state.into() state.into()
} else { } else {