mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-29 01:16:24 -05:00
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:
parent
59708c57e6
commit
e1e8533862
@ -11,7 +11,7 @@ use crate::{
|
||||
},
|
||||
},
|
||||
bitcoin::EncryptedSignature,
|
||||
database::{state::Swap, Database},
|
||||
database::{Database, Swap},
|
||||
network::request_response::AliceToBob,
|
||||
SwapAmounts,
|
||||
};
|
||||
|
@ -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,
|
||||
|
@ -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<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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user