Bob's recover function

This commit is contained in:
Daniel Karzel 2020-12-14 21:15:52 +11:00 committed by Franck Royer
parent bf39c34ada
commit 3692046758
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -1,15 +1,15 @@
use crate::{ use crate::{
bob::{event_loop::EventLoopHandle, negotiate::negotiate}, bob::{event_loop::EventLoopHandle, negotiate::negotiate},
state, state,
state::Bob, state::{Bob, Swap},
storage::Database, storage::Database,
SwapAmounts, SwapAmounts,
}; };
use anyhow::Result; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use std::{fmt, sync::Arc}; use std::{convert::TryFrom, fmt, sync::Arc};
use tracing::info; use tracing::info;
use uuid::Uuid; use uuid::Uuid;
use xmr_btc::{ use xmr_btc::{
@ -77,16 +77,24 @@ impl From<BobState> for state::Bob {
} }
} }
impl From<state::Bob> for BobState { impl TryFrom<state::Swap> for BobState {
fn from(bob: Bob) -> Self { type Error = anyhow::Error;
match bob {
Bob::Negotiated { state2, peer_id } => BobState::Negotiated(state2, peer_id), fn try_from(db_state: state::Swap) -> Result<Self, Self::Error> {
Bob::BtcLocked { state3, peer_id } => BobState::BtcLocked(state3, peer_id), if let Swap::Bob(state) = db_state {
Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id), let bob_State = match state {
Bob::EncSigSent { state4, peer_id } => BobState::EncSigSent(state4, peer_id), Bob::Negotiated { state2, peer_id } => BobState::Negotiated(state2, peer_id),
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), Bob::BtcLocked { state3, peer_id } => BobState::BtcLocked(state3, peer_id),
Bob::BtcCancelled(state4) => BobState::Cancelled(state4), Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id),
Bob::SwapComplete => BobState::SafelyAborted, Bob::EncSigSent { state4, peer_id } => BobState::EncSigSent(state4, peer_id),
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
Bob::BtcCancelled(state4) => BobState::Cancelled(state4),
Bob::SwapComplete => BobState::SafelyAborted,
};
Ok(bob_State)
} else {
bail!("Bob swap state expected.")
} }
} }
} }
@ -116,6 +124,32 @@ where
.await .await
} }
pub async fn recover<R>(
event_loop_handle: EventLoopHandle,
db: Database,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>,
rng: R,
swap_id: Uuid,
) -> Result<BobState>
where
R: RngCore + CryptoRng + Send,
{
let db_swap = db.get_state(swap_id)?;
let start_state = BobState::try_from(db_swap)?;
let state = swap(
start_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await?;
Ok(state)
}
pub fn is_complete(state: &BobState) -> bool { pub fn is_complete(state: &BobState) -> bool {
matches!( matches!(
state, state,