Skip to content

Commit 200c607

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 2ed7a64 commit 200c607

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

lightning/src/ln/channel.rs

+35-2
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;
@@ -540,6 +540,9 @@ pub(super) struct Channel<Signer: Sign> {
540540
// is fine, but as a sanity check in our failure to generate the second claim, we check here
541541
// that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
542542
historical_inbound_htlc_fulfills: HashSet<u64>,
543+
544+
/// This channel's type, as negotiated during channel open
545+
channel_type: ChannelTypeFeatures,
543546
}
544547

545548
#[cfg(any(test, feature = "fuzztarget"))]
@@ -761,6 +764,11 @@ impl<Signer: Sign> Channel<Signer> {
761764

762765
#[cfg(any(test, feature = "fuzztarget"))]
763766
historical_inbound_htlc_fulfills: HashSet::new(),
767+
768+
// We currently only actually support one channel type, and so send there here with no
769+
// attempts to retry on error messages. When we support more we'll need fallback
770+
// support (assuming we want to support old types).
771+
channel_type: ChannelTypeFeatures::known(),
764772
})
765773
}
766774

@@ -789,6 +797,23 @@ impl<Signer: Sign> Channel<Signer> {
789797
where K::Target: KeysInterface<Signer = Signer>,
790798
F::Target: FeeEstimator
791799
{
800+
// First check the channel type is known, failing before we do anything else if we don't
801+
// support this channel type.
802+
let channel_type = if let Some(channel_type) = &msg.channel_type {
803+
if channel_type.supports_unknown_bits() {
804+
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
805+
}
806+
if channel_type.requires_unknown_bits() {
807+
return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
808+
}
809+
channel_type.clone()
810+
} else {
811+
ChannelTypeFeatures::from_counterparty_init(&their_features)
812+
};
813+
if !channel_type.supports_static_remote_key() {
814+
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
815+
}
816+
792817
let holder_signer = keys_provider.get_channel_signer(true, msg.funding_satoshis);
793818
let pubkeys = holder_signer.pubkeys().clone();
794819
let counterparty_pubkeys = ChannelPublicKeys {
@@ -1028,6 +1053,8 @@ impl<Signer: Sign> Channel<Signer> {
10281053

10291054
#[cfg(any(test, feature = "fuzztarget"))]
10301055
historical_inbound_htlc_fulfills: HashSet::new(),
1056+
1057+
channel_type,
10311058
};
10321059

10331060
Ok(chan)
@@ -4217,7 +4244,7 @@ impl<Signer: Sign> Channel<Signer> {
42174244
Some(script) => script.clone().into_inner(),
42184245
None => Builder::new().into_script(),
42194246
}),
4220-
channel_type: None,
4247+
channel_type: Some(self.channel_type.clone()),
42214248
}
42224249
}
42234250

@@ -5407,13 +5434,17 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54075434

54085435
let mut announcement_sigs = None;
54095436
let mut target_closing_feerate_sats_per_kw = None;
5437+
// Prior to supporting channel type negotiation, all of our channels were static_remotekey
5438+
// only, so we default to that if none was written.
5439+
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
54105440
read_tlv_fields!(reader, {
54115441
(0, announcement_sigs, option),
54125442
(1, minimum_depth, option),
54135443
(3, counterparty_selected_channel_reserve_satoshis, option),
54145444
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
54155445
(7, shutdown_scriptpubkey, option),
54165446
(9, target_closing_feerate_sats_per_kw, option),
5447+
(11, channel_type, option),
54175448
});
54185449

54195450
let mut secp_ctx = Secp256k1::new();
@@ -5507,6 +5538,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
55075538

55085539
#[cfg(any(test, feature = "fuzztarget"))]
55095540
historical_inbound_htlc_fulfills,
5541+
5542+
channel_type: channel_type.unwrap(),
55105543
})
55115544
}
55125545
}

0 commit comments

Comments
 (0)