Skip to content

Commit 04813f6

Browse files
authored
Merge pull request #1145 from doitian/fix-977-cch-btc-expiry-overflow
fix: handle overflow in BTC final TLC expiry delta calculation
2 parents 0f75cbb + dfd65e8 commit 04813f6

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use crate::time::{Duration, SystemTime, UNIX_EPOCH};
3333
pub const ACTION_RETRY_BASE_MILLIS: u64 = 1000; // 1 second initial delay
3434
pub const ACTION_RETRY_MAX_MILLIS: u64 = 600_000; // 10 minute max delay
3535

36+
/// Average time per Bitcoin block in milliseconds (10 minutes = 600 seconds = 600,000 ms).
37+
pub const BTC_BLOCK_TIME_MILLIS: u64 = 600_000;
38+
3639
fn calculate_retry_delay(retry_count: u32) -> Duration {
3740
// Exponential backoff starting from ACTION_RETRY_BASE_MILLIS, capped at ACTION_RETRY_MAX_MILLIS
3841
let max_shift = (ACTION_RETRY_MAX_MILLIS / ACTION_RETRY_BASE_MILLIS).ilog2();
@@ -560,7 +563,16 @@ impl<S: CchOrderStore> CchState<S> {
560563
.final_tlc_minimum_expiry_delta()
561564
.copied()
562565
.unwrap_or(0);
563-
let btc_final_cltv_millis = self.config.btc_final_tlc_expiry_delta_blocks * 600 * 1000;
566+
let btc_final_cltv_millis = self
567+
.config
568+
.btc_final_tlc_expiry_delta_blocks
569+
.checked_mul(BTC_BLOCK_TIME_MILLIS)
570+
.ok_or_else(|| {
571+
CchError::ConfigError(format!(
572+
"btc_final_tlc_expiry_delta_blocks ({}) is too large and causes overflow when converting to milliseconds",
573+
self.config.btc_final_tlc_expiry_delta_blocks
574+
))
575+
})?;
564576
if ckb_final_tlc_millis >= btc_final_cltv_millis / 2 {
565577
return Err(CchError::CKBInvoiceFinalTlcExpiryDeltaTooLarge);
566578
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub enum CchStoreError {
1414

1515
#[derive(Error, Debug)]
1616
pub enum CchError {
17+
#[error("Configuration error: {0}")]
18+
ConfigError(String),
1719
#[error("Store error: {0}")]
1820
StoreError(#[from] CchStoreError),
1921
#[error("Outgoing invoice expiry time is too short")]

0 commit comments

Comments
 (0)