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

View File

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