@@ -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:: config:: { UserConfig , ChannelConfig } ;
31
32
32
33
use std;
33
34
use std:: default:: Default ;
@@ -230,13 +231,14 @@ const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
230
231
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
231
232
// inbound channel.
232
233
pub ( super ) struct Channel {
234
+ config : ChannelConfig ,
235
+
233
236
user_id : u64 ,
234
237
235
238
channel_id : [ u8 ; 32 ] ,
236
239
channel_state : u32 ,
237
240
channel_outbound : bool ,
238
241
secp_ctx : Secp256k1 < secp256k1:: All > ,
239
- announce_publicly : bool ,
240
242
channel_value_satoshis : u64 ,
241
243
242
244
local_keys : ChannelKeys ,
@@ -415,7 +417,7 @@ impl Channel {
415
417
}
416
418
417
419
// Constructors:
418
- 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 > {
420
+ 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 > {
419
421
let chan_keys = keys_provider. get_channel_keys ( false ) ;
420
422
421
423
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
@@ -441,12 +443,12 @@ impl Channel {
441
443
442
444
Ok ( Channel {
443
445
user_id : user_id,
446
+ config : config. channel_options . clone ( ) ,
444
447
445
448
channel_id : rng:: rand_u832 ( ) ,
446
449
channel_state : ChannelState :: OurInitSent as u32 ,
447
450
channel_outbound : true ,
448
451
secp_ctx : secp_ctx,
449
- announce_publicly : announce_publicly,
450
452
channel_value_satoshis : channel_value_satoshis,
451
453
452
454
local_keys : chan_keys,
@@ -526,8 +528,9 @@ impl Channel {
526
528
527
529
/// Creates a new channel from a remote sides' request for one.
528
530
/// Assumes chain_hash has already been checked and corresponds with what we expect!
529
- 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 > {
531
+ 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 > {
530
532
let chan_keys = keys_provider. get_channel_keys ( true ) ;
533
+ let mut local_config = ( * config) . channel_options . clone ( ) ;
531
534
532
535
// Check sanity of message fields:
533
536
if msg. funding_satoshis >= MAX_FUNDING_SATOSHIS {
@@ -560,22 +563,46 @@ impl Channel {
560
563
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
561
564
}
562
565
566
+ // Now check against optional parameters as set by config...
567
+ if msg. funding_satoshis < config. channel_limits . min_funding_satoshis {
568
+ return Err ( ChannelError :: Close ( "funding satoshis is less than the user specified limit" ) ) ;
569
+ }
570
+ if msg. htlc_minimum_msat > config. channel_limits . max_htlc_minimum_msat {
571
+ return Err ( ChannelError :: Close ( "htlc minimum msat is higher than the user specified limit" ) ) ;
572
+ }
573
+ if msg. max_htlc_value_in_flight_msat < config. channel_limits . min_max_htlc_value_in_flight_msat {
574
+ return Err ( ChannelError :: Close ( "max htlc value in flight msat is less than the user specified limit" ) ) ;
575
+ }
576
+ if msg. channel_reserve_satoshis > config. channel_limits . max_channel_reserve_satoshis {
577
+ return Err ( ChannelError :: Close ( "channel reserve satoshis is higher than the user specified limit" ) ) ;
578
+ }
579
+ if msg. max_accepted_htlcs < config. channel_limits . min_max_accepted_htlcs {
580
+ return Err ( ChannelError :: Close ( "max accepted htlcs is less than the user specified limit" ) ) ;
581
+ }
582
+ if msg. dust_limit_satoshis < config. channel_limits . min_dust_limit_satoshis {
583
+ return Err ( ChannelError :: Close ( "dust limit satoshis is less than the user specified limit" ) ) ;
584
+ }
585
+ if msg. dust_limit_satoshis > config. channel_limits . max_dust_limit_satoshis {
586
+ return Err ( ChannelError :: Close ( "dust limit satoshis is greater than the user specified limit" ) ) ;
587
+ }
588
+
563
589
// Convert things into internal flags and prep our state:
564
590
565
591
let their_announce = if ( msg. channel_flags & 1 ) == 1 { true } else { false } ;
566
- if require_announce && !their_announce {
567
- return Err ( ChannelError :: Close ( "Peer tried to open unannounced channel, but we require public ones" ) ) ;
568
- }
569
- if !allow_announce && their_announce {
570
- return Err ( ChannelError :: Close ( "Peer tried to open announced channel, but we require private ones" ) ) ;
592
+ if config. channel_limits . force_announced_channel_preference {
593
+ if local_config. announced_channel != their_announce {
594
+ return Err ( ChannelError :: Close ( "Peer tried to open channel but their announcement preference is different from ours" ) ) ;
595
+ }
571
596
}
597
+ // we either accept their preference or the preferences match
598
+ local_config. announced_channel = their_announce;
572
599
573
600
let background_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
574
601
575
602
let our_dust_limit_satoshis = Channel :: derive_our_dust_limit_satoshis ( background_feerate) ;
576
603
let our_channel_reserve_satoshis = Channel :: get_our_channel_reserve_satoshis ( msg. funding_satoshis ) ;
577
604
if our_channel_reserve_satoshis < our_dust_limit_satoshis {
578
- return Err ( ChannelError :: Close ( "Suitalbe channel reserve not found. aborting" ) ) ;
605
+ return Err ( ChannelError :: Close ( "Suitable channel reserve not found. aborting" ) ) ;
579
606
}
580
607
if msg. channel_reserve_satoshis < our_dust_limit_satoshis {
581
608
return Err ( ChannelError :: Close ( "channel_reserve_satoshis too small" ) ) ;
@@ -606,12 +633,12 @@ impl Channel {
606
633
607
634
let mut chan = Channel {
608
635
user_id : user_id,
636
+ config : local_config,
609
637
610
638
channel_id : msg. temporary_channel_id ,
611
639
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
612
640
channel_outbound : false ,
613
641
secp_ctx : secp_ctx,
614
- announce_publicly : their_announce,
615
642
616
643
local_keys : chan_keys,
617
644
shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
@@ -1262,7 +1289,7 @@ impl Channel {
1262
1289
1263
1290
// Message handlers:
1264
1291
1265
- pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel ) -> Result < ( ) , ChannelError > {
1292
+ pub fn accept_channel ( & mut self , msg : & msgs:: AcceptChannel , config : & UserConfig ) -> Result < ( ) , ChannelError > {
1266
1293
// Check sanity of message fields:
1267
1294
if !self . channel_outbound {
1268
1295
return Err ( ChannelError :: Close ( "Got an accept_channel message from an inbound peer" ) ) ;
@@ -1298,14 +1325,28 @@ impl Channel {
1298
1325
return Err ( ChannelError :: Close ( "max_accpted_htlcs > 483" ) ) ;
1299
1326
}
1300
1327
1301
- // TODO: Optional additional constraints mentioned in the spec
1302
- // MAY fail the channel if
1303
- // funding_satoshi is too small
1304
- // htlc_minimum_msat too large
1305
- // max_htlc_value_in_flight_msat too small
1306
- // channel_reserve_satoshis too large
1307
- // max_accepted_htlcs too small
1308
- // dust_limit_satoshis too small
1328
+ // Now check against optional parameters as set by config...
1329
+ if msg. htlc_minimum_msat > config. channel_limits . max_htlc_minimum_msat {
1330
+ return Err ( ChannelError :: Close ( "htlc minimum msat is higher than the user specified limit" ) ) ;
1331
+ }
1332
+ if msg. max_htlc_value_in_flight_msat < config. channel_limits . min_max_htlc_value_in_flight_msat {
1333
+ return Err ( ChannelError :: Close ( "max htlc value in flight msat is less than the user specified limit" ) ) ;
1334
+ }
1335
+ if msg. channel_reserve_satoshis > config. channel_limits . max_channel_reserve_satoshis {
1336
+ return Err ( ChannelError :: Close ( "channel reserve satoshis is higher than the user specified limit" ) ) ;
1337
+ }
1338
+ if msg. max_accepted_htlcs < config. channel_limits . min_max_accepted_htlcs {
1339
+ return Err ( ChannelError :: Close ( "max accepted htlcs is less than the user specified limit" ) ) ;
1340
+ }
1341
+ if msg. dust_limit_satoshis < config. channel_limits . min_dust_limit_satoshis {
1342
+ return Err ( ChannelError :: Close ( "dust limit satoshis is less than the user specified limit" ) ) ;
1343
+ }
1344
+ if msg. dust_limit_satoshis > config. channel_limits . max_dust_limit_satoshis {
1345
+ return Err ( ChannelError :: Close ( "dust limit satoshis is greater than the user specified limit" ) ) ;
1346
+ }
1347
+ if msg. minimum_depth > config. channel_limits . max_minimum_depth {
1348
+ return Err ( ChannelError :: Close ( "We consider the minimum depth to be unreasonably large" ) ) ;
1349
+ }
1309
1350
1310
1351
self . channel_monitor . set_their_base_keys ( & msg. htlc_basepoint , & msg. delayed_payment_basepoint ) ;
1311
1352
@@ -2571,6 +2612,10 @@ impl Channel {
2571
2612
self . channel_value_satoshis
2572
2613
}
2573
2614
2615
+ pub fn get_fee_proportional_millionths ( & self ) -> u32 {
2616
+ self . config . fee_proportional_millionths
2617
+ }
2618
+
2574
2619
#[ cfg( test) ]
2575
2620
pub fn get_feerate ( & self ) -> u64 {
2576
2621
self . feerate_per_kw
@@ -2624,7 +2669,7 @@ impl Channel {
2624
2669
}
2625
2670
2626
2671
pub fn should_announce ( & self ) -> bool {
2627
- self . announce_publicly
2672
+ self . config . announced_channel
2628
2673
}
2629
2674
2630
2675
pub fn is_outbound ( & self ) -> bool {
@@ -2823,7 +2868,7 @@ impl Channel {
2823
2868
delayed_payment_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . delayed_payment_base_key ) ,
2824
2869
htlc_basepoint : PublicKey :: from_secret_key ( & self . secp_ctx , & self . local_keys . htlc_base_key ) ,
2825
2870
first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret) ,
2826
- channel_flags : if self . announce_publicly { 1 } else { 0 } ,
2871
+ channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
2827
2872
shutdown_scriptpubkey : None ,
2828
2873
}
2829
2874
}
@@ -2927,7 +2972,7 @@ impl Channel {
2927
2972
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
2928
2973
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
2929
2974
pub fn get_channel_announcement ( & self , our_node_id : PublicKey , chain_hash : Sha256dHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , ChannelError > {
2930
- if !self . announce_publicly {
2975
+ if !self . config . announced_channel {
2931
2976
return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" ) ) ;
2932
2977
}
2933
2978
if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) == 0 {
@@ -3303,11 +3348,11 @@ impl Writeable for Channel {
3303
3348
writer. write_all ( & [ MIN_SERIALIZATION_VERSION ; 1 ] ) ?;
3304
3349
3305
3350
self . user_id . write ( writer) ?;
3351
+ self . config . write ( writer) ?;
3306
3352
3307
3353
self . channel_id . write ( writer) ?;
3308
3354
( self . channel_state | ChannelState :: PeerDisconnected as u32 ) . write ( writer) ?;
3309
3355
self . channel_outbound . write ( writer) ?;
3310
- self . announce_publicly . write ( writer) ?;
3311
3356
self . channel_value_satoshis . write ( writer) ?;
3312
3357
3313
3358
self . local_keys . write ( writer) ?;
@@ -3506,11 +3551,11 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3506
3551
}
3507
3552
3508
3553
let user_id = Readable :: read ( reader) ?;
3554
+ let config: ChannelConfig = Readable :: read ( reader) ?;
3509
3555
3510
3556
let channel_id = Readable :: read ( reader) ?;
3511
3557
let channel_state = Readable :: read ( reader) ?;
3512
3558
let channel_outbound = Readable :: read ( reader) ?;
3513
- let announce_publicly = Readable :: read ( reader) ?;
3514
3559
let channel_value_satoshis = Readable :: read ( reader) ?;
3515
3560
3516
3561
let local_keys = Readable :: read ( reader) ?;
@@ -3675,11 +3720,11 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3675
3720
Ok ( Channel {
3676
3721
user_id,
3677
3722
3723
+ config,
3678
3724
channel_id,
3679
3725
channel_state,
3680
3726
channel_outbound,
3681
3727
secp_ctx : Secp256k1 :: new ( ) ,
3682
- announce_publicly,
3683
3728
channel_value_satoshis,
3684
3729
3685
3730
local_keys,
@@ -3766,6 +3811,7 @@ mod tests {
3766
3811
use chain:: chaininterface:: { FeeEstimator , ConfirmationTarget } ;
3767
3812
use chain:: keysinterface:: KeysInterface ;
3768
3813
use chain:: transaction:: OutPoint ;
3814
+ use util:: config:: UserConfig ;
3769
3815
use util:: test_utils;
3770
3816
use util:: logger:: Logger ;
3771
3817
use secp256k1:: { Secp256k1 , Message , Signature } ;
@@ -3832,7 +3878,9 @@ mod tests {
3832
3878
let keys_provider: Arc < KeysInterface > = Arc :: new ( Keys { chan_keys } ) ;
3833
3879
3834
3880
let their_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & secp_ctx, & [ 42 ; 32 ] ) . unwrap ( ) ) ;
3835
- 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
3881
+ let mut config = UserConfig :: new ( ) ;
3882
+ config. channel_options . announced_channel = false ;
3883
+ 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
3836
3884
chan. their_to_self_delay = 144 ;
3837
3885
chan. our_dust_limit_satoshis = 546 ;
3838
3886
0 commit comments