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},
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<Alice> 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<Alice> 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 {

View File

@ -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<State3>,
},
@ -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<Swap> {
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,
};

View File

@ -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<Database>,
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>,
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?;

View File

@ -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<State3>,
},
BtcLocked {
bob_peer_id: PeerId,
amounts: SwapAmounts,
state3: Box<State3>,
},
XmrLocked {

View File

@ -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<W>(
bob_peer_id: PeerId,
amounts: SwapAmounts,
state3: alice::State3,
event_loop_handle: &mut EventLoopHandle,
monero_wallet: Arc<W>,
@ -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

View File

@ -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(),