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.
This commit is contained in:
Daniel Karzel 2021-04-29 11:02:26 +10:00
parent 095d67f946
commit 0c616c7437
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E
6 changed files with 24 additions and 62 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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