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 libp2p::{core::Multiaddr, PeerId};
use prettytable::{row, Table}; use prettytable::{row, Table};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::{convert::TryFrom, sync::Arc}; use std::sync::Arc;
use structopt::StructOpt; use structopt::StructOpt;
use swap::{ use swap::{
alice, alice,
@ -213,9 +213,11 @@ async fn main() -> Result<()> {
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
}) => { }) => {
let db_swap = db.get_state(swap_id)?; let db_state = if let Swap::Bob(db_state) = db.get_state(swap_id)? {
db_state
let bob_state = BobState::try_from(db_swap)?; } else {
bail!("Swap {} is not buy xmr.", swap_id)
};
let (bitcoin_wallet, monero_wallet) = setup_wallets( let (bitcoin_wallet, monero_wallet) = setup_wallets(
bitcoind_url, bitcoind_url,
@ -226,7 +228,7 @@ async fn main() -> Result<()> {
.await?; .await?;
bob_swap( bob_swap(
swap_id, swap_id,
bob_state, db_state.into(),
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
db, db,

View File

@ -1,14 +1,14 @@
use crate::{ use crate::{
bob::event_loop::EventLoopHandle, bob::event_loop::EventLoopHandle,
state, state,
state::{Bob, BobEndState, Swap}, state::{Bob, BobEndState},
storage::Database, storage::Database,
SwapAmounts, 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};
use std::{convert::TryFrom, fmt, sync::Arc}; use std::{fmt, sync::Arc};
use tokio::select; use tokio::select;
use tracing::info; use tracing::info;
use uuid::Uuid; use uuid::Uuid;
@ -74,31 +74,23 @@ impl From<BobState> for state::Bob {
} }
} }
impl TryFrom<state::Swap> for BobState { impl From<state::Bob> for BobState {
type Error = anyhow::Error; fn from(db_state: state::Bob) -> Self {
match db_state {
fn try_from(db_state: state::Swap) -> Result<Self, Self::Error> { Bob::Started { state0, amounts } => BobState::Started { state0, amounts },
if let Swap::Bob(state) = db_state { Bob::Negotiated { state2 } => BobState::Negotiated(state2),
let bob_State = match state { Bob::BtcLocked { state3 } => BobState::BtcLocked(state3),
Bob::Started { state0, amounts } => BobState::Started { state0, amounts }, Bob::XmrLocked { state4 } => BobState::XmrLocked(state4),
Bob::Negotiated { state2 } => BobState::Negotiated(state2), Bob::EncSigSent { state4 } => BobState::EncSigSent(state4),
Bob::BtcLocked { state3 } => BobState::BtcLocked(state3), Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
Bob::XmrLocked { state4 } => BobState::XmrLocked(state4), Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4),
Bob::EncSigSent { state4 } => BobState::EncSigSent(state4), Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4),
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), Bob::Done(end_state) => match end_state {
Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4), BobEndState::SafelyAborted => BobState::SafelyAborted,
Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4), BobEndState::XmrRedeemed => BobState::XmrRedeemed,
Bob::Done(end_state) => match end_state { BobEndState::BtcRefunded(state4) => BobState::BtcRefunded(*state4),
BobEndState::SafelyAborted => BobState::SafelyAborted, BobEndState::BtcPunished => BobState::BtcPunished,
BobEndState::XmrRedeemed => BobState::XmrRedeemed, },
BobEndState::BtcRefunded(state4) => BobState::BtcRefunded(*state4),
BobEndState::BtcPunished => BobState::BtcPunished,
},
};
Ok(bob_State)
} else {
bail!("Bob swap state expected.")
} }
} }
} }

View File

@ -2,7 +2,6 @@ use crate::testutils::{init_alice, init_bob};
use get_port::get_port; use get_port::get_port;
use libp2p::Multiaddr; use libp2p::Multiaddr;
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::convert::TryFrom;
use swap::{alice, bitcoin, bob, bob::swap::BobState, storage::Database}; use swap::{alice, bitcoin, bob, bob::swap::BobState, storage::Database};
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::clients::Cli; 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 {..})); assert!(matches!(bob_state, BobState::EncSigSent {..}));
let bob_db = Database::open(bob_db_datadir.path()).unwrap(); 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 {..})); assert!(matches!(state, swap::state::Bob::EncSigSent {..}));
} state.into()
} else {
unreachable!()
};
let (event_loop_after_restart, event_loop_handle_after_restart) = let (event_loop_after_restart, event_loop_handle_after_restart) =
testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr); testutils::init_bob_event_loop(alice_peer_id, alice_multiaddr);
tokio::spawn(event_loop_after_restart.run()); 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( let bob_state = bob::swap::swap(
resume_state, resume_state,
event_loop_handle_after_restart, event_loop_handle_after_restart,