alice::SwapFactory should be consumed once a swap is returned

This commit is contained in:
Franck Royer 2021-01-19 15:03:30 +11:00
parent 9b32409b8d
commit b21dc03ed0
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
8 changed files with 37 additions and 45 deletions

View File

@ -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(
swap_amounts.btc,
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
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)? {
state.into()

View File

@ -7,7 +7,7 @@ pub mod testutils;
#[tokio::test]
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 bob_swap = ctx.new_swap_as_bob().await;

View File

@ -4,7 +4,7 @@ pub mod testutils;
#[tokio::test]
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 bob_swap = ctx.new_swap_as_bob().await;

View File

@ -4,7 +4,7 @@ pub mod testutils;
#[tokio::test]
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 bob_swap = ctx.new_swap_as_bob().await;

View File

@ -7,7 +7,7 @@ pub mod testutils;
#[tokio::test]
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 bob_swap = ctx.new_swap_as_bob().await;

View File

@ -9,7 +9,7 @@ pub mod testutils;
/// the encsig and fail to refund or redeem. Alice punishes.
#[tokio::test]
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 bob_swap = ctx.new_swap_as_bob().await;

View File

@ -6,7 +6,7 @@ pub mod testutils;
/// then also refunds.
#[tokio::test]
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 bob_swap = ctx.new_swap_as_bob().await;

View File

@ -27,16 +27,22 @@ pub struct StartingBalances {
pub struct TestContext {
swap_amounts: SwapAmounts,
alice_swap_factory: alice::SwapFactory,
alice_swap_factory: Option<alice::SwapFactory>,
alice_starting_balances: StartingBalances,
alice_bitcoin_wallet: Arc<bitcoin::Wallet>,
alice_monero_wallet: Arc<monero::Wallet>,
bob_swap_factory: bob::SwapFactory,
bob_starting_balances: StartingBalances,
}
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
.alice_swap_factory
.take()
.unwrap()
.new_swap(self.swap_amounts)
.await
.unwrap();
@ -58,8 +64,14 @@ impl TestContext {
swap
}
pub async fn recover_alice_from_db(&self) -> alice::Swap {
let (swap, mut event_loop) = self.alice_swap_factory.resume().await.unwrap();
pub async fn recover_alice_from_db(&mut self) -> alice::Swap {
let (swap, mut event_loop) = self
.alice_swap_factory
.take()
.unwrap()
.resume()
.await
.unwrap();
tokio::spawn(async move { event_loop.run().await });
@ -77,13 +89,7 @@ impl TestContext {
pub async fn assert_alice_redeemed(&self, state: AliceState) {
assert!(matches!(state, AliceState::BtcRedeemed));
let btc_balance_after_swap = self
.alice_swap_factory
.bitcoin_wallet
.as_ref()
.balance()
.await
.unwrap();
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
assert_eq!(
btc_balance_after_swap,
self.alice_starting_balances.btc + self.swap_amounts.btc
@ -91,8 +97,7 @@ impl TestContext {
);
let xmr_balance_after_swap = self
.alice_swap_factory
.monero_wallet
.alice_monero_wallet
.as_ref()
.get_balance()
.await
@ -103,26 +108,18 @@ impl TestContext {
pub async fn assert_alice_refunded(&self, state: AliceState) {
assert!(matches!(state, AliceState::XmrRefunded));
let btc_balance_after_swap = self
.alice_swap_factory
.bitcoin_wallet
.as_ref()
.balance()
.await
.unwrap();
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
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
self.alice_swap_factory
.monero_wallet
self.alice_monero_wallet
.as_ref()
.inner
.refresh()
.await
.unwrap();
let xmr_balance_after_swap = self
.alice_swap_factory
.monero_wallet
.alice_monero_wallet
.as_ref()
.get_balance()
.await
@ -133,13 +130,7 @@ impl TestContext {
pub async fn assert_alice_punished(&self, state: AliceState) {
assert!(matches!(state, AliceState::BtcPunished));
let btc_balance_after_swap = self
.alice_swap_factory
.bitcoin_wallet
.as_ref()
.balance()
.await
.unwrap();
let btc_balance_after_swap = self.alice_bitcoin_wallet.as_ref().balance().await.unwrap();
assert_eq!(
btc_balance_after_swap,
self.alice_starting_balances.btc + self.swap_amounts.btc
@ -147,8 +138,7 @@ impl TestContext {
);
let xmr_balance_after_swap = self
.alice_swap_factory
.monero_wallet
.alice_monero_wallet
.as_ref()
.get_balance()
.await
@ -327,8 +317,8 @@ where
Seed::random().unwrap(),
config,
Uuid::new_v4(),
alice_bitcoin_wallet,
alice_monero_wallet,
alice_bitcoin_wallet.clone(),
alice_monero_wallet.clone(),
tempdir().unwrap().path().to_path_buf(),
listen_address,
)
@ -360,8 +350,10 @@ where
let test = TestContext {
swap_amounts,
alice_swap_factory,
alice_swap_factory: Some(alice_swap_factory),
alice_starting_balances,
alice_bitcoin_wallet,
alice_monero_wallet,
bob_swap_factory,
bob_starting_balances,
};