Replace Try_From state conversion with From for Bob

This commit is contained in:
Franck Royer 2020-12-23 12:14:36 +11:00
parent 5d1b10cc58
commit 4150e75488
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 32 additions and 39 deletions

View File

@ -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,

View File

@ -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<BobState> for state::Bob {
}
}
impl TryFrom<state::Swap> for BobState {
type Error = anyhow::Error;
fn try_from(db_state: state::Swap) -> Result<Self, Self::Error> {
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<state::Bob> 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,
},
}
}
}

View File

@ -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,