mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Use Bob swap factory in production
This commit is contained in:
parent
e56c48ee23
commit
75f89f3b25
134
swap/src/main.rs
134
swap/src/main.rs
@ -14,20 +14,16 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
use crate::cli::{Command, Options, Resume};
|
use crate::cli::{Command, Options, Resume};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use libp2p::{core::Multiaddr, PeerId};
|
|
||||||
use prettytable::{row, Table};
|
use prettytable::{row, Table};
|
||||||
use rand::rngs::OsRng;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use swap::{
|
use swap::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
config::Config,
|
config::Config,
|
||||||
database::{Database, Swap},
|
database::Database,
|
||||||
monero, network,
|
monero,
|
||||||
network::transport::build,
|
protocol::{alice, bob, bob::BobSwapFactory},
|
||||||
protocol::{alice, bob, bob::BobState},
|
|
||||||
seed::Seed,
|
|
||||||
trace::init_tracing,
|
trace::init_tracing,
|
||||||
StartingBalances, SwapAmounts,
|
StartingBalances, SwapAmounts,
|
||||||
};
|
};
|
||||||
@ -53,8 +49,6 @@ async fn main() -> Result<()> {
|
|||||||
let data_dir = std::path::Path::new(opt.data_dir.as_str()).to_path_buf();
|
let data_dir = std::path::Path::new(opt.data_dir.as_str()).to_path_buf();
|
||||||
let db_path = data_dir.join("database");
|
let db_path = data_dir.join("database");
|
||||||
|
|
||||||
let db = Database::open(db_path.as_path()).context("Could not open database")?;
|
|
||||||
|
|
||||||
let seed = swap::config::seed::Seed::from_file_or_generate(&data_dir)
|
let seed = swap::config::seed::Seed::from_file_or_generate(&data_dir)
|
||||||
.expect("Could not retrieve/initialize seed")
|
.expect("Could not retrieve/initialize seed")
|
||||||
.into();
|
.into();
|
||||||
@ -83,6 +77,11 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Swap sending {} and receiving {} starting with ID {}",
|
||||||
|
send_monero, receive_bitcoin, swap_id
|
||||||
|
);
|
||||||
|
|
||||||
let alice_factory = alice::AliceSwapFactory::new(
|
let alice_factory = alice::AliceSwapFactory::new(
|
||||||
seed,
|
seed,
|
||||||
config,
|
config,
|
||||||
@ -108,7 +107,12 @@ async fn main() -> Result<()> {
|
|||||||
send_bitcoin,
|
send_bitcoin,
|
||||||
receive_monero,
|
receive_monero,
|
||||||
} => {
|
} => {
|
||||||
let (bitcoin_wallet, monero_wallet, _) = setup_wallets(
|
let swap_amounts = SwapAmounts {
|
||||||
|
btc: send_bitcoin,
|
||||||
|
xmr: receive_monero,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (bitcoin_wallet, monero_wallet, starting_balances) = setup_wallets(
|
||||||
bitcoind_url,
|
bitcoind_url,
|
||||||
bitcoin_wallet_name.as_str(),
|
bitcoin_wallet_name.as_str(),
|
||||||
monero_wallet_rpc_url,
|
monero_wallet_rpc_url,
|
||||||
@ -116,47 +120,36 @@ async fn main() -> Result<()> {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let refund_address = bitcoin_wallet.new_address().await?;
|
|
||||||
let state0 = bob::state::State0::new(
|
|
||||||
&mut OsRng,
|
|
||||||
send_bitcoin,
|
|
||||||
receive_monero,
|
|
||||||
config.bitcoin_cancel_timelock,
|
|
||||||
config.bitcoin_punish_timelock,
|
|
||||||
refund_address,
|
|
||||||
config.monero_finality_confirmations,
|
|
||||||
);
|
|
||||||
|
|
||||||
let amounts = SwapAmounts {
|
|
||||||
btc: send_bitcoin,
|
|
||||||
xmr: receive_monero,
|
|
||||||
};
|
|
||||||
|
|
||||||
let bob_state = BobState::Started { state0, amounts };
|
|
||||||
|
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Swap sending {} and receiving {} started with ID {}",
|
"Swap sending {} and receiving {} starting with ID {}",
|
||||||
send_bitcoin, receive_monero, swap_id
|
send_bitcoin, receive_monero, swap_id
|
||||||
);
|
);
|
||||||
|
|
||||||
bob_swap(
|
let bob_factory = BobSwapFactory::new(
|
||||||
|
seed,
|
||||||
|
db_path,
|
||||||
swap_id,
|
swap_id,
|
||||||
bob_state,
|
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
monero_wallet,
|
monero_wallet,
|
||||||
db,
|
config,
|
||||||
alice_peer_id,
|
starting_balances,
|
||||||
alice_addr,
|
alice_addr,
|
||||||
seed,
|
alice_peer_id,
|
||||||
)
|
);
|
||||||
.await?;
|
let (swap, event_loop) = bob_factory.new_swap_as_bob(swap_amounts).await?;
|
||||||
|
|
||||||
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
bob::run(swap).await?;
|
||||||
}
|
}
|
||||||
Command::History => {
|
Command::History => {
|
||||||
let mut table = Table::new();
|
let mut table = Table::new();
|
||||||
|
|
||||||
table.add_row(row!["SWAP ID", "STATE"]);
|
table.add_row(row!["SWAP ID", "STATE"]);
|
||||||
|
|
||||||
|
let db = Database::open(db_path.as_path()).context("Could not open database")?;
|
||||||
|
|
||||||
for (swap_id, state) in db.all()? {
|
for (swap_id, state) in db.all()? {
|
||||||
table.add_row(row![swap_id, state]);
|
table.add_row(row![swap_id, state]);
|
||||||
}
|
}
|
||||||
@ -203,30 +196,29 @@ async fn main() -> Result<()> {
|
|||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
alice_addr,
|
alice_addr,
|
||||||
}) => {
|
}) => {
|
||||||
let db_state = if let Swap::Bob(db_state) = db.get_state(swap_id)? {
|
let (bitcoin_wallet, monero_wallet, starting_balances) = setup_wallets(
|
||||||
db_state
|
|
||||||
} else {
|
|
||||||
bail!("Swap {} is not buy xmr.", swap_id)
|
|
||||||
};
|
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet, _) = setup_wallets(
|
|
||||||
bitcoind_url,
|
bitcoind_url,
|
||||||
bitcoin_wallet_name.as_str(),
|
bitcoin_wallet_name.as_str(),
|
||||||
monero_wallet_rpc_url,
|
monero_wallet_rpc_url,
|
||||||
config,
|
config,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
bob_swap(
|
|
||||||
|
let bob_factory = BobSwapFactory::new(
|
||||||
|
seed,
|
||||||
|
db_path,
|
||||||
swap_id,
|
swap_id,
|
||||||
db_state.into(),
|
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
monero_wallet,
|
monero_wallet,
|
||||||
db,
|
config,
|
||||||
alice_peer_id,
|
starting_balances,
|
||||||
alice_addr,
|
alice_addr,
|
||||||
seed,
|
alice_peer_id,
|
||||||
)
|
);
|
||||||
.await?;
|
let (swap, event_loop) = bob_factory.recover_bob_from_db().await?;
|
||||||
|
|
||||||
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
bob::run(swap).await?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -268,43 +260,3 @@ async fn setup_wallets(
|
|||||||
|
|
||||||
Ok((bitcoin_wallet, monero_wallet, starting_balances))
|
Ok((bitcoin_wallet, monero_wallet, starting_balances))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
async fn bob_swap(
|
|
||||||
swap_id: Uuid,
|
|
||||||
state: BobState,
|
|
||||||
bitcoin_wallet: Arc<swap::bitcoin::Wallet>,
|
|
||||||
monero_wallet: Arc<swap::monero::Wallet>,
|
|
||||||
db: Database,
|
|
||||||
alice_peer_id: PeerId,
|
|
||||||
alice_addr: Multiaddr,
|
|
||||||
seed: Seed,
|
|
||||||
) -> Result<BobState> {
|
|
||||||
let identity = network::Seed::new(seed).derive_libp2p_identity();
|
|
||||||
let peer_id = identity.public().into_peer_id();
|
|
||||||
|
|
||||||
let bob_behaviour = bob::Behaviour::default();
|
|
||||||
let bob_transport = build(identity)?;
|
|
||||||
|
|
||||||
let (event_loop, handle) = bob::event_loop::EventLoop::new(
|
|
||||||
bob_transport,
|
|
||||||
bob_behaviour,
|
|
||||||
peer_id,
|
|
||||||
alice_peer_id,
|
|
||||||
alice_addr,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let swap = bob::Swap {
|
|
||||||
state,
|
|
||||||
event_loop_handle: handle,
|
|
||||||
db,
|
|
||||||
bitcoin_wallet,
|
|
||||||
monero_wallet,
|
|
||||||
swap_id,
|
|
||||||
};
|
|
||||||
|
|
||||||
let swap = bob::swap::run(swap);
|
|
||||||
|
|
||||||
tokio::spawn(event_loop.run());
|
|
||||||
swap.await
|
|
||||||
}
|
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
//! Run an XMR/BTC swap in the role of Bob.
|
//! Run an XMR/BTC swap in the role of Bob.
|
||||||
//! Bob holds BTC and wishes receive XMR.
|
//! Bob holds BTC and wishes receive XMR.
|
||||||
|
use anyhow::Result;
|
||||||
use libp2p::{core::Multiaddr, NetworkBehaviour, PeerId};
|
use libp2p::{core::Multiaddr, NetworkBehaviour, PeerId};
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
bitcoin::EncryptedSignature,
|
bitcoin::EncryptedSignature,
|
||||||
monero,
|
database, monero, network,
|
||||||
network::peer_tracker::{self, PeerTracker},
|
network::peer_tracker::{self, PeerTracker},
|
||||||
protocol::{alice, bob},
|
protocol::{alice, bob},
|
||||||
SwapAmounts,
|
StartingBalances, SwapAmounts,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
@ -22,8 +23,10 @@ pub use self::{
|
|||||||
state::*,
|
state::*,
|
||||||
swap::{run, run_until},
|
swap::{run, run_until},
|
||||||
};
|
};
|
||||||
use crate::database::Database;
|
use crate::{config::Config, database::Database, network::transport::build, seed::Seed};
|
||||||
use std::sync::Arc;
|
use libp2p::identity::Keypair;
|
||||||
|
use rand::rngs::OsRng;
|
||||||
|
use std::{path::PathBuf, sync::Arc};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
@ -44,6 +47,160 @@ pub struct Swap {
|
|||||||
pub swap_id: Uuid,
|
pub swap_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct BobSwapFactory {
|
||||||
|
identity: Keypair,
|
||||||
|
peer_id: PeerId,
|
||||||
|
|
||||||
|
db_path: PathBuf,
|
||||||
|
swap_id: Uuid,
|
||||||
|
|
||||||
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
pub monero_wallet: Arc<monero::Wallet>,
|
||||||
|
config: Config,
|
||||||
|
pub starting_balances: StartingBalances,
|
||||||
|
|
||||||
|
alice_connect_address: Multiaddr,
|
||||||
|
alice_connect_peer_id: PeerId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BobSwapFactory {
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn new(
|
||||||
|
seed: Seed,
|
||||||
|
db_path: PathBuf,
|
||||||
|
swap_id: Uuid,
|
||||||
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
monero_wallet: Arc<monero::Wallet>,
|
||||||
|
config: Config,
|
||||||
|
starting_balances: StartingBalances,
|
||||||
|
alice_connect_address: Multiaddr,
|
||||||
|
alice_connect_peer_id: PeerId,
|
||||||
|
) -> Self {
|
||||||
|
let identity = network::Seed::new(seed).derive_libp2p_identity();
|
||||||
|
let peer_id = identity.public().into_peer_id();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
identity,
|
||||||
|
peer_id,
|
||||||
|
db_path,
|
||||||
|
swap_id,
|
||||||
|
bitcoin_wallet,
|
||||||
|
monero_wallet,
|
||||||
|
config,
|
||||||
|
starting_balances,
|
||||||
|
alice_connect_address,
|
||||||
|
alice_connect_peer_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn new_swap_as_bob(
|
||||||
|
&self,
|
||||||
|
swap_amounts: SwapAmounts,
|
||||||
|
) -> Result<(bob::Swap, bob::EventLoop)> {
|
||||||
|
let initial_state = init_bob_state(
|
||||||
|
swap_amounts.btc,
|
||||||
|
swap_amounts.xmr,
|
||||||
|
self.bitcoin_wallet.clone(),
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
||||||
|
self.identity.clone(),
|
||||||
|
self.peer_id.clone(),
|
||||||
|
self.alice_connect_peer_id.clone(),
|
||||||
|
self.alice_connect_address.clone(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let db = Database::open(self.db_path.as_path())?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Swap {
|
||||||
|
state: initial_state,
|
||||||
|
event_loop_handle,
|
||||||
|
db,
|
||||||
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
||||||
|
monero_wallet: self.monero_wallet.clone(),
|
||||||
|
swap_id: self.swap_id,
|
||||||
|
},
|
||||||
|
event_loop,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn recover_bob_from_db(&self) -> Result<(bob::Swap, bob::EventLoop)> {
|
||||||
|
// reopen the existing database
|
||||||
|
let db = Database::open(self.db_path.clone().as_path())?;
|
||||||
|
|
||||||
|
let resume_state = if let database::Swap::Bob(state) = db.get_state(self.swap_id)? {
|
||||||
|
state.into()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
||||||
|
self.identity.clone(),
|
||||||
|
self.peer_id.clone(),
|
||||||
|
self.alice_connect_peer_id.clone(),
|
||||||
|
self.alice_connect_address.clone(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Swap {
|
||||||
|
state: resume_state,
|
||||||
|
event_loop_handle,
|
||||||
|
db,
|
||||||
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
||||||
|
monero_wallet: self.monero_wallet.clone(),
|
||||||
|
swap_id: self.swap_id,
|
||||||
|
},
|
||||||
|
event_loop,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn init_bob_state(
|
||||||
|
btc_to_swap: bitcoin::Amount,
|
||||||
|
xmr_to_swap: monero::Amount,
|
||||||
|
bob_btc_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
config: Config,
|
||||||
|
) -> Result<BobState> {
|
||||||
|
let amounts = SwapAmounts {
|
||||||
|
btc: btc_to_swap,
|
||||||
|
xmr: xmr_to_swap,
|
||||||
|
};
|
||||||
|
|
||||||
|
let refund_address = bob_btc_wallet.new_address().await?;
|
||||||
|
let state0 = bob::State0::new(
|
||||||
|
&mut OsRng,
|
||||||
|
btc_to_swap,
|
||||||
|
xmr_to_swap,
|
||||||
|
config.bitcoin_cancel_timelock,
|
||||||
|
config.bitcoin_punish_timelock,
|
||||||
|
refund_address,
|
||||||
|
config.monero_finality_confirmations,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(BobState::Started { state0, amounts })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_bob_event_loop(
|
||||||
|
identity: Keypair,
|
||||||
|
peer_id: PeerId,
|
||||||
|
alice_peer_id: PeerId,
|
||||||
|
alice_addr: Multiaddr,
|
||||||
|
) -> Result<(bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle)> {
|
||||||
|
let bob_behaviour = bob::Behaviour::default();
|
||||||
|
let bob_transport = build(identity)?;
|
||||||
|
|
||||||
|
bob::event_loop::EventLoop::new(
|
||||||
|
bob_transport,
|
||||||
|
bob_behaviour,
|
||||||
|
peer_id,
|
||||||
|
alice_peer_id,
|
||||||
|
alice_addr,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
ConnectionEstablished(PeerId),
|
ConnectionEstablished(PeerId),
|
||||||
|
@ -2,21 +2,18 @@ use crate::testutils;
|
|||||||
use bitcoin_harness::Bitcoind;
|
use bitcoin_harness::Bitcoind;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use get_port::get_port;
|
use get_port::get_port;
|
||||||
use libp2p::{core::Multiaddr, PeerId};
|
use libp2p::core::Multiaddr;
|
||||||
use monero_harness::{image, Monero};
|
use monero_harness::{image, Monero};
|
||||||
use rand::rngs::OsRng;
|
use std::sync::Arc;
|
||||||
use std::{path::PathBuf, sync::Arc};
|
|
||||||
use swap::{
|
use swap::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
config::Config,
|
config::Config,
|
||||||
database::Database,
|
monero,
|
||||||
monero, network,
|
|
||||||
network::transport::build,
|
|
||||||
protocol::{
|
protocol::{
|
||||||
alice,
|
alice,
|
||||||
alice::{AliceState, AliceSwapFactory},
|
alice::{AliceState, AliceSwapFactory},
|
||||||
bob,
|
bob,
|
||||||
bob::BobState,
|
bob::{BobState, BobSwapFactory},
|
||||||
},
|
},
|
||||||
seed::Seed,
|
seed::Seed,
|
||||||
StartingBalances, SwapAmounts,
|
StartingBalances, SwapAmounts,
|
||||||
@ -51,7 +48,8 @@ impl Test {
|
|||||||
let (swap, event_loop) = self
|
let (swap, event_loop) = self
|
||||||
.bob_swap_factory
|
.bob_swap_factory
|
||||||
.new_swap_as_bob(self.swap_amounts)
|
.new_swap_as_bob(self.swap_amounts)
|
||||||
.await;
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
tokio::spawn(async move { event_loop.run().await });
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
@ -71,7 +69,7 @@ impl Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn recover_bob_from_db(&self) -> bob::Swap {
|
pub async fn recover_bob_from_db(&self) -> bob::Swap {
|
||||||
let (swap, event_loop) = self.bob_swap_factory.recover_bob_from_db().await;
|
let (swap, event_loop) = self.bob_swap_factory.recover_bob_from_db().await.unwrap();
|
||||||
|
|
||||||
tokio::spawn(async move { event_loop.run().await });
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
@ -363,17 +361,26 @@ where
|
|||||||
btc: swap_amounts.btc * 10,
|
btc: swap_amounts.btc * 10,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (bob_bitcoin_wallet, bob_monero_wallet) = init_wallets(
|
||||||
|
"bob",
|
||||||
|
&containers.bitcoind,
|
||||||
|
&monero,
|
||||||
|
bob_starting_balances.clone(),
|
||||||
|
config,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
let bob_swap_factory = BobSwapFactory::new(
|
let bob_swap_factory = BobSwapFactory::new(
|
||||||
Seed::random().unwrap(),
|
Seed::random().unwrap(),
|
||||||
config,
|
tempdir().unwrap().path().to_path_buf(),
|
||||||
Uuid::new_v4(),
|
Uuid::new_v4(),
|
||||||
&monero,
|
bob_bitcoin_wallet,
|
||||||
&containers.bitcoind,
|
bob_monero_wallet,
|
||||||
|
config,
|
||||||
bob_starting_balances,
|
bob_starting_balances,
|
||||||
alice_swap_factory.listen_address(),
|
alice_swap_factory.listen_address(),
|
||||||
alice_swap_factory.peer_id(),
|
alice_swap_factory.peer_id(),
|
||||||
)
|
);
|
||||||
.await;
|
|
||||||
|
|
||||||
let test = Test {
|
let test = Test {
|
||||||
swap_amounts,
|
swap_amounts,
|
||||||
@ -384,112 +391,6 @@ where
|
|||||||
testfn(test).await
|
testfn(test).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BobSwapFactory {
|
|
||||||
seed: Seed,
|
|
||||||
|
|
||||||
db_path: PathBuf,
|
|
||||||
swap_id: Uuid,
|
|
||||||
|
|
||||||
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
||||||
monero_wallet: Arc<monero::Wallet>,
|
|
||||||
config: Config,
|
|
||||||
starting_balances: StartingBalances,
|
|
||||||
|
|
||||||
alice_connect_address: Multiaddr,
|
|
||||||
alice_connect_peer_id: PeerId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BobSwapFactory {
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
async fn new(
|
|
||||||
seed: Seed,
|
|
||||||
config: Config,
|
|
||||||
swap_id: Uuid,
|
|
||||||
monero: &Monero,
|
|
||||||
bitcoind: &Bitcoind<'_>,
|
|
||||||
starting_balances: StartingBalances,
|
|
||||||
alice_connect_address: Multiaddr,
|
|
||||||
alice_connect_peer_id: PeerId,
|
|
||||||
) -> Self {
|
|
||||||
let db_path = tempdir().unwrap().path().to_path_buf();
|
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) =
|
|
||||||
init_wallets("bob", bitcoind, monero, starting_balances.clone(), config).await;
|
|
||||||
|
|
||||||
Self {
|
|
||||||
seed,
|
|
||||||
db_path,
|
|
||||||
swap_id,
|
|
||||||
bitcoin_wallet,
|
|
||||||
monero_wallet,
|
|
||||||
config,
|
|
||||||
starting_balances,
|
|
||||||
alice_connect_address,
|
|
||||||
alice_connect_peer_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn new_swap_as_bob(&self, swap_amounts: SwapAmounts) -> (bob::Swap, bob::EventLoop) {
|
|
||||||
let initial_state = init_bob_state(
|
|
||||||
swap_amounts.btc,
|
|
||||||
swap_amounts.xmr,
|
|
||||||
self.bitcoin_wallet.clone(),
|
|
||||||
self.config,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
|
||||||
self.seed,
|
|
||||||
self.alice_connect_peer_id.clone(),
|
|
||||||
self.alice_connect_address.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let db = Database::open(self.db_path.as_path()).unwrap();
|
|
||||||
|
|
||||||
(
|
|
||||||
bob::Swap {
|
|
||||||
state: initial_state,
|
|
||||||
event_loop_handle,
|
|
||||||
db,
|
|
||||||
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
|
||||||
monero_wallet: self.monero_wallet.clone(),
|
|
||||||
swap_id: self.swap_id,
|
|
||||||
},
|
|
||||||
event_loop,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn recover_bob_from_db(&self) -> (bob::Swap, bob::EventLoop) {
|
|
||||||
// reopen the existing database
|
|
||||||
let db = Database::open(self.db_path.clone().as_path()).unwrap();
|
|
||||||
|
|
||||||
let resume_state =
|
|
||||||
if let swap::database::Swap::Bob(state) = db.get_state(self.swap_id).unwrap() {
|
|
||||||
state.into()
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
|
||||||
self.seed,
|
|
||||||
self.alice_connect_peer_id.clone(),
|
|
||||||
self.alice_connect_address.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
(
|
|
||||||
bob::Swap {
|
|
||||||
state: resume_state,
|
|
||||||
event_loop_handle,
|
|
||||||
db,
|
|
||||||
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
|
||||||
monero_wallet: self.monero_wallet.clone(),
|
|
||||||
swap_id: self.swap_id,
|
|
||||||
},
|
|
||||||
event_loop,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
||||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||||
let _ = bitcoind.init(5).await;
|
let _ = bitcoind.init(5).await;
|
||||||
@ -536,51 +437,6 @@ async fn init_wallets(
|
|||||||
(btc_wallet, xmr_wallet)
|
(btc_wallet, xmr_wallet)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_bob_state(
|
|
||||||
btc_to_swap: bitcoin::Amount,
|
|
||||||
xmr_to_swap: monero::Amount,
|
|
||||||
bob_btc_wallet: Arc<bitcoin::Wallet>,
|
|
||||||
config: Config,
|
|
||||||
) -> BobState {
|
|
||||||
let amounts = SwapAmounts {
|
|
||||||
btc: btc_to_swap,
|
|
||||||
xmr: xmr_to_swap,
|
|
||||||
};
|
|
||||||
|
|
||||||
let refund_address = bob_btc_wallet.new_address().await.unwrap();
|
|
||||||
let state0 = bob::State0::new(
|
|
||||||
&mut OsRng,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
config.bitcoin_cancel_timelock,
|
|
||||||
config.bitcoin_punish_timelock,
|
|
||||||
refund_address,
|
|
||||||
config.monero_finality_confirmations,
|
|
||||||
);
|
|
||||||
|
|
||||||
BobState::Started { state0, amounts }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_bob_event_loop(
|
|
||||||
seed: Seed,
|
|
||||||
alice_peer_id: PeerId,
|
|
||||||
alice_addr: Multiaddr,
|
|
||||||
) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) {
|
|
||||||
let identity = network::Seed::new(seed).derive_libp2p_identity();
|
|
||||||
let peer_id = identity.public().into_peer_id();
|
|
||||||
|
|
||||||
let bob_behaviour = bob::Behaviour::default();
|
|
||||||
let bob_transport = build(identity).unwrap();
|
|
||||||
bob::event_loop::EventLoop::new(
|
|
||||||
bob_transport,
|
|
||||||
bob_behaviour,
|
|
||||||
peer_id,
|
|
||||||
alice_peer_id,
|
|
||||||
alice_addr,
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is just to keep the containers alive
|
// This is just to keep the containers alive
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
struct Containers<'a> {
|
struct Containers<'a> {
|
||||||
|
Loading…
Reference in New Issue
Block a user