Skip to content

Commit 264180e

Browse files
author
Antoine Riard
committed
Replace config max counterpary dust_limit_satoshis by a constant.
Current Bitcoin Core's policy will reject a p2wsh as a dust if it's under 330 satoshis. A typical p2wsh output is 43 bytes big to which Core's `GetDustThreshold()` sums up a minimal spend of 67 bytes (even if a p2wsh witnessScript might be smaller). `dustRelayFee` is set to 3000 sat/kb, thus 110 * 3000 / 1000 = 330. As all time-sensitive outputs are p2wsh, a value of 330 sat is the lower bound desired to ensure good propagation of transactions. We give a bit margin to our counterparty and pick up 660 satoshis as an accepted `dust_limit_satoshis` upper bound. As this reasoning is tricky and error-prone we hardcode it instead of letting the user picking up a non-sense value. Further, this lower bound of 330 sats is also hardcoded as another constant (MIN_DUST_LIMIT_SATOSHIS) instead of being dynamically computed on feerate (derive_holder_dust_limit_satoshis`). Reducing risks of non-propagating transactions in casee of failing fee festimation.
1 parent 36570f4 commit 264180e

File tree

5 files changed

+43
-57
lines changed

5 files changed

+43
-57
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
319319
let mut config = UserConfig::default();
320320
config.channel_options.fee_proportional_millionths = 0;
321321
config.channel_options.announced_channel = true;
322-
config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
323322
let network = Network::Bitcoin;
324323
let params = ChainParameters {
325324
network,
@@ -339,7 +338,6 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
339338
let mut config = UserConfig::default();
340339
config.channel_options.fee_proportional_millionths = 0;
341340
config.channel_options.announced_channel = true;
342-
config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
343341

344342
let mut monitors = HashMap::new();
345343
let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap();

fuzz/src/full_stack.rs

Lines changed: 8 additions & 12 deletions
Large diffs are not rendered by default.

lightning/src/ln/channel.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ struct CommitmentTxInfoCached {
438438

439439
pub const OUR_MAX_HTLCS: u16 = 50; //TODO
440440
const SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT: u64 = 79; // prevout: 36, nSequence: 4, script len: 1, witness lengths: (3+1)/4, sig: 73/4, if-selector: 1, redeemScript: (6 ops + 2*33 pubkeys + 1*2 delay)/4
441-
const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence: 4, script len: 1, witness lengths: 3/4, sig: 73/4, pubkey: 33/4, output: 31 (TODO: Wrong? Useless?)
442441

443442
#[cfg(not(test))]
444443
const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
@@ -453,6 +452,22 @@ pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
453452
/// it's 2^24.
454453
pub const MAX_FUNDING_SATOSHIS: u64 = 1 << 24;
455454

455+
/// Maximum counterparty `dust_limit_satoshis` allowed. 2 * standard dust threshold on p2wsh output
456+
/// Scales up on Bitcoin Core's proceeding policy with dust outputs. A typical p2wsh output is 43
457+
/// bytes big to which Core's `GetDustThreshold()` sums up a minimal spend of 67 bytes (even if
458+
/// a p2wsh witnessScript might be *effectively* smaller), `dustRelayFee` is set to 3000sat/kb, thus
459+
/// 110 * 3000 / 1000 = 330. Per-protocol rules, all time-sensitive outputs are p2wsh, a value of
460+
/// 330 sats is the lower bound desired to ensure good propagation of transactions. We give a bit
461+
/// of margin to our counterparty and pick up 660 satoshis as an accepted `dust_limit_satoshis`
462+
/// upper bound to avoid negotiation conflicts with other implementations.
463+
pub const MAX_DUST_LIMIT_SATOSHIS: u64 = 2 * 330;
464+
465+
/// A typical p2wsh output is 43 bytes big to which Core's `GetDustThreshold()` sums up a minimal
466+
/// spend of 67 bytes (even if a p2wsh witnessScript might be *effectively* smaller), `dustRelayFee`
467+
/// is set to 3000sat/kb, thus 110 * 3000 / 1000 = 330. Per-protocol rules, all time-sensitive outputs
468+
/// are p2wsh, a value of 330 sats is the lower bound desired to ensure good propagation of transactions.
469+
pub const MIN_DUST_LIMIT_SATOSHIS: u64 = 330;
470+
456471
/// Used to return a simple Error back to ChannelManager. Will get converted to a
457472
/// msgs::ErrorAction::SendErrorMessage or msgs::ErrorAction::IgnoreError as appropriate with our
458473
/// channel_id in ChannelManager.
@@ -496,10 +511,6 @@ impl<Signer: Sign> Channel<Signer> {
496511
cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO
497512
}
498513

499-
fn derive_holder_dust_limit_satoshis(at_open_background_feerate: u32) -> u64 {
500-
cmp::max(at_open_background_feerate as u64 * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000, 546) //TODO
501-
}
502-
503514
// Constructors:
504515
pub fn new_outbound<K: Deref, F: Deref>(fee_estimator: &F, keys_provider: &K, counterparty_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, config: &UserConfig) -> Result<Channel<Signer>, APIError>
505516
where K::Target: KeysInterface<Signer = Signer>,
@@ -519,9 +530,9 @@ impl<Signer: Sign> Channel<Signer> {
519530
if holder_selected_contest_delay < BREAKDOWN_TIMEOUT {
520531
return Err(APIError::APIMisuseError {err: format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks", holder_selected_contest_delay)});
521532
}
522-
let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
523-
if Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(channel_value_satoshis) < Channel::<Signer>::derive_holder_dust_limit_satoshis(background_feerate) {
524-
return Err(APIError::FeeRateTooHigh{err: format!("Not enough reserve above dust limit can be found at current fee rate({})", background_feerate), feerate: background_feerate});
533+
let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(channel_value_satoshis);
534+
if holder_selected_channel_reserve_satoshis < MIN_DUST_LIMIT_SATOSHIS {
535+
return Err(APIError::APIMisuseError { err: format!("Holder selected channel reserve below implemention limit dust_limit_satoshis {}", holder_selected_channel_reserve_satoshis) });
525536
}
526537

527538
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
@@ -578,7 +589,7 @@ impl<Signer: Sign> Channel<Signer> {
578589

579590
feerate_per_kw: feerate,
580591
counterparty_dust_limit_satoshis: 0,
581-
holder_dust_limit_satoshis: Channel::<Signer>::derive_holder_dust_limit_satoshis(background_feerate),
592+
holder_dust_limit_satoshis: MIN_DUST_LIMIT_SATOSHIS,
582593
counterparty_max_htlc_value_in_flight_msat: 0,
583594
counterparty_selected_channel_reserve_satoshis: 0,
584595
counterparty_htlc_minimum_msat: 0,
@@ -699,11 +710,11 @@ impl<Signer: Sign> Channel<Signer> {
699710
if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
700711
return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, config.peer_channel_config_limits.min_max_accepted_htlcs)));
701712
}
702-
if msg.dust_limit_satoshis < config.peer_channel_config_limits.min_dust_limit_satoshis {
703-
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the user specified limit ({})", msg.dust_limit_satoshis, config.peer_channel_config_limits.min_dust_limit_satoshis)));
713+
if msg.dust_limit_satoshis < MIN_DUST_LIMIT_SATOSHIS {
714+
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_DUST_LIMIT_SATOSHIS)));
704715
}
705-
if msg.dust_limit_satoshis > config.peer_channel_config_limits.max_dust_limit_satoshis {
706-
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the user specified limit ({})", msg.dust_limit_satoshis, config.peer_channel_config_limits.max_dust_limit_satoshis)));
716+
if msg.dust_limit_satoshis > MAX_DUST_LIMIT_SATOSHIS {
717+
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_DUST_LIMIT_SATOSHIS)));
707718
}
708719

709720
// Convert things into internal flags and prep our state:
@@ -719,13 +730,12 @@ impl<Signer: Sign> Channel<Signer> {
719730

720731
let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
721732

722-
let holder_dust_limit_satoshis = Channel::<Signer>::derive_holder_dust_limit_satoshis(background_feerate);
723733
let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(msg.funding_satoshis);
724-
if holder_selected_channel_reserve_satoshis < holder_dust_limit_satoshis {
725-
return Err(ChannelError::Close(format!("Suitable channel reserve not found. remote_channel_reserve was ({}). dust_limit_satoshis is ({}).", holder_selected_channel_reserve_satoshis, holder_dust_limit_satoshis)));
734+
if holder_selected_channel_reserve_satoshis < MIN_DUST_LIMIT_SATOSHIS {
735+
return Err(ChannelError::Close(format!("Suitable channel reserve not found. remote_channel_reserve was ({}). dust_limit_satoshis is ({}).", holder_selected_channel_reserve_satoshis, MIN_DUST_LIMIT_SATOSHIS)));
726736
}
727-
if msg.channel_reserve_satoshis < holder_dust_limit_satoshis {
728-
return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is smaller than our dust limit ({})", msg.channel_reserve_satoshis, holder_dust_limit_satoshis)));
737+
if msg.channel_reserve_satoshis < MIN_DUST_LIMIT_SATOSHIS {
738+
return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is smaller than our dust limit ({})", msg.channel_reserve_satoshis, MIN_DUST_LIMIT_SATOSHIS)));
729739
}
730740
if holder_selected_channel_reserve_satoshis < msg.dust_limit_satoshis {
731741
return Err(ChannelError::Close(format!("Dust limit ({}) too high for the channel reserve we require the remote to keep ({})", msg.dust_limit_satoshis, holder_selected_channel_reserve_satoshis)));
@@ -817,7 +827,7 @@ impl<Signer: Sign> Channel<Signer> {
817827
feerate_per_kw: msg.feerate_per_kw,
818828
channel_value_satoshis: msg.funding_satoshis,
819829
counterparty_dust_limit_satoshis: msg.dust_limit_satoshis,
820-
holder_dust_limit_satoshis,
830+
holder_dust_limit_satoshis: MIN_DUST_LIMIT_SATOSHIS,
821831
counterparty_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
822832
counterparty_selected_channel_reserve_satoshis: msg.channel_reserve_satoshis,
823833
counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
@@ -1441,11 +1451,11 @@ impl<Signer: Sign> Channel<Signer> {
14411451
if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
14421452
return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, config.peer_channel_config_limits.min_max_accepted_htlcs)));
14431453
}
1444-
if msg.dust_limit_satoshis < config.peer_channel_config_limits.min_dust_limit_satoshis {
1445-
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the user specified limit ({})", msg.dust_limit_satoshis, config.peer_channel_config_limits.min_dust_limit_satoshis)));
1454+
if msg.dust_limit_satoshis < MIN_DUST_LIMIT_SATOSHIS {
1455+
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_DUST_LIMIT_SATOSHIS)));
14461456
}
1447-
if msg.dust_limit_satoshis > config.peer_channel_config_limits.max_dust_limit_satoshis {
1448-
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the user specified limit ({})", msg.dust_limit_satoshis, config.peer_channel_config_limits.max_dust_limit_satoshis)));
1457+
if msg.dust_limit_satoshis > MAX_DUST_LIMIT_SATOSHIS {
1458+
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_DUST_LIMIT_SATOSHIS)));
14491459
}
14501460
if msg.minimum_depth > config.peer_channel_config_limits.max_minimum_depth {
14511461
return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", config.peer_channel_config_limits.max_minimum_depth, msg.minimum_depth)));
@@ -4935,14 +4945,14 @@ mod tests {
49354945
// Create Node B's channel by receiving Node A's open_channel message
49364946
// Make sure A's dust limit is as we expect.
49374947
let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.block_hash());
4938-
assert_eq!(open_channel_msg.dust_limit_satoshis, 1560);
49394948
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
49404949
let node_b_chan = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, node_b_node_id, InitFeatures::known(), &open_channel_msg, 7, &config).unwrap();
49414950

49424951
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
49434952
let mut accept_channel_msg = node_b_chan.get_accept_channel();
49444953
accept_channel_msg.dust_limit_satoshis = 546;
49454954
node_a_chan.accept_channel(&accept_channel_msg, &config, InitFeatures::known()).unwrap();
4955+
node_a_chan.holder_dust_limit_satoshis = 1560;
49464956

49474957
// Put some inbound and outbound HTLCs in A's channel.
49484958
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
17921792
// transaction fee with 0 HTLCs (183 sats)).
17931793
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 98817000, InitFeatures::known(), InitFeatures::known());
17941794

1795-
let dust_amt = 546000; // Dust amount
1795+
let dust_amt = 329000; // Dust amount
17961796
// In the previous code, routing this dust payment would cause nodes[0] to perceive a channel
17971797
// reserve violation even though it's a dust HTLC and therefore shouldn't count towards the
17981798
// commitment transaction fee.

lightning/src/util/config.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,6 @@ pub struct ChannelHandshakeLimits {
9898
///
9999
/// Default value: 0.
100100
pub min_max_accepted_htlcs: u16,
101-
/// Outputs below a certain value will not be added to on-chain transactions. The dust value is
102-
/// required to always be higher than this value so this only applies to HTLC outputs (and
103-
/// potentially to-self outputs before any payments have been made).
104-
/// Thus, HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
105-
/// This setting allows you to set a minimum dust limit for their commitment transactions,
106-
/// reflecting the reality that tiny outputs are not considered standard transactions and will
107-
/// not propagate through the Bitcoin network.
108-
///
109-
/// Default value: 546, the current dust limit on the Bitcoin network.
110-
pub min_dust_limit_satoshis: u64,
111-
/// Maximum allowed threshold above which outputs will not be generated in their commitment
112-
/// transactions.
113-
/// HTLCs below this amount plus HTLC transaction fees are not enforceable on-chain.
114-
///
115-
/// Default value: u64::max_value.
116-
pub max_dust_limit_satoshis: u64,
117101
/// Before a channel is usable the funding transaction will need to be confirmed by at least a
118102
/// certain number of blocks, specified by the node which is not the funder (as the funder can
119103
/// assume they aren't going to double-spend themselves).
@@ -145,8 +129,6 @@ impl Default for ChannelHandshakeLimits {
145129
min_max_htlc_value_in_flight_msat: 0,
146130
max_channel_reserve_satoshis: <u64>::max_value(),
147131
min_max_accepted_htlcs: 0,
148-
min_dust_limit_satoshis: 546,
149-
max_dust_limit_satoshis: <u64>::max_value(),
150132
max_minimum_depth: 144,
151133
force_announced_channel_preference: true,
152134
their_to_self_delay: MAX_LOCAL_BREAKDOWN_TIMEOUT,

0 commit comments

Comments
 (0)