Skip to content

Commit 9df3e75

Browse files
committed
Support negotiating anchors throughout channel open
1 parent a46e19d commit 9df3e75

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

lightning/src/ln/channel.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ impl<Signer: Sign> Channel<Signer> {
880880
if !config.channel_handshake_config.announced_channel && config.channel_handshake_config.negotiate_scid_privacy {
881881
ret.set_scid_privacy_required();
882882
}
883+
if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
884+
ret.set_anchors_zero_fee_htlc_tx_required();
885+
}
883886
ret
884887
}
885888

@@ -905,7 +908,8 @@ impl<Signer: Sign> Channel<Signer> {
905908
where K::Target: KeysInterface<Signer = Signer>,
906909
F::Target: FeeEstimator,
907910
{
908-
let opt_anchors = false; // TODO - should be based on features
911+
let opt_anchors = config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
912+
their_features.supports_anchors_zero_fee_htlc_tx();
909913

910914
let holder_selected_contest_delay = config.channel_handshake_config.our_to_self_delay;
911915
let holder_signer = keys_provider.get_channel_signer(false, channel_value_satoshis);
@@ -1104,7 +1108,6 @@ impl<Signer: Sign> Channel<Signer> {
11041108
F::Target: FeeEstimator,
11051109
L::Target: Logger,
11061110
{
1107-
let opt_anchors = false; // TODO - should be based on features
11081111
let announced_channel = if (msg.channel_flags & 1) == 1 { true } else { false };
11091112

11101113
// First check the channel type is known, failing before we do anything else if we don't
@@ -1118,13 +1121,24 @@ impl<Signer: Sign> Channel<Signer> {
11181121
return Err(ChannelError::Close("Channel Type field contains unknown bits".to_owned()));
11191122
}
11201123

1121-
// We currently only allow four channel types, so write it all out here - we allow
1122-
// `only_static_remote_key` or `static_remote_key | zero_conf` in all contexts, and
1123-
// further allow `static_remote_key | scid_privacy` or
1124-
// `static_remote_key | scid_privacy | zero_conf`, if the channel is not
1125-
// publicly announced.
1124+
// We currently only allow 8 channel types, and all must support `static_remote_key`, so
1125+
// write it all out here - we allow
1126+
// - `only_static_remote_key`
1127+
// - `static_remote_key | zero_conf`
1128+
// - `static_remote_key | scid_privacy`
1129+
// - `static_remote_key | scid_privacy | zero_conf`
1130+
// - `static_remote_key | anchors_zero_fee_htlc_tx`
1131+
// - `static_remote_key | anchors_zero_fee_htlc_tx | zero_conf`
1132+
// - `static_remote_key | anchors_zero_fee_htlc_tx | scid_privacy`
1133+
// - `static_remote_key | anchors_zero_fee_htlc_tx | scid_privacy | zero_conf`
1134+
if !channel_type.requires_static_remote_key() {
1135+
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
1136+
}
11261137
if *channel_type != ChannelTypeFeatures::only_static_remote_key() {
1127-
if !channel_type.requires_scid_privacy() && !channel_type.requires_zero_conf() {
1138+
// If we have more features than just `static_remote_key`, make sure we only allow
1139+
// those we support.
1140+
if !channel_type.requires_scid_privacy() && !channel_type.requires_zero_conf() &&
1141+
!channel_type.requires_anchors_zero_fee_htlc_tx() {
11281142
return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
11291143
}
11301144

@@ -1136,9 +1150,7 @@ impl<Signer: Sign> Channel<Signer> {
11361150
} else {
11371151
ChannelTypeFeatures::from_counterparty_init(&their_features)
11381152
};
1139-
if !channel_type.supports_static_remote_key() {
1140-
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
1141-
}
1153+
let opt_anchors = channel_type.supports_anchors_zero_fee_htlc_tx();
11421154

11431155
let holder_signer = keys_provider.get_channel_signer(true, msg.funding_satoshis);
11441156
let pubkeys = holder_signer.pubkeys().clone();
@@ -2081,7 +2093,12 @@ impl<Signer: Sign> Channel<Signer> {
20812093
} else if their_features.supports_channel_type() {
20822094
// Assume they've accepted the channel type as they said they understand it.
20832095
} else {
2084-
self.channel_type = ChannelTypeFeatures::from_counterparty_init(&their_features)
2096+
self.channel_type = ChannelTypeFeatures::from_counterparty_init(&their_features);
2097+
self.channel_transaction_parameters.opt_anchors = if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
2098+
Some(())
2099+
} else {
2100+
None
2101+
};
20852102
}
20862103

20872104
let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
@@ -6554,11 +6571,6 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
65546571
return Err(DecodeError::UnknownRequiredFeature);
65556572
}
65566573

6557-
if channel_parameters.opt_anchors.is_some() {
6558-
// Relax this check when ChannelTypeFeatures supports anchors.
6559-
return Err(DecodeError::InvalidValue);
6560-
}
6561-
65626574
let mut secp_ctx = Secp256k1::new();
65636575
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
65646576

lightning/src/ln/features.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ mod tests {
763763
init_features.set_channel_type_optional();
764764
init_features.set_scid_privacy_optional();
765765
init_features.set_zero_conf_optional();
766+
init_features.set_anchors_zero_fee_htlc_tx_optional();
766767

767768
assert!(init_features.initial_routing_sync());
768769
assert!(!init_features.supports_upfront_shutdown_script());

lightning/src/util/config.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ pub struct ChannelHandshakeConfig {
126126
///
127127
/// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey
128128
pub commit_upfront_shutdown_pubkey: bool,
129-
130129
/// The Proportion of the channel value to configure as counterparty's channel reserve,
131130
/// i.e., `their_channel_reserve_satoshis` for both outbound and inbound channels.
132131
///
@@ -149,7 +148,22 @@ pub struct ChannelHandshakeConfig {
149148
/// as 1000 sats instead, which is a safe implementation-specific lower bound.
150149
/// Maximum value: 1,000,000, any values larger than 1 Million will be treated as 1 Million (or 100%)
151150
/// instead, although channel negotiations will fail in that case.
152-
pub their_channel_reserve_proportional_millionths: u32
151+
pub their_channel_reserve_proportional_millionths: u32,
152+
/// If set, we attempt to negotiate the `anchors_zero_fee_htlc_tx`option for outbound channels.
153+
///
154+
/// If this option is set, channels may be created that will not be readable by LDK versions
155+
/// prior to 0.0.110, causing [`ChannelManager`]'s read method to return a
156+
/// [`DecodeError::InvalidValue`].
157+
///
158+
/// Note that setting this to true does *not* prevent us from opening channels with
159+
/// counterparties that do not support the `anchors_zero_fee_htlc_tx` option; we will simply
160+
/// fall back to a `static_remote_key` channel.
161+
///
162+
/// Default value: false. This value is likely to change to true in the future.
163+
///
164+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
165+
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
166+
pub negotiate_anchors_zero_fee_htlc_tx: bool,
153167
}
154168

155169
impl Default for ChannelHandshakeConfig {
@@ -163,6 +177,7 @@ impl Default for ChannelHandshakeConfig {
163177
announced_channel: false,
164178
commit_upfront_shutdown_pubkey: true,
165179
their_channel_reserve_proportional_millionths: 10_000,
180+
negotiate_anchors_zero_fee_htlc_tx: false,
166181
}
167182
}
168183
}

0 commit comments

Comments
 (0)