@@ -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 ,
@@ -449,7 +452,14 @@ impl Channel {
449
452
}
450
453
451
454
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
455
+ #[ cfg( test) ]
456
+ {
457
+ 547
458
+ }
459
+ #[ cfg( not( test) ) ]
460
+ {
461
+ at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000 //TODO
462
+ }
453
463
}
454
464
455
465
fn derive_our_htlc_minimum_msat ( _at_open_channel_feerate_per_kw : u64 ) -> u64 {
@@ -469,11 +479,10 @@ impl Channel {
469
479
}
470
480
471
481
// 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 > {
482
+ 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
483
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
474
484
return Err ( APIError :: APIMisuseError { err : "funding value > 2^24" } ) ;
475
485
}
476
-
477
486
if push_msat > channel_value_satoshis * 1000 {
478
487
return Err ( APIError :: APIMisuseError { err : "push value > channel value" } ) ;
479
488
}
@@ -493,15 +502,15 @@ impl Channel {
493
502
& PublicKey :: from_secret_key ( & secp_ctx, & chan_keys. delayed_payment_base_key ) ,
494
503
& chan_keys. htlc_base_key ,
495
504
BREAKDOWN_TIMEOUT , our_channel_monitor_claim_script) ;
496
-
505
+
497
506
Ok ( Channel {
498
507
user_id : user_id,
508
+ config : config. channel_options . clone ( ) ,
499
509
500
510
channel_id : rng:: rand_u832 ( ) ,
501
511
channel_state : ChannelState :: OurInitSent as u32 ,
502
512
channel_outbound : true ,
503
513
secp_ctx : secp_ctx,
504
- announce_publicly : announce_publicly,
505
514
channel_value_satoshis : channel_value_satoshis,
506
515
507
516
local_keys : chan_keys,
@@ -579,7 +588,8 @@ impl Channel {
579
588
580
589
/// Creates a new channel from a remote sides' request for one.
581
590
/// 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 > {
591
+ 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 > {
592
+ let mut local_config = ( * config) . channel_options . clone ( ) ;
583
593
// Check sanity of message fields:
584
594
if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
585
595
return Err ( ChannelError :: Close ( "funding value > 2^24" ) ) ;
@@ -610,23 +620,48 @@ impl Channel {
610
620
if msg. max_accepted_htlcs > 483 {
611
621
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
612
622
}
623
+ //optional parameter checking
624
+ // MAY fail the channel if
625
+ if msg. funding_satoshis < config. channel_limits . min_funding_satoshis {
626
+ return Err ( ChannelError :: Close ( "funding satoshis is less than the user specified limit" ) ) ;
627
+ }
628
+ if msg. htlc_minimum_msat > config. channel_limits . max_htlc_minimum_msat {
629
+ return Err ( ChannelError :: Close ( "htlc minimum msat is higher than the user specified limit" ) ) ;
630
+ }
631
+ if msg. max_htlc_value_in_flight_msat < config. channel_limits . min_max_htlc_value_in_flight_msat {
632
+ return Err ( ChannelError :: Close ( "max htlc value in flight msat is less than the user specified limit" ) ) ;
633
+ }
634
+ if msg. channel_reserve_satoshis > config. channel_limits . max_channel_reserve_satoshis {
635
+ return Err ( ChannelError :: Close ( "channel reserve satoshis is higher than the user specified limit" ) ) ;
636
+ }
637
+ if msg. max_accepted_htlcs < config. channel_limits . min_max_accepted_htlcs {
638
+ return Err ( ChannelError :: Close ( "max accepted htlcs is less than the user specified limit" ) ) ;
639
+ }
640
+ if msg. dust_limit_satoshis < config. channel_limits . min_dust_limit_satoshis {
641
+ println ! ( "{:?}" , msg. dust_limit_satoshis) ;
642
+ return Err ( ChannelError :: Close ( "dust limit satoshis is less than the user specified limit" ) ) ;
643
+ }
644
+ if msg. dust_limit_satoshis > config. channel_limits . max_dust_limit_satoshis {
645
+ return Err ( ChannelError :: Close ( "dust limit satoshis is greater than the user specified limit" ) ) ;
646
+ }
613
647
614
648
// Convert things into internal flags and prep our state:
615
649
616
650
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" ) ) ;
651
+ if config. channel_limits . force_announced_channel_preference {
652
+ if local_config. announced_channel != their_announce {
653
+ return Err ( ChannelError :: Close ( "Peer tried to open channel but their announcement preference is different from ours" ) ) ;
654
+ }
622
655
}
656
+ //we either accept their preference or the preferences match
657
+ local_config. announced_channel = their_announce;
623
658
624
659
let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
625
660
626
661
let our_dust_limit_satoshis = Channel :: derive_our_dust_limit_satoshis ( background_feerate) ;
627
662
let our_channel_reserve_satoshis = Channel :: get_our_channel_reserve_satoshis ( msg. funding_satoshis ) ;
628
663
if our_channel_reserve_satoshis < our_dust_limit_satoshis {
629
- return Err ( ChannelError :: Close ( "Suitalbe channel reserve not found. aborting" ) ) ;
664
+ return Err ( ChannelError :: Close ( "Suitable channel reserve not found. aborting" ) ) ;
630
665
}
631
666
if msg. channel_reserve_satoshis < our_dust_limit_satoshis {
632
667
return Err ( ChannelError :: Close ( "channel_reserve_satoshis too small" ) ) ;
@@ -660,12 +695,12 @@ impl Channel {
660
695
661
696
let mut chan = Channel {
662
697
user_id : user_id,
698
+ config : local_config,
663
699
664
700
channel_id : msg. temporary_channel_id ,
665
701
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
666
702
channel_outbound : false ,
667
703
secp_ctx : secp_ctx,
668
- announce_publicly : their_announce,
669
704
670
705
local_keys : chan_keys,
671
706
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
@@ -1314,7 +1349,7 @@ impl Channel {
1314
1349
1315
1350
// Message handlers:
1316
1351
1317
- pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel ) -> Result < ( ) , ChannelError > {
1352
+ pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel , config : & UserConfig ) -> Result < ( ) , ChannelError > {
1318
1353
// Check sanity of message fields:
1319
1354
if !self . channel_outbound {
1320
1355
return Err ( ChannelError :: Close ( "Got an accept_channel message from an inbound peer" ) ) ;
@@ -1352,16 +1387,10 @@ impl Channel {
1352
1387
if msg. max_accepted_htlcs > 483 {
1353
1388
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
1354
1389
}
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
-
1390
+ //Optional user definined limits
1391
+ if msg. minimum_depth > config. channel_limits . minimum_depth {
1392
+ return Err ( ChannelError :: Close ( "We consider the minimum depth to be unreasonably large" ) ) ;
1393
+ }
1365
1394
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1366
1395
1367
1396
self . their_dust_limit_satoshis = msg. dust_limit_satoshis ;
@@ -2607,6 +2636,10 @@ impl Channel {
2607
2636
self . channel_value_satoshis
2608
2637
}
2609
2638
2639
+ pub fn get_fee_proportional_millionths ( & self ) -> u32 {
2640
+ self . config . fee_proportional_millionths
2641
+ }
2642
+
2610
2643
#[ cfg( test) ]
2611
2644
pub fn get_feerate ( & self ) -> u64 {
2612
2645
self . feerate_per_kw
@@ -2648,7 +2681,7 @@ impl Channel {
2648
2681
}
2649
2682
2650
2683
pub fn should_announce ( & self ) -> bool {
2651
- self . announce_publicly
2684
+ self . config . announced_channel
2652
2685
}
2653
2686
2654
2687
pub fn is_outbound ( & self ) -> bool {
@@ -2844,7 +2877,7 @@ impl Channel {
2844
2877
delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
2845
2878
htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
2846
2879
first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret) ,
2847
- channel_flags : if self . announce_publicly { 1 } else { 0 } ,
2880
+ channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
2848
2881
shutdown_scriptpubkey : None ,
2849
2882
}
2850
2883
}
@@ -2948,7 +2981,7 @@ impl Channel {
2948
2981
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
2949
2982
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
2950
2983
pub fn get_channel_announcement ( & self , our_node_id : PublicKey , chain_hash : Sha256dHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , ChannelError > {
2951
- if !self . announce_publicly {
2984
+ if !self . config . announced_channel {
2952
2985
return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" ) ) ;
2953
2986
}
2954
2987
if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -3318,6 +3351,7 @@ mod tests {
3318
3351
3319
3352
#[ test]
3320
3353
fn outbound_commitment_test ( ) {
3354
+ use util:: configurations:: UserConfig ;
3321
3355
// Test vectors from BOLT 3 Appendix C:
3322
3356
let feeest = TestFeeEstimator { fee_est : 15000 } ;
3323
3357
let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
@@ -3339,7 +3373,9 @@ mod tests {
3339
3373
hex:: decode( "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb" ) . unwrap( ) [ ..] ) ;
3340
3374
3341
3375
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
3376
+ let mut config = UserConfig :: new ( ) ;
3377
+ config. channel_options . announced_channel = false ;
3378
+ 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
3379
chan. their_to_self_delay = 144 ;
3344
3380
chan. our_dust_limit_satoshis = 546 ;
3345
3381
0 commit comments