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

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
bob::event_loop::EventLoopHandle, bob::event_loop::EventLoopHandle,
state, state,
state::{Bob, Swap}, state::{Bob, BobEndState, Swap},
storage::Database, storage::Database,
SwapAmounts, SwapAmounts,
}; };
@ -58,10 +58,7 @@ impl fmt::Display for BobState {
impl From<BobState> for state::Bob { impl From<BobState> for state::Bob {
fn from(bob_state: BobState) -> Self { fn from(bob_state: BobState) -> Self {
match bob_state { match bob_state {
BobState::Started { .. } => { BobState::Started { state0, amounts } => Bob::Started { state0, amounts },
// TODO: Do we want to resume just started swaps
unimplemented!("Cannot save a swap that has just started")
}
BobState::Negotiated(state2) => Bob::Negotiated { state2 }, BobState::Negotiated(state2) => Bob::Negotiated { state2 },
BobState::BtcLocked(state3) => Bob::BtcLocked { state3 }, BobState::BtcLocked(state3) => Bob::BtcLocked { state3 },
BobState::XmrLocked(state4) => Bob::XmrLocked { state4 }, BobState::XmrLocked(state4) => Bob::XmrLocked { state4 },
@ -69,10 +66,10 @@ impl From<BobState> for state::Bob {
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5), BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4), BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4), BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
BobState::BtcRefunded(_) BobState::BtcRefunded(state4) => Bob::Done(BobEndState::BtcRefunded(Box::new(state4))),
| BobState::XmrRedeemed BobState::XmrRedeemed => Bob::Done(BobEndState::XmrRedeemed),
| BobState::BtcPunished BobState::BtcPunished => Bob::Done(BobEndState::BtcPunished),
| BobState::SafelyAborted => Bob::SwapComplete, 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> { fn try_from(db_state: state::Swap) -> Result<Self, Self::Error> {
if let Swap::Bob(state) = db_state { if let Swap::Bob(state) = db_state {
let bob_State = match state { let bob_State = match state {
Bob::Started { state0, amounts } => BobState::Started { state0, amounts },
Bob::Negotiated { state2 } => BobState::Negotiated(state2), Bob::Negotiated { state2 } => BobState::Negotiated(state2),
Bob::BtcLocked { state3 } => BobState::BtcLocked(state3), Bob::BtcLocked { state3 } => BobState::BtcLocked(state3),
Bob::XmrLocked { state4 } => BobState::XmrLocked(state4), Bob::XmrLocked { state4 } => BobState::XmrLocked(state4),
@ -90,7 +88,12 @@ impl TryFrom<state::Swap> for BobState {
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4), Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4),
Bob::BtcCancelled(state4) => BobState::BtcCancelled(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) Ok(bob_State)

View File

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

View File

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