Skip to content

Commit 7671ae5

Browse files
authored
Merge pull request #1351 from TheBlueMatt/2022-03-scid-privacy
Implement the SCIDAlias Channel Type and provide SCID Privacy
2 parents 6176e2f + 952cee4 commit 7671ae5

File tree

12 files changed

+519
-84
lines changed

12 files changed

+519
-84
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ jobs:
239239
check_commits:
240240
runs-on: ubuntu-latest
241241
env:
242-
# rustc 1.53 regressed and panics when building our (perfectly fine) docs.
243-
# See https://github.com/rust-lang/rust/issues/84738
244-
TOOLCHAIN: 1.52.1
242+
TOOLCHAIN: 1.57.0
245243
steps:
246244
- name: Checkout source code
247245
uses: actions/checkout@v3

fuzz/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
215215
forwarding_info: None,
216216
},
217217
funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
218+
channel_type: None,
218219
short_channel_id: Some(scid),
219220
inbound_scid_alias: None,
220221
channel_value_satoshis: slice_to_be64(get_slice!(8)),

lightning/src/ln/channel.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,31 @@ impl<Signer: Sign> Channel<Signer> {
810810
self.channel_transaction_parameters.opt_anchors.is_some()
811811
}
812812

813+
fn get_initial_channel_type(config: &UserConfig) -> ChannelTypeFeatures {
814+
// The default channel type (ie the first one we try) depends on whether the channel is
815+
// public - if it is, we just go with `only_static_remotekey` as it's the only option
816+
// available. If it's private, we first try `scid_privacy` as it provides better privacy
817+
// with no other changes, and fall back to `only_static_remotekey`
818+
let mut ret = ChannelTypeFeatures::only_static_remote_key();
819+
if !config.channel_options.announced_channel && config.own_channel_config.negotiate_scid_privacy {
820+
ret.set_scid_privacy_required();
821+
}
822+
ret
823+
}
824+
825+
/// If we receive an error message, it may only be a rejection of the channel type we tried,
826+
/// not of our ability to open any channel at all. Thus, on error, we should first call this
827+
/// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
828+
pub(crate) fn maybe_handle_error_without_close(&mut self, chain_hash: BlockHash) -> Result<msgs::OpenChannel, ()> {
829+
if !self.is_outbound() || self.channel_state != ChannelState::OurInitSent as u32 { return Err(()); }
830+
if self.channel_type == ChannelTypeFeatures::only_static_remote_key() {
831+
// We've exhausted our options
832+
return Err(());
833+
}
834+
self.channel_type = ChannelTypeFeatures::only_static_remote_key(); // We only currently support two types
835+
Ok(self.get_open_channel(chain_hash))
836+
}
837+
813838
// Constructors:
814839
pub fn new_outbound<K: Deref, F: Deref>(
815840
fee_estimator: &F, keys_provider: &K, counterparty_node_id: PublicKey, their_features: &InitFeatures,
@@ -967,10 +992,7 @@ impl<Signer: Sign> Channel<Signer> {
967992
#[cfg(any(test, fuzzing))]
968993
historical_inbound_htlc_fulfills: HashSet::new(),
969994

970-
// We currently only actually support one channel type, so don't retry with new types
971-
// on error messages. When we support more we'll need fallback support (assuming we
972-
// want to support old types).
973-
channel_type: ChannelTypeFeatures::only_static_remote_key(),
995+
channel_type: Self::get_initial_channel_type(&config),
974996
})
975997
}
976998

@@ -1009,15 +1031,26 @@ impl<Signer: Sign> Channel<Signer> {
10091031
L::Target: Logger,
10101032
{
10111033
let opt_anchors = false; // TODO - should be based on features
1034+
let announced_channel = if (msg.channel_flags & 1) == 1 { true } else { false };
10121035

10131036
// First check the channel type is known, failing before we do anything else if we don't
10141037
// support this channel type.
10151038
let channel_type = if let Some(channel_type) = &msg.channel_type {
10161039
if channel_type.supports_any_optional_bits() {
10171040
return Err(ChannelError::Close("Channel Type field contained optional bits - this is not allowed".to_owned()));
10181041
}
1019-
if *channel_type != ChannelTypeFeatures::only_static_remote_key() {
1020-
return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
1042+
// We currently only allow two channel types, so write it all out here - we allow
1043+
// `only_static_remote_key` in all contexts, and further allow
1044+
// `static_remote_key|scid_privacy` if the channel is not publicly announced.
1045+
let mut allowed_type = ChannelTypeFeatures::only_static_remote_key();
1046+
if *channel_type != allowed_type {
1047+
allowed_type.set_scid_privacy_required();
1048+
if *channel_type != allowed_type {
1049+
return Err(ChannelError::Close("Channel Type was not understood".to_owned()));
1050+
}
1051+
if announced_channel {
1052+
return Err(ChannelError::Close("SCID Alias/Privacy Channel Type cannot be set on a public channel".to_owned()));
1053+
}
10211054
}
10221055
channel_type.clone()
10231056
} else {
@@ -1098,14 +1131,13 @@ impl<Signer: Sign> Channel<Signer> {
10981131

10991132
// Convert things into internal flags and prep our state:
11001133

1101-
let announce = if (msg.channel_flags & 1) == 1 { true } else { false };
11021134
if config.peer_channel_config_limits.force_announced_channel_preference {
1103-
if local_config.announced_channel != announce {
1135+
if local_config.announced_channel != announced_channel {
11041136
return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours".to_owned()));
11051137
}
11061138
}
11071139
// we either accept their preference or the preferences match
1108-
local_config.announced_channel = announce;
1140+
local_config.announced_channel = announced_channel;
11091141

11101142
let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(msg.funding_satoshis);
11111143
if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
@@ -4232,6 +4264,11 @@ impl<Signer: Sign> Channel<Signer> {
42324264
self.user_id
42334265
}
42344266

4267+
/// Gets the channel's type
4268+
pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
4269+
&self.channel_type
4270+
}
4271+
42354272
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
42364273
/// is_usable() returns true).
42374274
/// Allowed in any state (including after shutdown)

0 commit comments

Comments
 (0)