@@ -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:: { UserConfig , ChannelConfig } ;
29
30
30
31
use std;
31
32
use std:: default:: Default ;
@@ -277,18 +278,20 @@ const MULTI_STATE_FLAGS: u32 = (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::PeerDis
277
278
278
279
const INITIAL_COMMITMENT_NUMBER : u64 = ( 1 << 48 ) - 1 ;
279
280
281
+
280
282
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
281
283
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
282
284
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
283
285
// inbound channel.
284
286
pub ( super ) struct Channel {
287
+ config : ChannelConfig ,
288
+
285
289
user_id : u64 ,
286
290
287
291
channel_id : [ u8 ; 32 ] ,
288
292
channel_state : u32 ,
289
293
channel_outbound : bool ,
290
294
secp_ctx : Secp256k1 < secp256k1:: All > ,
291
- announce_publicly : bool ,
292
295
channel_value_satoshis : u64 ,
293
296
294
297
local_keys : ChannelKeys ,
@@ -441,15 +444,21 @@ impl Channel {
441
444
}
442
445
443
446
/// Returns a minimum channel reserve value **they** need to maintain
444
- ///
445
447
/// Guaranteed to return a value no larger than channel_value_satoshis
446
448
fn get_our_channel_reserve_satoshis ( channel_value_satoshis : u64 ) -> u64 {
447
449
let ( q, _) = channel_value_satoshis. overflowing_div ( 100 ) ;
448
450
cmp:: min ( channel_value_satoshis, cmp:: max ( q, 1000 ) ) //TODO
449
451
}
450
452
451
453
fn derive_our_dust_limit_satoshis ( at_open_background_feerate : u64 ) -> u64 {
452
- at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000 //TODO
454
+ #[ cfg( test) ]
455
+ {
456
+ 547
457
+ }
458
+ #[ cfg( not( test) ) ]
459
+ {
460
+ at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000 //TODO
461
+ }
453
462
}
454
463
455
464
fn derive_our_htlc_minimum_msat ( _at_open_channel_feerate_per_kw : u64 ) -> u64 {
@@ -469,11 +478,10 @@ impl Channel {
469
478
}
470
479
471
480
// Constructors:
472
- 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 > {
481
+ 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 > , config : & UserConfig ) -> Result < Channel , APIError > {
473
482
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
474
483
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
475
484
}
476
-
477
485
if push_msat > channel_value_satoshis * 1000 {
478
486
return Err ( APIError :: APIMisuseError { err : "push value > channel value" } ) ;
479
487
}
@@ -493,15 +501,15 @@ impl Channel {
493
501
& PublicKey :: from_secret_key ( & secp_ctx, & chan_keys. delayed_payment_base_key ) ,
494
502
& chan_keys. htlc_base_key ,
495
503
BREAKDOWN_TIMEOUT , our_channel_monitor_claim_script) ;
496
-
504
+
497
505
Ok ( Channel {
498
506
user_id : user_id,
507
+ config : config. channel_options . clone ( ) ,
499
508
500
509
channel_id : rng:: rand_u832 ( ) ,
501
510
channel_state : ChannelState :: OurInitSent as u32 ,
502
511
channel_outbound : true ,
503
512
secp_ctx : secp_ctx,
504
- announce_publicly : announce_publicly,
505
513
channel_value_satoshis : channel_value_satoshis,
506
514
507
515
local_keys : chan_keys,
@@ -579,7 +587,8 @@ impl Channel {
579
587
580
588
/// Creates a new channel from a remote sides' request for one.
581
589
/// Assumes chain_hash has already been checked and corresponds with what we expect!
582
- 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 > {
590
+ pub fn new_from_req ( fee_estimator : & FeeEstimator , chan_keys : ChannelKeys , their_node_id : PublicKey , msg : & msgs:: OpenChannel , user_id : u64 , logger : Arc < Logger > , config : & UserConfig ) -> Result < Channel , ChannelError > {
591
+ let mut local_config = ( * config) . channel_options . clone ( ) ;
583
592
// Check sanity of message fields:
584
593
if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
585
594
return Err ( ChannelError :: Close ( "funding value > 2^24" ) ) ;
@@ -610,23 +619,48 @@ impl Channel {
610
619
if msg. max_accepted_htlcs > 483 {
611
620
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
612
621
}
622
+ //optional parameter checking
623
+ // MAY fail the channel if
624
+ if msg. funding_satoshis < config. channel_limits . min_funding_satoshis {
625
+ return Err ( ChannelError :: Close ( "funding satoshis is less than the user specified limit" ) ) ;
626
+ }
627
+ if msg. htlc_minimum_msat > config. channel_limits . max_htlc_minimum_msat {
628
+ return Err ( ChannelError :: Close ( "htlc minimum msat is higher than the user specified limit" ) ) ;
629
+ }
630
+ if msg. max_htlc_value_in_flight_msat < config. channel_limits . min_max_htlc_value_in_flight_msat {
631
+ return Err ( ChannelError :: Close ( "max htlc value in flight msat is less than the user specified limit" ) ) ;
632
+ }
633
+ if msg. channel_reserve_satoshis > config. channel_limits . max_channel_reserve_satoshis {
634
+ return Err ( ChannelError :: Close ( "channel reserve satoshis is higher than the user specified limit" ) ) ;
635
+ }
636
+ if msg. max_accepted_htlcs < config. channel_limits . min_max_accepted_htlcs {
637
+ return Err ( ChannelError :: Close ( "max accepted htlcs is less than the user specified limit" ) ) ;
638
+ }
639
+ if msg. dust_limit_satoshis < config. channel_limits . min_dust_limit_satoshis {
640
+ println ! ( "{:?}" , msg. dust_limit_satoshis) ;
641
+ return Err ( ChannelError :: Close ( "dust limit satoshis is less than the user specified limit" ) ) ;
642
+ }
643
+ if msg. dust_limit_satoshis > config. channel_limits . max_dust_limit_satoshis {
644
+ return Err ( ChannelError :: Close ( "dust limit satoshis is greater than the user specified limit" ) ) ;
645
+ }
613
646
614
647
// Convert things into internal flags and prep our state:
615
648
616
649
let their_announce = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
617
- if require_announce && !their_announce {
618
- return Err ( ChannelError :: Close ( "Peer tried to open unannounced channel, but we require public ones" ) ) ;
619
- }
620
- if !allow_announce && their_announce {
621
- return Err ( ChannelError :: Close ( "Peer tried to open announced channel, but we require private ones" ) ) ;
650
+ if config. channel_limits . force_announced_channel_preference {
651
+ if local_config. announced_channel != their_announce {
652
+ return Err ( ChannelError :: Close ( "Peer tried to open channel but their announcement preference is different from ours" ) ) ;
653
+ }
622
654
}
655
+ //we either accept their preference or the preferences match
656
+ local_config. announced_channel = their_announce;
623
657
624
658
let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
625
659
626
660
let our_dust_limit_satoshis = Channel :: derive_our_dust_limit_satoshis ( background_feerate) ;
627
661
let our_channel_reserve_satoshis = Channel :: get_our_channel_reserve_satoshis ( msg. funding_satoshis ) ;
628
662
if our_channel_reserve_satoshis < our_dust_limit_satoshis {
629
- return Err ( ChannelError :: Close ( "Suitalbe channel reserve not found. aborting" ) ) ;
663
+ return Err ( ChannelError :: Close ( "Suitable channel reserve not found. aborting" ) ) ;
630
664
}
631
665
if msg. channel_reserve_satoshis < our_dust_limit_satoshis {
632
666
return Err ( ChannelError :: Close ( "channel_reserve_satoshis too small" ) ) ;
@@ -660,12 +694,12 @@ impl Channel {
660
694
661
695
let mut chan = Channel {
662
696
user_id : user_id,
697
+ config : local_config,
663
698
664
699
channel_id : msg. temporary_channel_id ,
665
700
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
666
701
channel_outbound : false ,
667
702
secp_ctx : secp_ctx,
668
- announce_publicly : their_announce,
669
703
670
704
local_keys : chan_keys,
671
705
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1314,7 +1348,7 @@ impl Channel {
1314
1348
1315
1349
// Message handlers:
1316
1350
1317
- pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel ) -> Result < ( ) , ChannelError > {
1351
+ pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel , config : & UserConfig ) -> Result < ( ) , ChannelError > {
1318
1352
// Check sanity of message fields:
1319
1353
if !self . channel_outbound {
1320
1354
return Err ( ChannelError :: Close ( "Got an accept_channel message from an inbound peer" ) ) ;
@@ -1352,16 +1386,10 @@ impl Channel {
1352
1386
if msg. max_accepted_htlcs > 483 {
1353
1387
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
1354
1388
}
1355
-
1356
- // TODO: Optional additional constraints mentioned in the spec
1357
- // MAY fail the channel if
1358
- // funding_satoshi is too small
1359
- // htlc_minimum_msat too large
1360
- // max_htlc_value_in_flight_msat too small
1361
- // channel_reserve_satoshis too large
1362
- // max_accepted_htlcs too small
1363
- // dust_limit_satoshis too small
1364
-
1389
+ //Optional user definined limits
1390
+ if msg. minimum_depth > config. channel_limits . minimum_depth {
1391
+ return Err ( ChannelError :: Close ( "We consider the minimum depth to be unreasonably large" ) ) ;
1392
+ }
1365
1393
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1366
1394
1367
1395
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2607,6 +2635,10 @@ impl Channel {
2607
2635
self . channel_value_satoshis
2608
2636
}
2609
2637
2638
+ pub fn get_fee_proportional_millionths ( & self ) -> u32 {
2639
+ self . config . fee_proportional_millionths
2640
+ }
2641
+
2610
2642
#[ cfg( test) ]
2611
2643
pub fn get_feerate ( & self ) -> u64 {
2612
2644
self . feerate_per_kw
@@ -2648,7 +2680,7 @@ impl Channel {
2648
2680
}
2649
2681
2650
2682
pub fn should_announce ( & self ) -> bool {
2651
- self . announce_publicly
2683
+ self . config . announced_channel
2652
2684
}
2653
2685
2654
2686
pub fn is_outbound ( & self ) -> bool {
@@ -2844,7 +2876,7 @@ impl Channel {
2844
2876
delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
2845
2877
htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
2846
2878
first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret) ,
2847
- channel_flags : if self . announce_publicly { 1 } else { 0 } ,
2879
+ channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
2848
2880
shutdown_scriptpubkey : None ,
2849
2881
}
2850
2882
}
@@ -2948,7 +2980,7 @@ impl Channel {
2948
2980
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
2949
2981
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
2950
2982
pub fn get_channel_announcement ( & self , our_node_id : PublicKey , chain_hash : Sha256dHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , ChannelError > {
2951
- if !self . announce_publicly {
2983
+ if !self . config . announced_channel {
2952
2984
return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" ) ) ;
2953
2985
}
2954
2986
if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -3318,6 +3350,7 @@ mod tests {
3318
3350
3319
3351
#[ test]
3320
3352
fn outbound_commitment_test ( ) {
3353
+ use util:: configurations:: UserConfig ;
3321
3354
// Test vectors from BOLT 3 Appendix C:
3322
3355
let feeest = TestFeeEstimator { fee_est : 15000 } ;
3323
3356
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -3339,7 +3372,9 @@ mod tests {
3339
3372
hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
3340
3373
3341
3374
let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
3342
- 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
3375
+ let mut config = UserConfig :: new ( ) ;
3376
+ config. channel_options . announced_channel = false ;
3377
+ 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
3343
3378
chan. their_to_self_delay = 144 ;
3344
3379
chan. our_dust_limit_satoshis = 546 ;
3345
3380
0 commit comments