Skip to content

Commit

Permalink
Tests: Adapt to new progress report
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Nov 8, 2022
1 parent 42811b2 commit 619defd
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 270 deletions.
16 changes: 8 additions & 8 deletions src/bus/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ impl ToYamlString for OfferStatusPair {}
serde(crate = "serde_crate")
)]
pub enum Outcome {
#[display("Success(Swapped)")]
Buy,
#[display("Failure(Refunded)")]
Refund,
#[display("Failure(Punished)")]
Punish,
#[display("Failure(Aborted)")]
Abort,
#[display("Success Swap")]
SuccessSwap,
#[display("Failure Refund")]
FailureRefund,
#[display("Failure Punish")]
FailurePunish,
#[display("Failure Abort")]
FailureAbort,
}

#[derive(Clone, Debug, Display, NetworkEncode, NetworkDecode)]
Expand Down
4 changes: 2 additions & 2 deletions src/databased/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ fn test_lmdb_state() {
assert_eq!(offer_1, offers_retrieved[0].offer);

database
.set_offer_status(&offer_1, &OfferStatus::Ended(Outcome::Buy))
.set_offer_status(&offer_1, &OfferStatus::Ended(Outcome::SuccessSwap))
.unwrap();
let offers_retrieved = database.get_offers(OfferStatusSelector::Ended).unwrap();
assert_eq!(offer_1, offers_retrieved[0].offer);
Expand All @@ -776,7 +776,7 @@ fn test_lmdb_state() {
let offers_retrieved = database.get_offers(OfferStatusSelector::All).unwrap();
let status_1 = OfferStatusPair {
offer: offer_1,
status: OfferStatus::Ended(Outcome::Buy),
status: OfferStatus::Ended(Outcome::SuccessSwap),
};
let status_2 = OfferStatusPair {
offer: offer_2,
Expand Down
8 changes: 4 additions & 4 deletions src/farcasterd/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ pub struct Stats {
impl Stats {
pub fn incr_outcome(&mut self, outcome: &Outcome) {
match outcome {
Outcome::Buy => self.success += 1,
Outcome::Refund => self.refund += 1,
Outcome::Punish => self.punish += 1,
Outcome::Abort => self.abort += 1,
Outcome::SuccessSwap => self.success += 1,
Outcome::FailureRefund => self.refund += 1,
Outcome::FailurePunish => self.punish += 1,
Outcome::FailureAbort => self.abort += 1,
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/farcasterd/trade_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,16 +1171,16 @@ fn attempt_transition_to_end(
runtime.clean_up_after_swap(&swap_id, event.endpoints)?;
runtime.stats.incr_outcome(&outcome);
match outcome {
Outcome::Buy => {
Outcome::SuccessSwap => {
debug!("Success on swap {}", swap_id);
}
Outcome::Refund => {
Outcome::FailureRefund => {
warn!("Refund on swap {}", swap_id);
}
Outcome::Punish => {
Outcome::FailurePunish => {
warn!("Punish on swap {}", swap_id);
}
Outcome::Abort => {
Outcome::FailureAbort => {
warn!("Aborted swap {}", swap_id);
}
}
Expand Down
42 changes: 24 additions & 18 deletions src/swapd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ impl Runtime {
|| (self.state.a_refundsig() && !self.state.a_btc_locked()) =>
{
// just cancel the swap, no additional logic required
self.state_update(State::Alice(AliceState::FinishA(Outcome::Abort)))?;
self.state_update(State::Alice(AliceState::FinishA(Outcome::FailureAbort)))?;
self.abort_swap(endpoints)?;
if let ServiceId::Client(_) = source {
self.send_ctl(
Expand All @@ -1488,7 +1488,7 @@ impl Runtime {

CtlMsg::AbortSwap if self.state.b_start() => {
// just cancel the swap, no additional logic required, since funding was not yet retrieved
self.state_update(State::Bob(BobState::FinishB(Outcome::Abort)))?;
self.state_update(State::Bob(BobState::FinishB(Outcome::FailureAbort)))?;
self.abort_swap(endpoints)?;
if let ServiceId::Client(_) = source {
self.send_ctl(
Expand All @@ -1513,7 +1513,7 @@ impl Runtime {
)),
)?;
// cancel the swap to invalidate its state
self.state_update(State::Bob(BobState::FinishB(Outcome::Abort)))?;
self.state_update(State::Bob(BobState::FinishB(Outcome::FailureAbort)))?;
if let ServiceId::Client(_) = source {
self.send_ctl(
endpoints,
Expand Down Expand Up @@ -1991,11 +1991,13 @@ impl Runtime {
BusMsg::Sync(SyncMsg::Task(abort_all)),
)?;
let success = if self.state.b_buy_sig() {
self.state_update(State::Bob(BobState::FinishB(Outcome::Buy)))?;
Some(Outcome::Buy)
self.state_update(State::Bob(BobState::FinishB(Outcome::SuccessSwap)))?;
Some(Outcome::SuccessSwap)
} else if self.state.a_refund_seen() {
self.state_update(State::Alice(AliceState::FinishA(Outcome::Refund)))?;
Some(Outcome::Refund)
self.state_update(State::Alice(AliceState::FinishA(
Outcome::FailureRefund,
)))?;
Some(Outcome::FailureRefund)
} else {
error!("Unexpected sweeping state, not sending finalization commands to wallet and farcasterd");
None
Expand Down Expand Up @@ -2380,7 +2382,7 @@ impl Runtime {
self.syncer_state.awaiting_funding = false;
}
self.state_update(State::Alice(AliceState::FinishA(
Outcome::Refund,
Outcome::FailureRefund,
)))?;
let abort_all = Task::Abort(Abort {
task_target: TaskTarget::AllTasks,
Expand All @@ -2399,7 +2401,7 @@ impl Runtime {
BusMsg::Sync(SyncMsg::Task(abort_all)),
)?;
let swap_success_req =
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::Refund));
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::FailureRefund));
self.send_wallet(
ServiceBus::Ctl,
endpoints,
Expand All @@ -2418,7 +2420,9 @@ impl Runtime {
{
// FIXME: swap ends here for alice
// wallet + farcaster
self.state_update(State::Alice(AliceState::FinishA(Outcome::Buy)))?;
self.state_update(State::Alice(AliceState::FinishA(
Outcome::SuccessSwap,
)))?;
let abort_all = Task::Abort(Abort {
task_target: TaskTarget::AllTasks,
respond: Boolean::False,
Expand All @@ -2436,7 +2440,7 @@ impl Runtime {
BusMsg::Sync(SyncMsg::Task(abort_all)),
)?;
let swap_success_req =
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::Buy));
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::SuccessSwap));
self.send_wallet(
ServiceBus::Ctl,
endpoints,
Expand Down Expand Up @@ -2496,9 +2500,11 @@ impl Runtime {
self.syncer_state.bitcoin_syncer(),
BusMsg::Sync(SyncMsg::Task(abort_all)),
)?;
self.state_update(State::Bob(BobState::FinishB(Outcome::Refund)))?;
self.state_update(State::Bob(BobState::FinishB(
Outcome::FailureRefund,
)))?;
let swap_success_req =
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::Refund));
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::FailureRefund));
self.send_ctl(
endpoints,
ServiceId::Wallet,
Expand Down Expand Up @@ -2530,17 +2536,17 @@ impl Runtime {
)?;
match self.state.swap_role() {
SwapRole::Alice => self.state_update(State::Alice(
AliceState::FinishA(Outcome::Punish),
AliceState::FinishA(Outcome::FailurePunish),
))?,
SwapRole::Bob => {
warn!("{}", "You were punished!".err());
self.state_update(State::Bob(BobState::FinishB(
Outcome::Punish,
Outcome::FailurePunish,
)))?
}
}
let swap_success_req =
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::Punish));
BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::FailurePunish));
self.send_ctl(
endpoints,
ServiceId::Wallet,
Expand Down Expand Up @@ -2589,7 +2595,7 @@ impl Runtime {
&& self.syncer_state.tasks.sweeping_addr.is_some()
&& &self.syncer_state.tasks.sweeping_addr.unwrap() == id =>
{
self.state_update(State::Bob(BobState::FinishB(Outcome::Abort)))?;
self.state_update(State::Bob(BobState::FinishB(Outcome::FailureAbort)))?;
endpoints.send_to(
ServiceBus::Ctl,
self.identity(),
Expand Down Expand Up @@ -2790,7 +2796,7 @@ impl Runtime {
}

fn abort_swap(&mut self, endpoints: &mut Endpoints) -> Result<(), Error> {
let swap_success_req = BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::Abort));
let swap_success_req = BusMsg::Ctl(CtlMsg::SwapOutcome(Outcome::FailureAbort));
self.send_ctl(endpoints, ServiceId::Wallet, swap_success_req.clone())?;
self.send_ctl(endpoints, ServiceId::Farcasterd, swap_success_req)?;
info!("{} | Aborted swap.", self.swap_id.swap_id());
Expand Down
16 changes: 13 additions & 3 deletions src/swapd/state_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ use super::{
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
#[display(Debug)]
pub enum StateReport {
#[display("StartA")]
StartA,
#[display("CommitA")]
CommitA,
#[display("RevealA")]
RevealA,
#[display(Debug)]
RefundSigA {
arb_block_height: u64,
acc_block_height: u64,
Expand All @@ -37,11 +40,16 @@ pub enum StateReport {
blocks_until_punish_possible: Option<i64>,
blocks_until_safe_buy: Option<u32>,
},
#[display("{0}")]
FinishA(Outcome),
#[display("StartB")]
StartB,
#[display("CommitB")]
CommitB,
#[display("RevealB")]
RevealB,
CorearbB {
#[display(Debug)]
CoreArbB {
arb_block_height: u64,
acc_block_height: u64,
arb_locked: bool,
Expand All @@ -55,6 +63,7 @@ pub enum StateReport {
blocks_until_refund: Option<i64>,
blocks_until_punish_possible: Option<i64>,
},
#[display(Debug)]
BuySigB {
arb_block_height: u64,
acc_block_height: u64,
Expand All @@ -67,6 +76,7 @@ pub enum StateReport {
blocks_until_punish_possible: Option<i64>,
blocks_until_safe_monero_buy_sweep: Option<u32>,
},
#[display("{0}")]
FinishB(Outcome),
}

Expand Down Expand Up @@ -114,7 +124,7 @@ impl StateReport {
State::Bob(BobState::StartB { .. }) => StateReport::StartB,
State::Bob(BobState::CommitB { .. }) => StateReport::CommitB,
State::Bob(BobState::RevealB { .. }) => StateReport::RevealB,
State::Bob(BobState::CorearbB { buy_tx_seen, .. }) => StateReport::CorearbB {
State::Bob(BobState::CorearbB { buy_tx_seen, .. }) => StateReport::CoreArbB {
buy_published: *buy_tx_seen,
arb_block_height: syncer_state.bitcoin_height,
acc_block_height: syncer_state.monero_height,
Expand Down
2 changes: 1 addition & 1 deletion src/swapd/swap_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl State {
matches!(self, State::Bob(BobState::BuySigB { .. }))
}
pub fn b_outcome_abort(&self) -> bool {
matches!(self, State::Bob(BobState::FinishB(Outcome::Abort)))
matches!(self, State::Bob(BobState::FinishB(Outcome::FailureAbort)))
}
pub fn remote_commit(&self) -> Option<&Commit> {
match self {
Expand Down
2 changes: 1 addition & 1 deletion src/walletd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ impl Runtime {
CtlMsg::SwapOutcome(success) => {
let swap_id = get_swap_id(&source)?;
let success = match success {
Outcome::Buy => success.bright_green_bold(),
Outcome::SuccessSwap => success.bright_green_bold(),
_ => success.err(),
};
info!(
Expand Down
Loading

0 comments on commit 619defd

Please sign in to comment.