Move loading the state into the function

In the production code it is a weird indirection that we load the state and then pass in the state and the database.
In the tests we have one additional load by doing it inside the command, but loading from the db is not expensive.
pull/452/head
Daniel Karzel 3 years ago
parent 095d67f946
commit 0c616c7437
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E

@ -242,9 +242,7 @@ async fn main() -> Result<()> {
)
.await?;
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
let cancel =
bob::cancel(swap_id, resume_state, Arc::new(bitcoin_wallet), db, force).await?;
let cancel = bob::cancel(swap_id, Arc::new(bitcoin_wallet), db, force).await?;
match cancel {
Ok((txid, _)) => {
@ -279,9 +277,7 @@ async fn main() -> Result<()> {
)
.await?;
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
bob::refund(swap_id, resume_state, Arc::new(bitcoin_wallet), db, force).await??;
bob::refund(swap_id, Arc::new(bitcoin_wallet), db, force).await??;
}
};
Ok(())

@ -13,11 +13,12 @@ pub enum Error {
pub async fn cancel(
swap_id: Uuid,
state: BobState,
bitcoin_wallet: Arc<Wallet>,
db: Database,
force: bool,
) -> Result<Result<(Txid, BobState), Error>> {
let state = db.get_state(swap_id)?.try_into_bob()?.into();
let state6 = match state {
BobState::BtcLocked(state3) => state3.cancel(),
BobState::XmrLockProofReceived { state, .. } => state.cancel(),

@ -11,11 +11,12 @@ pub struct SwapNotCancelledYet(Uuid);
pub async fn refund(
swap_id: Uuid,
state: BobState,
bitcoin_wallet: Arc<Wallet>,
db: Database,
force: bool,
) -> Result<Result<BobState, SwapNotCancelledYet>> {
let state = db.get_state(swap_id)?.try_into_bob()?.into();
let state6 = if force {
match state {
BobState::BtcLocked(state3) => state3.cancel(),

@ -36,14 +36,8 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
// Bob manually cancels
bob_join_handle.abort();
let (_, state) = bob::cancel(
bob_swap.id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await??;
let (_, state) =
bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
assert!(matches!(state, BobState::BtcCancelled { .. }));
let (bob_swap, bob_join_handle) = ctx
@ -53,14 +47,8 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
// Bob manually refunds
bob_join_handle.abort();
let bob_state = bob::refund(
bob_swap.id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await??;
let bob_state =
bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
ctx.assert_bob_refunded(bob_state).await;

@ -25,16 +25,10 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob tries but fails to manually cancel
let result = bob::cancel(
bob_swap.id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await?
.err()
.unwrap();
let result = bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false)
.await?
.err()
.unwrap();
assert!(matches!(result, Error::CancelTimelockNotExpiredYet));
@ -44,16 +38,10 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob tries but fails to manually refund
bob::refund(
bob_swap.id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
false,
)
.await?
.err()
.unwrap();
bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false)
.await?
.err()
.unwrap();
let (bob_swap, _) = ctx
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)

@ -24,15 +24,9 @@ async fn given_bob_manually_forces_cancel_when_timelock_not_expired_errors() {
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob forces a cancel that will fail
let is_error = bob::cancel(
bob_swap.id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
true,
)
.await
.is_err();
let is_error = bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, true)
.await
.is_err();
assert!(is_error);
@ -42,15 +36,9 @@ async fn given_bob_manually_forces_cancel_when_timelock_not_expired_errors() {
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob forces a refund that will fail
let is_error = bob::refund(
bob_swap.id,
bob_swap.state,
bob_swap.bitcoin_wallet,
bob_swap.db,
true,
)
.await
.is_err();
let is_error = bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, true)
.await
.is_err();
assert!(is_error);
let (bob_swap, _) = ctx

Loading…
Cancel
Save