Skip to content

Commit

Permalink
Make the factory code usable in production
Browse files Browse the repository at this point in the history
- Introduce Test abstraction instead of tow harnesses, move test specific data into Test
- Change the abstraction from actors to swap, because we are creating swaps, not actors
- rename actor::swap  to run, because we are running a swap
  • Loading branch information
da-kami committed Jan 18, 2021
1 parent e372d96 commit 00835ba
Show file tree
Hide file tree
Showing 12 changed files with 562 additions and 558 deletions.
25 changes: 14 additions & 11 deletions swap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,17 @@ 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(),
monero_wallet.clone(),
event_loop_handle: handle,
bitcoin_wallet,
monero_wallet,
config,
swap_id,
db,
);
};

let swap = alice::swap::run(swap);

tokio::spawn(async move { event_loop.run().await });
swap.await
Expand All @@ -331,15 +333,16 @@ 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,
event_loop_handle: handle,
db,
bitcoin_wallet.clone(),
monero_wallet.clone(),
OsRng,
bitcoin_wallet,
monero_wallet,
swap_id,
);
};

let swap = bob::swap::run(swap);

tokio::spawn(event_loop.run());
swap.await
Expand Down
16 changes: 15 additions & 1 deletion swap/src/protocol/alice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libp2p::{
use tracing::{debug, info};

use crate::{
bitcoin, monero,
network::{
peer_tracker::{self, PeerTracker},
request_response::AliceToBob,
Expand All @@ -26,8 +27,11 @@ pub use self::{
message1::Message1,
message2::Message2,
state::*,
swap::{run_until, swap},
swap::{run, run_until},
};
use crate::{config::Config, database::Database};
use std::sync::Arc;
use uuid::Uuid;

mod amounts;
pub mod event_loop;
Expand All @@ -39,6 +43,16 @@ pub mod state;
mod steps;
pub mod swap;

pub struct Swap {
pub state: AliceState,
pub event_loop_handle: EventLoopHandle,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
pub config: Config,
pub swap_id: Uuid,
pub db: Database,
}

pub type Swarm = libp2p::Swarm<Behaviour>;

pub fn new_swarm(
Expand Down
114 changes: 59 additions & 55 deletions swap/src/protocol/alice/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ use crate::{
bitcoin,
bitcoin::{TransactionBlockHeight, WatchForRawTransaction},
config::Config,
database::{Database, Swap},
database,
database::Database,
monero,
monero::CreateWalletForOutput,
protocol::alice::{
event_loop::EventLoopHandle,
steps::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
publish_bitcoin_redeem_transaction, publish_cancel_transaction,
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin,
protocol::{
alice,
alice::{
event_loop::EventLoopHandle,
steps::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate,
publish_bitcoin_punish_transaction, publish_bitcoin_redeem_transaction,
publish_cancel_transaction, wait_for_bitcoin_encrypted_signature,
wait_for_bitcoin_refund, wait_for_locked_bitcoin,
},
AliceState,
},
AliceState,
},
ExpiredTimelocks,
};
Expand All @@ -35,28 +40,6 @@ trait Rng: RngCore + CryptoRng + Send {}

impl<T> Rng for T where T: RngCore + CryptoRng + Send {}

pub async fn swap(
state: AliceState,
event_loop_handle: EventLoopHandle,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
config: Config,
swap_id: Uuid,
db: Database,
) -> Result<AliceState> {
run_until(
state,
is_complete,
event_loop_handle,
bitcoin_wallet,
monero_wallet,
config,
swap_id,
db,
)
.await
}

pub fn is_complete(state: &AliceState) -> bool {
matches!(
state,
Expand All @@ -81,10 +64,31 @@ pub fn is_encsig_learned(state: &AliceState) -> bool {
)
}

pub async fn run(swap: alice::Swap) -> Result<AliceState> {
run_until(swap, is_complete).await
}

pub async fn run_until(
swap: alice::Swap,
is_target_state: fn(&AliceState) -> bool,
) -> Result<AliceState> {
do_run_until(
swap.state,
is_target_state,
swap.event_loop_handle,
swap.bitcoin_wallet,
swap.monero_wallet,
swap.config,
swap.swap_id,
swap.db,
)
.await
}

// State machine driver for swap execution
#[async_recursion]
#[allow(clippy::too_many_arguments)]
pub async fn run_until(
async fn do_run_until(
state: AliceState,
is_target_state: fn(&AliceState) -> bool,
mut event_loop_handle: EventLoopHandle,
Expand All @@ -110,9 +114,9 @@ pub async fn run_until(
};

let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand Down Expand Up @@ -153,9 +157,9 @@ pub async fn run_until(
};

let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand Down Expand Up @@ -194,9 +198,9 @@ pub async fn run_until(
};

let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand Down Expand Up @@ -234,9 +238,9 @@ pub async fn run_until(
};

let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand Down Expand Up @@ -272,9 +276,9 @@ pub async fn run_until(

let state = AliceState::CancelTimelockExpired { state3 };
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
return run_until(
return do_run_until(
state,
is_target_state,
event_loop_handle,
Expand All @@ -300,9 +304,9 @@ pub async fn run_until(

let state = AliceState::BtcRedeemed;
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand All @@ -327,9 +331,9 @@ pub async fn run_until(

let state = AliceState::BtcCancelled { state3, tx_cancel };
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand Down Expand Up @@ -360,10 +364,10 @@ pub async fn run_until(
None => {
let state = AliceState::BtcPunishable { tx_refund, state3 };
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;

run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand All @@ -386,9 +390,9 @@ pub async fn run_until(

let state = AliceState::BtcRefunded { spend_key, state3 };
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand All @@ -411,7 +415,7 @@ pub async fn run_until(

let state = AliceState::XmrRefunded;
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
Ok(state)
}
Expand Down Expand Up @@ -441,9 +445,9 @@ pub async fn run_until(
Either::Left(_) => {
let state = AliceState::BtcPunished;
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand All @@ -465,9 +469,9 @@ pub async fn run_until(
)?;
let state = AliceState::BtcRefunded { spend_key, state3 };
let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state))
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?;
run_until(
do_run_until(
state,
is_target_state,
event_loop_handle,
Expand Down
16 changes: 15 additions & 1 deletion swap/src/protocol/bob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use libp2p::{
use tracing::{debug, info};

use crate::{
bitcoin,
bitcoin::EncryptedSignature,
monero,
network::{
peer_tracker::{self, PeerTracker},
transport::SwapTransport,
Expand All @@ -26,8 +28,11 @@ pub use self::{
message2::Message2,
message3::Message3,
state::*,
swap::{run_until, swap},
swap::{run, run_until},
};
use crate::database::Database;
use std::sync::Arc;
use uuid::Uuid;

mod amounts;
pub mod event_loop;
Expand All @@ -38,6 +43,15 @@ mod message3;
pub mod state;
pub mod swap;

pub struct Swap {
pub state: BobState,
pub event_loop_handle: bob::EventLoopHandle,
pub db: Database,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
pub swap_id: Uuid,
}

pub type Swarm = libp2p::Swarm<Behaviour>;

pub fn new_swarm(transport: SwapTransport, behaviour: Behaviour) -> Result<Swarm> {
Expand Down
Loading

0 comments on commit 00835ba

Please sign in to comment.