@@ -28,6 +28,7 @@ use util::ser::{Readable, ReadableArgs, Writeable, Writer, WriterWriteAdaptor};
28
28
use util:: sha2:: Sha256 ;
29
29
use util:: logger:: Logger ;
30
30
use util:: errors:: APIError ;
31
+ use util:: configurations:: { UserConfig , ChannelConfig } ;
31
32
32
33
use std;
33
34
use std:: default:: Default ;
@@ -225,18 +226,20 @@ const MULTI_STATE_FLAGS: u32 = (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDis
225
226
226
227
const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
227
228
229
+
228
230
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
229
231
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
230
232
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
231
233
// inbound channel.
232
234
pub ( super ) struct Channel {
235
+ config : ChannelConfig ,
236
+
233
237
user_id : u64 ,
234
238
235
239
channel_id : [ u8 ; 32 ] ,
236
240
channel_state : u32 ,
237
241
channel_outbound : bool ,
238
242
secp_ctx : Secp256k1 < secp256k1:: All > ,
239
- announce_publicly : bool ,
240
243
channel_value_satoshis : u64 ,
241
244
242
245
local_keys : ChannelKeys ,
@@ -391,15 +394,21 @@ impl Channel {
391
394
}
392
395
393
396
/// Returns a minimum channel reserve value **they** need to maintain
394
- ///
395
397
/// Guaranteed to return a value no larger than channel_value_satoshis
396
398
fn get_our_channel_reserve_satoshis ( channel_value_satoshis : u64 ) -> u64 {
397
399
let ( q, _) = channel_value_satoshis. overflowing_div ( 100 ) ;
398
400
cmp:: min ( channel_value_satoshis, cmp:: max ( q, 1000 ) ) //TODO
399
401
}
400
402
401
403
fn derive_our_dust_limit_satoshis ( at_open_background_feerate : u64 ) -> u64 {
402
- at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000 //TODO
404
+ #[ cfg( test) ]
405
+ {
406
+ 547
407
+ }
408
+ #[ cfg( not( test) ) ]
409
+ {
410
+ at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000 //TODO
411
+ }
403
412
}
404
413
405
414
fn derive_our_htlc_minimum_msat ( _at_open_channel_feerate_per_kw : u64 ) -> u64 {
@@ -419,13 +428,12 @@ impl Channel {
419
428
}
420
429
421
430
// Constructors:
422
- pub fn new_outbound ( fee_estimator : & FeeEstimator , keys_provider : & Arc < KeysInterface > , their_node_id : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , announce_publicly : bool , user_id : u64 , logger : Arc < Logger > ) -> Result < Channel , APIError > {
431
+ pub fn new_outbound ( fee_estimator : & FeeEstimator , keys_provider : & Arc < KeysInterface > , their_node_id : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 , logger : Arc < Logger > , config : & UserConfig ) -> Result < Channel , APIError > {
423
432
let chan_keys = keys_provider. get_channel_keys ( false ) ;
424
433
425
434
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
426
435
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
427
436
}
428
-
429
437
if push_msat > channel_value_satoshis * 1000 {
430
438
return Err ( APIError :: APIMisuseError { err : "push value > channel value" } ) ;
431
439
}
@@ -445,12 +453,12 @@ impl Channel {
445
453
446
454
Ok ( Channel {
447
455
user_id : user_id,
456
+ config : config. channel_options . clone ( ) ,
448
457
449
458
channel_id : rng:: rand_u832 ( ) ,
450
459
channel_state : ChannelState :: OurInitSent as u32 ,
451
460
channel_outbound : true ,
452
461
secp_ctx : secp_ctx,
453
- announce_publicly : announce_publicly,
454
462
channel_value_satoshis : channel_value_satoshis,
455
463
456
464
local_keys : chan_keys,
@@ -529,8 +537,10 @@ impl Channel {
529
537
530
538
/// Creates a new channel from a remote sides' request for one.
531
539
/// Assumes chain_hash has already been checked and corresponds with what we expect!
532
- pub fn new_from_req ( fee_estimator : & FeeEstimator , keys_provider : & Arc < KeysInterface > , their_node_id : PublicKey , msg : & msgs:: OpenChannel , user_id : u64 , require_announce : bool , allow_announce : bool , logger : Arc < Logger > ) -> Result < Channel , ChannelError > {
540
+ pub fn new_from_req ( fee_estimator : & FeeEstimator , keys_provider : & Arc < KeysInterface > , their_node_id : PublicKey , msg : & msgs:: OpenChannel , user_id : u64 , logger : Arc < Logger > , config : & UserConfig ) -> Result < Channel , ChannelError > {
533
541
let chan_keys = keys_provider. get_channel_keys ( true ) ;
542
+ let mut local_config = ( * config) . channel_options . clone ( ) ;
543
+
534
544
535
545
// Check sanity of message fields:
536
546
if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
@@ -562,23 +572,48 @@ impl Channel {
562
572
if msg. max_accepted_htlcs > 483 {
563
573
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
564
574
}
575
+ //optional parameter checking
576
+ // MAY fail the channel if
577
+ if msg. funding_satoshis < config. channel_limits . min_funding_satoshis {
578
+ return Err ( ChannelError :: Close ( "funding satoshis is less than the user specified limit" ) ) ;
579
+ }
580
+ if msg. htlc_minimum_msat > config. channel_limits . max_htlc_minimum_msat {
581
+ return Err ( ChannelError :: Close ( "htlc minimum msat is higher than the user specified limit" ) ) ;
582
+ }
583
+ if msg. max_htlc_value_in_flight_msat < config. channel_limits . min_max_htlc_value_in_flight_msat {
584
+ return Err ( ChannelError :: Close ( "max htlc value in flight msat is less than the user specified limit" ) ) ;
585
+ }
586
+ if msg. channel_reserve_satoshis > config. channel_limits . max_channel_reserve_satoshis {
587
+ return Err ( ChannelError :: Close ( "channel reserve satoshis is higher than the user specified limit" ) ) ;
588
+ }
589
+ if msg. max_accepted_htlcs < config. channel_limits . min_max_accepted_htlcs {
590
+ return Err ( ChannelError :: Close ( "max accepted htlcs is less than the user specified limit" ) ) ;
591
+ }
592
+ if msg. dust_limit_satoshis < config. channel_limits . min_dust_limit_satoshis {
593
+ println ! ( "{:?}" , msg. dust_limit_satoshis) ;
594
+ return Err ( ChannelError :: Close ( "dust limit satoshis is less than the user specified limit" ) ) ;
595
+ }
596
+ if msg. dust_limit_satoshis > config. channel_limits . max_dust_limit_satoshis {
597
+ return Err ( ChannelError :: Close ( "dust limit satoshis is greater than the user specified limit" ) ) ;
598
+ }
565
599
566
600
// Convert things into internal flags and prep our state:
567
601
568
602
let their_announce = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
569
- if require_announce && !their_announce {
570
- return Err ( ChannelError :: Close ( "Peer tried to open unannounced channel, but we require public ones" ) ) ;
571
- }
572
- if !allow_announce && their_announce {
573
- return Err ( ChannelError :: Close ( "Peer tried to open announced channel, but we require private ones" ) ) ;
603
+ if config. channel_limits . force_announced_channel_preference {
604
+ if local_config. announced_channel != their_announce {
605
+ return Err ( ChannelError :: Close ( "Peer tried to open channel but their announcement preference is different from ours" ) ) ;
606
+ }
574
607
}
608
+ //we either accept their preference or the preferences match
609
+ local_config. announced_channel = their_announce;
575
610
576
611
let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
577
612
578
613
let our_dust_limit_satoshis = Channel :: derive_our_dust_limit_satoshis ( background_feerate) ;
579
614
let our_channel_reserve_satoshis = Channel :: get_our_channel_reserve_satoshis ( msg. funding_satoshis ) ;
580
615
if our_channel_reserve_satoshis < our_dust_limit_satoshis {
581
- return Err ( ChannelError :: Close ( "Suitalbe channel reserve not found. aborting" ) ) ;
616
+ return Err ( ChannelError :: Close ( "Suitable channel reserve not found. aborting" ) ) ;
582
617
}
583
618
if msg. channel_reserve_satoshis < our_dust_limit_satoshis {
584
619
return Err ( ChannelError :: Close ( "channel_reserve_satoshis too small" ) ) ;
@@ -609,12 +644,12 @@ impl Channel {
609
644
610
645
let mut chan = Channel {
611
646
user_id : user_id,
647
+ config : local_config,
612
648
613
649
channel_id : msg. temporary_channel_id ,
614
650
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
615
651
channel_outbound : false ,
616
652
secp_ctx : secp_ctx,
617
- announce_publicly : their_announce,
618
653
619
654
local_keys : chan_keys,
620
655
shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
@@ -1264,7 +1299,7 @@ impl Channel {
1264
1299
1265
1300
// Message handlers:
1266
1301
1267
- pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel ) -> Result < ( ) , ChannelError > {
1302
+ pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel , config : & UserConfig ) -> Result < ( ) , ChannelError > {
1268
1303
// Check sanity of message fields:
1269
1304
if !self . channel_outbound {
1270
1305
return Err ( ChannelError :: Close ( "Got an accept_channel message from an inbound peer" ) ) ;
@@ -1302,16 +1337,10 @@ impl Channel {
1302
1337
if msg. max_accepted_htlcs > 483 {
1303
1338
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
1304
1339
}
1305
-
1306
- // TODO: Optional additional constraints mentioned in the spec
1307
- // MAY fail the channel if
1308
- // funding_satoshi is too small
1309
- // htlc_minimum_msat too large
1310
- // max_htlc_value_in_flight_msat too small
1311
- // channel_reserve_satoshis too large
1312
- // max_accepted_htlcs too small
1313
- // dust_limit_satoshis too small
1314
-
1340
+ //Optional user definined limits
1341
+ if msg. minimum_depth > config. channel_limits . minimum_depth {
1342
+ return Err ( ChannelError :: Close ( "We consider the minimum depth to be unreasonably large" ) ) ;
1343
+ }
1315
1344
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1316
1345
1317
1346
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2575,6 +2604,10 @@ impl Channel {
2575
2604
self . channel_value_satoshis
2576
2605
}
2577
2606
2607
+ pub fn get_fee_proportional_millionths ( & self ) -> u32 {
2608
+ self . config . fee_proportional_millionths
2609
+ }
2610
+
2578
2611
#[ cfg( test) ]
2579
2612
pub fn get_feerate ( & self ) -> u64 {
2580
2613
self . feerate_per_kw
@@ -2628,7 +2661,7 @@ impl Channel {
2628
2661
}
2629
2662
2630
2663
pub fn should_announce ( & self ) -> bool {
2631
- self . announce_publicly
2664
+ self . config . announced_channel
2632
2665
}
2633
2666
2634
2667
pub fn is_outbound ( & self ) -> bool {
@@ -2827,7 +2860,7 @@ impl Channel {
2827
2860
delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
2828
2861
htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
2829
2862
first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret) ,
2830
- channel_flags : if self . announce_publicly { 1 } else { 0 } ,
2863
+ channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
2831
2864
shutdown_scriptpubkey : None ,
2832
2865
}
2833
2866
}
@@ -2931,7 +2964,7 @@ impl Channel {
2931
2964
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
2932
2965
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
2933
2966
pub fn get_channel_announcement ( & self , our_node_id : PublicKey , chain_hash : Sha256dHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , ChannelError > {
2934
- if !self . announce_publicly {
2967
+ if !self . config . announced_channel {
2935
2968
return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" ) ) ;
2936
2969
}
2937
2970
if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -3307,11 +3340,11 @@ impl Writeable for Channel {
3307
3340
writer. write_all ( & [ MIN_SERIALIZATION_VERSION ; 1 ] ) ?;
3308
3341
3309
3342
self . user_id . write ( writer) ?;
3343
+ self . config . write ( writer) ?;
3310
3344
3311
3345
self . channel_id . write ( writer) ?;
3312
3346
( self . channel_state | ChannelState :: PeerDisconnected as u32 ) . write ( writer) ?;
3313
3347
self . channel_outbound . write ( writer) ?;
3314
- self . announce_publicly . write ( writer) ?;
3315
3348
self . channel_value_satoshis . write ( writer) ?;
3316
3349
3317
3350
self . local_keys . write ( writer) ?;
@@ -3509,11 +3542,10 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3509
3542
}
3510
3543
3511
3544
let user_id = Readable :: read ( reader) ?;
3512
-
3545
+ let config : ChannelConfig = Readable :: read ( reader ) ? ;
3513
3546
let channel_id = Readable :: read ( reader) ?;
3514
3547
let channel_state = Readable :: read ( reader) ?;
3515
3548
let channel_outbound = Readable :: read ( reader) ?;
3516
- let announce_publicly = Readable :: read ( reader) ?;
3517
3549
let channel_value_satoshis = Readable :: read ( reader) ?;
3518
3550
3519
3551
let local_keys = Readable :: read ( reader) ?;
@@ -3676,12 +3708,11 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3676
3708
3677
3709
Ok ( Channel {
3678
3710
user_id,
3679
-
3711
+ config ,
3680
3712
channel_id,
3681
3713
channel_state,
3682
3714
channel_outbound,
3683
3715
secp_ctx : Secp256k1 :: new ( ) ,
3684
- announce_publicly,
3685
3716
channel_value_satoshis,
3686
3717
3687
3718
local_keys,
@@ -3813,6 +3844,7 @@ mod tests {
3813
3844
3814
3845
#[ test]
3815
3846
fn outbound_commitment_test ( ) {
3847
+ use util:: configurations:: UserConfig ;
3816
3848
// Test vectors from BOLT 3 Appendix C:
3817
3849
let feeest = TestFeeEstimator { fee_est : 15000 } ;
3818
3850
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -3833,7 +3865,9 @@ mod tests {
3833
3865
let keys_provider: Arc < KeysInterface > = Arc :: new ( Keys { chan_keys } ) ;
3834
3866
3835
3867
let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
3836
- let mut chan = Channel :: new_outbound ( & feeest, & keys_provider, their_node_id, 10000000 , 100000 , false , 42 , Arc :: clone ( & logger) ) . unwrap ( ) ; // Nothing uses their network key in this test
3868
+ let mut config = UserConfig :: new ( ) ;
3869
+ config. channel_options . announced_channel = false ;
3870
+ let mut chan = Channel :: new_outbound ( & feeest, & keys_provider, their_node_id, 10000000 , 100000 , 42 , Arc :: clone ( & logger) , & config) . unwrap ( ) ; // Nothing uses their network key in this test
3837
3871
chan. their_to_self_delay = 144 ;
3838
3872
chan. our_dust_limit_satoshis = 546 ;
3839
3873
0 commit comments