From 9d8f8b0a1f1bb66ce9ed0a588ef7e9383151a1f8 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 7 Jan 2021 11:45:55 +1100 Subject: [PATCH 1/6] P2p socket should be listening for external connections --- swap/src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/src/cli.rs b/swap/src/cli.rs index 4d7f7798..6ab1e3f9 100644 --- a/swap/src/cli.rs +++ b/swap/src/cli.rs @@ -30,7 +30,7 @@ pub enum Command { )] monero_wallet_rpc_url: Url, - #[structopt(long = "p2p-address", default_value = "/ip4/127.0.0.1/tcp/9876")] + #[structopt(long = "p2p-address", default_value = "/ip4/0.0.0.0/tcp/9876")] listen_addr: Multiaddr, #[structopt(long = "send-xmr", help = "Monero amount as floating point nr without denomination (e.g. 125.1)", parse(try_from_str = parse_xmr))] From 95ecb02e7af2ca6f44281b0bd978bbb6097e615f Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 7 Jan 2021 11:46:58 +1100 Subject: [PATCH 2/6] Small changes to make debugging easier and tests --- swap/src/config.rs | 2 +- swap/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/swap/src/config.rs b/swap/src/config.rs index bd56630e..154a82cf 100644 --- a/swap/src/config.rs +++ b/swap/src/config.rs @@ -90,7 +90,7 @@ mod mainnet { mod testnet { use super::*; - pub static BOB_TIME_TO_ACT: Lazy = Lazy::new(|| Duration::from_secs(5 * 60)); + pub static BOB_TIME_TO_ACT: Lazy = Lazy::new(|| Duration::from_secs(60 * 60)); // This does not reflect recommended values for mainnet! pub static BITCOIN_FINALITY_CONFIRMATIONS: u32 = 1; diff --git a/swap/src/main.rs b/swap/src/main.rs index 74db62e3..81702042 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -38,7 +38,7 @@ extern crate prettytable; #[tokio::main] async fn main() -> Result<()> { - init_tracing(LevelFilter::Info).expect("initialize tracing"); + init_tracing(LevelFilter::Trace).expect("initialize tracing"); let opt = Options::from_args(); From 17356eaff9037eb28daf24782ef99dd21cb25321 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 7 Jan 2021 11:53:07 +1100 Subject: [PATCH 3/6] Do not guard waiting for encrypted signature with arbitrary timeout We already select waiting for this message with the cancellation expiry, we do not need add another guard that tries to guess how long it would for the Monero transaction to be finalised. --- swap/src/config.rs | 21 --------------------- swap/src/protocol/alice/steps.rs | 8 ++++---- swap/src/protocol/alice/swap.rs | 6 ++---- 3 files changed, 6 insertions(+), 29 deletions(-) diff --git a/swap/src/config.rs b/swap/src/config.rs index 154a82cf..5e138f00 100644 --- a/swap/src/config.rs +++ b/swap/src/config.rs @@ -7,7 +7,6 @@ pub struct Config { pub bob_time_to_act: Duration, pub bitcoin_finality_confirmations: u32, pub bitcoin_avg_block_time: Duration, - pub monero_max_finality_time: Duration, pub monero_finality_confirmations: u32, pub bitcoin_cancel_timelock: Timelock, pub bitcoin_punish_timelock: Timelock, @@ -21,10 +20,6 @@ impl Config { 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, monero_finality_confirmations: mainnet::MONERO_FINALITY_CONFIRMATIONS, bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK, bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK, @@ -38,10 +33,6 @@ impl Config { bob_time_to_act: *testnet::BOB_TIME_TO_ACT, bitcoin_finality_confirmations: testnet::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_avg_block_time: *testnet::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: (*testnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) - * testnet::MONERO_FINALITY_CONFIRMATIONS, monero_finality_confirmations: testnet::MONERO_FINALITY_CONFIRMATIONS, bitcoin_cancel_timelock: testnet::BITCOIN_CANCEL_TIMELOCK, bitcoin_punish_timelock: testnet::BITCOIN_PUNISH_TIMELOCK, @@ -55,10 +46,6 @@ impl Config { bob_time_to_act: *regtest::BOB_TIME_TO_ACT, bitcoin_finality_confirmations: regtest::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_avg_block_time: *regtest::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: (*regtest::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) - * regtest::MONERO_FINALITY_CONFIRMATIONS, monero_finality_confirmations: regtest::MONERO_FINALITY_CONFIRMATIONS, bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK, bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK, @@ -80,8 +67,6 @@ mod mainnet { pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 15; - pub static MONERO_AVG_BLOCK_TIME: Lazy = Lazy::new(|| Duration::from_secs(2 * 60)); - // Set to 12 hours, arbitrary value to be reviewed properly pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(72); pub static BITCOIN_PUNISH_TIMELOCK: Timelock = Timelock::new(72); @@ -100,10 +85,6 @@ mod testnet { // This does not reflect recommended values for mainnet! pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 5; - // The average blocktime on Monero stagenet is not as constant as on mainnet, - // hence 4 minutes it set - pub static MONERO_AVG_BLOCK_TIME: Lazy = Lazy::new(|| Duration::from_secs(4 * 60)); - // This does not reflect recommended values for mainnet! pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(6); pub static BITCOIN_PUNISH_TIMELOCK: Timelock = Timelock::new(6); @@ -121,8 +102,6 @@ mod regtest { pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 1; - pub static MONERO_AVG_BLOCK_TIME: Lazy = Lazy::new(|| Duration::from_secs(60)); - pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(50); pub static BITCOIN_PUNISH_TIMELOCK: Timelock = Timelock::new(50); diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index 9afc6bb0..568e4b8b 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -7,7 +7,7 @@ use futures::{ use libp2p::request_response::ResponseChannel; use rand::rngs::OsRng; use sha2::Sha256; -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; use tokio::time::timeout; use tracing::{info, trace}; @@ -147,11 +147,11 @@ where pub async fn wait_for_bitcoin_encrypted_signature( event_loop_handle: &mut EventLoopHandle, - timeout_duration: Duration, ) -> Result { - let msg3 = timeout(timeout_duration, event_loop_handle.recv_message3()) + let msg3 = event_loop_handle + .recv_message3() .await - .context("Failed to receive Bitcoin encrypted signature from Bob")??; + .context("Failed to receive Bitcoin encrypted signature from Bob")?; Ok(msg3.tx_redeem_encsig) } diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 4afaa682..78ab1a72 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -214,10 +214,8 @@ pub async fn run_until( // expressed more cleanly let state = match state3.expired_timelocks(bitcoin_wallet.as_ref()).await? { ExpiredTimelocks::None => { - let wait_for_enc_sig = wait_for_bitcoin_encrypted_signature( - &mut event_loop_handle, - config.monero_max_finality_time, - ); + let wait_for_enc_sig = + wait_for_bitcoin_encrypted_signature(&mut event_loop_handle); let state3_clone = state3.clone(); let cancel_timelock_expires = state3_clone .wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref()); From ef906876a05caac75c47319951bbc6e0b97ff004 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 7 Jan 2021 13:19:22 +1100 Subject: [PATCH 4/6] Increase timelocks on testnet To allow time for stagenet Monero transaction to be mined. --- swap/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/src/config.rs b/swap/src/config.rs index 5e138f00..dedd421e 100644 --- a/swap/src/config.rs +++ b/swap/src/config.rs @@ -86,7 +86,7 @@ mod testnet { pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 5; // This does not reflect recommended values for mainnet! - pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(6); + pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(12); pub static BITCOIN_PUNISH_TIMELOCK: Timelock = Timelock::new(6); } From 047f990d050158cfe0de4fe5e4f501ff1ac7a8f0 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 7 Jan 2021 14:44:31 +1100 Subject: [PATCH 5/6] Improve error reporting on signature verification --- swap/src/protocol/alice/state.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 292fe03d..b25f0720 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context, Result}; use ecdsa_fun::{ adaptor::{Adaptor, EncryptedSignature}, nonce::Deterministic, @@ -254,10 +254,12 @@ impl State2 { pub fn receive(self, msg: bob::Message2) -> Result { let tx_cancel = bitcoin::TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B); - bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig)?; + bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig) + .context("Failed to verify cancel transaction")?; let tx_punish = bitcoin::TxPunish::new(&tx_cancel, &self.punish_address, self.punish_timelock); - bitcoin::verify_sig(&self.B, &tx_punish.digest(), &msg.tx_punish_sig)?; + bitcoin::verify_sig(&self.B, &tx_punish.digest(), &msg.tx_punish_sig) + .context("Failed to verify Punish Transaction")?; Ok(State3 { a: self.a, From 6ffc66867cfab2f8111c3fb2cc5c7cc42df43fe3 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 8 Jan 2021 11:31:21 +1100 Subject: [PATCH 6/6] Typo Co-authored-by: Daniel Karzel --- swap/src/protocol/alice/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index b25f0720..40512799 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -259,7 +259,7 @@ impl State2 { let tx_punish = bitcoin::TxPunish::new(&tx_cancel, &self.punish_address, self.punish_timelock); bitcoin::verify_sig(&self.B, &tx_punish.digest(), &msg.tx_punish_sig) - .context("Failed to verify Punish Transaction")?; + .context("Failed to verify punish transaction")?; Ok(State3 { a: self.a,