Remove redundant data

This commit is contained in:
Franck Royer 2021-02-15 13:09:37 +11:00
parent fb3b2aa546
commit 144da75270
No known key found for this signature in database
GPG key ID: A82ED75A8DFC50A4
6 changed files with 7 additions and 55 deletions

View file

@ -2,7 +2,7 @@ use crate::{
bitcoin::{EncryptedSignature, TxCancel, TxRefund}, bitcoin::{EncryptedSignature, TxCancel, TxRefund},
monero, monero,
monero::monero_private_key, monero::monero_private_key,
protocol::{alice, alice::AliceState, SwapAmounts}, protocol::{alice, alice::AliceState},
}; };
use ::bitcoin::hashes::core::fmt::Display; use ::bitcoin::hashes::core::fmt::Display;
use libp2p::PeerId; use libp2p::PeerId;
@ -101,10 +101,6 @@ impl From<Alice> for AliceState {
bob_peer_id, bob_peer_id,
} => AliceState::Started { } => AliceState::Started {
bob_peer_id, bob_peer_id,
amounts: SwapAmounts {
btc: state3.btc,
xmr: state3.xmr,
},
state3: Box::new(state3), state3: Box::new(state3),
}, },
Alice::BtcLocked { Alice::BtcLocked {
@ -112,10 +108,6 @@ impl From<Alice> for AliceState {
bob_peer_id, bob_peer_id,
} => AliceState::BtcLocked { } => AliceState::BtcLocked {
bob_peer_id, bob_peer_id,
amounts: SwapAmounts {
btc: state3.btc,
xmr: state3.xmr,
},
state3: Box::new(state3), state3: Box::new(state3),
}, },
Alice::XmrLocked(state3) => AliceState::XmrLocked { Alice::XmrLocked(state3) => AliceState::XmrLocked {

View file

@ -1,9 +1,6 @@
//! Run an XMR/BTC swap in the role of Alice. //! Run an XMR/BTC swap in the role of Alice.
//! Alice holds XMR and wishes receive BTC. //! Alice holds XMR and wishes receive BTC.
use crate::{ use crate::{bitcoin, database, database::Database, execution_params::ExecutionParams, monero};
bitcoin, database, database::Database, execution_params::ExecutionParams, monero,
protocol::SwapAmounts,
};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
use std::sync::Arc; use std::sync::Arc;
@ -57,7 +54,6 @@ pub struct Builder {
enum InitParams { enum InitParams {
None, None,
New { New {
swap_amounts: SwapAmounts,
bob_peer_id: PeerId, bob_peer_id: PeerId,
state3: Box<State3>, state3: Box<State3>,
}, },
@ -88,15 +84,9 @@ impl Builder {
} }
} }
pub fn with_init_params( pub fn with_init_params(self, bob_peer_id: PeerId, state3: State3) -> Self {
self,
swap_amounts: SwapAmounts,
bob_peer_id: PeerId,
state3: State3,
) -> Self {
Self { Self {
init_params: InitParams::New { init_params: InitParams::New {
swap_amounts,
bob_peer_id, bob_peer_id,
state3: Box::new(state3), state3: Box::new(state3),
}, },
@ -107,12 +97,10 @@ impl Builder {
pub async fn build(self) -> Result<Swap> { pub async fn build(self) -> Result<Swap> {
match self.init_params { match self.init_params {
InitParams::New { InitParams::New {
swap_amounts,
bob_peer_id, bob_peer_id,
ref state3, ref state3,
} => { } => {
let initial_state = AliceState::Started { let initial_state = AliceState::Started {
amounts: swap_amounts,
state3: state3.clone(), state3: state3.clone(),
bob_peer_id, bob_peer_id,
}; };

View file

@ -10,17 +10,16 @@ use crate::{
AliceState, Behaviour, Builder, OutEvent, QuoteResponse, State0, State3, TransferProof, AliceState, Behaviour, Builder, OutEvent, QuoteResponse, State0, State3, TransferProof,
}, },
bob::{EncryptedSignature, QuoteRequest}, bob::{EncryptedSignature, QuoteRequest},
SwapAmounts,
}, },
seed::Seed, seed::Seed,
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{Context, Result};
use futures::future::RemoteHandle; use futures::future::RemoteHandle;
use libp2p::{ use libp2p::{
core::Multiaddr, futures::FutureExt, request_response::ResponseChannel, PeerId, Swarm, core::Multiaddr, futures::FutureExt, request_response::ResponseChannel, PeerId, Swarm,
}; };
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::{collections::HashMap, sync::Arc}; use std::sync::Arc;
use tokio::sync::{broadcast, mpsc}; use tokio::sync::{broadcast, mpsc};
use tracing::{debug, error, trace, warn}; use tracing::{debug, error, trace, warn};
use uuid::Uuid; use uuid::Uuid;
@ -89,10 +88,6 @@ pub struct EventLoop {
db: Arc<Database>, db: Arc<Database>,
listen_address: Multiaddr, 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<PeerId, SwapAmounts>,
recv_encrypted_signature: broadcast::Sender<EncryptedSignature>, recv_encrypted_signature: broadcast::Sender<EncryptedSignature>,
send_transfer_proof: mpsc::Receiver<(PeerId, TransferProof)>, send_transfer_proof: mpsc::Receiver<(PeerId, TransferProof)>,
@ -137,7 +132,6 @@ impl EventLoop {
monero_wallet, monero_wallet,
db, db,
listen_address, listen_address,
swap_amounts: Default::default(),
recv_encrypted_signature: recv_encrypted_signature.sender, recv_encrypted_signature: recv_encrypted_signature.sender,
send_transfer_proof: send_transfer_proof.receiver, send_transfer_proof: send_transfer_proof.receiver,
send_transfer_proof_sender: send_transfer_proof.sender, send_transfer_proof_sender: send_transfer_proof.sender,
@ -225,12 +219,6 @@ impl EventLoop {
) )
.await?; .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); self.swarm.start_execution_setup(bob_peer_id, state0);
// Continues once the execution setup protocol is done // Continues once the execution setup protocol is done
Ok(()) Ok(())
@ -244,13 +232,6 @@ impl EventLoop {
let swap_id = Uuid::new_v4(); let swap_id = Uuid::new_v4();
let handle = self.new_handle(); 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( let swap = Builder::new(
self.peer_id, self.peer_id,
self.execution_params, self.execution_params,
@ -261,7 +242,7 @@ impl EventLoop {
self.listen_address.clone(), self.listen_address.clone(),
handle, handle,
) )
.with_init_params(swap_amounts, bob_peer_id, state3) .with_init_params(bob_peer_id, state3)
.build() .build()
.await?; .await?;

View file

@ -10,7 +10,6 @@ use crate::{
protocol::{ protocol::{
alice::{Message1, Message3, TransferProof}, alice::{Message1, Message3, TransferProof},
bob::{EncryptedSignature, Message0, Message2, Message4}, bob::{EncryptedSignature, Message0, Message2, Message4},
SwapAmounts,
}, },
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
@ -25,12 +24,10 @@ use std::fmt;
pub enum AliceState { pub enum AliceState {
Started { Started {
bob_peer_id: PeerId, bob_peer_id: PeerId,
amounts: SwapAmounts,
state3: Box<State3>, state3: Box<State3>,
}, },
BtcLocked { BtcLocked {
bob_peer_id: PeerId, bob_peer_id: PeerId,
amounts: SwapAmounts,
state3: Box<State3>, state3: Box<State3>,
}, },
XmrLocked { XmrLocked {

View file

@ -12,7 +12,6 @@ use crate::{
protocol::{ protocol::{
alice, alice,
alice::{event_loop::EventLoopHandle, TransferProof}, alice::{event_loop::EventLoopHandle, TransferProof},
SwapAmounts,
}, },
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@ -55,7 +54,6 @@ where
pub async fn lock_xmr<W>( pub async fn lock_xmr<W>(
bob_peer_id: PeerId, bob_peer_id: PeerId,
amounts: SwapAmounts,
state3: alice::State3, state3: alice::State3,
event_loop_handle: &mut EventLoopHandle, event_loop_handle: &mut EventLoopHandle,
monero_wallet: Arc<W>, monero_wallet: Arc<W>,
@ -71,7 +69,7 @@ where
let public_view_key = state3.v.public(); let public_view_key = state3.v.public();
let (transfer_proof, _) = monero_wallet let (transfer_proof, _) = monero_wallet
.transfer(public_spend_key, public_view_key, amounts.xmr) .transfer(public_spend_key, public_view_key, state3.xmr)
.await?; .await?;
// TODO(Franck): Wait for Monero to be confirmed once // TODO(Franck): Wait for Monero to be confirmed once

View file

@ -94,7 +94,6 @@ async fn run_until_internal(
AliceState::Started { AliceState::Started {
state3, state3,
bob_peer_id, bob_peer_id,
amounts,
} => { } => {
let _ = wait_for_locked_bitcoin( let _ = wait_for_locked_bitcoin(
state3.tx_lock.txid(), state3.tx_lock.txid(),
@ -105,7 +104,6 @@ async fn run_until_internal(
let state = AliceState::BtcLocked { let state = AliceState::BtcLocked {
bob_peer_id, bob_peer_id,
amounts,
state3, state3,
}; };
@ -126,12 +124,10 @@ async fn run_until_internal(
} }
AliceState::BtcLocked { AliceState::BtcLocked {
bob_peer_id, bob_peer_id,
amounts,
state3, state3,
} => { } => {
lock_xmr( lock_xmr(
bob_peer_id, bob_peer_id,
amounts,
*state3.clone(), *state3.clone(),
&mut event_loop_handle, &mut event_loop_handle,
monero_wallet.clone(), monero_wallet.clone(),