Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ln-resource-mgr/src/htlc_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ impl ReputationParams {
/// Calculates the opportunity_cost of a htlc being held on our channel - allowing one [`reputation_period`]'s
/// grace period, then charging for every subsequent period.
pub(super) fn opportunity_cost(&self, fee_msat: u64, hold_time: Duration) -> u64 {
(hold_time.as_secs() / self.resolution_period.as_secs()).saturating_mul(fee_msat)
(0_f64.max(
(hold_time.as_secs_f64() - self.resolution_period.as_secs_f64())
/ self.resolution_period.as_secs_f64(),
) * (fee_msat as f64))
.round() as u64
}

/// Calculates the worst case reputation damage of a htlc, assuming it'll be held for its full expiry_delta.
Expand Down
20 changes: 14 additions & 6 deletions ln-resource-mgr/src/outgoing_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ mod tests {
// Less than resolution_period has zero cost.
assert_eq!(params.opportunity_cost(100, Duration::from_secs(10)), 0);

// Equal to resolution_period or within one period is equal to fee.
assert_eq!(params.opportunity_cost(100, Duration::from_secs(60)), 100);
assert_eq!(params.opportunity_cost(100, Duration::from_secs(65)), 100);
// Above resolution period it is gradually incremented.
assert_eq!(params.opportunity_cost(100, Duration::from_secs(61)), 2);
assert_eq!(params.opportunity_cost(100, Duration::from_secs(90)), 50);
assert_eq!(params.opportunity_cost(100, Duration::from_secs(120)), 100);

// Multiple periods above resolution_period charges multiples of fee.
assert_eq!(params.opportunity_cost(100, Duration::from_secs(600)), 1000);
assert_eq!(params.opportunity_cost(100, Duration::from_secs(600)), 900);
}

#[test]
Expand All @@ -164,7 +165,14 @@ mod tests {
slow_resolve,
AccountableSignal::Accountable,
true,
Ok(-2000),
Ok(-1000),
),
(
1000,
Duration::from_secs(90),
AccountableSignal::Accountable,
true,
Ok(500),
),
(
1000,
Expand All @@ -178,7 +186,7 @@ mod tests {
slow_resolve,
AccountableSignal::Accountable,
false,
Ok(-3000),
Ok(-2000),
),
(
1000,
Expand Down