From 4d9559fc2e648740d127a90281c295150f0ca5ff Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 4 Jan 2021 20:29:11 +1100 Subject: [PATCH] Monero network settings and testnet --- swap/src/main.rs | 4 +-- swap/src/monero.rs | 26 ++++++++++++------- swap/tests/happy_path.rs | 2 +- swap/tests/happy_path_restart_alice.rs | 2 +- .../happy_path_restart_bob_after_comm.rs | 2 +- .../happy_path_restart_bob_before_comm.rs | 2 +- swap/tests/refund_restart_alice.rs | 4 +-- swap/tests/testutils/mod.rs | 5 +++- xmr-btc/src/bob.rs | 2 ++ xmr-btc/src/config.rs | 19 ++++++++++++++ 10 files changed, 49 insertions(+), 19 deletions(-) diff --git a/swap/src/main.rs b/swap/src/main.rs index 419b3320..defa119c 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -44,7 +44,7 @@ async fn main() -> Result<()> { let opt = Options::from_args(); - let config = Config::mainnet(); + let config = Config::testnet(); info!("Database: {}", opt.db_path); let db = Database::open(std::path::Path::new(opt.db_path.as_str())) @@ -257,7 +257,7 @@ async fn setup_wallets( ); let bitcoin_wallet = Arc::new(bitcoin_wallet); - let monero_wallet = monero::Wallet::new(monero_wallet_rpc_url); + let monero_wallet = monero::Wallet::new(monero_wallet_rpc_url, config.monero_network); let monero_balance = monero_wallet.get_balance().await?; info!( "Connection to Monero wallet succeeded, balance: {}", diff --git a/swap/src/monero.rs b/swap/src/monero.rs index 3c860c5c..7c90c7b8 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -8,16 +8,22 @@ use url::Url; pub use xmr_btc::monero::*; #[derive(Debug)] -pub struct Wallet(pub wallet::Client); +pub struct Wallet { + pub inner: wallet::Client, + pub network: Network, +} impl Wallet { - pub fn new(url: Url) -> Self { - Self(wallet::Client::new(url)) + pub fn new(url: Url, network: Network) -> Self { + Self { + inner: wallet::Client::new(url), + network, + } } /// Get the balance of the primary account. pub async fn get_balance(&self) -> Result { - let amount = self.0.get_balance(0).await?; + let amount = self.inner.get_balance(0).await?; Ok(Amount::from_piconero(amount)) } @@ -32,10 +38,10 @@ impl Transfer for Wallet { amount: Amount, ) -> Result<(TransferProof, Amount)> { let destination_address = - Address::standard(Network::Mainnet, public_spend_key, public_view_key.into()); + Address::standard(self.network, public_spend_key, public_view_key.into()); let res = self - .0 + .inner .transfer(0, amount.as_piconero(), &destination_address.to_string()) .await?; @@ -62,10 +68,10 @@ impl CreateWalletForOutput for Wallet { let public_spend_key = PublicKey::from_private_key(&private_spend_key); let public_view_key = PublicKey::from_private_key(&private_view_key.into()); - let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key); + let address = Address::standard(self.network, public_spend_key, public_view_key); let _ = self - .0 + .inner .generate_from_keys( &address.to_string(), &private_spend_key.to_string(), @@ -96,14 +102,14 @@ impl WatchForTransfer for Wallet { InsufficientFunds { expected: Amount, actual: Amount }, } - let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key.into()); + let address = Address::standard(self.network, public_spend_key, public_view_key.into()); let res = (|| async { // NOTE: Currently, this is conflating IO errors with the transaction not being // in the blockchain yet, or not having enough confirmations on it. All these // errors warrant a retry, but the strategy should probably differ per case let proof = self - .0 + .inner .check_tx_key( &String::from(transfer_proof.tx_hash()), &transfer_proof.tx_key().to_string(), diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index b6d435bc..874378e1 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -110,7 +110,7 @@ async fn happy_path() { let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap(); - bob_xmr_wallet.as_ref().0.refresh().await.unwrap(); + bob_xmr_wallet.as_ref().inner.refresh().await.unwrap(); let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap(); assert_eq!( diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index 4ab738d3..bb7b2bab 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -150,7 +150,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap); let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap(); - bob_xmr_wallet_clone.as_ref().0.refresh().await.unwrap(); + bob_xmr_wallet_clone.as_ref().inner.refresh().await.unwrap(); let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap(); assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap); diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index fb7310c8..5b9c6007 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -153,7 +153,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap); let xmr_alice_final = alice_xmr_wallet_clone.as_ref().get_balance().await.unwrap(); - bob_xmr_wallet_clone.as_ref().0.refresh().await.unwrap(); + bob_xmr_wallet_clone.as_ref().inner.refresh().await.unwrap(); let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap(); assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap); diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index ede14655..c6d24634 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -138,7 +138,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap(); - bob_xmr_wallet.as_ref().0.refresh().await.unwrap(); + bob_xmr_wallet.as_ref().inner.refresh().await.unwrap(); let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap(); assert_eq!( diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index 58fc7ee7..6eb03c18 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -156,11 +156,11 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { - bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE); assert!(btc_bob_final_alice_submitted_cancel || btc_bob_final_bob_submitted_cancel); - alice_xmr_wallet.as_ref().0.refresh().await.unwrap(); + alice_xmr_wallet.as_ref().inner.refresh().await.unwrap(); let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap(); assert_eq!(xmr_alice_final, xmr_to_swap); - bob_xmr_wallet.as_ref().0.refresh().await.unwrap(); + bob_xmr_wallet.as_ref().inner.refresh().await.unwrap(); let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap(); assert_eq!(xmr_bob_final, bob_xmr_starting_balance); } diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 6ec96aae..d47c2f69 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -46,7 +46,10 @@ pub async fn init_wallets( } }; - let xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet(name).unwrap().client())); + let xmr_wallet = Arc::new(swap::monero::Wallet { + inner: monero.wallet(name).unwrap().client(), + network: config.monero_network, + }); let btc_wallet = Arc::new( swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network) diff --git a/xmr-btc/src/bob.rs b/xmr-btc/src/bob.rs index 696d3cbd..fde43449 100644 --- a/xmr-btc/src/bob.rs +++ b/xmr-btc/src/bob.rs @@ -901,6 +901,8 @@ impl State5 { // NOTE: This actually generates and opens a new wallet, closing the currently // open one. + // TODO: This means that the wallet-rpc HAS to be started with the --wallet-dir + // flag, but that is mutually exclusive with --wallet-file ! monero_wallet .create_and_load_wallet_for_output(s, self.v) .await?; diff --git a/xmr-btc/src/config.rs b/xmr-btc/src/config.rs index 5715270e..37e59976 100644 --- a/xmr-btc/src/config.rs +++ b/xmr-btc/src/config.rs @@ -11,6 +11,7 @@ pub struct Config { pub bitcoin_cancel_timelock: Timelock, pub bitcoin_punish_timelock: Timelock, pub bitcoin_network: ::bitcoin::Network, + pub monero_network: ::monero::Network, } impl Config { @@ -26,6 +27,23 @@ impl Config { bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK, bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK, bitcoin_network: ::bitcoin::Network::Bitcoin, + monero_network: ::monero::Network::Mainnet, + } + } + + pub fn testnet() -> Self { + Self { + bob_time_to_act: *mainnet::BOB_TIME_TO_ACT, + bitcoin_finality_confirmations: mainnet::BITCOIN_FINALITY_CONFIRMATIONS, + bitcoin_avg_block_time: *mainnet::BITCOIN_AVG_BLOCK_TIME, + // We apply a scaling factor (1.5) so that the swap is not aborted when the + // blockchain is slow + monero_max_finality_time: (*mainnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) + * mainnet::MONERO_FINALITY_CONFIRMATIONS, + bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK, + bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK, + bitcoin_network: ::bitcoin::Network::Testnet, + monero_network: ::monero::Network::Stagenet, } } @@ -41,6 +59,7 @@ impl Config { bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK, bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK, bitcoin_network: ::bitcoin::Network::Regtest, + monero_network: ::monero::Network::default(), } } }