Browse Source

Simplify state transition code

The complexity of the code part was not worth the information we got out of it, so it was simplified.
Should hopefully make it a bit more readable and easier to understand.
upload-correct-windows-binary
Daniel Karzel 3 years ago
parent
commit
2eb135d155
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 129
      daemon/src/model/cfd.rs

129
daemon/src/model/cfd.rs

@ -428,41 +428,32 @@ impl Cfd {
let new_state = match event { let new_state = match event {
CfdStateChangeEvent::Monitor(event) => match event { CfdStateChangeEvent::Monitor(event) => match event {
monitor::Event::LockFinality(_) => match self.state.clone() { monitor::Event::LockFinality(_) => {
PendingOpen { dlc, .. } => CfdState::Open { if let PendingOpen { dlc, .. } = self.state.clone() {
CfdState::Open {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: SystemTime::now(), transition_timestamp: SystemTime::now(),
}, },
dlc, dlc,
},
OutgoingOrderRequest { .. } => unreachable!("taker-only state"),
IncomingOrderRequest { .. } | Accepted { .. } | ContractSetup { .. } => {
bail!("Did not expect lock finality yet: ignoring")
} }
Open { .. } | OpenCommitted { .. } | MustRefund { .. } => { } else {
bail!("State already assumes lock finality: ignoring") bail!(
"Cannot transition to Open because of unexpected state {}",
self.state
)
} }
Rejected { .. } | Refunded { .. } | SetupFailed { .. } => {
bail!("The cfd is already in final state {}", self.state)
} }
},
monitor::Event::CommitFinality(_) => { monitor::Event::CommitFinality(_) => {
let dlc = match self.state.clone() { let dlc = if let Open { dlc, .. } = self.state.clone() {
Open { dlc, .. } => dlc,
PendingOpen { dlc, .. } => {
tracing::debug!(%order_id, "Was waiting on lock finality, jumping ahead");
dlc dlc
} } else if let PendingOpen { dlc, .. } = self.state.clone() {
OutgoingOrderRequest { .. } => unreachable!("taker-only state"), tracing::debug!(%order_id, "Was in unexpected state {}, jumping ahead to OpenCommitted", self.state);
IncomingOrderRequest { .. } | Accepted { .. } | ContractSetup { .. } => { dlc
bail!("Did not expect commit finality yet: ignoring") } else {
} bail!(
OpenCommitted { .. } | MustRefund { .. } => { "Cannot transition to OpenCommitted because of unexpected state {}",
bail!("State already assumes commit finality: ignoring") self.state
} )
Rejected { .. } | Refunded { .. } | SetupFailed { .. } => {
bail!("The cfd is already in final state {}", self.state)
}
}; };
OpenCommitted { OpenCommitted {
@ -496,8 +487,8 @@ impl Cfd {
dlc, dlc,
cet_status: CetStatus::Ready(price), cet_status: CetStatus::Ready(price),
}, },
PendingOpen { dlc, .. } => { PendingOpen { dlc, .. } | Open { dlc, .. } => {
tracing::debug!(%order_id, "Was waiting on lock finality, jumping ahead"); tracing::debug!(%order_id, "Was in unexpected state {}, jumping ahead to MustRefund", self.state);
CfdState::OpenCommitted { CfdState::OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: SystemTime::now(), transition_timestamp: SystemTime::now(),
@ -506,56 +497,22 @@ impl Cfd {
cet_status: CetStatus::TimelockExpired, cet_status: CetStatus::TimelockExpired,
} }
} }
Open { dlc, .. } => { _ => bail!(
tracing::debug!(%order_id, "Was not aware of commit TX broadcast, jumping ahead"); "Cannot transition to OpenCommitted because of unexpected state {}",
CfdState::OpenCommitted { self.state
common: CfdStateCommon { ),
transition_timestamp: SystemTime::now(),
},
dlc,
cet_status: CetStatus::TimelockExpired,
}
}
OutgoingOrderRequest { .. } => unreachable!("taker-only state"),
IncomingOrderRequest { .. } | Accepted { .. } | ContractSetup { .. } => {
bail!("Did not expect CET timelock expiry yet: ignoring")
}
OpenCommitted {
cet_status: CetStatus::TimelockExpired,
..
}
| OpenCommitted {
cet_status: CetStatus::Ready(_),
..
} => bail!("State already assumes CET timelock expiry: ignoring"),
MustRefund { .. } => {
bail!("Refund path does not care about CET timelock expiry: ignoring")
}
Rejected { .. } | Refunded { .. } | SetupFailed { .. } => {
bail!("The cfd is already in final state {}", self.state)
}
}, },
monitor::Event::RefundTimelockExpired(_) => { monitor::Event::RefundTimelockExpired(_) => {
let dlc = match self.state.clone() { let dlc = if let OpenCommitted { dlc, .. } = self.state.clone() {
OpenCommitted { dlc, .. } => dlc,
MustRefund { .. } => {
bail!("State already assumes refund timelock expiry: ignoring")
}
OutgoingOrderRequest { .. } => unreachable!("taker-only state"),
IncomingOrderRequest { .. } | Accepted { .. } | ContractSetup { .. } => {
bail!("Did not expect refund timelock expiry yet: ignoring")
}
PendingOpen { dlc, .. } => {
tracing::debug!(%order_id, "Was waiting on lock finality, jumping ahead");
dlc dlc
} } else if let Open { dlc, .. } | PendingOpen { dlc, .. } = self.state.clone() {
Open { dlc, .. } => { tracing::debug!(%order_id, "Was in unexpected state {}, jumping ahead to MustRefund", self.state);
tracing::debug!(%order_id, "Was waiting on CET timelock expiry, jumping ahead");
dlc dlc
} } else {
Rejected { .. } | Refunded { .. } | SetupFailed { .. } => { bail!(
bail!("The cfd is already in final state {}", self.state) "Cannot transition to OpenCommitted because of unexpected state {}",
} self.state
)
}; };
MustRefund { MustRefund {
@ -566,24 +523,12 @@ impl Cfd {
} }
} }
monitor::Event::RefundFinality(_) => { monitor::Event::RefundFinality(_) => {
match self.state { if let MustRefund { .. } = self.state.clone() {
MustRefund { .. } => (), } else {
OutgoingOrderRequest { .. } => unreachable!("taker-only state"), tracing::debug!(
IncomingOrderRequest { .. } | Accepted { .. } | ContractSetup { .. } => { "Was in unexpected state {}, jumping ahead to Refunded",
bail!("Did not expect refund finality yet: ignoring") self.state
} );
PendingOpen { .. } => {
tracing::debug!(%order_id, "Was waiting on lock finality, jumping ahead");
}
Open { .. } => {
tracing::debug!(%order_id, "Was waiting on CET timelock expiry, jumping ahead");
}
OpenCommitted { .. } => {
tracing::debug!(%order_id, "Was waiting on refund timelock expiry, jumping ahead");
}
Rejected { .. } | Refunded { .. } | SetupFailed { .. } => {
bail!("The cfd is already in final state {}", self.state)
}
} }
Refunded { Refunded {

Loading…
Cancel
Save