Move db state conversion with db type definition

Those conversion are specific to the database and should be defined with
in the database module.
This commit is contained in:
Franck Royer 2020-12-23 15:11:09 +11:00
parent 2a778f5644
commit 5ed18469e4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 160 additions and 164 deletions

View File

@ -10,11 +10,9 @@ use crate::{
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin,
},
},
bitcoin,
bitcoin::EncryptedSignature,
network::request_response::AliceToBob,
state,
state::{Alice, AliceEndState, Swap},
state::Swap,
storage::Database,
SwapAmounts,
};
@ -105,118 +103,6 @@ impl fmt::Display for AliceState {
}
}
impl From<&AliceState> for state::Alice {
fn from(alice_state: &AliceState) -> Self {
match alice_state {
AliceState::Negotiated { state3, .. } => Alice::Negotiated(state3.as_ref().clone()),
AliceState::BtcLocked { state3, .. } => Alice::BtcLocked(state3.as_ref().clone()),
AliceState::XmrLocked { state3 } => Alice::XmrLocked(state3.as_ref().clone()),
AliceState::EncSigLearned {
state3,
encrypted_signature,
} => Alice::EncSigLearned {
state3: state3.as_ref().clone(),
encrypted_signature: encrypted_signature.clone(),
},
AliceState::BtcRedeemed => Alice::Done(AliceEndState::BtcRedeemed),
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.as_ref().clone()),
AliceState::BtcRefunded { spend_key, state3 } => Alice::BtcRefunded {
spend_key: *spend_key,
state3: state3.as_ref().clone(),
},
AliceState::BtcPunishable { state3, .. } => {
Alice::BtcPunishable(state3.as_ref().clone())
}
AliceState::XmrRefunded => Alice::Done(AliceEndState::XmrRefunded),
AliceState::CancelTimelockExpired { state3 } => {
Alice::CancelTimelockExpired(state3.as_ref().clone())
}
AliceState::BtcPunished => Alice::Done(AliceEndState::BtcPunished),
AliceState::SafelyAborted => Alice::Done(AliceEndState::SafelyAborted),
AliceState::Started { amounts, state0 } => Alice::Started {
amounts: *amounts,
state0: state0.clone(),
},
}
}
}
impl From<state::Alice> for AliceState {
fn from(db_state: state::Alice) -> Self {
use AliceState::*;
match db_state {
Alice::Started { amounts, state0 } => Started { amounts, state0 },
Alice::Negotiated(state3) => Negotiated {
channel: None,
amounts: SwapAmounts {
btc: state3.btc,
xmr: state3.xmr,
},
state3: Box::new(state3),
},
Alice::BtcLocked(state3) => BtcLocked {
channel: None,
amounts: SwapAmounts {
btc: state3.btc,
xmr: state3.xmr,
},
state3: Box::new(state3),
},
Alice::XmrLocked(state3) => XmrLocked {
state3: Box::new(state3),
},
Alice::EncSigLearned {
state3: state,
encrypted_signature,
} => EncSigLearned {
state3: Box::new(state),
encrypted_signature,
},
Alice::CancelTimelockExpired(state3) => AliceState::CancelTimelockExpired {
state3: Box::new(state3),
},
Alice::BtcCancelled(state) => {
let tx_cancel = bitcoin::TxCancel::new(
&state.tx_lock,
state.cancel_timelock,
state.a.public(),
state.B,
);
BtcCancelled {
state3: Box::new(state),
tx_cancel,
}
}
Alice::BtcPunishable(state3) => {
let tx_cancel = bitcoin::TxCancel::new(
&state3.tx_lock,
state3.cancel_timelock,
state3.a.public(),
state3.B,
);
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &state3.refund_address);
BtcPunishable {
tx_refund,
state3: Box::new(state3),
}
}
Alice::BtcRefunded {
state3, spend_key, ..
} => BtcRefunded {
spend_key,
state3: Box::new(state3),
},
Alice::Done(end_state) => match end_state {
AliceEndState::SafelyAborted => SafelyAborted,
AliceEndState::BtcRedeemed => BtcRedeemed,
AliceEndState::XmrRefunded => XmrRefunded,
AliceEndState::BtcPunished => BtcPunished,
},
}
}
}
pub async fn swap(
state: AliceState,
event_loop_handle: EventLoopHandle,

View File

@ -1,10 +1,4 @@
use crate::{
bob::event_loop::EventLoopHandle,
state,
state::{Bob, BobEndState},
storage::Database,
SwapAmounts,
};
use crate::{bob::event_loop::EventLoopHandle, state, storage::Database, SwapAmounts};
use anyhow::{bail, Result};
use async_recursion::async_recursion;
use rand::{CryptoRng, RngCore};
@ -55,46 +49,6 @@ impl fmt::Display for BobState {
}
}
impl From<BobState> for state::Bob {
fn from(bob_state: BobState) -> Self {
match bob_state {
BobState::Started { state0, amounts } => Bob::Started { state0, amounts },
BobState::Negotiated(state2) => Bob::Negotiated { state2 },
BobState::BtcLocked(state3) => Bob::BtcLocked { state3 },
BobState::XmrLocked(state4) => Bob::XmrLocked { state4 },
BobState::EncSigSent(state4) => Bob::EncSigSent { state4 },
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
BobState::BtcRefunded(state4) => Bob::Done(BobEndState::BtcRefunded(Box::new(state4))),
BobState::XmrRedeemed => Bob::Done(BobEndState::XmrRedeemed),
BobState::BtcPunished => Bob::Done(BobEndState::BtcPunished),
BobState::SafelyAborted => Bob::Done(BobEndState::SafelyAborted),
}
}
}
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,
},
}
}
}
// TODO(Franck): Make this a method on a struct
#[allow(clippy::too_many_arguments)]
pub async fn swap<R>(

View File

@ -1,7 +1,12 @@
use crate::SwapAmounts;
use crate::{alice::swap::AliceState, bob::swap::BobState, SwapAmounts};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use xmr_btc::{alice, bitcoin::EncryptedSignature, bob, monero, serde::monero_private_key};
use xmr_btc::{
alice,
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
bob, monero,
serde::monero_private_key,
};
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Swap {
@ -44,6 +49,117 @@ pub enum AliceEndState {
BtcPunished,
}
impl From<&AliceState> for Alice {
fn from(alice_state: &AliceState) -> Self {
match alice_state {
AliceState::Negotiated { state3, .. } => Alice::Negotiated(state3.as_ref().clone()),
AliceState::BtcLocked { state3, .. } => Alice::BtcLocked(state3.as_ref().clone()),
AliceState::XmrLocked { state3 } => Alice::XmrLocked(state3.as_ref().clone()),
AliceState::EncSigLearned {
state3,
encrypted_signature,
} => Alice::EncSigLearned {
state3: state3.as_ref().clone(),
encrypted_signature: encrypted_signature.clone(),
},
AliceState::BtcRedeemed => Alice::Done(AliceEndState::BtcRedeemed),
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.as_ref().clone()),
AliceState::BtcRefunded { spend_key, state3 } => Alice::BtcRefunded {
spend_key: *spend_key,
state3: state3.as_ref().clone(),
},
AliceState::BtcPunishable { state3, .. } => {
Alice::BtcPunishable(state3.as_ref().clone())
}
AliceState::XmrRefunded => Alice::Done(AliceEndState::XmrRefunded),
AliceState::CancelTimelockExpired { state3 } => {
Alice::CancelTimelockExpired(state3.as_ref().clone())
}
AliceState::BtcPunished => Alice::Done(AliceEndState::BtcPunished),
AliceState::SafelyAborted => Alice::Done(AliceEndState::SafelyAborted),
AliceState::Started { amounts, state0 } => Alice::Started {
amounts: *amounts,
state0: state0.clone(),
},
}
}
}
impl From<Alice> for AliceState {
fn from(db_state: Alice) -> Self {
match db_state {
Alice::Started { amounts, state0 } => AliceState::Started { amounts, state0 },
Alice::Negotiated(state3) => AliceState::Negotiated {
channel: None,
amounts: SwapAmounts {
btc: state3.btc,
xmr: state3.xmr,
},
state3: Box::new(state3),
},
Alice::BtcLocked(state3) => AliceState::BtcLocked {
channel: None,
amounts: SwapAmounts {
btc: state3.btc,
xmr: state3.xmr,
},
state3: Box::new(state3),
},
Alice::XmrLocked(state3) => AliceState::XmrLocked {
state3: Box::new(state3),
},
Alice::EncSigLearned {
state3: state,
encrypted_signature,
} => AliceState::EncSigLearned {
state3: Box::new(state),
encrypted_signature,
},
Alice::CancelTimelockExpired(state3) => AliceState::CancelTimelockExpired {
state3: Box::new(state3),
},
Alice::BtcCancelled(state) => {
let tx_cancel = TxCancel::new(
&state.tx_lock,
state.cancel_timelock,
state.a.public(),
state.B,
);
AliceState::BtcCancelled {
state3: Box::new(state),
tx_cancel,
}
}
Alice::BtcPunishable(state3) => {
let tx_cancel = TxCancel::new(
&state3.tx_lock,
state3.cancel_timelock,
state3.a.public(),
state3.B,
);
let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address);
AliceState::BtcPunishable {
tx_refund,
state3: Box::new(state3),
}
}
Alice::BtcRefunded {
state3, spend_key, ..
} => AliceState::BtcRefunded {
spend_key,
state3: Box::new(state3),
},
Alice::Done(end_state) => match end_state {
AliceEndState::SafelyAborted => AliceState::SafelyAborted,
AliceEndState::BtcRedeemed => AliceState::BtcRedeemed,
AliceEndState::XmrRefunded => AliceState::XmrRefunded,
AliceEndState::BtcPunished => AliceState::BtcPunished,
},
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Bob {
Started {
@ -76,6 +192,46 @@ pub enum BobEndState {
BtcPunished,
}
impl From<BobState> for Bob {
fn from(bob_state: BobState) -> Self {
match bob_state {
BobState::Started { state0, amounts } => Bob::Started { state0, amounts },
BobState::Negotiated(state2) => Bob::Negotiated { state2 },
BobState::BtcLocked(state3) => Bob::BtcLocked { state3 },
BobState::XmrLocked(state4) => Bob::XmrLocked { state4 },
BobState::EncSigSent(state4) => Bob::EncSigSent { state4 },
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
BobState::BtcRefunded(state4) => Bob::Done(BobEndState::BtcRefunded(Box::new(state4))),
BobState::XmrRedeemed => Bob::Done(BobEndState::XmrRedeemed),
BobState::BtcPunished => Bob::Done(BobEndState::BtcPunished),
BobState::SafelyAborted => Bob::Done(BobEndState::SafelyAborted),
}
}
}
impl From<Bob> for BobState {
fn from(db_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,
},
}
}
}
impl From<Alice> for Swap {
fn from(from: Alice) -> Self {
Swap::Alice(from)