Skip to content

Commit 246ecec

Browse files
committed
Revert "f Expose overflow errors"
This reverts commit 865d340.
1 parent 865d340 commit 246ecec

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

lightning/src/util/anchor_channel_reserves.rs

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::chain::chainmonitor::ChainMonitor;
2525
use crate::chain::chainmonitor::Persist;
2626
use crate::chain::Filter;
2727
use crate::events::bump_transaction::Utxo;
28-
use crate::ln::chan_utils::MAX_HTLCS;
2928
use crate::ln::channelmanager::AChannelManager;
3029
use crate::prelude::new_hash_set;
3130
use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -34,7 +33,6 @@ use bitcoin::constants::WITNESS_SCALE_FACTOR;
3433
use bitcoin::Amount;
3534
use bitcoin::FeeRate;
3635
use bitcoin::Weight;
37-
use core::cmp::min;
3836
use core::ops::Deref;
3937

4038
// Transaction weights based on:
@@ -152,15 +150,6 @@ pub struct AnchorChannelReserveContext {
152150
pub taproot_wallet: bool,
153151
}
154152

155-
/// Errors around anchor channel reserve calculations.
156-
#[derive(Debug, Clone, PartialEq, Eq)]
157-
pub enum AnchorChannelReserveError {
158-
/// An overflow occurred in a fee calculation, caused by an excessive fee rate or weight.
159-
FeeOverflow,
160-
/// An overflow occurred calculating a total amount of reserves.
161-
AmountOverflow,
162-
}
163-
164153
/// A default for the [AnchorChannelReserveContext] parameters is provided as follows:
165154
/// - The upper bound fee rate is set to the 99th percentile of the median block fee rate since 2019:
166155
/// ~50 sats/vbyte.
@@ -182,31 +171,28 @@ impl Default for AnchorChannelReserveContext {
182171
///
183172
/// This reserve currently needs to be allocated as a disjoint set of UTXOs per channel,
184173
/// as claims are not yet aggregated across channels.
185-
pub fn get_reserve_per_channel(
186-
context: &AnchorChannelReserveContext,
187-
) -> Result<Amount, AnchorChannelReserveError> {
188-
let num_htlcs = min(context.expected_accepted_htlcs, MAX_HTLCS) as u64;
174+
pub fn get_reserve_per_channel(context: &AnchorChannelReserveContext) -> Amount {
189175
let weight = Weight::from_wu(
190176
COMMITMENT_TRANSACTION_BASE_WEIGHT +
191177
// Reserves are calculated in terms of accepted HTLCs, as their timeout defines the urgency of
192178
// on-chain resolution. Each accepted HTLC is assumed to be forwarded to calculate an upper
193179
// bound for the reserve, resulting in `expected_accepted_htlcs` inbound HTLCs and
194180
// `expected_accepted_htlcs` outbound HTLCs per channel in aggregate.
195-
2 * num_htlcs * COMMITMENT_TRANSACTION_PER_HTLC_WEIGHT +
181+
2 * (context.expected_accepted_htlcs as u64) * COMMITMENT_TRANSACTION_PER_HTLC_WEIGHT +
196182
anchor_output_spend_transaction_weight(context) +
197183
// As an upper bound, it is assumed that each HTLC is resolved in a separate transaction.
198184
// However, they might be aggregated when possible depending on timelocks and expiries.
199-
htlc_success_transaction_weight(context) * num_htlcs +
200-
htlc_timeout_transaction_weight(context) * num_htlcs,
185+
htlc_success_transaction_weight(context) * (context.expected_accepted_htlcs as u64) +
186+
htlc_timeout_transaction_weight(context) * (context.expected_accepted_htlcs as u64),
201187
);
202-
context.upper_bound_fee_rate.fee_wu(weight).ok_or(AnchorChannelReserveError::FeeOverflow)
188+
context.upper_bound_fee_rate.fee_wu(weight).unwrap_or(Amount::MAX)
203189
}
204190

205191
/// Calculates the number of anchor channels that can be supported by the reserve provided
206192
/// by `utxos`.
207193
pub fn get_supportable_anchor_channels(
208194
context: &AnchorChannelReserveContext, utxos: &[Utxo],
209-
) -> Result<u64, AnchorChannelReserveError> {
195+
) -> u64 {
210196
// Get the reserve needed per channel, replacing the fee for an initial spend with the actual value
211197
// below.
212198
let default_satisfaction_fee = context
@@ -216,36 +202,30 @@ pub fn get_supportable_anchor_channels(
216202
} else {
217203
P2WPKH_INPUT_WEIGHT
218204
}))
219-
.ok_or(AnchorChannelReserveError::FeeOverflow)?;
220-
let reserve_per_channel = get_reserve_per_channel(context)? - default_satisfaction_fee;
205+
.unwrap_or(Amount::MAX);
206+
let reserve_per_channel = get_reserve_per_channel(context) - default_satisfaction_fee;
221207

222208
let mut total_fractional_amount = Amount::from_sat(0);
223209
let mut num_whole_utxos = 0;
224210
for utxo in utxos {
225211
let satisfaction_fee = context
226212
.upper_bound_fee_rate
227213
.fee_wu(Weight::from_wu(utxo.satisfaction_weight))
228-
.ok_or(AnchorChannelReserveError::FeeOverflow)?;
229-
let amount = if let Some(amount) = utxo.output.value.checked_sub(satisfaction_fee) {
230-
amount
231-
} else {
232-
// The UTXO is considered dust at the given fee rate.
233-
continue;
234-
};
214+
.unwrap_or(Amount::MAX);
215+
let amount = utxo.output.value.checked_sub(satisfaction_fee).unwrap_or(Amount::MIN);
235216
if amount >= reserve_per_channel {
236217
num_whole_utxos += 1;
237218
} else {
238-
total_fractional_amount = total_fractional_amount
239-
.checked_add(amount)
240-
.ok_or(AnchorChannelReserveError::AmountOverflow)?;
219+
total_fractional_amount =
220+
total_fractional_amount.checked_add(amount).unwrap_or(Amount::MAX);
241221
}
242222
}
243223
// We require disjoint sets of UTXOs for the reserve of each channel,
244224
// as claims are currently only aggregated per channel.
245225
//
246226
// A worst-case coin selection is assumed for fractional UTXOs, selecting up to double the
247227
// required amount.
248-
Ok(num_whole_utxos + total_fractional_amount.to_sat() / reserve_per_channel.to_sat() / 2)
228+
num_whole_utxos + total_fractional_amount.to_sat() / reserve_per_channel.to_sat() / 2
249229
}
250230

251231
/// Verifies whether the anchor channel reserve provided by `utxos` is sufficient to support
@@ -278,7 +258,7 @@ pub fn can_support_additional_anchor_channel<
278258
>(
279259
context: &AnchorChannelReserveContext, utxos: &[Utxo], a_channel_manager: &AChannelManagerRef,
280260
chain_monitor: &ChainMonitorRef,
281-
) -> Result<bool, AnchorChannelReserveError>
261+
) -> bool
282262
where
283263
AChannelManagerRef::Target: AChannelManager,
284264
FilterRef::Target: Filter,
@@ -309,8 +289,7 @@ where
309289
anchor_channels.insert(channel.channel_id);
310290
}
311291
}
312-
get_supportable_anchor_channels(context, utxos)
313-
.map(|num_channels| num_channels > anchor_channels.len() as u64)
292+
get_supportable_anchor_channels(context, utxos) > anchor_channels.len() as u64
314293
}
315294

316295
#[cfg(test)]
@@ -329,7 +308,7 @@ mod test {
329308
expected_accepted_htlcs: 1,
330309
taproot_wallet: false,
331310
}),
332-
Ok(Amount::from_sat(4349))
311+
Amount::from_sat(4349)
333312
);
334313
}
335314

@@ -350,7 +329,7 @@ mod test {
350329
#[test]
351330
fn test_get_supportable_anchor_channels() {
352331
let context = AnchorChannelReserveContext::default();
353-
let reserve_per_channel = get_reserve_per_channel(&context).unwrap();
332+
let reserve_per_channel = get_reserve_per_channel(&context);
354333
// Only 3 disjoint sets with a value greater than the required reserve can be created.
355334
let utxos = vec![
356335
make_p2wpkh_utxo(reserve_per_channel * 3 / 2),
@@ -359,7 +338,7 @@ mod test {
359338
make_p2wpkh_utxo(reserve_per_channel * 99 / 100),
360339
make_p2wpkh_utxo(reserve_per_channel * 20 / 100),
361340
];
362-
assert_eq!(get_supportable_anchor_channels(&context, utxos.as_slice()), Ok(3));
341+
assert_eq!(get_supportable_anchor_channels(&context, utxos.as_slice()), 3);
363342
}
364343

365344
#[test]

0 commit comments

Comments
 (0)