@@ -26,6 +26,7 @@ use util::ser::Writeable;
26
26
use util:: sha2:: Sha256 ;
27
27
use util:: logger:: Logger ;
28
28
use util:: errors:: APIError ;
29
+ use util:: configurations:: { UserConfigurations , ChannelLimits , ChannelOptions } ;
29
30
30
31
use std;
31
32
use std:: default:: Default ;
@@ -273,18 +274,20 @@ const MULTI_STATE_FLAGS: u32 = (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDis
273
274
274
275
const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
275
276
277
+
276
278
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
277
279
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
278
280
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
279
281
// inbound channel.
280
282
pub ( super ) struct Channel {
283
+ config : ChannelOptions ,
284
+
281
285
user_id : u64 ,
282
286
283
287
channel_id : [ u8 ; 32 ] ,
284
288
channel_state : u32 ,
285
289
channel_outbound : bool ,
286
290
secp_ctx : Secp256k1 < secp256k1:: All > ,
287
- announce_publicly : bool ,
288
291
channel_value_satoshis : u64 ,
289
292
290
293
local_keys : ChannelKeys ,
@@ -453,7 +456,7 @@ impl Channel {
453
456
}
454
457
455
458
// 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 > {
457
460
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
458
461
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
459
462
}
@@ -480,12 +483,12 @@ impl Channel {
480
483
481
484
Ok ( Channel {
482
485
user_id : user_id,
486
+ config : configurations. channel_options . clone ( ) ,
483
487
484
488
channel_id : rng:: rand_u832 ( ) ,
485
489
channel_state : ChannelState :: OurInitSent as u32 ,
486
490
channel_outbound : true ,
487
491
secp_ctx : secp_ctx,
488
- announce_publicly : announce_publicly,
489
492
channel_value_satoshis : channel_value_satoshis,
490
493
491
494
local_keys : chan_keys,
@@ -555,7 +558,8 @@ impl Channel {
555
558
556
559
/// Creates a new channel from a remote sides' request for one.
557
560
/// 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 ( ) ;
559
563
// Check sanity of message fields:
560
564
if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
561
565
return Err ( ChannelError :: Close ( "funding value > 2^24" ) ) ;
@@ -586,16 +590,40 @@ impl Channel {
586
590
if msg. max_accepted_htlcs > 483 {
587
591
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
588
592
}
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
+ }
589
613
590
614
// Convert things into internal flags and prep our state:
591
615
592
616
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
+ }
598
624
}
625
+ //we either accept their preference or the preferences match
626
+ local_config. announced_channel = their_announce;
599
627
600
628
let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
601
629
@@ -636,12 +664,12 @@ impl Channel {
636
664
637
665
let mut chan = Channel {
638
666
user_id : user_id,
667
+ config : local_config,
639
668
640
669
channel_id : msg. temporary_channel_id ,
641
670
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
642
671
channel_outbound : false ,
643
672
secp_ctx : secp_ctx,
644
- announce_publicly : their_announce,
645
673
646
674
local_keys : chan_keys,
647
675
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1282,7 +1310,7 @@ impl Channel {
1282
1310
1283
1311
// Message handlers:
1284
1312
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 > {
1286
1314
// Check sanity of message fields:
1287
1315
if !self . channel_outbound {
1288
1316
return Err ( ChannelError :: Close ( "Got an accept_channel message from an inbound peer" ) ) ;
@@ -1320,16 +1348,10 @@ impl Channel {
1320
1348
if msg. max_accepted_htlcs > 483 {
1321
1349
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
1322
1350
}
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
+ }
1333
1355
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1334
1356
1335
1357
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2493,7 +2515,7 @@ impl Channel {
2493
2515
}
2494
2516
2495
2517
pub fn should_announce ( & self ) -> bool {
2496
- self . announce_publicly
2518
+ self . config . announced_channel
2497
2519
}
2498
2520
2499
2521
pub fn is_outbound ( & self ) -> bool {
@@ -2683,7 +2705,7 @@ impl Channel {
2683
2705
delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
2684
2706
htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
2685
2707
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 } ,
2687
2709
shutdown_scriptpubkey : None ,
2688
2710
}
2689
2711
}
@@ -2787,7 +2809,7 @@ impl Channel {
2787
2809
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
2788
2810
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
2789
2811
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 {
2791
2813
return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" ) ) ;
2792
2814
}
2793
2815
if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -3154,6 +3176,7 @@ mod tests {
3154
3176
3155
3177
#[ test]
3156
3178
fn outbound_commitment_test ( ) {
3179
+ use util:: configurations:: UserConfigurations ;
3157
3180
// Test vectors from BOLT 3 Appendix C:
3158
3181
let feeest = TestFeeEstimator { fee_est : 15000 } ;
3159
3182
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -3175,7 +3198,9 @@ mod tests {
3175
3198
hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
3176
3199
3177
3200
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
3179
3204
chan. their_to_self_delay = 144 ;
3180
3205
chan. our_dust_limit_satoshis = 546 ;
3181
3206
0 commit comments