Skip to content

Commit a44401f

Browse files
committed
refactor cch order db operations
- Make cch order db operations synchronous - Added helper methods `get_order_or_none `and `get_active_order_or_none` to CchState to reduce code duplication and centralize the common pattern of handling NotFound errors vs other errors. This also eliminates redundant final status checks.
1 parent 1be7e48 commit a44401f

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

crates/fiber-lib/src/cch/actor.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ impl Actor for CchActor {
196196
let result = state
197197
.orders_db
198198
.get_cch_order(&payment_hash)
199-
.await
200199
.map_err(Into::into);
201200
if !port.is_closed() {
202201
// ignore error
@@ -227,10 +226,9 @@ impl Actor for CchActor {
227226
action,
228227
retry_count,
229228
} => {
230-
let order = match state.orders_db.get_cch_order(&payment_hash).await {
231-
Err(CchDbError::NotFound(_)) => return Ok(()),
232-
Err(err) => return Err(err.into()),
233-
Ok(order) => order,
229+
let order = match state.get_active_order_or_none(&payment_hash)? {
230+
None => return Ok(()),
231+
Some(order) => order,
234232
};
235233
if let Err(err) = ActionDispatcher::execute(state, &myself, &order, action).await {
236234
let delay = calculate_retry_delay(retry_count);
@@ -255,11 +253,7 @@ impl Actor for CchActor {
255253
}
256254
#[cfg(test)]
257255
CchMessage::InsertOrder(order, port) => {
258-
let result = state
259-
.orders_db
260-
.insert_cch_order(order)
261-
.await
262-
.map_err(Into::into);
256+
let result = state.orders_db.insert_cch_order(order).map_err(Into::into);
263257
if !port.is_closed() {
264258
let _ = port.send(result);
265259
}
@@ -270,6 +264,26 @@ impl Actor for CchActor {
270264
}
271265

272266
impl CchState {
267+
/// Get a CCH order by payment hash, returning None if not found.
268+
/// This handles the common pattern of checking for NotFound vs other errors.
269+
fn get_order_or_none(&mut self, payment_hash: &Hash256) -> Result<Option<CchOrder>, CchError> {
270+
match self.orders_db.get_cch_order(payment_hash) {
271+
Err(CchDbError::NotFound(_)) => Ok(None),
272+
Err(err) => Err(err.into()),
273+
Ok(order) => Ok(Some(order)),
274+
}
275+
}
276+
277+
/// Get a CCH order by payment hash, returning None if not found or the order status is final.
278+
fn get_active_order_or_none(
279+
&mut self,
280+
payment_hash: &Hash256,
281+
) -> Result<Option<CchOrder>, CchError> {
282+
Ok(self
283+
.get_order_or_none(payment_hash)?
284+
.filter(|order| !order.is_final()))
285+
}
286+
273287
async fn send_btc(&mut self, send_btc: SendBTC) -> Result<CchOrder, CchError> {
274288
let duration_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?;
275289

@@ -345,7 +359,7 @@ impl CchState {
345359
wrapped_btc_type_script,
346360
};
347361

348-
self.orders_db.insert_cch_order(order.clone()).await?;
362+
self.orders_db.insert_cch_order(order.clone())?;
349363
Ok(order)
350364
}
351365

@@ -410,36 +424,26 @@ impl CchState {
410424
wrapped_btc_type_script,
411425
};
412426

413-
self.orders_db.insert_cch_order(order.clone()).await?;
427+
self.orders_db.insert_cch_order(order.clone())?;
414428
Ok(order)
415429
}
416430

417431
async fn handle_tracking_event(
418432
&mut self,
419433
event: CchTrackingEvent,
420434
) -> Result<Vec<CchOrderAction>> {
421-
let order = match self.orders_db.get_cch_order(event.payment_hash()).await {
422-
Err(CchDbError::NotFound(_)) => return Ok(vec![]),
423-
Err(err) => return Err(err.into()),
424-
Ok(order) => order,
435+
let order = match self.get_active_order_or_none(event.payment_hash())? {
436+
None => return Ok(vec![]),
437+
Some(order) => order,
425438
};
426-
if order.is_final() {
427-
tracing::debug!(
428-
"order {} is {:?}, skipping tracking event {:?}",
429-
order.payment_hash,
430-
order.status,
431-
event
432-
);
433-
return Ok(vec![]);
434-
}
435439

436440
let CchOrderTransition {
437441
order,
438442
dirty,
439443
actions,
440444
} = CchOrderStateMachine::apply(order, event.into())?;
441445
if dirty {
442-
self.orders_db.update_cch_order(order.clone()).await?;
446+
self.orders_db.update_cch_order(order.clone())?;
443447
}
444448
Ok(actions)
445449
}

crates/fiber-lib/src/cch/order/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ pub struct CchOrdersDb {
2222
}
2323

2424
impl CchOrdersDb {
25-
pub async fn insert_cch_order(&mut self, order: CchOrder) -> Result<(), CchDbError> {
25+
pub fn insert_cch_order(&mut self, order: CchOrder) -> Result<(), CchDbError> {
2626
let key = order.payment_hash;
2727
match self.orders.insert(key, order) {
2828
Some(_) => Err(CchDbError::Duplicated(key.to_string())),
2929
None => Ok(()),
3030
}
3131
}
3232

33-
pub async fn get_cch_order(&mut self, payment_hash: &Hash256) -> Result<CchOrder, CchDbError> {
33+
pub fn get_cch_order(&mut self, payment_hash: &Hash256) -> Result<CchOrder, CchDbError> {
3434
self.orders
3535
.get(payment_hash)
3636
.ok_or_else(|| CchDbError::NotFound(payment_hash.to_string()))
3737
.cloned()
3838
}
3939

40-
pub async fn update_cch_order(&mut self, order: CchOrder) -> Result<(), CchDbError> {
40+
pub fn update_cch_order(&mut self, order: CchOrder) -> Result<(), CchDbError> {
4141
let key = order.payment_hash;
4242
match self.orders.insert(key, order) {
4343
Some(_) => Ok(()),

0 commit comments

Comments
 (0)