Make make_init_state and init_loop associated to alice::SwapFactory

This commit is contained in:
Franck Royer 2021-01-20 13:06:10 +11:00
parent c11042ff0d
commit 83b72c0a45
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -107,19 +107,11 @@ impl Builder {
pub async fn build(self) -> Result<(Swap, EventLoop)> { pub async fn build(self) -> Result<(Swap, EventLoop)> {
match self.init_params { match self.init_params {
InitParams::New { swap_amounts } => { InitParams::New { swap_amounts } => {
let initial_state = init_alice_state( let initial_state = self
swap_amounts.btc, .make_initial_state(swap_amounts.btc, swap_amounts.xmr)
swap_amounts.xmr, .await?;
self.bitcoin_wallet.clone(),
self.config,
)
.await?;
let (event_loop, event_loop_handle) = init_alice_event_loop( let (event_loop, event_loop_handle) = self.init_event_loop()?;
self.listen_address.clone(),
self.identity.clone(),
self.peer_id.clone(),
)?;
let db = Database::open(self.db_path.as_path())?; let db = Database::open(self.db_path.as_path())?;
@ -150,11 +142,7 @@ impl Builder {
) )
}; };
let (event_loop, event_loop_handle) = init_alice_event_loop( let (event_loop, event_loop_handle) = self.init_event_loop()?;
self.listen_address.clone(),
self.identity.clone(),
self.peer_id.clone(),
)?;
Ok(( Ok((
Swap { Swap {
@ -179,49 +167,49 @@ impl Builder {
pub fn listen_address(&self) -> Multiaddr { pub fn listen_address(&self) -> Multiaddr {
self.listen_address.clone() self.listen_address.clone()
} }
}
async fn init_alice_state( async fn make_initial_state(
btc_to_swap: bitcoin::Amount, &self,
xmr_to_swap: monero::Amount, btc_to_swap: bitcoin::Amount,
alice_btc_wallet: Arc<bitcoin::Wallet>, xmr_to_swap: monero::Amount,
config: Config, ) -> Result<AliceState> {
) -> Result<AliceState> { let rng = &mut OsRng;
let rng = &mut OsRng;
let amounts = SwapAmounts { let amounts = SwapAmounts {
btc: btc_to_swap, btc: btc_to_swap,
xmr: xmr_to_swap, xmr: xmr_to_swap,
}; };
let a = bitcoin::SecretKey::new_random(rng); let a = bitcoin::SecretKey::new_random(rng);
let s_a = cross_curve_dleq::Scalar::random(rng); let s_a = cross_curve_dleq::Scalar::random(rng);
let v_a = monero::PrivateViewKey::new_random(rng); let v_a = monero::PrivateViewKey::new_random(rng);
let redeem_address = alice_btc_wallet.as_ref().new_address().await?; let redeem_address = self.bitcoin_wallet.as_ref().new_address().await?;
let punish_address = redeem_address.clone(); let punish_address = redeem_address.clone();
let state0 = State0::new( let state0 = State0::new(
a, a,
s_a, s_a,
v_a, v_a,
amounts.btc, amounts.btc,
amounts.xmr, amounts.xmr,
config.bitcoin_cancel_timelock, self.config.bitcoin_cancel_timelock,
config.bitcoin_punish_timelock, self.config.bitcoin_punish_timelock,
redeem_address, redeem_address,
punish_address, punish_address,
); );
Ok(AliceState::Started { amounts, state0 }) Ok(AliceState::Started { amounts, state0 })
} }
fn init_alice_event_loop( fn init_event_loop(&self) -> Result<(EventLoop, EventLoopHandle)> {
listen: Multiaddr, let alice_behaviour = Behaviour::default();
identity: Keypair, let alice_transport = build(self.identity.clone())?;
peer_id: PeerId, EventLoop::new(
) -> Result<(EventLoop, EventLoopHandle)> { alice_transport,
let alice_behaviour = Behaviour::default(); alice_behaviour,
let alice_transport = build(identity)?; self.listen_address.clone(),
EventLoop::new(alice_transport, alice_behaviour, listen, peer_id) self.peer_id.clone(),
)
}
} }
#[derive(Debug)] #[derive(Debug)]