@@ -26,6 +26,7 @@ use util::ser::Writeable;
2626use util:: sha2:: Sha256 ;
2727use util:: logger:: Logger ;
2828use util:: errors:: APIError ;
29+ use util:: configurations:: { UserConfigurations , ChannelLimits , ChannelOptions } ;
2930
3031use std;
3132use std:: default:: Default ;
@@ -273,18 +274,20 @@ const MULTI_STATE_FLAGS: u32 = (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDis
273274
274275const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
275276
277+
276278// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
277279// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
278280// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
279281// inbound channel.
280282pub ( super ) struct Channel {
283+ config : ChannelOptions ,
284+
281285 user_id : u64 ,
282286
283287 channel_id : [ u8 ; 32 ] ,
284288 channel_state : u32 ,
285289 channel_outbound : bool ,
286290 secp_ctx : Secp256k1 < secp256k1:: All > ,
287- announce_publicly : bool ,
288291 channel_value_satoshis : u64 ,
289292
290293 local_keys : ChannelKeys ,
@@ -453,7 +456,7 @@ impl Channel {
453456 }
454457
455458 // Constructors:
456- pub fn new_outbound ( fee_estimator : & FeeEstimator , chan_keys : ChannelKeys , their_node_id : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , announce_publicly : bool , user_id : u64 , logger : Arc < Logger > ) -> Result < Channel , APIError > {
459+ pub fn new_outbound ( fee_estimator : & FeeEstimator , chan_keys : ChannelKeys , their_node_id : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 , logger : Arc < Logger > , configurations : & UserConfigurations ) -> Result < Channel , APIError > {
457460 if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
458461 return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
459462 }
@@ -480,12 +483,12 @@ impl Channel {
480483
481484 Ok ( Channel {
482485 user_id : user_id,
486+ config : configurations. channel_options . clone ( ) ,
483487
484488 channel_id : rng:: rand_u832 ( ) ,
485489 channel_state : ChannelState :: OurInitSent as u32 ,
486490 channel_outbound : true ,
487491 secp_ctx : secp_ctx,
488- announce_publicly : announce_publicly,
489492 channel_value_satoshis : channel_value_satoshis,
490493
491494 local_keys : chan_keys,
@@ -555,7 +558,8 @@ impl Channel {
555558
556559 /// Creates a new channel from a remote sides' request for one.
557560 /// Assumes chain_hash has already been checked and corresponds with what we expect!
558- pub fn new_from_req ( fee_estimator : & FeeEstimator , chan_keys : ChannelKeys , their_node_id : PublicKey , msg : & msgs:: OpenChannel , user_id : u64 , require_announce : bool , allow_announce : bool , logger : Arc < Logger > ) -> Result < Channel , ChannelError > {
561+ pub fn new_from_req ( fee_estimator : & FeeEstimator , chan_keys : ChannelKeys , their_node_id : PublicKey , msg : & msgs:: OpenChannel , user_id : u64 , logger : Arc < Logger > , configurations : & UserConfigurations ) -> Result < Channel , ChannelError > {
562+ let mut local_config = ( * configurations) . channel_options . clone ( ) ;
559563 // Check sanity of message fields:
560564 if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
561565 return Err ( ChannelError :: Close ( "funding value > 2^24" ) ) ;
@@ -586,16 +590,40 @@ impl Channel {
586590 if msg. max_accepted_htlcs > 483 {
587591 return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
588592 }
593+ //optional parameter checking
594+ // MAY fail the channel if
595+ if msg. funding_satoshis < configurations. channel_limits . funding_satoshis {
596+ return Err ( ChannelError :: Close ( "funding satoshis is less than the user specified limit" ) ) ;
597+ }
598+ if msg. htlc_minimum_msat > configurations. channel_limits . htlc_minimum_msat {
599+ return Err ( ChannelError :: Close ( "htlc minimum msat is higher than the user specified limit" ) ) ;
600+ }
601+ if msg. max_htlc_value_in_flight_msat < configurations. channel_limits . max_htlc_value_in_flight_msat {
602+ return Err ( ChannelError :: Close ( "max htlc value in flight msat is less than the user specified limit" ) ) ;
603+ }
604+ if msg. channel_reserve_satoshis > configurations. channel_limits . channel_reserve_satoshis {
605+ return Err ( ChannelError :: Close ( "channel reserve satoshis is higher than the user specified limit" ) ) ;
606+ }
607+ if msg. max_accepted_htlcs < configurations. channel_limits . max_accepted_htlcs {
608+ return Err ( ChannelError :: Close ( "max accepted htlcs is less than the user specified limit" ) ) ;
609+ }
610+ if msg. dust_limit_satoshis < configurations. channel_limits . dust_limit_satoshis {
611+ return Err ( ChannelError :: Close ( "dust limit satoshis is less than the user specified limit" ) ) ;
612+ }
589613
590614 // Convert things into internal flags and prep our state:
591615
592616 let their_announce = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
593- if require_announce && !their_announce {
594- return Err ( ChannelError :: Close ( "Peer tried to open unannounced channel, but we require public ones" ) ) ;
595- }
596- if !allow_announce && their_announce {
597- return Err ( ChannelError :: Close ( "Peer tried to open announced channel, but we require private ones" ) ) ;
617+ if local_config. force_announced_channel_preference {
618+ if local_config. announced_channel && !their_announce {
619+ return Err ( ChannelError :: Close ( "Peer tried to open unannounced channel, but we require public ones" ) ) ;
620+ }
621+ if !local_config. announced_channel && their_announce {
622+ return Err ( ChannelError :: Close ( "Peer tried to open announced channel, but we require private ones" ) ) ;
623+ }
598624 }
625+ //we either accept their preference or the preferences match
626+ local_config. announced_channel = their_announce;
599627
600628 let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
601629
@@ -636,12 +664,12 @@ impl Channel {
636664
637665 let mut chan = Channel {
638666 user_id : user_id,
667+ config : local_config,
639668
640669 channel_id : msg. temporary_channel_id ,
641670 channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
642671 channel_outbound : false ,
643672 secp_ctx : secp_ctx,
644- announce_publicly : their_announce,
645673
646674 local_keys : chan_keys,
647675 cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1282,7 +1310,7 @@ impl Channel {
12821310
12831311 // Message handlers:
12841312
1285- pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel ) -> Result < ( ) , ChannelError > {
1313+ pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel , config : & UserConfigurations ) -> Result < ( ) , ChannelError > {
12861314 // Check sanity of message fields:
12871315 if !self . channel_outbound {
12881316 return Err ( ChannelError :: Close ( "Got an accept_channel message from an inbound peer" ) ) ;
@@ -1320,16 +1348,10 @@ impl Channel {
13201348 if msg. max_accepted_htlcs > 483 {
13211349 return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
13221350 }
1323-
1324- // TODO: Optional additional constraints mentioned in the spec
1325- // MAY fail the channel if
1326- // funding_satoshi is too small
1327- // htlc_minimum_msat too large
1328- // max_htlc_value_in_flight_msat too small
1329- // channel_reserve_satoshis too large
1330- // max_accepted_htlcs too small
1331- // dust_limit_satoshis too small
1332-
1351+ //Optional user definined limits
1352+ if msg. minimum_depth > config. channel_limits . minimum_depth {
1353+ return Err ( ChannelError :: Close ( "We consider the minimum depth to be unreasonably large" ) ) ;
1354+ }
13331355 self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
13341356
13351357 self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2493,7 +2515,7 @@ impl Channel {
24932515 }
24942516
24952517 pub fn should_announce ( & self ) -> bool {
2496- self . announce_publicly
2518+ self . config . announced_channel
24972519 }
24982520
24992521 pub fn is_outbound ( & self ) -> bool {
@@ -2683,7 +2705,7 @@ impl Channel {
26832705 delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
26842706 htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
26852707 first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret) ,
2686- channel_flags : if self . announce_publicly { 1 } else { 0 } ,
2708+ channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
26872709 shutdown_scriptpubkey : None ,
26882710 }
26892711 }
@@ -2787,7 +2809,7 @@ impl Channel {
27872809 /// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
27882810 /// https://github.com/lightningnetwork/lightning-rfc/issues/468
27892811 pub fn get_channel_announcement ( & self , our_node_id : PublicKey , chain_hash : Sha256dHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , ChannelError > {
2790- if !self . announce_publicly {
2812+ if !self . config . announced_channel {
27912813 return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" ) ) ;
27922814 }
27932815 if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -3154,6 +3176,7 @@ mod tests {
31543176
31553177 #[ test]
31563178 fn outbound_commitment_test ( ) {
3179+ use util:: configurations:: UserConfigurations ;
31573180 // Test vectors from BOLT 3 Appendix C:
31583181 let feeest = TestFeeEstimator { fee_est : 15000 } ;
31593182 let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -3175,7 +3198,9 @@ mod tests {
31753198 hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
31763199
31773200 let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
3178- let mut chan = Channel :: new_outbound ( & feeest, chan_keys, their_node_id, 10000000 , 100000 , false , 42 , Arc :: clone ( & logger) ) . unwrap ( ) ; // Nothing uses their network key in this test
3201+ let mut config = UserConfigurations :: new ( ) ;
3202+ config. channel_options . announced_channel = false ;
3203+ let mut chan = Channel :: new_outbound ( & feeest, chan_keys, their_node_id, 10000000 , 100000 , 42 , Arc :: clone ( & logger) , & config) . unwrap ( ) ; // Nothing uses their network key in this test
31793204 chan. their_to_self_delay = 144 ;
31803205 chan. our_dust_limit_satoshis = 546 ;
31813206
0 commit comments