@@ -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 ;
29
30
30
31
use std;
31
32
use std:: default:: Default ;
@@ -259,11 +260,14 @@ 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
+
270
+ config : UserConfigurations ,
267
271
user_id : u64 ,
268
272
269
273
channel_id : [ u8 ; 32 ] ,
@@ -403,7 +407,7 @@ impl Channel {
403
407
}
404
408
405
409
// Constructors:
406
- 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 > {
410
+ 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 > , configurations : & UserConfigurations ) -> Result < Channel , APIError > {
407
411
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
408
412
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
409
413
}
@@ -430,7 +434,7 @@ impl Channel {
430
434
431
435
Ok ( Channel {
432
436
user_id : user_id,
433
-
437
+ config : configurations . clone ( ) ,
434
438
channel_id : rng:: rand_u832 ( ) ,
435
439
channel_state : ChannelState :: OurInitSent as u32 ,
436
440
channel_outbound : true ,
@@ -500,7 +504,7 @@ impl Channel {
500
504
/// Assumes chain_hash has already been checked and corresponds with what we expect!
501
505
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
502
506
/// that we're rejecting the new channel.
503
- 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 > {
507
+ 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 > , configurations : & UserConfigurations ) -> Result < Channel , HandleError > {
504
508
macro_rules! return_error_message {
505
509
( $msg: expr ) => {
506
510
return Err ( HandleError { err: $msg, action: Some ( msgs:: ErrorAction :: SendErrorMessage { msg: msgs:: ErrorMessage { channel_id: msg. temporary_channel_id, data: $msg. to_string( ) } } ) } ) ;
@@ -539,6 +543,26 @@ impl Channel {
539
543
if msg. max_accepted_htlcs > 483 {
540
544
return_error_message ! ( "max_accpted_htlcs > 483" ) ;
541
545
}
546
+ //optional parameter checking
547
+ // MAY fail the channel if
548
+ if msg. funding_satoshis < configurations. channel_limits . funding_satoshis {
549
+ return_error_message ! ( "funding satoshis is less than the user specified limit" ) ;
550
+ }
551
+ if msg. htlc_minimum_msat > configurations. channel_limits . htlc_minimum_msat {
552
+ return_error_message ! ( "htlc minimum msat is higher than the user specified limit" ) ;
553
+ }
554
+ if msg. max_htlc_value_in_flight_msat < configurations. channel_limits . max_htlc_value_in_flight_msat {
555
+ return_error_message ! ( "max htlc value in flight msat is less than the user specified limit" ) ;
556
+ }
557
+ if msg. channel_reserve_satoshis > configurations. channel_limits . channel_reserve_satoshis {
558
+ return_error_message ! ( "channel reserve satoshis is higher than the user specified limit" ) ;
559
+ }
560
+ if msg. max_accepted_htlcs < configurations. channel_limits . max_accepted_htlcs {
561
+ return_error_message ! ( "max accepted htlcs is less than the user specified limit" ) ;
562
+ }
563
+ if msg. dust_limit_satoshis < configurations. channel_limits . dust_limit_satoshis {
564
+ return_error_message ! ( "dust limit satoshis is less than the user specified limit" ) ;
565
+ }
542
566
543
567
// Convert things into internal flags and prep our state:
544
568
@@ -589,7 +613,7 @@ impl Channel {
589
613
590
614
let mut chan = Channel {
591
615
user_id : user_id,
592
-
616
+ config : ( * configurations ) . clone ( ) ,
593
617
channel_id : msg. temporary_channel_id ,
594
618
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
595
619
channel_outbound : false ,
@@ -1245,15 +1269,6 @@ impl Channel {
1245
1269
return_error_message ! ( "max_accpted_htlcs > 483" ) ;
1246
1270
}
1247
1271
1248
- // TODO: Optional additional constraints mentioned in the spec
1249
- // MAY fail the channel if
1250
- // funding_satoshi is too small
1251
- // htlc_minimum_msat too large
1252
- // max_htlc_value_in_flight_msat too small
1253
- // channel_reserve_satoshis too large
1254
- // max_accepted_htlcs too small
1255
- // dust_limit_satoshis too small
1256
-
1257
1272
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1258
1273
1259
1274
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2872,6 +2887,7 @@ mod tests {
2872
2887
2873
2888
#[ test]
2874
2889
fn outbound_commitment_test ( ) {
2890
+ use util:: configurations:: UserConfigurations ;
2875
2891
// Test vectors from BOLT 3 Appendix C:
2876
2892
let feeest = TestFeeEstimator { fee_est : 15000 } ;
2877
2893
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -2893,7 +2909,7 @@ mod tests {
2893
2909
hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
2894
2910
2895
2911
let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
2896
- 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
2912
+ let mut chan = Channel :: new_outbound ( & feeest, chan_keys, their_node_id, 10000000 , 100000 , false , 42 , Arc :: clone ( & logger) , & UserConfigurations :: new ( ) ) . unwrap ( ) ; // Nothing uses their network key in this test
2897
2913
chan. their_to_self_delay = 144 ;
2898
2914
chan. our_dust_limit_satoshis = 546 ;
2899
2915
0 commit comments