diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index 8ec72734..deb18d18 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -16,7 +16,7 @@ use anyhow::{bail, Context, Result}; use libp2p::{core::Multiaddr, PeerId}; use prettytable::{row, Table}; use rand::rngs::OsRng; -use std::{convert::TryFrom, sync::Arc}; +use std::sync::Arc; use structopt::StructOpt; use swap::{ alice, @@ -213,9 +213,11 @@ async fn main() -> Result<()> { alice_peer_id, alice_addr, }) => { - let db_swap = db.get_state(swap_id)?; - - let bob_state = BobState::try_from(db_swap)?; + let db_state = if let Swap::Bob(db_state) = db.get_state(swap_id)? { + db_state + } else { + bail!("Swap {} is not buy xmr.", swap_id) + }; let (bitcoin_wallet, monero_wallet) = setup_wallets( bitcoind_url, @@ -226,7 +228,7 @@ async fn main() -> Result<()> { .await?; bob_swap( swap_id, - bob_state, + db_state.into(), bitcoin_wallet, monero_wallet, db, diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index 6dd21e3f..d3b6ec05 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -1,14 +1,14 @@ use crate::{ bob::event_loop::EventLoopHandle, state, - state::{Bob, BobEndState, Swap}, + state::{Bob, BobEndState}, storage::Database, SwapAmounts, }; use anyhow::{bail, Result}; use async_recursion::async_recursion; use rand::{CryptoRng, RngCore}; -use std::{convert::TryFrom, fmt, sync::Arc}; +use std::{fmt, sync::Arc}; use tokio::select; use tracing::info; use uuid::Uuid; @@ -74,31 +74,23 @@ impl From for state::Bob { } } -impl TryFrom for BobState { - type Error = anyhow::Error; - - fn try_from(db_state: state::Swap) -> Result { - if let Swap::Bob(state) = db_state { - let bob_State = match state { - Bob::Started { state0, amounts } => BobState::Started { state0, amounts }, - Bob::Negotiated { state2 } => BobState::Negotiated(state2), - Bob::BtcLocked { state3 } => BobState::BtcLocked(state3), - Bob::XmrLocked { state4 } => BobState::XmrLocked(state4), - Bob::EncSigSent { state4 } => BobState::EncSigSent(state4), - Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), - Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4), - Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4), - Bob::Done(end_state) => match end_state { - BobEndState::SafelyAborted => BobState::SafelyAborted, - BobEndState::XmrRedeemed => BobState::XmrRedeemed, - BobEndState::BtcRefunded(state4) => BobState::BtcRefunded(*state4), - BobEndState::BtcPunished => BobState::BtcPunished, - }, - }; - - Ok(bob_State) - } else { - bail!("Bob swap state expected.") +impl From for BobState { + fn from(db_state: state::Bob) -> Self { + match db_state { + Bob::Started { state0, amounts } => BobState::Started { state0, amounts }, + Bob::Negotiated { state2 } => BobState::Negotiated(state2), + Bob::BtcLocked { state3 } => BobState::BtcLocked(state3), + Bob::XmrLocked { state4 } => BobState::XmrLocked(state4), + Bob::EncSigSent { state4 } => BobState::EncSigSent(state4), + Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), + Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4), + Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4), + Bob::Done(end_state) => match end_state { + BobEndState::SafelyAborted => BobState::SafelyAborted, + BobEndState::XmrRedeemed => BobState::XmrRedeemed, + BobEndState::BtcRefunded(state4) => BobState::BtcRefunded(*state4), + BobEndState::BtcPunished => BobState::BtcPunished, + }, } } } diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 173a2c4e..a3ec875d 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -2,7 +2,6 @@ use crate::testutils::{init_alice, init_bob}; use get_port::get_port; use libp2p::Multiaddr; use rand::rngs::OsRng; -use std::convert::TryFrom; use swap::{alice, bitcoin, bob, bob::swap::BobState, storage::Database}; use tempfile::tempdir; use testcontainers::clients::Cli; @@ -114,19 +113,19 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { assert!(matches!(bob_state, BobState::EncSigSent {..})); let bob_db = Database::open(bob_db_datadir.path()).unwrap(); - let state_before_restart = bob_db.get_state(bob_swap_id).unwrap(); - if let swap::state::Swap::Bob(state) = state_before_restart.clone() { + 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 (event_loop_after_restart, event_loop_handle_after_restart) = testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr); tokio::spawn(event_loop_after_restart.run()); - let db_swap = bob_db.get_state(bob_swap_id).unwrap(); - let resume_state = BobState::try_from(db_swap).unwrap(); - let bob_state = bob::swap::swap( resume_state, event_loop_handle_after_restart,