Skip to content

Commit 33dd6a2

Browse files
committed
Support send/recv'ing the new channel_type field in open_channel
This implements the channel type negotiation, though as we currently only support channels with only static_remotekey set, it doesn't implement the negotiation explicitly.
1 parent 153c2c7 commit 33dd6a2

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bitcoin::secp256k1::{Secp256k1,Signature};
2323
use bitcoin::secp256k1;
2424

2525
use ln::{PaymentPreimage, PaymentHash};
26-
use ln::features::{ChannelFeatures, InitFeatures};
26+
use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures};
2727
use ln::msgs;
2828
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
2929
use ln::script::ShutdownScript;
@@ -527,6 +527,9 @@ pub(super) struct Channel<Signer: Sign> {
527527
// is fine, but as a sanity check in our failure to generate the second claim, we check here
528528
// that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
529529
historical_inbound_htlc_fulfills: HashSet<u64>,
530+
531+
/// This channel's type, as negotiated during channel open
532+
channel_type: ChannelTypeFeatures,
530533
}
531534

532535
#[cfg(any(test, feature = "fuzztarget"))]
@@ -748,6 +751,11 @@ impl<Signer: Sign> Channel<Signer> {
748751

749752
#[cfg(any(test, feature = "fuzztarget"))]
750753
historical_inbound_htlc_fulfills: HashSet::new(),
754+
755+
// We currently only actually support one channel type, so don't retry with new types
756+
// on error messages. When we support more we'll need fallback support (assuming we
757+
// want to support old types).
758+
channel_type: ChannelTypeFeatures::only_static_remote_key(),
751759
})
752760
}
753761

@@ -776,6 +784,23 @@ impl<Signer: Sign> Channel<Signer> {
776784
where K::Target: KeysInterface<Signer = Signer>,
777785
F::Target: FeeEstimator
778786
{
787+
// First check the channel type is known, failing before we do anything else if we don't
788+
// support this channel type.
789+
let channel_type = if let Some(channel_type) = &msg.channel_type {
790+
if channel_type.supports_any_optional_bits() {
791+
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
792+
}
793+
if *channel_type != ChannelTypeFeatures::only_static_remote_key() {
794+
return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
795+
}
796+
channel_type.clone()
797+
} else {
798+
ChannelTypeFeatures::from_counterparty_init(&their_features)
799+
};
800+
if !channel_type.supports_static_remote_key() {
801+
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
802+
}
803+
779804
let holder_signer = keys_provider.get_channel_signer(true, msg.funding_satoshis);
780805
let pubkeys = holder_signer.pubkeys().clone();
781806
let counterparty_pubkeys = ChannelPublicKeys {
@@ -1015,6 +1040,8 @@ impl<Signer: Sign> Channel<Signer> {
10151040

10161041
#[cfg(any(test, feature = "fuzztarget"))]
10171042
historical_inbound_htlc_fulfills: HashSet::new(),
1043+
1044+
channel_type,
10181045
};
10191046

10201047
Ok(chan)
@@ -4204,7 +4231,7 @@ impl<Signer: Sign> Channel<Signer> {
42044231
Some(script) => script.clone().into_inner(),
42054232
None => Builder::new().into_script(),
42064233
}),
4207-
channel_type: None,
4234+
channel_type: Some(self.channel_type.clone()),
42084235
}
42094236
}
42104237

@@ -5394,15 +5421,26 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
53945421

53955422
let mut announcement_sigs = None;
53965423
let mut target_closing_feerate_sats_per_kw = None;
5424+
// Prior to supporting channel type negotiation, all of our channels were static_remotekey
5425+
// only, so we default to that if none was written.
5426+
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
53975427
read_tlv_fields!(reader, {
53985428
(0, announcement_sigs, option),
53995429
(1, minimum_depth, option),
54005430
(3, counterparty_selected_channel_reserve_satoshis, option),
54015431
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
54025432
(7, shutdown_scriptpubkey, option),
54035433
(9, target_closing_feerate_sats_per_kw, option),
5434+
(11, channel_type, option),
54045435
});
54055436

5437+
let chan_features = channel_type.as_ref().unwrap();
5438+
if chan_features.supports_unknown_bits() || chan_features.requires_unknown_bits() {
5439+
// If the channel was written by a new version and negotiated with features we don't
5440+
// understand yet, refuse to read it.
5441+
return Err(DecodeError::UnknownRequiredFeature);
5442+
}
5443+
54065444
let mut secp_ctx = Secp256k1::new();
54075445
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
54085446

@@ -5494,6 +5532,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54945532

54955533
#[cfg(any(test, feature = "fuzztarget"))]
54965534
historical_inbound_htlc_fulfills,
5535+
5536+
channel_type: channel_type.unwrap(),
54975537
})
54985538
}
54995539
}

0 commit comments

Comments
 (0)