@@ -25,6 +25,7 @@ use util::{transaction_utils,rng};
25
25
use util:: sha2:: Sha256 ;
26
26
use util:: logger:: Logger ;
27
27
use util:: errors:: APIError ;
28
+ use util:: configurations:: UserConfigurations ;
28
29
29
30
use std;
30
31
use std:: default:: Default ;
@@ -258,11 +259,14 @@ const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 |
258
259
259
260
const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
260
261
262
+
261
263
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
262
264
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
263
265
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
264
266
// inbound channel.
265
267
pub struct Channel {
268
+
269
+ config : UserConfigurations ,
266
270
user_id : u64 ,
267
271
268
272
channel_id : [ u8 ; 32 ] ,
@@ -402,7 +406,7 @@ impl Channel {
402
406
}
403
407
404
408
// Constructors:
405
- 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 > {
409
+ 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 > {
406
410
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
407
411
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
408
412
}
@@ -429,7 +433,7 @@ impl Channel {
429
433
430
434
Ok ( Channel {
431
435
user_id : user_id,
432
-
436
+ config : configurations . clone ( ) ,
433
437
channel_id : rng:: rand_u832 ( ) ,
434
438
channel_state : ChannelState :: OurInitSent as u32 ,
435
439
channel_outbound : true ,
@@ -499,7 +503,7 @@ impl Channel {
499
503
/// Assumes chain_hash has already been checked and corresponds with what we expect!
500
504
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
501
505
/// that we're rejecting the new channel.
502
- 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 > {
506
+ 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 > {
503
507
macro_rules! return_error_message {
504
508
( $msg: expr ) => {
505
509
return Err ( HandleError { err: $msg, action: Some ( msgs:: ErrorAction :: SendErrorMessage { msg: msgs:: ErrorMessage { channel_id: msg. temporary_channel_id, data: $msg. to_string( ) } } ) } ) ;
@@ -538,6 +542,26 @@ impl Channel {
538
542
if msg. max_accepted_htlcs > 483 {
539
543
return_error_message ! ( "max_accpted_htlcs > 483" ) ;
540
544
}
545
+ //optional parameter checking
546
+ // MAY fail the channel if
547
+ if msg. funding_satoshis < configurations. channel_limits . funding_satoshis {
548
+ return_error_message ! ( "funding satoshis is less than the user specified limit" ) ;
549
+ }
550
+ if msg. htlc_minimum_msat > configurations. channel_limits . htlc_minimum_msat {
551
+ return_error_message ! ( "htlc minimum msat is higher than the user specified limit" ) ;
552
+ }
553
+ if msg. max_htlc_value_in_flight_msat < configurations. channel_limits . max_htlc_value_in_flight_msat {
554
+ return_error_message ! ( "max htlc value in flight msat is less than the user specified limit" ) ;
555
+ }
556
+ if msg. channel_reserve_satoshis > configurations. channel_limits . channel_reserve_satoshis {
557
+ return_error_message ! ( "channel reserve satoshis is higher than the user specified limit" ) ;
558
+ }
559
+ if msg. max_accepted_htlcs < configurations. channel_limits . max_accepted_htlcs {
560
+ return_error_message ! ( "max accepted htlcs is less than the user specified limit" ) ;
561
+ }
562
+ if msg. dust_limit_satoshis < configurations. channel_limits . dust_limit_satoshis {
563
+ return_error_message ! ( "dust limit satoshis is less than the user specified limit" ) ;
564
+ }
541
565
542
566
// Convert things into internal flags and prep our state:
543
567
@@ -588,7 +612,7 @@ impl Channel {
588
612
589
613
let mut chan = Channel {
590
614
user_id : user_id,
591
-
615
+ config : ( * configurations ) . clone ( ) ,
592
616
channel_id : msg. temporary_channel_id ,
593
617
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
594
618
channel_outbound : false ,
@@ -1244,15 +1268,6 @@ impl Channel {
1244
1268
return_error_message ! ( "max_accpted_htlcs > 483" ) ;
1245
1269
}
1246
1270
1247
- // TODO: Optional additional constraints mentioned in the spec
1248
- // MAY fail the channel if
1249
- // funding_satoshi is too small
1250
- // htlc_minimum_msat too large
1251
- // max_htlc_value_in_flight_msat too small
1252
- // channel_reserve_satoshis too large
1253
- // max_accepted_htlcs too small
1254
- // dust_limit_satoshis too small
1255
-
1256
1271
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1257
1272
1258
1273
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2866,6 +2881,7 @@ mod tests {
2866
2881
2867
2882
#[ test]
2868
2883
fn outbound_commitment_test ( ) {
2884
+ use util:: configurations:: UserConfigurations ;
2869
2885
// Test vectors from BOLT 3 Appendix C:
2870
2886
let feeest = TestFeeEstimator { fee_est : 15000 } ;
2871
2887
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -2887,7 +2903,7 @@ mod tests {
2887
2903
hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
2888
2904
2889
2905
let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
2890
- 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
2906
+ 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
2891
2907
chan. their_to_self_delay = 144 ;
2892
2908
chan. our_dust_limit_satoshis = 546 ;
2893
2909
0 commit comments