From 144da75270c22cc92dcfa26dc47a50db7f5bac01 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 15 Feb 2021 13:09:37 +1100 Subject: [PATCH] Remove redundant data --- swap/src/database/alice.rs | 10 +--------- swap/src/protocol/alice.rs | 16 ++-------------- swap/src/protocol/alice/event_loop.rs | 25 +++---------------------- swap/src/protocol/alice/state.rs | 3 --- swap/src/protocol/alice/steps.rs | 4 +--- swap/src/protocol/alice/swap.rs | 4 ---- 6 files changed, 7 insertions(+), 55 deletions(-) diff --git a/swap/src/database/alice.rs b/swap/src/database/alice.rs index 3eadc70d..22b90afe 100644 --- a/swap/src/database/alice.rs +++ b/swap/src/database/alice.rs @@ -2,7 +2,7 @@ use crate::{ bitcoin::{EncryptedSignature, TxCancel, TxRefund}, monero, monero::monero_private_key, - protocol::{alice, alice::AliceState, SwapAmounts}, + protocol::{alice, alice::AliceState}, }; use ::bitcoin::hashes::core::fmt::Display; use libp2p::PeerId; @@ -101,10 +101,6 @@ impl From for AliceState { bob_peer_id, } => AliceState::Started { bob_peer_id, - amounts: SwapAmounts { - btc: state3.btc, - xmr: state3.xmr, - }, state3: Box::new(state3), }, Alice::BtcLocked { @@ -112,10 +108,6 @@ impl From for AliceState { bob_peer_id, } => AliceState::BtcLocked { bob_peer_id, - amounts: SwapAmounts { - btc: state3.btc, - xmr: state3.xmr, - }, state3: Box::new(state3), }, Alice::XmrLocked(state3) => AliceState::XmrLocked { diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index b266045f..329b370c 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -1,9 +1,6 @@ //! Run an XMR/BTC swap in the role of Alice. //! Alice holds XMR and wishes receive BTC. -use crate::{ - bitcoin, database, database::Database, execution_params::ExecutionParams, monero, - protocol::SwapAmounts, -}; +use crate::{bitcoin, database, database::Database, execution_params::ExecutionParams, monero}; use anyhow::{bail, Result}; use libp2p::{core::Multiaddr, PeerId}; use std::sync::Arc; @@ -57,7 +54,6 @@ pub struct Builder { enum InitParams { None, New { - swap_amounts: SwapAmounts, bob_peer_id: PeerId, state3: Box, }, @@ -88,15 +84,9 @@ impl Builder { } } - pub fn with_init_params( - self, - swap_amounts: SwapAmounts, - bob_peer_id: PeerId, - state3: State3, - ) -> Self { + pub fn with_init_params(self, bob_peer_id: PeerId, state3: State3) -> Self { Self { init_params: InitParams::New { - swap_amounts, bob_peer_id, state3: Box::new(state3), }, @@ -107,12 +97,10 @@ impl Builder { pub async fn build(self) -> Result { match self.init_params { InitParams::New { - swap_amounts, bob_peer_id, ref state3, } => { let initial_state = AliceState::Started { - amounts: swap_amounts, state3: state3.clone(), bob_peer_id, }; diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 159c046f..e887b170 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -10,17 +10,16 @@ use crate::{ AliceState, Behaviour, Builder, OutEvent, QuoteResponse, State0, State3, TransferProof, }, bob::{EncryptedSignature, QuoteRequest}, - SwapAmounts, }, seed::Seed, }; -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use futures::future::RemoteHandle; use libp2p::{ core::Multiaddr, futures::FutureExt, request_response::ResponseChannel, PeerId, Swarm, }; use rand::rngs::OsRng; -use std::{collections::HashMap, sync::Arc}; +use std::sync::Arc; use tokio::sync::{broadcast, mpsc}; use tracing::{debug, error, trace, warn}; use uuid::Uuid; @@ -89,10 +88,6 @@ pub struct EventLoop { db: Arc, listen_address: Multiaddr, - // Amounts agreed upon for swaps currently in the execution setup phase - // Note: We can do one execution setup per peer at a given time. - swap_amounts: HashMap, - recv_encrypted_signature: broadcast::Sender, send_transfer_proof: mpsc::Receiver<(PeerId, TransferProof)>, @@ -137,7 +132,6 @@ impl EventLoop { monero_wallet, db, listen_address, - swap_amounts: Default::default(), recv_encrypted_signature: recv_encrypted_signature.sender, send_transfer_proof: send_transfer_proof.receiver, send_transfer_proof_sender: send_transfer_proof.sender, @@ -225,12 +219,6 @@ impl EventLoop { ) .await?; - // if a node restart during execution setup, the swap is aborted (safely). - self.swap_amounts.insert(bob_peer_id, SwapAmounts { - btc: btc_amount, - xmr: xmr_amount, - }); - self.swarm.start_execution_setup(bob_peer_id, state0); // Continues once the execution setup protocol is done Ok(()) @@ -244,13 +232,6 @@ impl EventLoop { let swap_id = Uuid::new_v4(); let handle = self.new_handle(); - let swap_amounts = self.swap_amounts.remove(&bob_peer_id).ok_or_else(|| { - anyhow!( - "execution setup done for an unknown peer id: {}, node restarted in between?", - bob_peer_id - ) - })?; - let swap = Builder::new( self.peer_id, self.execution_params, @@ -261,7 +242,7 @@ impl EventLoop { self.listen_address.clone(), handle, ) - .with_init_params(swap_amounts, bob_peer_id, state3) + .with_init_params(bob_peer_id, state3) .build() .await?; diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index cee4a0e6..9609e1ed 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -10,7 +10,6 @@ use crate::{ protocol::{ alice::{Message1, Message3, TransferProof}, bob::{EncryptedSignature, Message0, Message2, Message4}, - SwapAmounts, }, }; use anyhow::{anyhow, Context, Result}; @@ -25,12 +24,10 @@ use std::fmt; pub enum AliceState { Started { bob_peer_id: PeerId, - amounts: SwapAmounts, state3: Box, }, BtcLocked { bob_peer_id: PeerId, - amounts: SwapAmounts, state3: Box, }, XmrLocked { diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index d8b29c87..bd1f9a2a 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -12,7 +12,6 @@ use crate::{ protocol::{ alice, alice::{event_loop::EventLoopHandle, TransferProof}, - SwapAmounts, }, }; use anyhow::{Context, Result}; @@ -55,7 +54,6 @@ where pub async fn lock_xmr( bob_peer_id: PeerId, - amounts: SwapAmounts, state3: alice::State3, event_loop_handle: &mut EventLoopHandle, monero_wallet: Arc, @@ -71,7 +69,7 @@ where let public_view_key = state3.v.public(); let (transfer_proof, _) = monero_wallet - .transfer(public_spend_key, public_view_key, amounts.xmr) + .transfer(public_spend_key, public_view_key, state3.xmr) .await?; // TODO(Franck): Wait for Monero to be confirmed once diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index d5a5aef2..67875db7 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -94,7 +94,6 @@ async fn run_until_internal( AliceState::Started { state3, bob_peer_id, - amounts, } => { let _ = wait_for_locked_bitcoin( state3.tx_lock.txid(), @@ -105,7 +104,6 @@ async fn run_until_internal( let state = AliceState::BtcLocked { bob_peer_id, - amounts, state3, }; @@ -126,12 +124,10 @@ async fn run_until_internal( } AliceState::BtcLocked { bob_peer_id, - amounts, state3, } => { lock_xmr( bob_peer_id, - amounts, *state3.clone(), &mut event_loop_handle, monero_wallet.clone(),