mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
alice::SwapFactory
should be consumed once a swap is returned
This commit is contained in:
parent
9b32409b8d
commit
b21dc03ed0
@ -92,7 +92,7 @@ impl SwapFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn new_swap(&self, swap_amounts: SwapAmounts) -> Result<(Swap, EventLoop)> {
|
pub async fn new_swap(self, swap_amounts: SwapAmounts) -> Result<(Swap, EventLoop)> {
|
||||||
let initial_state = init_alice_state(
|
let initial_state = init_alice_state(
|
||||||
swap_amounts.btc,
|
swap_amounts.btc,
|
||||||
swap_amounts.xmr,
|
swap_amounts.xmr,
|
||||||
@ -123,9 +123,9 @@ impl SwapFactory {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn resume(&self) -> Result<(Swap, EventLoop)> {
|
pub async fn resume(self) -> Result<(Swap, EventLoop)> {
|
||||||
// reopen the existing database
|
// reopen the existing database
|
||||||
let db = Database::open(self.db_path.clone().as_path())?;
|
let db = Database::open(self.db_path.as_path())?;
|
||||||
|
|
||||||
let resume_state = if let database::Swap::Alice(state) = db.get_state(self.swap_id)? {
|
let resume_state = if let database::Swap::Alice(state) = db.get_state(self.swap_id)? {
|
||||||
state.into()
|
state.into()
|
||||||
|
@ -7,7 +7,7 @@ pub mod testutils;
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn happy_path() {
|
async fn happy_path() {
|
||||||
testutils::setup_test(|ctx| async move {
|
testutils::setup_test(|mut ctx| async move {
|
||||||
let alice_swap = ctx.new_swap_as_alice().await;
|
let alice_swap = ctx.new_swap_as_alice().await;
|
||||||
let bob_swap = ctx.new_swap_as_bob().await;
|
let bob_swap = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ pub mod testutils;
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||||
testutils::setup_test(|ctx| async move {
|
testutils::setup_test(|mut ctx| async move {
|
||||||
let alice_swap = ctx.new_swap_as_alice().await;
|
let alice_swap = ctx.new_swap_as_alice().await;
|
||||||
let bob_swap = ctx.new_swap_as_bob().await;
|
let bob_swap = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ pub mod testutils;
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
|
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
|
||||||
testutils::setup_test(|ctx| async move {
|
testutils::setup_test(|mut ctx| async move {
|
||||||
let alice_swap = ctx.new_swap_as_alice().await;
|
let alice_swap = ctx.new_swap_as_alice().await;
|
||||||
let bob_swap = ctx.new_swap_as_bob().await;
|
let bob_swap = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ pub mod testutils;
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
|
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
|
||||||
testutils::setup_test(|ctx| async move {
|
testutils::setup_test(|mut ctx| async move {
|
||||||
let alice_swap = ctx.new_swap_as_alice().await;
|
let alice_swap = ctx.new_swap_as_alice().await;
|
||||||
let bob_swap = ctx.new_swap_as_bob().await;
|
let bob_swap = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ pub mod testutils;
|
|||||||
/// the encsig and fail to refund or redeem. Alice punishes.
|
/// the encsig and fail to refund or redeem. Alice punishes.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
||||||
testutils::setup_test(|ctx| async move {
|
testutils::setup_test(|mut ctx| async move {
|
||||||
let alice_swap = ctx.new_swap_as_alice().await;
|
let alice_swap = ctx.new_swap_as_alice().await;
|
||||||
let bob_swap = ctx.new_swap_as_bob().await;
|
let bob_swap = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ pub mod testutils;
|
|||||||
/// then also refunds.
|
/// then also refunds.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
|
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
|
||||||
testutils::setup_test(|ctx| async move {
|
testutils::setup_test(|mut ctx| async move {
|
||||||
let alice_swap = ctx.new_swap_as_alice().await;
|
let alice_swap = ctx.new_swap_as_alice().await;
|
||||||
let bob_swap = ctx.new_swap_as_bob().await;
|
let bob_swap = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
@ -27,16 +27,22 @@ pub struct StartingBalances {
|
|||||||
|
|
||||||
pub struct TestContext {
|
pub struct TestContext {
|
||||||
swap_amounts: SwapAmounts,
|
swap_amounts: SwapAmounts,
|
||||||
alice_swap_factory: alice::SwapFactory,
|
|
||||||
|
alice_swap_factory: Option<alice::SwapFactory>,
|
||||||
alice_starting_balances: StartingBalances,
|
alice_starting_balances: StartingBalances,
|
||||||
|
alice_bitcoin_wallet: Arc<bitcoin::Wallet>,
|
||||||
|
alice_monero_wallet: Arc<monero::Wallet>,
|
||||||
|
|
||||||
bob_swap_factory: bob::SwapFactory,
|
bob_swap_factory: bob::SwapFactory,
|
||||||
bob_starting_balances: StartingBalances,
|
bob_starting_balances: StartingBalances,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestContext {
|
impl TestContext {
|
||||||
pub async fn new_swap_as_alice(&self) -> alice::Swap {
|
pub async fn new_swap_as_alice(&mut self) -> alice::Swap {
|
||||||
let (swap, mut event_loop) = self
|
let (swap, mut event_loop) = self
|
||||||
.alice_swap_factory
|
.alice_swap_factory
|
||||||
|
.take()
|
||||||
|
.unwrap()
|
||||||
.new_swap(self.swap_amounts)
|
.new_swap(self.swap_amounts)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -58,8 +64,14 @@ impl TestContext {
|
|||||||
swap
|
swap
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn recover_alice_from_db(&self) -> alice::Swap {
|
pub async fn recover_alice_from_db(&mut self) -> alice::Swap {
|
||||||
let (swap, mut event_loop) = self.alice_swap_factory.resume().await.unwrap();
|
let (swap, mut event_loop) = self
|
||||||
|
.alice_swap_factory
|
||||||
|
.take()
|
||||||
|
.unwrap()
|
||||||
|
.resume()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
tokio::spawn(async move { event_loop.run().await });
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
@ -77,13 +89,7 @@ impl TestContext {
|
|||||||
pub async fn assert_alice_redeemed(&self, state: AliceState) {
|
pub async fn assert_alice_redeemed(&self, state: AliceState) {
|
||||||
assert!(matches!(state, AliceState::BtcRedeemed));
|
assert!(matches!(state, AliceState::BtcRedeemed));
|
||||||
|
|
||||||
let btc_balance_after_swap = self
|
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
.alice_swap_factory
|
|
||||||
.bitcoin_wallet
|
|
||||||
.as_ref()
|
|
||||||
.balance()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
btc_balance_after_swap,
|
btc_balance_after_swap,
|
||||||
self.alice_starting_balances.btc + self.swap_amounts.btc
|
self.alice_starting_balances.btc + self.swap_amounts.btc
|
||||||
@ -91,8 +97,7 @@ impl TestContext {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let xmr_balance_after_swap = self
|
let xmr_balance_after_swap = self
|
||||||
.alice_swap_factory
|
.alice_monero_wallet
|
||||||
.monero_wallet
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.get_balance()
|
.get_balance()
|
||||||
.await
|
.await
|
||||||
@ -103,26 +108,18 @@ impl TestContext {
|
|||||||
pub async fn assert_alice_refunded(&self, state: AliceState) {
|
pub async fn assert_alice_refunded(&self, state: AliceState) {
|
||||||
assert!(matches!(state, AliceState::XmrRefunded));
|
assert!(matches!(state, AliceState::XmrRefunded));
|
||||||
|
|
||||||
let btc_balance_after_swap = self
|
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
.alice_swap_factory
|
|
||||||
.bitcoin_wallet
|
|
||||||
.as_ref()
|
|
||||||
.balance()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(btc_balance_after_swap, self.alice_starting_balances.btc);
|
assert_eq!(btc_balance_after_swap, self.alice_starting_balances.btc);
|
||||||
|
|
||||||
// Ensure that Alice's balance is refreshed as we use a newly created wallet
|
// Ensure that Alice's balance is refreshed as we use a newly created wallet
|
||||||
self.alice_swap_factory
|
self.alice_monero_wallet
|
||||||
.monero_wallet
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.inner
|
.inner
|
||||||
.refresh()
|
.refresh()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let xmr_balance_after_swap = self
|
let xmr_balance_after_swap = self
|
||||||
.alice_swap_factory
|
.alice_monero_wallet
|
||||||
.monero_wallet
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.get_balance()
|
.get_balance()
|
||||||
.await
|
.await
|
||||||
@ -133,13 +130,7 @@ impl TestContext {
|
|||||||
pub async fn assert_alice_punished(&self, state: AliceState) {
|
pub async fn assert_alice_punished(&self, state: AliceState) {
|
||||||
assert!(matches!(state, AliceState::BtcPunished));
|
assert!(matches!(state, AliceState::BtcPunished));
|
||||||
|
|
||||||
let btc_balance_after_swap = self
|
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
.alice_swap_factory
|
|
||||||
.bitcoin_wallet
|
|
||||||
.as_ref()
|
|
||||||
.balance()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
btc_balance_after_swap,
|
btc_balance_after_swap,
|
||||||
self.alice_starting_balances.btc + self.swap_amounts.btc
|
self.alice_starting_balances.btc + self.swap_amounts.btc
|
||||||
@ -147,8 +138,7 @@ impl TestContext {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let xmr_balance_after_swap = self
|
let xmr_balance_after_swap = self
|
||||||
.alice_swap_factory
|
.alice_monero_wallet
|
||||||
.monero_wallet
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.get_balance()
|
.get_balance()
|
||||||
.await
|
.await
|
||||||
@ -327,8 +317,8 @@ where
|
|||||||
Seed::random().unwrap(),
|
Seed::random().unwrap(),
|
||||||
config,
|
config,
|
||||||
Uuid::new_v4(),
|
Uuid::new_v4(),
|
||||||
alice_bitcoin_wallet,
|
alice_bitcoin_wallet.clone(),
|
||||||
alice_monero_wallet,
|
alice_monero_wallet.clone(),
|
||||||
tempdir().unwrap().path().to_path_buf(),
|
tempdir().unwrap().path().to_path_buf(),
|
||||||
listen_address,
|
listen_address,
|
||||||
)
|
)
|
||||||
@ -360,8 +350,10 @@ where
|
|||||||
|
|
||||||
let test = TestContext {
|
let test = TestContext {
|
||||||
swap_amounts,
|
swap_amounts,
|
||||||
alice_swap_factory,
|
alice_swap_factory: Some(alice_swap_factory),
|
||||||
alice_starting_balances,
|
alice_starting_balances,
|
||||||
|
alice_bitcoin_wallet,
|
||||||
|
alice_monero_wallet,
|
||||||
bob_swap_factory,
|
bob_swap_factory,
|
||||||
bob_starting_balances,
|
bob_starting_balances,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user