mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-07-20 05:31:44 -04:00
Use Bob swap factory in production
This commit is contained in:
parent
e56c48ee23
commit
75f89f3b25
3 changed files with 225 additions and 260 deletions
|
@ -2,21 +2,18 @@ use crate::testutils;
|
|||
use bitcoin_harness::Bitcoind;
|
||||
use futures::Future;
|
||||
use get_port::get_port;
|
||||
use libp2p::{core::Multiaddr, PeerId};
|
||||
use libp2p::core::Multiaddr;
|
||||
use monero_harness::{image, Monero};
|
||||
use rand::rngs::OsRng;
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
use swap::{
|
||||
bitcoin,
|
||||
config::Config,
|
||||
database::Database,
|
||||
monero, network,
|
||||
network::transport::build,
|
||||
monero,
|
||||
protocol::{
|
||||
alice,
|
||||
alice::{AliceState, AliceSwapFactory},
|
||||
bob,
|
||||
bob::BobState,
|
||||
bob::{BobState, BobSwapFactory},
|
||||
},
|
||||
seed::Seed,
|
||||
StartingBalances, SwapAmounts,
|
||||
|
@ -51,7 +48,8 @@ impl Test {
|
|||
let (swap, event_loop) = self
|
||||
.bob_swap_factory
|
||||
.new_swap_as_bob(self.swap_amounts)
|
||||
.await;
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::spawn(async move { event_loop.run().await });
|
||||
|
||||
|
@ -71,7 +69,7 @@ impl Test {
|
|||
}
|
||||
|
||||
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 });
|
||||
|
||||
|
@ -363,17 +361,26 @@ where
|
|||
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(
|
||||
Seed::random().unwrap(),
|
||||
config,
|
||||
tempdir().unwrap().path().to_path_buf(),
|
||||
Uuid::new_v4(),
|
||||
&monero,
|
||||
&containers.bitcoind,
|
||||
bob_bitcoin_wallet,
|
||||
bob_monero_wallet,
|
||||
config,
|
||||
bob_starting_balances,
|
||||
alice_swap_factory.listen_address(),
|
||||
alice_swap_factory.peer_id(),
|
||||
)
|
||||
.await;
|
||||
);
|
||||
|
||||
let test = Test {
|
||||
swap_amounts,
|
||||
|
@ -384,112 +391,6 @@ where
|
|||
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<'_>) {
|
||||
let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap();
|
||||
let _ = bitcoind.init(5).await;
|
||||
|
@ -536,51 +437,6 @@ async fn init_wallets(
|
|||
(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
|
||||
#[allow(dead_code)]
|
||||
struct Containers<'a> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue