Improve state names and display

This commit is contained in:
Franck Royer 2020-12-22 15:49:30 +11:00
parent f0736d0906
commit a31db63e54
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 36 additions and 36 deletions

View File

@ -81,7 +81,7 @@ pub enum AliceState {
CancelTimelockExpired { CancelTimelockExpired {
state3: State3, state3: State3,
}, },
Punished, BtcPunished,
SafelyAborted, SafelyAborted,
} }
@ -90,16 +90,16 @@ impl fmt::Display for AliceState {
match self { match self {
AliceState::Started { .. } => write!(f, "started"), AliceState::Started { .. } => write!(f, "started"),
AliceState::Negotiated { .. } => write!(f, "negotiated"), AliceState::Negotiated { .. } => write!(f, "negotiated"),
AliceState::BtcLocked { .. } => write!(f, "btc_locked"), AliceState::BtcLocked { .. } => write!(f, "btc is locked"),
AliceState::XmrLocked { .. } => write!(f, "xmr_locked"), AliceState::XmrLocked { .. } => write!(f, "xmr is locked"),
AliceState::EncSigLearned { .. } => write!(f, "encsig_learned"), AliceState::EncSigLearned { .. } => write!(f, "encrypted signature is learned"),
AliceState::BtcRedeemed => write!(f, "btc_redeemed"), AliceState::BtcRedeemed => write!(f, "btc is redeemed"),
AliceState::BtcCancelled { .. } => write!(f, "btc_cancelled"), AliceState::BtcCancelled { .. } => write!(f, "btc is cancelled"),
AliceState::BtcRefunded { .. } => write!(f, "btc_refunded"), AliceState::BtcRefunded { .. } => write!(f, "btc is refunded"),
AliceState::Punished => write!(f, "punished"), AliceState::BtcPunished => write!(f, "btc is punished"),
AliceState::SafelyAborted => write!(f, "safely_aborted"), AliceState::SafelyAborted => write!(f, "safely aborted"),
AliceState::BtcPunishable { .. } => write!(f, "btc_punishable"), AliceState::BtcPunishable { .. } => write!(f, "btc is punishable"),
AliceState::XmrRefunded => write!(f, "xmr_refunded"), AliceState::XmrRefunded => write!(f, "xmr is refunded"),
AliceState::CancelTimelockExpired { .. } => write!(f, "cancel timelock is expired"), AliceState::CancelTimelockExpired { .. } => write!(f, "cancel timelock is expired"),
} }
} }
@ -126,7 +126,7 @@ impl From<&AliceState> for state::Alice {
AliceState::CancelTimelockExpired { state3 } => { AliceState::CancelTimelockExpired { state3 } => {
Alice::CancelTimelockExpired(state3.clone()) Alice::CancelTimelockExpired(state3.clone())
} }
AliceState::Punished => Alice::SwapComplete, AliceState::BtcPunished => Alice::SwapComplete,
AliceState::SafelyAborted => Alice::SwapComplete, AliceState::SafelyAborted => Alice::SwapComplete,
// TODO: Potentially add support to resume swaps that are not Negotiated // TODO: Potentially add support to resume swaps that are not Negotiated
AliceState::Started { .. } => { AliceState::Started { .. } => {
@ -242,7 +242,7 @@ pub fn is_complete(state: &AliceState) -> bool {
state, state,
AliceState::XmrRefunded AliceState::XmrRefunded
| AliceState::BtcRedeemed | AliceState::BtcRedeemed
| AliceState::Punished | AliceState::BtcPunished
| AliceState::SafelyAborted | AliceState::SafelyAborted
) )
} }
@ -620,7 +620,7 @@ pub async fn run_until(
match select(punish_tx_finalised, refund_tx_seen).await { match select(punish_tx_finalised, refund_tx_seen).await {
Either::Left(_) => { Either::Left(_) => {
let state = AliceState::Punished; let state = AliceState::BtcPunished;
let db_state = (&state).into(); let db_state = (&state).into();
db.insert_latest_state(swap_id, Swap::Alice(db_state)) db.insert_latest_state(swap_id, Swap::Alice(db_state))
.await?; .await?;
@ -664,7 +664,7 @@ pub async fn run_until(
} }
AliceState::XmrRefunded => Ok(AliceState::XmrRefunded), AliceState::XmrRefunded => Ok(AliceState::XmrRefunded),
AliceState::BtcRedeemed => Ok(AliceState::BtcRedeemed), AliceState::BtcRedeemed => Ok(AliceState::BtcRedeemed),
AliceState::Punished => Ok(AliceState::Punished), AliceState::BtcPunished => Ok(AliceState::BtcPunished),
AliceState::SafelyAborted => Ok(AliceState::SafelyAborted), AliceState::SafelyAborted => Ok(AliceState::SafelyAborted),
} }
} }

View File

@ -29,10 +29,10 @@ pub enum BobState {
EncSigSent(bob::State4), EncSigSent(bob::State4),
BtcRedeemed(bob::State5), BtcRedeemed(bob::State5),
CancelTimelockExpired(bob::State4), CancelTimelockExpired(bob::State4),
Cancelled(bob::State4), BtcCancelled(bob::State4),
BtcRefunded(bob::State4), BtcRefunded(bob::State4),
XmrRedeemed, XmrRedeemed,
Punished, BtcPunished,
SafelyAborted, SafelyAborted,
} }
@ -41,16 +41,16 @@ impl fmt::Display for BobState {
match self { match self {
BobState::Started { .. } => write!(f, "started"), BobState::Started { .. } => write!(f, "started"),
BobState::Negotiated(..) => write!(f, "negotiated"), BobState::Negotiated(..) => write!(f, "negotiated"),
BobState::BtcLocked(..) => write!(f, "btc_locked"), BobState::BtcLocked(..) => write!(f, "btc is locked"),
BobState::XmrLocked(..) => write!(f, "xmr_locked"), BobState::XmrLocked(..) => write!(f, "xmr is locked"),
BobState::EncSigSent(..) => write!(f, "encsig_sent"), BobState::EncSigSent(..) => write!(f, "encrypted signature is sent"),
BobState::BtcRedeemed(..) => write!(f, "btc_redeemed"), BobState::BtcRedeemed(..) => write!(f, "btc is redeemed"),
BobState::CancelTimelockExpired(..) => write!(f, "cancel_timelock_expired"), BobState::CancelTimelockExpired(..) => write!(f, "cancel timelock is expired"),
BobState::Cancelled(..) => write!(f, "cancelled"), BobState::BtcCancelled(..) => write!(f, "btc is cancelled"),
BobState::BtcRefunded(..) => write!(f, "btc_refunded"), BobState::BtcRefunded(..) => write!(f, "btc is refunded"),
BobState::XmrRedeemed => write!(f, "xmr_redeemed"), BobState::XmrRedeemed => write!(f, "xmr is redeemed"),
BobState::Punished => write!(f, "punished"), BobState::BtcPunished => write!(f, "btc is punished"),
BobState::SafelyAborted => write!(f, "safely_aborted"), BobState::SafelyAborted => write!(f, "safely aborted"),
} }
} }
} }
@ -68,10 +68,10 @@ impl From<BobState> for state::Bob {
BobState::EncSigSent(state4) => Bob::EncSigSent { state4 }, BobState::EncSigSent(state4) => Bob::EncSigSent { state4 },
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5), BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4), BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
BobState::Cancelled(state4) => Bob::BtcCancelled(state4), BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
BobState::BtcRefunded(_) BobState::BtcRefunded(_)
| BobState::XmrRedeemed | BobState::XmrRedeemed
| BobState::Punished | BobState::BtcPunished
| BobState::SafelyAborted => Bob::SwapComplete, | BobState::SafelyAborted => Bob::SwapComplete,
} }
} }
@ -89,7 +89,7 @@ impl TryFrom<state::Swap> for BobState {
Bob::EncSigSent { state4 } => BobState::EncSigSent(state4), Bob::EncSigSent { state4 } => BobState::EncSigSent(state4),
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4), Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4),
Bob::BtcCancelled(state4) => BobState::Cancelled(state4), Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4),
Bob::SwapComplete => BobState::SafelyAborted, Bob::SwapComplete => BobState::SafelyAborted,
}; };
@ -132,7 +132,7 @@ pub fn is_complete(state: &BobState) -> bool {
state, state,
BobState::BtcRefunded(..) BobState::BtcRefunded(..)
| BobState::XmrRedeemed | BobState::XmrRedeemed
| BobState::Punished | BobState::BtcPunished
| BobState::SafelyAborted | BobState::SafelyAborted
) )
} }
@ -381,7 +381,7 @@ where
state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?; state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
} }
let state = BobState::Cancelled(state4); let state = BobState::BtcCancelled(state4);
db.insert_latest_state(swap_id, state::Swap::Bob(state.clone().into())) db.insert_latest_state(swap_id, state::Swap::Bob(state.clone().into()))
.await?; .await?;
@ -397,7 +397,7 @@ where
) )
.await .await
} }
BobState::Cancelled(state) => { BobState::BtcCancelled(state) => {
// Bob has cancelled the swap // Bob has cancelled the swap
let state = match state.expired_timelock(bitcoin_wallet.as_ref()).await? { let state = match state.expired_timelock(bitcoin_wallet.as_ref()).await? {
ExpiredTimelocks::None => { ExpiredTimelocks::None => {
@ -407,7 +407,7 @@ where
state.refund_btc(bitcoin_wallet.as_ref()).await?; state.refund_btc(bitcoin_wallet.as_ref()).await?;
BobState::BtcRefunded(state) BobState::BtcRefunded(state)
} }
ExpiredTimelocks::Punish => BobState::Punished, ExpiredTimelocks::Punish => BobState::BtcPunished,
}; };
let db_state = state.clone().into(); let db_state = state.clone().into();
@ -426,7 +426,7 @@ where
.await .await
} }
BobState::BtcRefunded(state4) => Ok(BobState::BtcRefunded(state4)), BobState::BtcRefunded(state4) => Ok(BobState::BtcRefunded(state4)),
BobState::Punished => Ok(BobState::Punished), BobState::BtcPunished => Ok(BobState::BtcPunished),
BobState::SafelyAborted => Ok(BobState::SafelyAborted), BobState::SafelyAborted => Ok(BobState::SafelyAborted),
BobState::XmrRedeemed => Ok(BobState::XmrRedeemed), BobState::XmrRedeemed => Ok(BobState::XmrRedeemed),
} }

View File

@ -115,7 +115,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
Either::Right(_) => panic!("Bob event loop should not terminate."), Either::Right(_) => panic!("Bob event loop should not terminate."),
}; };
assert!(matches!(alice_state, AliceState::Punished)); assert!(matches!(alice_state, AliceState::BtcPunished));
let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state { let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state {
state3 state3
} else { } else {