@@ -292,7 +292,7 @@ type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource
292
292
293
293
struct MsgHandleErrInternal {
294
294
err : msgs:: LightningError ,
295
- chan_id : Option < ( [ u8 ; 32 ] , u64 ) > , // If Some a channel of ours has been closed
295
+ chan_id : Option < ( [ u8 ; 32 ] , u128 ) > , // If Some a channel of ours has been closed
296
296
shutdown_finish : Option < ( ShutdownResult , Option < msgs:: ChannelUpdate > ) > ,
297
297
}
298
298
impl MsgHandleErrInternal {
@@ -328,7 +328,7 @@ impl MsgHandleErrInternal {
328
328
Self { err, chan_id : None , shutdown_finish : None }
329
329
}
330
330
#[ inline]
331
- fn from_finish_shutdown ( err : String , channel_id : [ u8 ; 32 ] , user_channel_id : u64 , shutdown_res : ShutdownResult , channel_update : Option < msgs:: ChannelUpdate > ) -> Self {
331
+ fn from_finish_shutdown ( err : String , channel_id : [ u8 ; 32 ] , user_channel_id : u128 , shutdown_res : ShutdownResult , channel_update : Option < msgs:: ChannelUpdate > ) -> Self {
332
332
Self {
333
333
err : LightningError {
334
334
err : err. clone ( ) ,
@@ -1086,8 +1086,9 @@ pub struct ChannelDetails {
1086
1086
///
1087
1087
/// [`outbound_capacity_msat`]: ChannelDetails::outbound_capacity_msat
1088
1088
pub unspendable_punishment_reserve : Option < u64 > ,
1089
- /// The `user_channel_id` passed in to create_channel, or 0 if the channel was inbound.
1090
- pub user_channel_id : u64 ,
1089
+ /// The `user_channel_id` passed in to create_channel, or a random value if the channel was
1090
+ /// inbound.
1091
+ pub user_channel_id : u128 ,
1091
1092
/// Our total balance. This is the amount we would get if we close the channel.
1092
1093
/// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
1093
1094
/// amount is not likely to be recoverable on close.
@@ -1722,10 +1723,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
1722
1723
///
1723
1724
/// `user_channel_id` will be provided back as in
1724
1725
/// [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
1725
- /// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to 0
1726
- /// for inbound channels, so you may wish to avoid using 0 for `user_channel_id` here.
1727
- /// `user_channel_id` has no meaning inside of LDK, it is simply copied to events and otherwise
1728
- /// ignored.
1726
+ /// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to a
1727
+ /// randomized value for inbound channels. `user_channel_id` has no meaning inside of LDK, it
1728
+ /// is simply copied to events and otherwise ignored.
1729
1729
///
1730
1730
/// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
1731
1731
/// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
@@ -1744,7 +1744,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
1744
1744
/// [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
1745
1745
/// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
1746
1746
/// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
1747
- pub fn create_channel ( & self , their_network_key : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_channel_id : u64 , override_config : Option < UserConfig > ) -> Result < [ u8 ; 32 ] , APIError > {
1747
+ pub fn create_channel ( & self , their_network_key : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_channel_id : u128 , override_config : Option < UserConfig > ) -> Result < [ u8 ; 32 ] , APIError > {
1748
1748
if channel_value_satoshis < 1000 {
1749
1749
return Err ( APIError :: APIMisuseError { err : format ! ( "Channel value must be at least 1000 satoshis. It was {}" , channel_value_satoshis) } ) ;
1750
1750
}
@@ -4536,7 +4536,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4536
4536
///
4537
4537
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
4538
4538
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
4539
- pub fn accept_inbound_channel ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , user_channel_id : u64 ) -> Result < ( ) , APIError > {
4539
+ pub fn accept_inbound_channel ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , user_channel_id : u128 ) -> Result < ( ) , APIError > {
4540
4540
self . do_accept_inbound_channel ( temporary_channel_id, counterparty_node_id, false , user_channel_id)
4541
4541
}
4542
4542
@@ -4558,11 +4558,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4558
4558
///
4559
4559
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
4560
4560
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
4561
- pub fn accept_inbound_channel_from_trusted_peer_0conf ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , user_channel_id : u64 ) -> Result < ( ) , APIError > {
4561
+ pub fn accept_inbound_channel_from_trusted_peer_0conf ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , user_channel_id : u128 ) -> Result < ( ) , APIError > {
4562
4562
self . do_accept_inbound_channel ( temporary_channel_id, counterparty_node_id, true , user_channel_id)
4563
4563
}
4564
4564
4565
- fn do_accept_inbound_channel ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , accept_0conf : bool , user_channel_id : u64 ) -> Result < ( ) , APIError > {
4565
+ fn do_accept_inbound_channel ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , accept_0conf : bool , user_channel_id : u128 ) -> Result < ( ) , APIError > {
4566
4566
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
4567
4567
4568
4568
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
@@ -4610,9 +4610,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4610
4610
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "No inbound channels accepted" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) ) ;
4611
4611
}
4612
4612
4613
+ let mut random_bytes = [ 0u8 ; 16 ] ;
4614
+ random_bytes. copy_from_slice ( & self . keys_manager . get_secure_random_bytes ( ) [ ..16 ] ) ;
4615
+ let user_channel_id = u128:: from_be_bytes ( random_bytes) ;
4616
+
4613
4617
let outbound_scid_alias = self . create_and_insert_outbound_scid_alias ( ) ;
4614
4618
let mut channel = match Channel :: new_from_req ( & self . fee_estimator , & self . keys_manager ,
4615
- counterparty_node_id. clone ( ) , & their_features, msg, 0 , & self . default_configuration ,
4619
+ counterparty_node_id. clone ( ) , & their_features, msg, user_channel_id , & self . default_configuration ,
4616
4620
self . best_block . read ( ) . unwrap ( ) . height ( ) , & self . logger , outbound_scid_alias)
4617
4621
{
4618
4622
Err ( e) => {
@@ -6450,33 +6454,108 @@ impl_writeable_tlv_based!(ChannelCounterparty, {
6450
6454
( 11 , outbound_htlc_maximum_msat, option) ,
6451
6455
} ) ;
6452
6456
6453
- impl_writeable_tlv_based ! ( ChannelDetails , {
6454
- ( 1 , inbound_scid_alias, option) ,
6455
- ( 2 , channel_id, required) ,
6456
- ( 3 , channel_type, option) ,
6457
- ( 4 , counterparty, required) ,
6458
- ( 5 , outbound_scid_alias, option) ,
6459
- ( 6 , funding_txo, option) ,
6460
- ( 7 , config, option) ,
6461
- ( 8 , short_channel_id, option) ,
6462
- ( 10 , channel_value_satoshis, required) ,
6463
- ( 12 , unspendable_punishment_reserve, option) ,
6464
- ( 14 , user_channel_id, required) ,
6465
- ( 16 , balance_msat, required) ,
6466
- ( 18 , outbound_capacity_msat, required) ,
6467
- // Note that by the time we get past the required read above, outbound_capacity_msat will be
6468
- // filled in, so we can safely unwrap it here.
6469
- ( 19 , next_outbound_htlc_limit_msat, ( default_value, outbound_capacity_msat. 0 . unwrap( ) as u64 ) ) ,
6470
- ( 20 , inbound_capacity_msat, required) ,
6471
- ( 22 , confirmations_required, option) ,
6472
- ( 24 , force_close_spend_delay, option) ,
6473
- ( 26 , is_outbound, required) ,
6474
- ( 28 , is_channel_ready, required) ,
6475
- ( 30 , is_usable, required) ,
6476
- ( 32 , is_public, required) ,
6477
- ( 33 , inbound_htlc_minimum_msat, option) ,
6478
- ( 35 , inbound_htlc_maximum_msat, option) ,
6479
- } ) ;
6457
+ impl Writeable for ChannelDetails {
6458
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
6459
+ // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
6460
+ // versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
6461
+ let user_channel_id_low = self . user_channel_id as u64 ;
6462
+ let user_channel_id_high_opt = Some ( ( self . user_channel_id >> 64 ) as u64 ) ;
6463
+ write_tlv_fields ! ( writer, {
6464
+ ( 1 , self . inbound_scid_alias, option) ,
6465
+ ( 2 , self . channel_id, required) ,
6466
+ ( 3 , self . channel_type, option) ,
6467
+ ( 4 , self . counterparty, required) ,
6468
+ ( 5 , self . outbound_scid_alias, option) ,
6469
+ ( 6 , self . funding_txo, option) ,
6470
+ ( 7 , self . config, option) ,
6471
+ ( 8 , self . short_channel_id, option) ,
6472
+ ( 10 , self . channel_value_satoshis, required) ,
6473
+ ( 12 , self . unspendable_punishment_reserve, option) ,
6474
+ ( 14 , user_channel_id_low, required) ,
6475
+ ( 16 , self . balance_msat, required) ,
6476
+ ( 18 , self . outbound_capacity_msat, required) ,
6477
+ // Note that by the time we get past the required read above, outbound_capacity_msat will be
6478
+ // filled in, so we can safely unwrap it here.
6479
+ ( 19 , self . next_outbound_htlc_limit_msat, ( default_value, outbound_capacity_msat. 0 . unwrap( ) as u64 ) ) ,
6480
+ ( 20 , self . inbound_capacity_msat, required) ,
6481
+ ( 22 , self . confirmations_required, option) ,
6482
+ ( 24 , self . force_close_spend_delay, option) ,
6483
+ ( 26 , self . is_outbound, required) ,
6484
+ ( 28 , self . is_channel_ready, required) ,
6485
+ ( 30 , self . is_usable, required) ,
6486
+ ( 32 , self . is_public, required) ,
6487
+ ( 33 , self . inbound_htlc_minimum_msat, option) ,
6488
+ ( 35 , self . inbound_htlc_maximum_msat, option) ,
6489
+ ( 37 , user_channel_id_high_opt, option) ,
6490
+ } ) ;
6491
+ Ok ( ( ) )
6492
+ }
6493
+ }
6494
+
6495
+ impl Readable for ChannelDetails {
6496
+ fn read < R : Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
6497
+ init_and_read_tlv_fields ! ( reader, {
6498
+ ( 1 , inbound_scid_alias, option) ,
6499
+ ( 2 , channel_id, required) ,
6500
+ ( 3 , channel_type, option) ,
6501
+ ( 4 , counterparty, required) ,
6502
+ ( 5 , outbound_scid_alias, option) ,
6503
+ ( 6 , funding_txo, option) ,
6504
+ ( 7 , config, option) ,
6505
+ ( 8 , short_channel_id, option) ,
6506
+ ( 10 , channel_value_satoshis, required) ,
6507
+ ( 12 , unspendable_punishment_reserve, option) ,
6508
+ ( 14 , user_channel_id_low, required) ,
6509
+ ( 16 , balance_msat, required) ,
6510
+ ( 18 , outbound_capacity_msat, required) ,
6511
+ // Note that by the time we get past the required read above, outbound_capacity_msat will be
6512
+ // filled in, so we can safely unwrap it here.
6513
+ ( 19 , next_outbound_htlc_limit_msat, ( default_value, outbound_capacity_msat. 0 . unwrap( ) as u64 ) ) ,
6514
+ ( 20 , inbound_capacity_msat, required) ,
6515
+ ( 22 , confirmations_required, option) ,
6516
+ ( 24 , force_close_spend_delay, option) ,
6517
+ ( 26 , is_outbound, required) ,
6518
+ ( 28 , is_channel_ready, required) ,
6519
+ ( 30 , is_usable, required) ,
6520
+ ( 32 , is_public, required) ,
6521
+ ( 33 , inbound_htlc_minimum_msat, option) ,
6522
+ ( 35 , inbound_htlc_maximum_msat, option) ,
6523
+ ( 37 , user_channel_id_high_opt, option) ,
6524
+ } ) ;
6525
+
6526
+ // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
6527
+ // versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
6528
+ let user_channel_id_low: u64 = user_channel_id_low. 0 . unwrap ( ) ;
6529
+ let user_channel_id = user_channel_id_low as u128 +
6530
+ ( ( user_channel_id_high_opt. unwrap_or ( 0 as u64 ) as u128 ) << 64 ) ;
6531
+
6532
+ Ok ( Self {
6533
+ inbound_scid_alias,
6534
+ channel_id : channel_id. 0 . unwrap ( ) ,
6535
+ channel_type,
6536
+ counterparty : counterparty. 0 . unwrap ( ) ,
6537
+ outbound_scid_alias,
6538
+ funding_txo,
6539
+ config,
6540
+ short_channel_id,
6541
+ channel_value_satoshis : channel_value_satoshis. 0 . unwrap ( ) ,
6542
+ unspendable_punishment_reserve,
6543
+ user_channel_id,
6544
+ balance_msat : balance_msat. 0 . unwrap ( ) ,
6545
+ outbound_capacity_msat : outbound_capacity_msat. 0 . unwrap ( ) ,
6546
+ next_outbound_htlc_limit_msat : next_outbound_htlc_limit_msat. 0 . unwrap ( ) ,
6547
+ inbound_capacity_msat : inbound_capacity_msat. 0 . unwrap ( ) ,
6548
+ confirmations_required,
6549
+ force_close_spend_delay,
6550
+ is_outbound : is_outbound. 0 . unwrap ( ) ,
6551
+ is_channel_ready : is_channel_ready. 0 . unwrap ( ) ,
6552
+ is_usable : is_usable. 0 . unwrap ( ) ,
6553
+ is_public : is_public. 0 . unwrap ( ) ,
6554
+ inbound_htlc_minimum_msat,
6555
+ inbound_htlc_maximum_msat,
6556
+ } )
6557
+ }
6558
+ }
6480
6559
6481
6560
impl_writeable_tlv_based ! ( PhantomRouteHints , {
6482
6561
( 2 , channels, vec_type) ,
0 commit comments