mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Cleanup swap execution to not return EventLoopHandle, refactor both_refund test
This commit is contained in:
parent
3692046758
commit
91d4d5da25
@ -125,12 +125,10 @@ impl From<&AliceState> for state::Alice {
|
|||||||
AliceState::BtcRefunded { .. } => Alice::SwapComplete,
|
AliceState::BtcRefunded { .. } => Alice::SwapComplete,
|
||||||
AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()),
|
AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()),
|
||||||
AliceState::XmrRefunded => Alice::SwapComplete,
|
AliceState::XmrRefunded => Alice::SwapComplete,
|
||||||
// TODO(Franck): it may be more efficient to store the fact that we already want to
|
|
||||||
// abort
|
|
||||||
AliceState::Cancelling { state3 } => Alice::Cancelling(state3.clone()),
|
AliceState::Cancelling { state3 } => Alice::Cancelling(state3.clone()),
|
||||||
AliceState::Punished => Alice::SwapComplete,
|
AliceState::Punished => Alice::SwapComplete,
|
||||||
AliceState::SafelyAborted => Alice::SwapComplete,
|
AliceState::SafelyAborted => Alice::SwapComplete,
|
||||||
// todo: we may want to swap recovering from swaps that have not been negotiated
|
// TODO: Potentially add support to recover swaps that are not Negotiated
|
||||||
AliceState::Started { .. } => {
|
AliceState::Started { .. } => {
|
||||||
panic!("Alice attempted to save swap before being negotiated")
|
panic!("Alice attempted to save swap before being negotiated")
|
||||||
}
|
}
|
||||||
@ -223,7 +221,7 @@ pub async fn swap(
|
|||||||
config: Config,
|
config: Config,
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
db: Database,
|
db: Database,
|
||||||
) -> Result<(AliceState, EventLoopHandle)> {
|
) -> Result<AliceState> {
|
||||||
run_until(
|
run_until(
|
||||||
state,
|
state,
|
||||||
is_complete,
|
is_complete,
|
||||||
@ -247,7 +245,7 @@ pub async fn recover(
|
|||||||
) -> Result<AliceState> {
|
) -> Result<AliceState> {
|
||||||
let db_swap = db.get_state(swap_id)?;
|
let db_swap = db.get_state(swap_id)?;
|
||||||
let start_state = AliceState::try_from(db_swap)?;
|
let start_state = AliceState::try_from(db_swap)?;
|
||||||
let (state, _) = swap(
|
let state = swap(
|
||||||
start_state,
|
start_state,
|
||||||
event_loop_handle,
|
event_loop_handle,
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
@ -297,10 +295,10 @@ pub async fn run_until(
|
|||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
db: Database,
|
db: Database,
|
||||||
// TODO: Remove EventLoopHandle!
|
// TODO: Remove EventLoopHandle!
|
||||||
) -> Result<(AliceState, EventLoopHandle)> {
|
) -> Result<AliceState> {
|
||||||
info!("Current state:{}", state);
|
info!("Current state:{}", state);
|
||||||
if is_target_state(&state) {
|
if is_target_state(&state) {
|
||||||
Ok((state, event_loop_handle))
|
Ok(state)
|
||||||
} else {
|
} else {
|
||||||
match state {
|
match state {
|
||||||
AliceState::Started { amounts, state0 } => {
|
AliceState::Started { amounts, state0 } => {
|
||||||
@ -607,7 +605,7 @@ pub async fn run_until(
|
|||||||
let db_state = (&state).into();
|
let db_state = (&state).into();
|
||||||
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
||||||
.await?;
|
.await?;
|
||||||
Ok((state, event_loop_handle))
|
Ok(state)
|
||||||
}
|
}
|
||||||
AliceState::BtcPunishable { tx_refund, state3 } => {
|
AliceState::BtcPunishable { tx_refund, state3 } => {
|
||||||
let signed_tx_punish = build_bitcoin_punish_transaction(
|
let signed_tx_punish = build_bitcoin_punish_transaction(
|
||||||
@ -675,10 +673,10 @@ pub async fn run_until(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AliceState::XmrRefunded => Ok((AliceState::XmrRefunded, event_loop_handle)),
|
AliceState::XmrRefunded => Ok(AliceState::XmrRefunded),
|
||||||
AliceState::BtcRedeemed => Ok((AliceState::BtcRedeemed, event_loop_handle)),
|
AliceState::BtcRedeemed => Ok(AliceState::BtcRedeemed),
|
||||||
AliceState::Punished => Ok((AliceState::Punished, event_loop_handle)),
|
AliceState::Punished => Ok(AliceState::Punished),
|
||||||
AliceState::SafelyAborted => Ok((AliceState::SafelyAborted, event_loop_handle)),
|
AliceState::SafelyAborted => Ok(AliceState::SafelyAborted),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
|||||||
|
|
||||||
let alice_swap_id = Uuid::new_v4();
|
let alice_swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
let (alice_state, _) = alice::swap::run_until(
|
let alice_state = alice::swap::run_until(
|
||||||
start_state,
|
start_state,
|
||||||
alice::swap::is_encsig_learned,
|
alice::swap::is_encsig_learned,
|
||||||
alice_event_loop_handle,
|
alice_event_loop_handle,
|
||||||
@ -209,7 +209,7 @@ async fn given_alice_restarts_after_xmr_is_locked_refund_swap() {
|
|||||||
|
|
||||||
let alice_swap_id = Uuid::new_v4();
|
let alice_swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
let (alice_state, _) = alice::swap::run_until(
|
let alice_state = alice::swap::run_until(
|
||||||
start_state,
|
start_state,
|
||||||
alice::swap::is_xmr_locked,
|
alice::swap::is_xmr_locked,
|
||||||
alice_event_loop_handle,
|
alice_event_loop_handle,
|
||||||
|
@ -99,7 +99,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
|
|||||||
let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await });
|
let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await });
|
||||||
|
|
||||||
// Wait until alice has locked xmr and bob has locked btc
|
// Wait until alice has locked xmr and bob has locked btc
|
||||||
let ((alice_state, _), bob_state) = try_join(alice_fut, bob_btc_locked_fut).await.unwrap();
|
let (alice_state, bob_state) = try_join(alice_fut, bob_btc_locked_fut).await.unwrap();
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::Punished));
|
assert!(matches!(alice_state, AliceState::Punished));
|
||||||
let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state {
|
let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state {
|
||||||
|
@ -42,8 +42,8 @@ async fn both_refund() {
|
|||||||
|
|
||||||
let (
|
let (
|
||||||
alice_state,
|
alice_state,
|
||||||
mut alice_swarm_driver,
|
mut alice_event_loop,
|
||||||
alice_swarm_handle,
|
alice_event_loop_handle,
|
||||||
alice_btc_wallet,
|
alice_btc_wallet,
|
||||||
alice_xmr_wallet,
|
alice_xmr_wallet,
|
||||||
) = init_alice(
|
) = init_alice(
|
||||||
@ -57,9 +57,9 @@ async fn both_refund() {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let (bob_state, bob_swarm_driver, bob_swarm_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
||||||
init_bob(
|
init_bob(
|
||||||
alice_multiaddr,
|
alice_multiaddr.clone(),
|
||||||
&bitcoind,
|
&bitcoind,
|
||||||
&monero,
|
&monero,
|
||||||
btc_to_swap,
|
btc_to_swap,
|
||||||
@ -71,7 +71,7 @@ async fn both_refund() {
|
|||||||
|
|
||||||
let bob_fut = bob::swap::swap(
|
let bob_fut = bob::swap::swap(
|
||||||
bob_state,
|
bob_state,
|
||||||
bob_swarm_handle,
|
bob_event_loop_handle,
|
||||||
bob_db,
|
bob_db,
|
||||||
bob_btc_wallet.clone(),
|
bob_btc_wallet.clone(),
|
||||||
bob_xmr_wallet.clone(),
|
bob_xmr_wallet.clone(),
|
||||||
@ -79,7 +79,7 @@ async fn both_refund() {
|
|||||||
Uuid::new_v4(),
|
Uuid::new_v4(),
|
||||||
);
|
);
|
||||||
|
|
||||||
tokio::spawn(async move { bob_swarm_driver.run().await });
|
tokio::spawn(async move { bob_event_loop.run().await });
|
||||||
|
|
||||||
let alice_swap_id = Uuid::new_v4();
|
let alice_swap_id = Uuid::new_v4();
|
||||||
let alice_db_datadir = tempdir().unwrap();
|
let alice_db_datadir = tempdir().unwrap();
|
||||||
@ -88,7 +88,7 @@ async fn both_refund() {
|
|||||||
let alice_xmr_locked_fut = alice::swap::run_until(
|
let alice_xmr_locked_fut = alice::swap::run_until(
|
||||||
alice_state,
|
alice_state,
|
||||||
alice::swap::is_xmr_locked,
|
alice::swap::is_xmr_locked,
|
||||||
alice_swarm_handle,
|
alice_event_loop_handle,
|
||||||
alice_btc_wallet.clone(),
|
alice_btc_wallet.clone(),
|
||||||
alice_xmr_wallet.clone(),
|
alice_xmr_wallet.clone(),
|
||||||
Config::regtest(),
|
Config::regtest(),
|
||||||
@ -96,11 +96,10 @@ async fn both_refund() {
|
|||||||
alice_db,
|
alice_db,
|
||||||
);
|
);
|
||||||
|
|
||||||
tokio::spawn(async move { alice_swarm_driver.run().await });
|
tokio::spawn(async move { alice_event_loop.run().await });
|
||||||
|
|
||||||
// Wait until alice has locked xmr and bob has locked btc
|
// Wait until alice has locked xmr and bob has locked btc
|
||||||
let (bob_state, (alice_state, alice_swarm_handle)) =
|
let (bob_state, alice_state) = try_join(bob_fut, alice_xmr_locked_fut).await.unwrap();
|
||||||
try_join(bob_fut, alice_xmr_locked_fut).await.unwrap();
|
|
||||||
|
|
||||||
let bob_state4 = if let BobState::BtcRefunded(state4) = bob_state {
|
let bob_state4 = if let BobState::BtcRefunded(state4) = bob_state {
|
||||||
state4
|
state4
|
||||||
@ -109,10 +108,12 @@ async fn both_refund() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
||||||
|
let (mut alice_event_loop, alice_event_loop_handle) =
|
||||||
|
testutils::init_alice_eventloop(alice_multiaddr);
|
||||||
|
|
||||||
let (alice_state, _) = alice::swap::swap(
|
let alice_state = alice::swap::swap(
|
||||||
alice_state,
|
alice_state,
|
||||||
alice_swarm_handle,
|
alice_event_loop_handle,
|
||||||
alice_btc_wallet.clone(),
|
alice_btc_wallet.clone(),
|
||||||
alice_xmr_wallet.clone(),
|
alice_xmr_wallet.clone(),
|
||||||
Config::regtest(),
|
Config::regtest(),
|
||||||
@ -121,6 +122,7 @@ async fn both_refund() {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
tokio::spawn(async move { alice_event_loop.run().await });
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::XmrRefunded));
|
assert!(matches!(alice_state, AliceState::XmrRefunded));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user