@@ -810,6 +810,31 @@ impl<Signer: Sign> Channel<Signer> {
810
810
self . channel_transaction_parameters . opt_anchors . is_some ( )
811
811
}
812
812
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
+
813
838
// Constructors:
814
839
pub fn new_outbound < K : Deref , F : Deref > (
815
840
fee_estimator : & F , keys_provider : & K , counterparty_node_id : PublicKey , their_features : & InitFeatures ,
@@ -967,10 +992,7 @@ impl<Signer: Sign> Channel<Signer> {
967
992
#[ cfg( any( test, fuzzing) ) ]
968
993
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
969
994
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) ,
974
996
} )
975
997
}
976
998
@@ -1009,15 +1031,26 @@ impl<Signer: Sign> Channel<Signer> {
1009
1031
L :: Target : Logger ,
1010
1032
{
1011
1033
let opt_anchors = false ; // TODO - should be based on features
1034
+ let announced_channel = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
1012
1035
1013
1036
// First check the channel type is known, failing before we do anything else if we don't
1014
1037
// support this channel type.
1015
1038
let channel_type = if let Some ( channel_type) = & msg. channel_type {
1016
1039
if channel_type. supports_any_optional_bits ( ) {
1017
1040
return Err ( ChannelError :: Close ( "Channel Type field contained optional bits - this is not allowed" . to_owned ( ) ) ) ;
1018
1041
}
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
+ }
1021
1054
}
1022
1055
channel_type. clone ( )
1023
1056
} else {
@@ -1098,14 +1131,13 @@ impl<Signer: Sign> Channel<Signer> {
1098
1131
1099
1132
// Convert things into internal flags and prep our state:
1100
1133
1101
- let announce = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
1102
1134
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 {
1104
1136
return Err ( ChannelError :: Close ( "Peer tried to open channel but their announcement preference is different from ours" . to_owned ( ) ) ) ;
1105
1137
}
1106
1138
}
1107
1139
// we either accept their preference or the preferences match
1108
- local_config. announced_channel = announce ;
1140
+ local_config. announced_channel = announced_channel ;
1109
1141
1110
1142
let holder_selected_channel_reserve_satoshis = Channel :: < Signer > :: get_holder_selected_channel_reserve_satoshis ( msg. funding_satoshis ) ;
1111
1143
if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
@@ -4232,6 +4264,11 @@ impl<Signer: Sign> Channel<Signer> {
4232
4264
self . user_id
4233
4265
}
4234
4266
4267
+ /// Gets the channel's type
4268
+ pub fn get_channel_type ( & self ) -> & ChannelTypeFeatures {
4269
+ & self . channel_type
4270
+ }
4271
+
4235
4272
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
4236
4273
/// is_usable() returns true).
4237
4274
/// Allowed in any state (including after shutdown)
0 commit comments