forked from comit-network/xmr-btc-swap
-
Notifications
You must be signed in to change notification settings - Fork 46
test: Integration tests for Bob and Alice swaps #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codebestia
wants to merge
6
commits into
eigenwallet:master
Choose a base branch
from
codebestia:feat/integration-test-for-alice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0cdf17
test: (integration) alice locks wrong xmr amount
codebestia b9e3e2d
test: (integration) correct xmr amount bob redeems
codebestia 1919f16
test: (integration) alice locks significantly wrong amount
codebestia 545f211
chore: format test file
codebestia f9ad9d5
chore: update test suite
codebestia 2189f17
test: improve test and update test workflow
codebestia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| pub mod harness; | ||
|
|
||
| use swap::asb::FixedRate; | ||
| use swap::protocol::alice::AliceState; | ||
| use swap::protocol::bob::BobState; | ||
| use swap::protocol::{alice, bob}; | ||
| use swap_core::monero::Amount; | ||
|
|
||
| use crate::harness::SlowCancelConfig; | ||
|
|
||
| #[tokio::test] | ||
| async fn given_alice_locks_wrong_xmr_amount_bob_rejects() { | ||
| harness::setup_test(SlowCancelConfig, None, |mut ctx| async move { | ||
| // Run Bob until he finds a swap candidate | ||
| let (bob_swap, bob_join_handle) = ctx.bob_swap().await; | ||
| let bob_swap_id = bob_swap.id; | ||
| let bob_swap = tokio::spawn(bob::run_until(bob_swap, |s| { | ||
| matches!(s, BobState::XmrLockTransactionCandidate { .. }) | ||
| })); | ||
|
|
||
| // Run until Alice detects the Bitcoin as having been locked | ||
| let alice_swap = ctx.alice_next_swap().await; | ||
|
|
||
| let alice_state = alice::run_until( | ||
| alice_swap, | ||
| |s| matches!(s, AliceState::BtcLocked { .. }), | ||
| FixedRate::default(), | ||
| ) | ||
| .await?; | ||
|
|
||
|
|
||
| // Resume Alice such that she locks the wrong amount of Monero | ||
| ctx.restart_alice().await; | ||
| let mut alice_swap = ctx.alice_next_swap().await; | ||
|
|
||
| // Modify Alice such that she locks the wrong amount of Monero | ||
| alice_swap.state = alice_state.lower_by_one_piconero(); | ||
| let alice_swap = tokio::spawn(alice::run(alice_swap, FixedRate::default())); | ||
|
|
||
| // Assert that Bob recognises Alice as a swap candidate | ||
| let bob_state = bob_swap.await??; | ||
| assert!(matches!( | ||
| bob_state, | ||
| BobState::XmrLockTransactionCandidate { .. } | ||
| )); | ||
|
|
||
| // Resume Bob and wait run until Bob detects the problem | ||
| let (bob_swap, bob_join_handle) = ctx | ||
| .stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id) | ||
| .await; | ||
| let bob_state = | ||
| bob::run_until(bob_swap, | ||
| |s| matches!(s, BobState::WaitingForCancelTimelockExpiration { .. }) | ||
| ).await?; | ||
|
|
||
| // Assert that Bob detects the problem | ||
| assert!(matches!( | ||
| bob_state, | ||
| BobState::WaitingForCancelTimelockExpiration { .. } | ||
| )); | ||
|
|
||
| // Resume Bob and wait till completion | ||
| let (bob_swap, bob_join_handle) = ctx | ||
| .stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id) | ||
| .await; | ||
| let bob_state = | ||
| bob::run_until(bob_swap, |s| matches!(s, BobState::BtcRefunded(..))).await?; | ||
|
|
||
| // Assert that both Bob and Alice refunded | ||
| ctx.assert_bob_refunded(bob_state).await; | ||
| let alice_state = alice_swap.await??; | ||
| ctx.assert_alice_refunded(alice_state).await; | ||
|
|
||
|
|
||
| Ok(()) | ||
| }) | ||
| .await; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn given_significantly_wrong_xmr_amount_bob_immediately_aborts() { | ||
| harness::setup_test(SlowCancelConfig, None, |mut ctx| async move { | ||
| // Run Bob until he finds a swap candidate | ||
| let (bob_swap, bob_join_handle) = ctx.bob_swap().await; | ||
| let bob_swap_id = bob_swap.id; | ||
| let bob_swap = tokio::spawn(bob::run_until(bob_swap, |s| { | ||
| matches!(s, BobState::XmrLockTransactionCandidate { .. }) | ||
| })); | ||
|
|
||
| // Run until Alice detects the Bitcoin as having been locked | ||
| let alice_swap = ctx.alice_next_swap().await; | ||
|
|
||
| let alice_state = alice::run_until( | ||
| alice_swap, | ||
| |s| matches!(s, AliceState::BtcLocked { .. }), | ||
| FixedRate::default(), | ||
| ) | ||
| .await?; | ||
|
|
||
|
|
||
| // Resume Alice such that she locks half the amount | ||
| ctx.restart_alice().await; | ||
| let mut alice_swap = ctx.alice_next_swap().await; | ||
|
|
||
| // Modify Alice such that she locks half the amount | ||
| alice_swap.state = alice_state.lower_by_one_piconero(); | ||
| let alice_swap = tokio::spawn(alice::run(alice_swap, FixedRate::default())); | ||
|
|
||
| // Assert that Bob recognises Alice as a swap candidate | ||
| let bob_state = bob_swap.await??; | ||
| assert!(matches!( | ||
| bob_state, | ||
| BobState::XmrLockTransactionCandidate { .. } | ||
| )); | ||
|
|
||
| // Resume Bob and wait run until Bob detects the problem | ||
| let (bob_swap, bob_join_handle) = ctx | ||
| .stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id) | ||
| .await; | ||
| let bob_state = | ||
| bob::run_until(bob_swap, | ||
| |s| matches!(s, BobState::WaitingForCancelTimelockExpiration { .. }) | ||
| ).await?; | ||
|
|
||
| // Assert that Bob detects the problem | ||
| assert!(matches!( | ||
| bob_state, | ||
| BobState::WaitingForCancelTimelockExpiration { .. } | ||
| )); | ||
|
|
||
| // Resume Bob and wait till completion | ||
| let (bob_swap, bob_join_handle) = ctx | ||
| .stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id) | ||
| .await; | ||
| let bob_state = | ||
| bob::run_until(bob_swap, |s| matches!(s, BobState::BtcRefunded(..))).await?; | ||
|
|
||
| // Assert that both Bob and Alice refunded | ||
| ctx.assert_bob_refunded(bob_state).await; | ||
| let alice_state = alice_swap.await??; | ||
| ctx.assert_alice_refunded(alice_state).await; | ||
|
|
||
|
|
||
| Ok(()) | ||
| }) | ||
| .await; | ||
| } | ||
|
|
||
| trait FakeModifyMoneroAmount { | ||
| /// Reduces the Monero amount by one piconero | ||
| fn lower_by_one_piconero(self) -> Self; | ||
| // Halves the Monero amount | ||
| fn halve_xmr_amount(self) -> Self; | ||
| } | ||
|
|
||
| impl FakeModifyMoneroAmount for AliceState { | ||
| fn lower_by_one_piconero(self) -> Self { | ||
| let AliceState::BtcLocked { mut state3 } = self else { | ||
| panic!("Expected BtcLocked state to be able to modify the Monero amount"); | ||
| }; | ||
|
|
||
| let one_piconero = Amount::from_piconero(1); | ||
|
|
||
| state3.xmr = state3.xmr.checked_sub(one_piconero).unwrap(); | ||
|
|
||
| Self::BtcLocked { state3 } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add an empty line below |
||
|
|
||
| fn halve_xmr_amount(self) -> Self { | ||
| let AliceState::BtcLocked { mut state3 } = self else { | ||
| panic!("Expected BtcLocked state to be able to modify the Monero amount"); | ||
| }; | ||
| state3.xmr = Amount::from_piconero(state3.xmr.as_piconero() / 2); | ||
| Self::BtcLocked { state3 } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here: We need to ensure that Bob goes into
XmrLockTransactionCandidateand then intoWaitingForCancelTimelockExpiration.