@@ -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 ;
@@ -259,18 +260,20 @@ const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 |
259
260
260
261
const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
261
262
263
+
262
264
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
263
265
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
264
266
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
265
267
// inbound channel.
266
268
pub ( super ) struct Channel {
269
+ config : ChannelOptions ,
270
+
267
271
user_id : u64 ,
268
272
269
273
channel_id : [ u8 ; 32 ] ,
270
274
channel_state : u32 ,
271
275
channel_outbound : bool ,
272
276
secp_ctx : Secp256k1 < secp256k1:: All > ,
273
- announce_publicly : bool ,
274
277
channel_value_satoshis : u64 ,
275
278
276
279
local_keys : ChannelKeys ,
@@ -421,7 +424,7 @@ impl Channel {
421
424
}
422
425
423
426
// Constructors:
424
- 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 > {
427
+ 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 > {
425
428
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
426
429
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
427
430
}
@@ -448,12 +451,12 @@ impl Channel {
448
451
449
452
Ok ( Channel {
450
453
user_id : user_id,
454
+ config : configurations. channel_options . clone ( ) ,
451
455
452
456
channel_id : rng:: rand_u832 ( ) ,
453
457
channel_state : ChannelState :: OurInitSent as u32 ,
454
458
channel_outbound : true ,
455
459
secp_ctx : secp_ctx,
456
- announce_publicly : announce_publicly,
457
460
channel_value_satoshis : channel_value_satoshis,
458
461
459
462
local_keys : chan_keys,
@@ -520,12 +523,13 @@ impl Channel {
520
523
/// Assumes chain_hash has already been checked and corresponds with what we expect!
521
524
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
522
525
/// that we're rejecting the new channel.
523
- 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 , HandleError > {
526
+ 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 , HandleError > {
524
527
macro_rules! return_error_message {
525
528
( $msg: expr ) => {
526
529
return Err ( HandleError { err: $msg, action: Some ( msgs:: ErrorAction :: SendErrorMessage { msg: msgs:: ErrorMessage { channel_id: msg. temporary_channel_id, data: $msg. to_string( ) } } ) } ) ;
527
530
}
528
531
}
532
+ let mut local_config = ( * configurations) . channel_options . clone ( ) ;
529
533
530
534
// Check sanity of message fields:
531
535
if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
@@ -559,16 +563,40 @@ impl Channel {
559
563
if msg. max_accepted_htlcs > 483 {
560
564
return_error_message ! ( "max_accpted_htlcs > 483" ) ;
561
565
}
566
+ //optional parameter checking
567
+ // MAY fail the channel if
568
+ if msg. funding_satoshis < configurations. channel_limits . funding_satoshis {
569
+ return_error_message ! ( "funding satoshis is less than the user specified limit" ) ;
570
+ }
571
+ if msg. htlc_minimum_msat > configurations. channel_limits . htlc_minimum_msat {
572
+ return_error_message ! ( "htlc minimum msat is higher than the user specified limit" ) ;
573
+ }
574
+ if msg. max_htlc_value_in_flight_msat < configurations. channel_limits . max_htlc_value_in_flight_msat {
575
+ return_error_message ! ( "max htlc value in flight msat is less than the user specified limit" ) ;
576
+ }
577
+ if msg. channel_reserve_satoshis > configurations. channel_limits . channel_reserve_satoshis {
578
+ return_error_message ! ( "channel reserve satoshis is higher than the user specified limit" ) ;
579
+ }
580
+ if msg. max_accepted_htlcs < configurations. channel_limits . max_accepted_htlcs {
581
+ return_error_message ! ( "max accepted htlcs is less than the user specified limit" ) ;
582
+ }
583
+ if msg. dust_limit_satoshis < configurations. channel_limits . dust_limit_satoshis {
584
+ return_error_message ! ( "dust limit satoshis is less than the user specified limit" ) ;
585
+ }
562
586
563
587
// Convert things into internal flags and prep our state:
564
588
565
589
let their_announce = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
566
- if require_announce && !their_announce {
567
- return_error_message ! ( "Peer tried to open unannounced channel, but we require public ones" ) ;
568
- }
569
- if !allow_announce && their_announce {
570
- return_error_message ! ( "Peer tried to open announced channel, but we require private ones" ) ;
590
+ if local_config. force_announced_channel_preference {
591
+ if local_config. announced_channel && !their_announce {
592
+ return_error_message ! ( "Peer tried to open unannounced channel, but we require public ones" ) ;
593
+ }
594
+ if !local_config. announced_channel && their_announce {
595
+ return_error_message ! ( "Peer tried to open announced channel, but we require private ones" ) ;
596
+ }
571
597
}
598
+ //we either accept their preference or the preferences match
599
+ local_config. announced_channel = their_announce;
572
600
573
601
let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
574
602
@@ -609,12 +637,12 @@ impl Channel {
609
637
610
638
let mut chan = Channel {
611
639
user_id : user_id,
640
+ config : local_config,
612
641
613
642
channel_id : msg. temporary_channel_id ,
614
643
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
615
644
channel_outbound : false ,
616
645
secp_ctx : secp_ctx,
617
- announce_publicly : their_announce,
618
646
619
647
local_keys : chan_keys,
620
648
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1224,7 +1252,7 @@ impl Channel {
1224
1252
1225
1253
// Message handlers:
1226
1254
1227
- pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel ) -> Result < ( ) , HandleError > {
1255
+ pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel , config : & UserConfigurations ) -> Result < ( ) , HandleError > {
1228
1256
macro_rules! return_error_message {
1229
1257
( $msg: expr ) => {
1230
1258
return Err ( HandleError { err: $msg, action: Some ( msgs:: ErrorAction :: SendErrorMessage { msg: msgs:: ErrorMessage { channel_id: msg. temporary_channel_id, data: $msg. to_string( ) } } ) } ) ;
@@ -1267,16 +1295,10 @@ impl Channel {
1267
1295
if msg. max_accepted_htlcs > 483 {
1268
1296
return_error_message ! ( "max_accpted_htlcs > 483" ) ;
1269
1297
}
1270
-
1271
- // TODO: Optional additional constraints mentioned in the spec
1272
- // MAY fail the channel if
1273
- // funding_satoshi is too small
1274
- // htlc_minimum_msat too large
1275
- // max_htlc_value_in_flight_msat too small
1276
- // channel_reserve_satoshis too large
1277
- // max_accepted_htlcs too small
1278
- // dust_limit_satoshis too small
1279
-
1298
+ //Optional user definined limits
1299
+ if msg. minimum_depth > config. channel_limits . minimum_depth {
1300
+ return_error_message ! ( "We consider the minimum depth to be unreasonably large" ) ;
1301
+ }
1280
1302
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1281
1303
1282
1304
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2358,7 +2380,7 @@ impl Channel {
2358
2380
}
2359
2381
2360
2382
pub fn should_announce ( & self ) -> bool {
2361
- self . announce_publicly
2383
+ self . config . announced_channel
2362
2384
}
2363
2385
2364
2386
pub fn is_outbound ( & self ) -> bool {
@@ -2548,7 +2570,7 @@ impl Channel {
2548
2570
delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
2549
2571
htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
2550
2572
first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret) ,
2551
- channel_flags : if self . announce_publicly { 1 } else { 0 } ,
2573
+ channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
2552
2574
shutdown_scriptpubkey : None ,
2553
2575
}
2554
2576
}
@@ -2652,7 +2674,7 @@ impl Channel {
2652
2674
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
2653
2675
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
2654
2676
pub fn get_channel_announcement ( & self , our_node_id : PublicKey , chain_hash : Sha256dHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , HandleError > {
2655
- if !self . announce_publicly {
2677
+ if !self . config . announced_channel {
2656
2678
return Err ( HandleError { err : "Channel is not available for public announcements" , action : Some ( msgs:: ErrorAction :: IgnoreError ) } ) ;
2657
2679
}
2658
2680
if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -2999,6 +3021,7 @@ mod tests {
2999
3021
3000
3022
#[ test]
3001
3023
fn outbound_commitment_test ( ) {
3024
+ use util:: configurations:: UserConfigurations ;
3002
3025
// Test vectors from BOLT 3 Appendix C:
3003
3026
let feeest = TestFeeEstimator { fee_est : 15000 } ;
3004
3027
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -3020,7 +3043,9 @@ mod tests {
3020
3043
hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
3021
3044
3022
3045
let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
3023
- 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
3046
+ let mut config = UserConfigurations :: new ( ) ;
3047
+ config. channel_options . announced_channel = false ;
3048
+ 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
3024
3049
chan. their_to_self_delay = 144 ;
3025
3050
chan. our_dust_limit_satoshis = 546 ;
3026
3051
0 commit comments