Skip to content

Commit e682ec2

Browse files
committed
f
- fix comment and remove unnecessary channel id - add sanity checks when creating channel
1 parent a99d724 commit e682ec2

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

lightning/src/ln/resource_manager.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ impl BucketResources {
244244
struct PendingHTLC {
245245
incoming_amount_msat: u64,
246246
fee: u64,
247-
outgoing_channel: u64,
248247
outgoing_accountable: bool,
249248
added_at_unix_seconds: u64,
250249
in_flight_risk: u64,
@@ -282,24 +281,30 @@ impl Channel {
282281
general_bucket_pct: u8, congestion_bucket_pct: u8, reputation_window: Duration,
283282
revenue_window_weeks: u8, revenue_week_avg: u8, timestamp_unix_secs: u64,
284283
) -> Result<Self, ()> {
285-
if max_accepted_htlcs > 483
286-
|| (max_htlc_value_in_flight_msat / 1000) >= TOTAL_BITCOIN_SUPPLY_SATOSHIS
287-
{
284+
let max_in_flight_sat = max_htlc_value_in_flight_msat / 1000;
285+
if max_accepted_htlcs > 483 || max_in_flight_sat >= TOTAL_BITCOIN_SUPPLY_SATOSHIS {
286+
return Err(());
287+
}
288+
289+
if max_accepted_htlcs < 12 || max_in_flight_sat < 1000 {
288290
return Err(());
289291
}
290292

291293
if general_bucket_pct + congestion_bucket_pct >= 100 {
292294
return Err(());
293295
}
294296

295-
let general_bucket_slots_allocated = max_accepted_htlcs * general_bucket_pct as u16 / 100;
297+
let general_bucket_slots_allocated =
298+
(max_accepted_htlcs as f64 * general_bucket_pct as f64 / 100.0).round() as u16;
296299
let general_bucket_liquidity_allocated =
297-
max_htlc_value_in_flight_msat * general_bucket_pct as u64 / 100;
300+
(max_htlc_value_in_flight_msat as f64 * general_bucket_pct as f64 / 100.0).round()
301+
as u64;
298302

299303
let congestion_bucket_slots_allocated =
300-
max_accepted_htlcs * congestion_bucket_pct as u16 / 100;
304+
(max_accepted_htlcs as f64 * congestion_bucket_pct as f64 / 100.0).round() as u16;
301305
let congestion_bucket_liquidity_allocated =
302-
max_htlc_value_in_flight_msat * congestion_bucket_pct as u64 / 100;
306+
(max_htlc_value_in_flight_msat as f64 * congestion_bucket_pct as f64 / 100.0).round()
307+
as u64;
303308

304309
let protected_bucket_slots_allocated =
305310
max_accepted_htlcs - general_bucket_slots_allocated - congestion_bucket_slots_allocated;
@@ -366,11 +371,11 @@ impl Channel {
366371
match self.last_congestion_misuse.entry(outgoing_scid) {
367372
Entry::Vacant(_) => Ok(false),
368373
Entry::Occupied(last_misuse) => {
369-
// If the last misuse of the congestion bucket was over more than the
370-
// revenue window, remote the entry.
371374
if at_timestamp < *last_misuse.get() {
372375
return Err(());
373376
}
377+
// If the last misuse of the congestion bucket was over more than two
378+
// weeks ago, remove the entry.
374379
const TWO_WEEKS: u64 = 2016 * 10 * 60;
375380
let since_last_misuse = at_timestamp - last_misuse.get();
376381
if since_last_misuse < TWO_WEEKS {
@@ -734,6 +739,10 @@ mod tests {
734739
(TOTAL_BITCOIN_SUPPLY_SATOSHIS * 1000 + 1, 483, 40, 20),
735740
// Invalid bucket percentages
736741
(100_000, 483, 70, 50),
742+
// Invalid max_accepted_htlcs (< 12)
743+
(100_000_000, 11, 40, 20),
744+
// Invalid max_htlc_value_in_flight_msat (< 1000 sats)
745+
(999_999, 100, 40, 20),
737746
];
738747

739748
for (max_inflight, max_htlcs, general_pct, congestion_pct) in cases {

0 commit comments

Comments
 (0)