Skip to content

Commit

Permalink
Support send/recv'ing the new channel_type field in open_channel
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheBlueMatt committed Sep 18, 2021
1 parent f899254 commit 74930bd
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bitcoin::secp256k1::{Secp256k1,Signature};
use bitcoin::secp256k1;

use ln::{PaymentPreimage, PaymentHash};
use ln::features::{ChannelFeatures, InitFeatures};
use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures};
use ln::msgs;
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
use ln::script::ShutdownScript;
Expand Down Expand Up @@ -527,6 +527,9 @@ pub(super) struct Channel<Signer: Sign> {
// is fine, but as a sanity check in our failure to generate the second claim, we check here
// that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
historical_inbound_htlc_fulfills: HashSet<u64>,

/// This channel's type, as negotiated during channel open
channel_type: ChannelTypeFeatures,
}

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

#[cfg(any(test, feature = "fuzztarget"))]
historical_inbound_htlc_fulfills: HashSet::new(),

// We currently only actually support one channel type, and so send there here with no
// attempts to retry on error messages. When we support more we'll need fallback
// support (assuming we want to support old types).
channel_type: ChannelTypeFeatures::known(),
})
}

Expand Down Expand Up @@ -776,6 +784,23 @@ impl<Signer: Sign> Channel<Signer> {
where K::Target: KeysInterface<Signer = Signer>,
F::Target: FeeEstimator
{
// First check the channel type is known, failing before we do anything else if we don't
// support this channel type.
let channel_type = if let Some(channel_type) = &msg.channel_type {
if channel_type.supports_unknown_bits() {
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
}
if channel_type.requires_unknown_bits() {
return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
}
channel_type.clone()
} else {
ChannelTypeFeatures::from_counterparty_init(&their_features)
};
if !channel_type.supports_static_remote_key() {
return Err(ChannelError::Close("Channel Type was not understood - we require static remote key".to_owned()));
}

let holder_signer = keys_provider.get_channel_signer(true, msg.funding_satoshis);
let pubkeys = holder_signer.pubkeys().clone();
let counterparty_pubkeys = ChannelPublicKeys {
Expand Down Expand Up @@ -1015,6 +1040,8 @@ impl<Signer: Sign> Channel<Signer> {

#[cfg(any(test, feature = "fuzztarget"))]
historical_inbound_htlc_fulfills: HashSet::new(),

channel_type,
};

Ok(chan)
Expand Down Expand Up @@ -4204,7 +4231,7 @@ impl<Signer: Sign> Channel<Signer> {
Some(script) => script.clone().into_inner(),
None => Builder::new().into_script(),
}),
channel_type: None,
channel_type: Some(self.channel_type.clone()),
}
}

Expand Down Expand Up @@ -5394,15 +5421,25 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>

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

if channel_type.as_ref().unwrap().supports_unknown_bits() || channel_type.as_ref().unwrap().requires_unknown_bits() {
// If the channel was written by a new version and negotiated with features we don't
// understand yet, refuse to read it.
return Err(DecodeError::UnknownRequiredFeature);
}

let mut secp_ctx = Secp256k1::new();
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());

Expand Down Expand Up @@ -5494,6 +5531,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>

#[cfg(any(test, feature = "fuzztarget"))]
historical_inbound_htlc_fulfills,

channel_type: channel_type.unwrap(),
})
}
}
Expand Down

0 comments on commit 74930bd

Please sign in to comment.