Re-export swap executor function.

Move is_state functions to state file.
This commit is contained in:
rishflab 2021-01-08 12:55:46 +11:00
parent 6358b0c1a9
commit 76d6cf0f2e
13 changed files with 72 additions and 69 deletions

View File

@ -302,7 +302,7 @@ async fn alice_swap(
let (mut event_loop, handle) =
alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen_addr)?;
let swap = alice::swap::swap(
let swap = alice::swap(
state,
handle,
bitcoin_wallet.clone(),
@ -333,7 +333,7 @@ async fn bob_swap(
let (event_loop, handle) =
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)?;
let swap = bob::swap::swap(
let swap = bob::swap(
state,
handle,
db,

View File

@ -18,7 +18,9 @@ use crate::{
protocol::{bob, SwapAmounts},
};
pub use self::{amounts::*, message0::Message0, message1::Message1, message2::Message2, state::*};
pub use self::{
amounts::*, message0::Message0, message1::Message1, message2::Message2, state::*, swap::*,
};
mod amounts;
pub mod event_loop;

View File

@ -86,6 +86,30 @@ impl fmt::Display for AliceState {
}
}
pub fn is_complete(state: &AliceState) -> bool {
matches!(
state,
AliceState::XmrRefunded
| AliceState::BtcRedeemed
| AliceState::BtcPunished
| AliceState::SafelyAborted
)
}
pub fn is_xmr_locked(state: &AliceState) -> bool {
matches!(
state,
AliceState::XmrLocked{..}
)
}
pub fn is_encsig_learned(state: &AliceState) -> bool {
matches!(
state,
AliceState::EncSigLearned{..}
)
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct State0 {
pub a: bitcoin::SecretKey,

View File

@ -21,6 +21,7 @@ use crate::{
protocol::{
alice::{
event_loop::EventLoopHandle,
is_complete,
steps::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate,
@ -60,30 +61,6 @@ pub async fn swap(
.await
}
pub fn is_complete(state: &AliceState) -> bool {
matches!(
state,
AliceState::XmrRefunded
| AliceState::BtcRedeemed
| AliceState::BtcPunished
| AliceState::SafelyAborted
)
}
pub fn is_xmr_locked(state: &AliceState) -> bool {
matches!(
state,
AliceState::XmrLocked{..}
)
}
pub fn is_encsig_learned(state: &AliceState) -> bool {
matches!(
state,
AliceState::EncSigLearned{..}
)
}
// State machine driver for swap execution
#[async_recursion]
#[allow(clippy::too_many_arguments)]

View File

@ -19,7 +19,7 @@ use crate::{
pub use self::{
amounts::*, message0::Message0, message1::Message1, message2::Message2, message3::Message3,
state::*,
state::*, swap::*,
};
mod amounts;

View File

@ -58,6 +58,28 @@ impl fmt::Display for BobState {
}
}
pub fn is_complete(state: &BobState) -> bool {
matches!(
state,
BobState::BtcRefunded(..)
| BobState::XmrRedeemed
| BobState::BtcPunished
| BobState::SafelyAborted
)
}
pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..))
}
pub fn is_xmr_locked(state: &BobState) -> bool {
matches!(state, BobState::XmrLocked(..))
}
pub fn is_encsig_sent(state: &BobState) -> bool {
matches!(state, BobState::EncSigSent(..))
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct State0 {
b: bitcoin::SecretKey,

View File

@ -41,28 +41,6 @@ where
.await
}
pub fn is_complete(state: &BobState) -> bool {
matches!(
state,
BobState::BtcRefunded(..)
| BobState::XmrRedeemed
| BobState::BtcPunished
| BobState::SafelyAborted
)
}
pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..))
}
pub fn is_xmr_locked(state: &BobState) -> bool {
matches!(state, BobState::XmrLocked(..))
}
pub fn is_encsig_sent(state: &BobState) -> bool {
matches!(state, BobState::EncSigSent(..))
}
// State machine driver for swap execution
#[allow(clippy::too_many_arguments)]
#[async_recursion]

View File

@ -80,7 +80,7 @@ async fn happy_path() {
)
.await;
let alice_swap_fut = alice::swap::swap(
let alice_swap_fut = alice::swap(
alice_state,
alice_event_loop_handle,
alice_btc_wallet.clone(),
@ -93,7 +93,7 @@ async fn happy_path() {
let alice_fut = select(alice_swap_fut, alice_event_loop.run().boxed());
let bob_swap_fut = bob::swap::swap(
let bob_swap_fut = bob::swap(
bob_state,
bob_event_loop_handle,
bob_db,

View File

@ -102,9 +102,9 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
let alice_swap_id = Uuid::new_v4();
let alice_state = alice::swap::run_until(
let alice_state = alice::run_until(
start_state,
alice::swap::is_encsig_learned,
alice::is_encsig_learned,
alice_event_loop_handle,
alice_btc_wallet.clone(),
alice_xmr_wallet.clone(),

View File

@ -85,7 +85,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
let bob_btc_wallet_clone = bob_btc_wallet.clone();
let bob_xmr_wallet_clone = bob_xmr_wallet.clone();
let alice_swap_handle = tokio::spawn(alice::swap::swap(
let alice_swap_handle = tokio::spawn(alice::swap(
alice_state,
alice_event_loop_handle,
alice_btc_wallet.clone(),
@ -103,9 +103,9 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
let bob_db_datadir = tempdir().unwrap();
let bob_db = Database::open(bob_db_datadir.path()).unwrap();
let bob_state = bob::swap::run_until(
let bob_state = bob::run_until(
bob_state,
bob::swap::is_encsig_sent,
bob::is_encsig_sent,
bob_event_loop_handle,
bob_db,
bob_btc_wallet.clone(),
@ -131,7 +131,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
init_bob_event_loop(alice_peer_id, alice_multiaddr);
tokio::spawn(event_loop_after_restart.run());
let bob_state = bob::swap::swap(
let bob_state = bob::swap(
resume_state,
event_loop_handle_after_restart,
bob_db,

View File

@ -76,7 +76,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
)
.await;
let alice_fut = alice::swap::swap(
let alice_fut = alice::swap(
alice_state,
alice_event_loop_handle,
alice_btc_wallet.clone(),
@ -91,9 +91,9 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
let bob_xmr_locked_fut = {
let bob_db = Database::open(bob_db_datadir.path()).unwrap();
bob::swap::run_until(
bob::run_until(
bob_state,
bob::swap::is_xmr_locked,
bob::is_xmr_locked,
bob_event_loop_handle_1,
bob_db,
bob_btc_wallet.clone(),
@ -117,7 +117,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
let (bob_event_loop_2, bob_event_loop_handle_2) =
init_bob_event_loop(alice_peer_id, alice_multiaddr);
let bob_fut = bob::swap::swap(
let bob_fut = bob::swap(
bob_restart_state,
bob_event_loop_handle_2,
Database::open(bob_db_datadir.path()).unwrap(),

View File

@ -79,9 +79,9 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
)
.await;
let bob_btc_locked_fut = bob::swap::run_until(
let bob_btc_locked_fut = bob::run_until(
bob_state,
bob::swap::is_btc_locked,
bob::is_btc_locked,
bob_event_loop_handle,
bob_db,
bob_btc_wallet.clone(),
@ -93,7 +93,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
let bob_fut = select(bob_btc_locked_fut, bob_event_loop.run().boxed());
let alice_fut = alice::swap::swap(
let alice_fut = alice::swap(
alice_state,
alice_event_loop_handle,
alice_btc_wallet.clone(),

View File

@ -81,7 +81,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
)
.await;
let bob_fut = bob::swap::swap(
let bob_fut = bob::swap(
bob_state,
bob_event_loop_handle,
bob_db,
@ -96,9 +96,9 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
let alice_xmr_locked_fut = {
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
alice::swap::run_until(
alice::run_until(
alice_state,
alice::swap::is_xmr_locked,
alice::is_xmr_locked,
alice_event_loop_handle_1,
alice_btc_wallet.clone(),
alice_xmr_wallet.clone(),
@ -128,7 +128,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
let alice_final_state = {
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
alice::swap::swap(
alice::swap(
alice_restart_state,
alice_event_loop_handle_2,
alice_btc_wallet.clone(),