mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-02-25 09:01:21 -05:00
Align Alice DB states with swap states
This commit is contained in:
parent
a31db63e54
commit
69e1c2bb27
22
Cargo.lock
generated
22
Cargo.lock
generated
@ -3289,6 +3289,27 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strum"
|
||||||
|
version = "0.20.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c"
|
||||||
|
dependencies = [
|
||||||
|
"strum_macros",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strum_macros"
|
||||||
|
version = "0.20.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "subtle"
|
name = "subtle"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -3337,6 +3358,7 @@ dependencies = [
|
|||||||
"sled",
|
"sled",
|
||||||
"spectral",
|
"spectral",
|
||||||
"structopt",
|
"structopt",
|
||||||
|
"strum",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"testcontainers",
|
"testcontainers",
|
||||||
"time",
|
"time",
|
||||||
|
@ -34,6 +34,7 @@ serde_json = "1"
|
|||||||
sha2 = "0.9"
|
sha2 = "0.9"
|
||||||
sled = "0.34"
|
sled = "0.34"
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
|
strum = { version = "0.20", features = ["derive"] }
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
time = "0.2"
|
time = "0.2"
|
||||||
tokio = { version = "0.2", features = ["rt-threaded", "time", "macros", "sync"] }
|
tokio = { version = "0.2", features = ["rt-threaded", "time", "macros", "sync"] }
|
||||||
|
@ -14,7 +14,7 @@ use crate::{
|
|||||||
bitcoin::EncryptedSignature,
|
bitcoin::EncryptedSignature,
|
||||||
network::request_response::AliceToBob,
|
network::request_response::AliceToBob,
|
||||||
state,
|
state,
|
||||||
state::{Alice, Swap},
|
state::{Alice, EndState, Swap},
|
||||||
storage::Database,
|
storage::Database,
|
||||||
SwapAmounts,
|
SwapAmounts,
|
||||||
};
|
};
|
||||||
@ -118,20 +118,23 @@ 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::SwapComplete,
|
AliceState::BtcRedeemed => Alice::Done(EndState::BtcRedeemed),
|
||||||
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.clone()),
|
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.clone()),
|
||||||
AliceState::BtcRefunded { .. } => Alice::SwapComplete,
|
AliceState::BtcRefunded { spend_key, state3 } => Alice::BtcRefunded {
|
||||||
|
spend_key: *spend_key,
|
||||||
|
state3: state3.clone(),
|
||||||
|
},
|
||||||
AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()),
|
AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()),
|
||||||
AliceState::XmrRefunded => Alice::SwapComplete,
|
AliceState::XmrRefunded => Alice::Done(EndState::XmrRefunded),
|
||||||
AliceState::CancelTimelockExpired { state3 } => {
|
AliceState::CancelTimelockExpired { state3 } => {
|
||||||
Alice::CancelTimelockExpired(state3.clone())
|
Alice::CancelTimelockExpired(state3.clone())
|
||||||
}
|
}
|
||||||
AliceState::BtcPunished => Alice::SwapComplete,
|
AliceState::BtcPunished => Alice::Done(EndState::BtcPunished),
|
||||||
AliceState::SafelyAborted => Alice::SwapComplete,
|
AliceState::SafelyAborted => Alice::Done(EndState::SafelyAborted),
|
||||||
// TODO: Potentially add support to resume swaps that are not Negotiated
|
AliceState::Started { amounts, state0 } => Alice::Started {
|
||||||
AliceState::Started { .. } => {
|
amounts: *amounts,
|
||||||
panic!("Alice attempted to save swap before being negotiated")
|
state0: state0.clone(),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -143,6 +146,7 @@ impl TryFrom<state::Swap> for AliceState {
|
|||||||
use AliceState::*;
|
use AliceState::*;
|
||||||
if let Swap::Alice(state) = db_state {
|
if let Swap::Alice(state) = db_state {
|
||||||
let alice_state = match state {
|
let alice_state = match state {
|
||||||
|
Alice::Started { amounts, state0 } => Started { amounts, state0 },
|
||||||
Alice::Negotiated(state3) => Negotiated {
|
Alice::Negotiated(state3) => Negotiated {
|
||||||
channel: None,
|
channel: None,
|
||||||
amounts: SwapAmounts {
|
amounts: SwapAmounts {
|
||||||
@ -198,15 +202,19 @@ impl TryFrom<state::Swap> for AliceState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Alice::BtcRefunded {
|
Alice::BtcRefunded {
|
||||||
state, spend_key, ..
|
state3: state,
|
||||||
|
spend_key,
|
||||||
|
..
|
||||||
} => BtcRefunded {
|
} => BtcRefunded {
|
||||||
spend_key,
|
spend_key,
|
||||||
state3: state,
|
state3: state,
|
||||||
},
|
},
|
||||||
Alice::SwapComplete => {
|
Alice::Done(end_state) => match end_state {
|
||||||
// TODO(Franck): Better fine grain
|
EndState::SafelyAborted => SafelyAborted,
|
||||||
AliceState::SafelyAborted
|
EndState::BtcRedeemed => BtcRedeemed,
|
||||||
}
|
EndState::XmrRefunded => XmrRefunded,
|
||||||
|
EndState::BtcPunished => BtcPunished,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Ok(alice_state)
|
Ok(alice_state)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::SwapAmounts;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use xmr_btc::{alice, bitcoin::EncryptedSignature, bob, monero, serde::monero_private_key};
|
use xmr_btc::{alice, bitcoin::EncryptedSignature, bob, monero, serde::monero_private_key};
|
||||||
@ -12,6 +13,10 @@ pub enum Swap {
|
|||||||
#[allow(clippy::large_enum_variant)]
|
#[allow(clippy::large_enum_variant)]
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
pub enum Alice {
|
pub enum Alice {
|
||||||
|
Started {
|
||||||
|
amounts: SwapAmounts,
|
||||||
|
state0: alice::State0,
|
||||||
|
},
|
||||||
Negotiated(alice::State3),
|
Negotiated(alice::State3),
|
||||||
BtcLocked(alice::State3),
|
BtcLocked(alice::State3),
|
||||||
XmrLocked(alice::State3),
|
XmrLocked(alice::State3),
|
||||||
@ -28,12 +33,19 @@ pub enum Alice {
|
|||||||
BtcCancelled(alice::State3),
|
BtcCancelled(alice::State3),
|
||||||
BtcPunishable(alice::State3),
|
BtcPunishable(alice::State3),
|
||||||
BtcRefunded {
|
BtcRefunded {
|
||||||
state: alice::State3,
|
state3: alice::State3,
|
||||||
#[serde(with = "monero_private_key")]
|
#[serde(with = "monero_private_key")]
|
||||||
spend_key: monero::PrivateKey,
|
spend_key: monero::PrivateKey,
|
||||||
view_key: monero::PrivateViewKey,
|
|
||||||
},
|
},
|
||||||
SwapComplete,
|
Done(EndState),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
|
pub enum EndState {
|
||||||
|
SafelyAborted,
|
||||||
|
BtcRedeemed,
|
||||||
|
XmrRefunded,
|
||||||
|
BtcPunished,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
@ -72,6 +84,7 @@ impl Display for Swap {
|
|||||||
impl Display for Alice {
|
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::Negotiated(_) => f.write_str("Handshake complete"),
|
Alice::Negotiated(_) => f.write_str("Handshake complete"),
|
||||||
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"),
|
||||||
@ -80,7 +93,7 @@ impl Display for Alice {
|
|||||||
Alice::BtcCancelled(_) => f.write_str("Bitcoin cancel transaction published"),
|
Alice::BtcCancelled(_) => f.write_str("Bitcoin cancel transaction published"),
|
||||||
Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"),
|
Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"),
|
||||||
Alice::BtcRefunded { .. } => f.write_str("Monero refundable"),
|
Alice::BtcRefunded { .. } => f.write_str("Monero refundable"),
|
||||||
Alice::SwapComplete => f.write_str("Swap complete"),
|
Alice::Done(end_state) => write!(f, "Done: {}", end_state),
|
||||||
Alice::EncSigLearned { .. } => f.write_str("Encrypted signature learned"),
|
Alice::EncSigLearned { .. } => f.write_str("Encrypted signature learned"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ where
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::state::{Alice, Bob};
|
use crate::state::{Alice, Bob, EndState};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@ -90,7 +90,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_1 = Swap::Alice(Alice::SwapComplete);
|
let state_1 = Swap::Alice(Alice::Done(EndState::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
|
||||||
@ -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::SwapComplete);
|
let state = Swap::Alice(Alice::Done(EndState::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,7 +146,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_1 = Swap::Alice(Alice::SwapComplete);
|
let state_1 = Swap::Alice(Alice::Done(EndState::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
|
||||||
|
@ -435,7 +435,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||||
pub struct State0 {
|
pub struct State0 {
|
||||||
pub a: bitcoin::SecretKey,
|
pub a: bitcoin::SecretKey,
|
||||||
pub s_a: cross_curve_dleq::Scalar,
|
pub s_a: cross_curve_dleq::Scalar,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user