Skip to content

Commit cf3b061

Browse files
carlaKCTheBlueMatt
andcommitted
ln+util: add test option to enable negotiation of zero fee channels
To allow testing along the way in this PR, turn on negotiation of zero fee channels. Co-authored-by: Matt Corallo <[email protected]>
1 parent a3a4af9 commit cf3b061

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

lightning/src/ln/channel.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4860,7 +4860,16 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48604860
// counterparty is advertising the feature, but rejecting channels proposing the feature for
48614861
// whatever reason.
48624862
let channel_type = &mut funding.channel_transaction_parameters.channel_type_features;
4863-
if channel_type.supports_anchors_zero_fee_htlc_tx() {
4863+
if channel_type.supports_anchor_zero_fee_commitments() {
4864+
channel_type.clear_anchor_zero_fee_commitments();
4865+
channel_type.set_anchors_zero_fee_htlc_tx_required();
4866+
channel_type.set_static_remote_key_required();
4867+
4868+
self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
4869+
assert!(!channel_type.supports_anchor_zero_fee_commitments());
4870+
assert!(channel_type.supports_anchors_zero_fee_htlc_tx());
4871+
assert!(channel_type.supports_static_remote_key());
4872+
} else if channel_type.supports_anchors_zero_fee_htlc_tx() {
48644873
channel_type.clear_anchors_zero_fee_htlc_tx();
48654874
self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
48664875
assert!(!channel_type.supports_anchors_nonzero_fee_htlc_tx());
@@ -9906,8 +9915,9 @@ pub(super) fn channel_type_from_open_channel(
99069915

99079916
// We only support the channel types defined by the `ChannelManager` in
99089917
// `provided_channel_type_features`. The channel type must always support
9909-
// `static_remote_key`.
9910-
if !channel_type.requires_static_remote_key() {
9918+
// `static_remote_key`, except for `option_zero_fee_commitments` which
9919+
// assumes this feature.
9920+
if !channel_type.requires_static_remote_key() && !channel_type.requires_anchor_zero_fee_commitments(){
99119921
return Err(ChannelError::close("Channel Type was not understood - we require static remote key".to_owned()));
99129922
}
99139923
// Make sure we support all of the features behind the channel type.
@@ -10492,10 +10502,21 @@ fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures)
1049210502
ret.set_scid_privacy_required();
1049310503
}
1049410504

10495-
// Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
10496-
// set it now. If they don't understand it, we'll fall back to our default of
10497-
// `only_static_remotekey`.
10498-
if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
10505+
// Optionally, if the user would like to negotiate `option_zero_fee_commitments` we set it now.
10506+
// If they don't understand it (or we don't want it), we check the same conditions for
10507+
// `option_anchors_zero_fee_htlc_tx`. The counterparty can still refuse the channel and we'll
10508+
// try to fall back (all the way to `only_static_remotekey`).
10509+
#[cfg(not(test))]
10510+
let negotiate_zero_fee_commitments = false;
10511+
10512+
#[cfg(test)]
10513+
let negotiate_zero_fee_commitments = config.channel_handshake_config.negotiate_anchor_zero_fee_commitments;
10514+
10515+
if negotiate_zero_fee_commitments && their_features.supports_anchor_zero_fee_commitments() {
10516+
ret.set_anchor_zero_fee_commitments_required();
10517+
// `option_static_remote_key` is assumed by `option_zero_fee_commitments`.
10518+
ret.clear_static_remote_key();
10519+
} else if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
1049910520
their_features.supports_anchors_zero_fee_htlc_tx() {
1050010521
ret.set_anchors_zero_fee_htlc_tx_required();
1050110522
}

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12931,6 +12931,14 @@ pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
1293112931
// quiescent-dependent protocols (e.g., splicing).
1293212932
#[cfg(any(test, fuzzing))]
1293312933
features.set_quiescence_optional();
12934+
12935+
#[cfg(test)]
12936+
{
12937+
if config.channel_handshake_config.negotiate_anchor_zero_fee_commitments {
12938+
features.set_anchor_zero_fee_commitments_optional();
12939+
}
12940+
}
12941+
1293412942
features
1293512943
}
1293612944

@@ -16165,6 +16173,18 @@ mod tests {
1616516173
do_test_channel_type_downgrade(anchors_config, |features| features.supports_anchors_zero_fee_htlc_tx())
1616616174
}
1616716175

16176+
#[test]
16177+
fn test_zero_fee_commitments_downgrade() {
16178+
// Tests that the local node will retry without zero fee commitments in the case where the
16179+
// remote node supports the feature but does not accept it.
16180+
let mut zero_fee_config = test_default_channel_config();
16181+
zero_fee_config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
16182+
zero_fee_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
16183+
zero_fee_config.manually_accept_inbound_channels = true;
16184+
16185+
do_test_channel_type_downgrade(zero_fee_config, |features| features.supports_anchor_zero_fee_commitments())
16186+
}
16187+
1616816188
fn do_test_channel_type_downgrade<F>(user_cfg: UserConfig, start_type_set: F)
1616916189
where F: Fn(&ChannelTypeFeatures) -> bool {
1617016190
let chanmon_cfgs = create_chanmon_cfgs(2);

lightning/src/util/config.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,41 @@ pub struct ChannelHandshakeConfig {
183183
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
184184
pub negotiate_anchors_zero_fee_htlc_tx: bool,
185185

186+
/// If set, we attempt to negotiate the `zero_fee_commitments` option for all future channels.
187+
///
188+
/// These channels operate very similarly to the `anchors_zero_fee_htlc` channels but rely on
189+
/// [TRUC] to assign zero fee to the commitment transactions themselves, avoiding many protocol
190+
/// edge-cases involving fee updates and greatly simplifying the concept of your "balance" in
191+
/// lightning.
192+
///
193+
/// Like `anchors_zero_fee_htlc` channels, this feature requires having a reserve of onchain
194+
/// funds readily available to bump transactions in the event of a channel force close to avoid
195+
/// the possibility of losing funds.
196+
///
197+
/// Note that if you wish accept inbound channels with anchor outputs, you must enable
198+
/// [`UserConfig::manually_accept_inbound_channels`] and manually accept them with
199+
/// [`ChannelManager::accept_inbound_channel`]. This is done to give you the chance to check
200+
/// whether your reserve of onchain funds is enough to cover the fees for all existing and new
201+
/// channels featuring anchor outputs in the event of a force close.
202+
///
203+
/// If this option is set, channels may be created that will not be readable by LDK versions
204+
/// prior to 0.2, causing [`ChannelManager`]'s read method to return a
205+
/// [`DecodeError::InvalidValue`].
206+
///
207+
/// Note that setting this to true does *not* prevent us from opening channels with
208+
/// counterparties that do not support the `zero_fee_commitments` option; we will simply fall
209+
/// back to a `anchors_zero_fee_htlc` (if [`Self::negotiate_anchors_zero_fee_htlc_tx`]
210+
/// is set) or `static_remote_key` channel.
211+
///
212+
/// Default value: `false` (This value is likely to change to `true` in the future.)
213+
///
214+
/// [TRUC]: (https://bitcoinops.org/en/topics/version-3-transaction-relay/)
215+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
216+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
217+
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
218+
#[cfg(test)]
219+
pub negotiate_anchor_zero_fee_commitments: bool,
220+
186221
/// The maximum number of HTLCs in-flight from our counterparty towards us at the same time.
187222
///
188223
/// Increasing the value can help improve liquidity and stability in
@@ -212,6 +247,8 @@ impl Default for ChannelHandshakeConfig {
212247
commit_upfront_shutdown_pubkey: true,
213248
their_channel_reserve_proportional_millionths: 10_000,
214249
negotiate_anchors_zero_fee_htlc_tx: false,
250+
#[cfg(test)]
251+
negotiate_anchor_zero_fee_commitments: false,
215252
our_max_accepted_htlcs: 50,
216253
}
217254
}
@@ -233,6 +270,8 @@ impl Readable for ChannelHandshakeConfig {
233270
commit_upfront_shutdown_pubkey: Readable::read(reader)?,
234271
their_channel_reserve_proportional_millionths: Readable::read(reader)?,
235272
negotiate_anchors_zero_fee_htlc_tx: Readable::read(reader)?,
273+
#[cfg(test)]
274+
negotiate_anchor_zero_fee_commitments: Readable::read(reader)?,
236275
our_max_accepted_htlcs: Readable::read(reader)?,
237276
})
238277
}

0 commit comments

Comments
 (0)