From a44303f839a4d93248a57d55d9fc6485cbc53db6 Mon Sep 17 00:00:00 2001 From: rishflab Date: Tue, 3 Nov 2020 16:08:46 +1100 Subject: [PATCH] Add monerod to config Fixed rebase issues --- swap/src/alice.rs | 3 --- swap/src/bitcoin.rs | 22 +++--------------- swap/src/bob.rs | 1 + swap/src/cli.rs | 6 +++++ swap/src/main.rs | 55 +++++++++++++++++++++++++-------------------- swap/src/monero.rs | 5 +++-- swap/tests/e2e.rs | 15 +++++++++---- 7 files changed, 55 insertions(+), 52 deletions(-) diff --git a/swap/src/alice.rs b/swap/src/alice.rs index 63638dfd..036e4dc8 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -44,8 +44,6 @@ pub async fn swap( bitcoin_wallet: Arc, monero_wallet: Arc, listen: Multiaddr, - redeem_address: ::bitcoin::Address, - punish_address: ::bitcoin::Address, transport: SwapTransport, behaviour: Alice, ) -> Result<()> { @@ -71,7 +69,6 @@ pub async fn swap( // TODO: For retry, use `backoff::ExponentialBackoff` in production as opposed // to `ConstantBackoff`. - #[async_trait] impl ReceiveBitcoinRedeemEncsig for Network { async fn receive_bitcoin_redeem_encsig(&mut self) -> xmr_btc::bitcoin::EncryptedSignature { diff --git a/swap/src/bitcoin.rs b/swap/src/bitcoin.rs index f9e1493d..2d88ae2c 100644 --- a/swap/src/bitcoin.rs +++ b/swap/src/bitcoin.rs @@ -4,7 +4,7 @@ use anyhow::Result; use async_trait::async_trait; use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; use bitcoin::{util::psbt::PartiallySignedTransaction, Address, Transaction}; -use bitcoin_harness::{bitcoind_rpc::PsbtBase64, Bitcoind}; +use bitcoin_harness::bitcoind_rpc::PsbtBase64; use reqwest::Url; use tokio::time; use xmr_btc::bitcoin::{ @@ -20,8 +20,8 @@ pub const TX_LOCK_MINE_TIMEOUT: u64 = 3600; pub struct Wallet(pub bitcoin_harness::Wallet); impl Wallet { - pub async fn new(name: &str, url: &Url) -> Result { - let wallet = bitcoin_harness::Wallet::new(name, url.clone()).await?; + pub async fn new(name: &str, url: Url) -> Result { + let wallet = bitcoin_harness::Wallet::new(name, url).await?; Ok(Self(wallet)) } @@ -46,22 +46,6 @@ impl Wallet { } } -pub async fn make_wallet( - name: &str, - bitcoind: &Bitcoind<'_>, - fund_amount: Amount, -) -> Result { - let wallet = Wallet::new(name, &bitcoind.node_url).await?; - let buffer = Amount::from_btc(1.0).unwrap(); - let amount = fund_amount + buffer; - - let address = wallet.0.new_address().await.unwrap(); - - bitcoind.mint(address, amount).await.unwrap(); - - Ok(wallet) -} - #[async_trait] impl BuildTxLockPsbt for Wallet { async fn build_tx_lock_psbt( diff --git a/swap/src/bob.rs b/swap/src/bob.rs index 39b96afe..2f973b99 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -39,6 +39,7 @@ use xmr_btc::{ monero::CreateWalletForOutput, }; +#[allow(clippy::too_many_arguments)] pub async fn swap( bitcoin_wallet: Arc, monero_wallet: Arc, diff --git a/swap/src/cli.rs b/swap/src/cli.rs index 24e0041d..aff4d67c 100644 --- a/swap/src/cli.rs +++ b/swap/src/cli.rs @@ -8,6 +8,9 @@ pub enum Options { #[structopt(default_value = "http://127.0.0.1:8332", long = "bitcoind")] bitcoind_url: Url, + #[structopt(default_value = "http://127.0.0.1:18083", long = "monerod")] + monerod_url: Url, + #[structopt(default_value = "/ip4/127.0.0.1/tcp/9876", long = "listen-addr")] listen_addr: Multiaddr, @@ -24,6 +27,9 @@ pub enum Options { #[structopt(default_value = "http://127.0.0.1:8332", long = "bitcoind")] bitcoind_url: Url, + #[structopt(default_value = "http://127.0.0.1:18083", long = "monerod")] + monerod_url: Url, + #[structopt(long = "tor")] tor: bool, }, diff --git a/swap/src/main.rs b/swap/src/main.rs index d55ce097..2f8fc74c 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -21,21 +21,18 @@ use structopt::StructOpt; use swap::{ alice, alice::Alice, - bitcoin::Wallet, - bob, + bitcoin, bob, bob::Bob, + monero, network::transport::{build, build_tor, SwapTransport}, Cmd, Rsp, SwapAmounts, }; use tracing::info; -use url::Url; -use xmr_btc::bitcoin::{BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock}; mod cli; mod trace; use cli::Options; -use swap::{alice, bitcoin, bob, monero, Cmd, Rsp, SwapAmounts}; // TODO: Add root seed file instead of generating new seed each run. @@ -47,7 +44,8 @@ async fn main() -> Result<()> { match opt { Options::Alice { - bitcoind_url: url, + bitcoind_url, + monerod_url, listen_addr, tor_port, } => { @@ -75,19 +73,27 @@ async fn main() -> Result<()> { } }; - let bitcoin_wallet = bitcoin::Wallet::new("alice", &url) + let bitcoin_wallet = bitcoin::Wallet::new("alice", bitcoind_url) .await .expect("failed to create bitcoin wallet"); let bitcoin_wallet = Arc::new(bitcoin_wallet); - let monero_wallet = Arc::new(monero::Wallet::localhost(MONERO_WALLET_RPC_PORT)); + let monero_wallet = Arc::new(monero::Wallet::new(monerod_url)); - swap_as_alice(listen_addr, redeem, punish, transport, behaviour).await?; + swap_as_alice( + bitcoin_wallet, + monero_wallet, + listen_addr, + transport, + behaviour, + ) + .await?; } Options::Bob { alice_addr, satoshis, - bitcoind_url: url, + bitcoind_url, + monerod_url, tor, } => { info!("running swap node as Bob ..."); @@ -100,17 +106,18 @@ async fn main() -> Result<()> { false => build(local_key_pair)?, }; - let bitcoin_wallet = Wallet::new("bob", &url) + let bitcoin_wallet = bitcoin::Wallet::new("bob", bitcoind_url) .await .expect("failed to create bitcoin wallet"); + let bitcoin_wallet = Arc::new(bitcoin_wallet); - let monero_wallet = Arc::new(monero::Wallet::localhost(MONERO_WALLET_RPC_PORT)); + let monero_wallet = Arc::new(monero::Wallet::new(monerod_url)); swap_as_bob( + bitcoin_wallet, + monero_wallet, satoshis, alice_addr, - refund, - bitcoin_wallet, transport, behaviour, ) @@ -143,12 +150,10 @@ async fn swap_as_alice( bitcoin_wallet: Arc, monero_wallet: Arc, addr: Multiaddr, - redeem: bitcoin::Address, - punish: bitcoin::Address, transport: SwapTransport, behaviour: Alice, ) -> Result<()> { - alice::swap(addr, redeem, punish, transport, behaviour).await + alice::swap(bitcoin_wallet, monero_wallet, addr, transport, behaviour).await } async fn swap_as_bob( @@ -156,18 +161,20 @@ async fn swap_as_bob( monero_wallet: Arc, sats: u64, alice: Multiaddr, - refund: bitcoin::Address, - wallet: W, transport: SwapTransport, behaviour: Bob, -) -> Result<()> -where - W: BuildTxLockPsbt + SignTxLock + BroadcastSignedTransaction + Send + Sync + 'static, -{ +) -> Result<()> { let (cmd_tx, mut cmd_rx) = mpsc::channel(1); let (mut rsp_tx, rsp_rx) = mpsc::channel(1); tokio::spawn(bob::swap( - sats, alice, cmd_tx, rsp_rx, refund, wallet, transport, behaviour, + bitcoin_wallet, + monero_wallet, + sats, + alice, + cmd_tx, + rsp_rx, + transport, + behaviour, )); loop { diff --git a/swap/src/monero.rs b/swap/src/monero.rs index 0d215738..eb9f9bbc 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -5,6 +5,7 @@ use monero::{Address, Network, PrivateKey}; use monero_harness::rpc::wallet; use std::{str::FromStr, time::Duration}; +use url::Url; pub use xmr_btc::monero::{ Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicKey, PublicViewKey, Transfer, TransferProof, TxHash, WatchForTransfer, *, @@ -13,8 +14,8 @@ pub use xmr_btc::monero::{ pub struct Wallet(pub wallet::Client); impl Wallet { - pub fn localhost(port: u16) -> Self { - Self(wallet::Client::localhost(port)) + pub fn new(url: Url) -> Self { + Self(wallet::Client::new(url)) } /// Get the balance of the primary account. diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs index 09ba8fb0..cb49ef05 100644 --- a/swap/tests/e2e.rs +++ b/swap/tests/e2e.rs @@ -5,7 +5,7 @@ mod e2e_test { use libp2p::Multiaddr; use monero_harness::Monero; use std::sync::Arc; - use swap::{alice, bob}; + use swap::{alice, bob, network::transport::build}; use testcontainers::clients::Cli; use tracing_subscriber::util::SubscriberInitExt; @@ -37,12 +37,12 @@ mod e2e_test { let xmr_bob = 0; let alice_btc_wallet = Arc::new( - swap::bitcoin::Wallet::new("alice", &bitcoind.node_url) + swap::bitcoin::Wallet::new("alice", bitcoind.node_url.clone()) .await .unwrap(), ); let bob_btc_wallet = Arc::new( - swap::bitcoin::Wallet::new("bob", &bitcoind.node_url) + swap::bitcoin::Wallet::new("bob", bitcoind.node_url.clone()) .await .unwrap(), ); @@ -57,15 +57,20 @@ mod e2e_test { let alice_xmr_wallet = Arc::new(swap::monero::Wallet(monero.alice_wallet_rpc_client())); let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.bob_wallet_rpc_client())); + let alice_behaviour = alice::Alice::default(); + let alice_transport = build(alice_behaviour.identity()).unwrap(); let alice_swap = alice::swap( alice_btc_wallet.clone(), alice_xmr_wallet.clone(), alice_multiaddr.clone(), - None, + alice_transport, + alice_behaviour, ); let (cmd_tx, mut _cmd_rx) = mpsc::channel(1); let (mut rsp_tx, rsp_rx) = mpsc::channel(1); + let bob_behaviour = bob::Bob::default(); + let bob_transport = build(bob_behaviour.identity()).unwrap(); let bob_swap = bob::swap( bob_btc_wallet.clone(), bob_xmr_wallet.clone(), @@ -73,6 +78,8 @@ mod e2e_test { alice_multiaddr, cmd_tx, rsp_rx, + bob_transport, + bob_behaviour, ); // automate the verification step by accepting any amounts sent over by Alice