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,
self.bitcoin_wallet.clone(),
self.config,
)
.await?; .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,13 +167,11 @@ 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(
&self,
btc_to_swap: bitcoin::Amount, btc_to_swap: bitcoin::Amount,
xmr_to_swap: monero::Amount, xmr_to_swap: monero::Amount,
alice_btc_wallet: Arc<bitcoin::Wallet>,
config: Config,
) -> Result<AliceState> { ) -> Result<AliceState> {
let rng = &mut OsRng; let rng = &mut OsRng;
@ -197,7 +183,7 @@ async fn init_alice_state(
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,
@ -205,8 +191,8 @@ async fn init_alice_state(
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,
); );
@ -214,14 +200,16 @@ async fn init_alice_state(
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,
identity: Keypair,
peer_id: PeerId,
) -> Result<(EventLoop, EventLoopHandle)> {
let alice_behaviour = Behaviour::default(); let alice_behaviour = Behaviour::default();
let alice_transport = build(identity)?; let alice_transport = build(self.identity.clone())?;
EventLoop::new(alice_transport, alice_behaviour, listen, peer_id) EventLoop::new(
alice_transport,
alice_behaviour,
self.listen_address.clone(),
self.peer_id.clone(),
)
}
} }
#[derive(Debug)] #[derive(Debug)]