Skip to content

Commit 4810d95

Browse files
authored
Merge pull request #962 from jjyr/ensure-add-tlc-expiry-is-large-than-final_tlc_minimum_expiry_delta
Ensure add tlc expiry is large than final tlc minimum expiry delta
2 parents 05269e7 + 841cf7b commit 4810d95

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

crates/fiber-lib/src/fiber/channel.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ where
952952
Some(invoice_expiry) => u64::try_from(
953953
invoice_expiry
954954
.as_millis()
955-
.saturating_add(invoice.data.timestamp),
955+
.saturating_add(invoice.data.timestamp)
956+
.min(tlc.expiry.into()),
956957
)
957958
.unwrap_or(u64::MAX),
958959
None => tlc.expiry,
@@ -1098,6 +1099,11 @@ where
10981099
if !matches!(invoice_status, CkbInvoiceStatus::Open) {
10991100
return Err(ProcessingChannelError::FinalInvoiceInvalid(invoice_status));
11001101
}
1102+
1103+
// ensure tlc expiry is large than the now + final_tlc_minimum_expiry_delta
1104+
if invoice.is_tlc_expire_too_soon(add_tlc.expiry) {
1105+
return Err(ProcessingChannelError::IncorrectFinalTlcExpiry);
1106+
}
11011107
}
11021108

11031109
let Some(tlc) = state.tlc_state.get_mut(&add_tlc.tlc_id) else {

crates/fiber-lib/src/invoice/invoice_impl.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub struct CkbScript(#[serde_as(as = "EntityHex")] pub Script);
113113
#[serde(rename_all = "snake_case")]
114114
pub enum Attribute {
115115
#[serde(with = "U64Hex")]
116-
/// The final tlc minimum expiry delta, in milliseconds, default is 1 day
116+
/// The final tlc minimum expiry delta, in milliseconds, default is 160 minutes
117117
FinalHtlcMinimumExpiryDelta(u64),
118118
#[serde(with = "duration_hex")]
119119
/// The expiry time of the invoice, in seconds
@@ -297,6 +297,19 @@ impl CkbInvoice {
297297
})
298298
}
299299

300+
pub fn is_tlc_expire_too_soon(&self, tlc_expiry: u64) -> bool {
301+
let now = UNIX_EPOCH
302+
.elapsed()
303+
.expect("Duration since unix epoch")
304+
.as_millis();
305+
let required_expiry = now
306+
+ (self
307+
.final_tlc_minimum_expiry_delta()
308+
.cloned()
309+
.unwrap_or_default() as u128);
310+
(tlc_expiry as u128) < required_expiry
311+
}
312+
300313
/// Check that the invoice is signed correctly and that key recovery works
301314
pub fn check_signature(&self) -> Result<(), InvoiceError> {
302315
if self.signature.is_none() {

crates/fiber-lib/src/rpc/invoice.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,22 @@ where
411411
invoice_builder = invoice_builder.payment_secret(payment_secret.into());
412412
}
413413
};
414-
if let Some(final_expiry_delta) = params.final_expiry_delta {
415-
if final_expiry_delta < MIN_TLC_EXPIRY_DELTA {
416-
return error(&format!(
417-
"final_expiry_delta must be greater than or equal to {}",
418-
MIN_TLC_EXPIRY_DELTA
419-
));
420-
}
421-
if final_expiry_delta > MAX_PAYMENT_TLC_EXPIRY_LIMIT {
422-
return error(&format!(
423-
"final_expiry_delta must be less than or equal to {}",
424-
MAX_PAYMENT_TLC_EXPIRY_LIMIT
425-
));
426-
}
427-
invoice_builder = invoice_builder.final_expiry_delta(final_expiry_delta);
428-
};
414+
415+
let final_expiry_delta = params.final_expiry_delta.unwrap_or(MIN_TLC_EXPIRY_DELTA);
416+
if final_expiry_delta < MIN_TLC_EXPIRY_DELTA {
417+
return error(&format!(
418+
"final_expiry_delta must be greater than or equal to {}",
419+
MIN_TLC_EXPIRY_DELTA
420+
));
421+
}
422+
if final_expiry_delta > MAX_PAYMENT_TLC_EXPIRY_LIMIT {
423+
return error(&format!(
424+
"final_expiry_delta must be less than or equal to {}",
425+
MAX_PAYMENT_TLC_EXPIRY_LIMIT
426+
));
427+
}
428+
invoice_builder = invoice_builder.final_expiry_delta(final_expiry_delta);
429+
429430
if let Some(udt_type_script) = &params.udt_type_script {
430431
invoice_builder = invoice_builder.udt_type_script(udt_type_script.clone().into());
431432
};

0 commit comments

Comments
 (0)