Align Bob DB states with swap states

This commit is contained in:
Franck Royer 2020-12-23 12:08:51 +11:00
parent e541f7b83d
commit 5d1b10cc58
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 59 additions and 35 deletions

View File

@ -14,7 +14,7 @@ use crate::{
bitcoin::EncryptedSignature,
network::request_response::AliceToBob,
state,
state::{Alice, EndState, Swap},
state::{Alice, AliceEndState, Swap},
storage::Database,
SwapAmounts,
};
@ -118,19 +118,19 @@ impl From<&AliceState> for state::Alice {
state: state3.clone(),
encrypted_signature: encrypted_signature.clone(),
},
AliceState::BtcRedeemed => Alice::Done(EndState::BtcRedeemed),
AliceState::BtcRedeemed => Alice::Done(AliceEndState::BtcRedeemed),
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.clone()),
AliceState::BtcRefunded { spend_key, state3 } => Alice::BtcRefunded {
spend_key: *spend_key,
state3: state3.clone(),
},
AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()),
AliceState::XmrRefunded => Alice::Done(EndState::XmrRefunded),
AliceState::XmrRefunded => Alice::Done(AliceEndState::XmrRefunded),
AliceState::CancelTimelockExpired { state3 } => {
Alice::CancelTimelockExpired(state3.clone())
}
AliceState::BtcPunished => Alice::Done(EndState::BtcPunished),
AliceState::SafelyAborted => Alice::Done(EndState::SafelyAborted),
AliceState::BtcPunished => Alice::Done(AliceEndState::BtcPunished),
AliceState::SafelyAborted => Alice::Done(AliceEndState::SafelyAborted),
AliceState::Started { amounts, state0 } => Alice::Started {
amounts: *amounts,
state0: state0.clone(),
@ -204,10 +204,10 @@ impl From<state::Alice> for AliceState {
state3: state,
},
Alice::Done(end_state) => match end_state {
EndState::SafelyAborted => SafelyAborted,
EndState::BtcRedeemed => BtcRedeemed,
EndState::XmrRefunded => XmrRefunded,
EndState::BtcPunished => BtcPunished,
AliceEndState::SafelyAborted => SafelyAborted,
AliceEndState::BtcRedeemed => BtcRedeemed,
AliceEndState::XmrRefunded => XmrRefunded,
AliceEndState::BtcPunished => BtcPunished,
},
}
}

View File

@ -1,7 +1,7 @@
use crate::{
bob::event_loop::EventLoopHandle,
state,
state::{Bob, Swap},
state::{Bob, BobEndState, Swap},
storage::Database,
SwapAmounts,
};
@ -58,10 +58,7 @@ impl fmt::Display for BobState {
impl From<BobState> for state::Bob {
fn from(bob_state: BobState) -> Self {
match bob_state {
BobState::Started { .. } => {
// TODO: Do we want to resume just started swaps
unimplemented!("Cannot save a swap that has just started")
}
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 },
@ -69,10 +66,10 @@ impl From<BobState> for state::Bob {
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
BobState::BtcRefunded(_)
| BobState::XmrRedeemed
| BobState::BtcPunished
| BobState::SafelyAborted => Bob::SwapComplete,
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),
}
}
}
@ -83,6 +80,7 @@ impl TryFrom<state::Swap> for BobState {
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),
@ -90,7 +88,12 @@ impl TryFrom<state::Swap> for BobState {
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4),
Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4),
Bob::SwapComplete => BobState::SafelyAborted,
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)

View File

@ -32,11 +32,11 @@ pub enum Alice {
#[serde(with = "monero_private_key")]
spend_key: monero::PrivateKey,
},
Done(EndState),
Done(AliceEndState),
}
#[derive(Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq)]
pub enum EndState {
pub enum AliceEndState {
SafelyAborted,
BtcRedeemed,
XmrRefunded,
@ -45,14 +45,34 @@ pub enum EndState {
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Bob {
Negotiated { state2: bob::State2 },
BtcLocked { state3: bob::State3 },
XmrLocked { state4: bob::State4 },
EncSigSent { state4: bob::State4 },
Started {
state0: bob::State0,
amounts: SwapAmounts,
},
Negotiated {
state2: bob::State2,
},
BtcLocked {
state3: bob::State3,
},
XmrLocked {
state4: bob::State4,
},
EncSigSent {
state4: bob::State4,
},
BtcRedeemed(bob::State5),
CancelTimelockExpired(bob::State4),
BtcCancelled(bob::State4),
SwapComplete,
Done(BobEndState),
}
#[derive(Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq)]
pub enum BobEndState {
SafelyAborted,
XmrRedeemed,
BtcRefunded(Box<bob::State4>),
BtcPunished,
}
impl From<Alice> for Swap {
@ -80,7 +100,7 @@ impl Display for Alice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Alice::Started { .. } => write!(f, "Started"),
Alice::Negotiated(_) => f.write_str("Handshake complete"),
Alice::Negotiated(_) => f.write_str("Negotiated"),
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
Alice::XmrLocked(_) => f.write_str("Monero locked"),
Alice::CancelTimelockExpired(_) => f.write_str("Cancel timelock is expired"),
@ -96,13 +116,14 @@ impl Display for Alice {
impl Display for Bob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Bob::Negotiated { .. } => f.write_str("Handshake complete"),
Bob::Started { .. } => write!(f, "Started"),
Bob::Negotiated { .. } => f.write_str("Negotiated"),
Bob::BtcLocked { .. } => f.write_str("Bitcoin locked"),
Bob::XmrLocked { .. } => f.write_str("Monero locked"),
Bob::CancelTimelockExpired(_) => f.write_str("Cancel timelock is expired"),
Bob::BtcCancelled(_) => f.write_str("Bitcoin refundable"),
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
Bob::SwapComplete => f.write_str("Swap complete"),
Bob::Done(end_state) => write!(f, "Done: {}", end_state),
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
}
}

View File

@ -81,7 +81,7 @@ where
#[cfg(test)]
mod tests {
use crate::state::{Alice, Bob, EndState};
use crate::state::{Alice, AliceEndState, Bob, BobEndState};
use super::*;
@ -90,13 +90,13 @@ mod tests {
let db_dir = tempfile::tempdir().unwrap();
let db = Database::open(db_dir.path()).unwrap();
let state_1 = Swap::Alice(Alice::Done(EndState::BtcRedeemed));
let state_1 = Swap::Alice(Alice::Done(AliceEndState::BtcRedeemed));
let swap_id_1 = Uuid::new_v4();
db.insert_latest_state(swap_id_1, state_1.clone())
.await
.expect("Failed to save second state");
let state_2 = Swap::Bob(Bob::SwapComplete);
let state_2 = Swap::Bob(Bob::Done(BobEndState::XmrRedeemed));
let swap_id_2 = Uuid::new_v4();
db.insert_latest_state(swap_id_2, state_2.clone())
.await
@ -119,7 +119,7 @@ mod tests {
let db_dir = tempfile::tempdir().unwrap();
let db = Database::open(db_dir.path()).unwrap();
let state = Swap::Alice(Alice::Done(EndState::SafelyAborted));
let state = Swap::Alice(Alice::Done(AliceEndState::SafelyAborted));
let swap_id = Uuid::new_v4();
db.insert_latest_state(swap_id, state.clone())
@ -146,13 +146,13 @@ mod tests {
let db_dir = tempfile::tempdir().unwrap();
let db = Database::open(db_dir.path()).unwrap();
let state_1 = Swap::Alice(Alice::Done(EndState::BtcPunished));
let state_1 = Swap::Alice(Alice::Done(AliceEndState::BtcPunished));
let swap_id_1 = Uuid::new_v4();
db.insert_latest_state(swap_id_1, state_1.clone())
.await
.expect("Failed to save second state");
let state_2 = Swap::Bob(Bob::SwapComplete);
let state_2 = Swap::Bob(Bob::Done(BobEndState::BtcPunished));
let swap_id_2 = Uuid::new_v4();
db.insert_latest_state(swap_id_2, state_2.clone())
.await