@@ -501,11 +501,27 @@ impl ChannelManager {
501
501
/// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
502
502
/// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
503
503
pub fn create_channel ( & self , their_network_key : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 ) -> Result < ( ) , APIError > {
504
+ self . create_channel_with_id ( their_network_key, rng:: rand_u832 ( ) , channel_value_satoshis, push_msat, user_id)
505
+ }
506
+
507
+ /// Creates a new outbound channel to the given remote node and with the given value.
508
+ ///
509
+ /// user_id will be provided back as user_channel_id in FundingGenerationReady and
510
+ /// FundingBroadcastSafe events to allow tracking of which events correspond with which
511
+ /// create_channel call. Note that user_channel_id defaults to 0 for inbound channels, so you
512
+ /// may wish to avoid using 0 for user_id here.
513
+ ///
514
+ /// If successful, will generate a SendOpenChannel message event, so you should probably poll
515
+ /// PeerManager::process_events afterwards.
516
+ ///
517
+ /// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
518
+ /// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
519
+ pub fn create_channel_with_id ( & self , their_network_key : PublicKey , channel_id : [ u8 ; 32 ] , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 ) -> Result < ( ) , APIError > {
504
520
if channel_value_satoshis < 1000 {
505
521
return Err ( APIError :: APIMisuseError { err : "channel_value must be at least 1000 satoshis" } ) ;
506
522
}
507
523
508
- let channel = Channel :: new_outbound ( & * self . fee_estimator , & self . keys_manager , their_network_key, channel_value_satoshis, push_msat, user_id, Arc :: clone ( & self . logger ) , & self . default_configuration ) ?;
524
+ let channel = Channel :: new_outbound ( & * self . fee_estimator , & self . keys_manager , their_network_key, channel_id , channel_value_satoshis, push_msat, user_id, Arc :: clone ( & self . logger ) , & self . default_configuration ) ?;
509
525
let res = channel. get_open_channel ( self . genesis_hash . clone ( ) , & * self . fee_estimator ) ;
510
526
511
527
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
@@ -5119,6 +5135,63 @@ mod tests {
5119
5135
assert ! ( nodes[ 2 ] . node. list_channels( ) . is_empty( ) ) ;
5120
5136
}
5121
5137
5138
+ #[ test]
5139
+ #[ should_panic]
5140
+ fn bolt2_open_channel_sending_node_checks1 ( ) { //This test needs to be on its own as we are catching a panic
5141
+ let nodes = create_network ( 2 ) ;
5142
+
5143
+ // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
5144
+ // Note: currently a collision on a channel id will panic, not creating the channel with an error will be better as the system can still continue to function
5145
+ let channel_value_satoshis=10000 ;
5146
+ let push_msat=10001 ;
5147
+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , channel_value_satoshis, push_msat, 42 ) . unwrap ( ) ;
5148
+ let node0_to_1_send_open_channel = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
5149
+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , & node0_to_1_send_open_channel) . unwrap ( ) ;
5150
+
5151
+ //Create a second channel with a channel_id collision
5152
+ let channel_id=node0_to_1_send_open_channel. temporary_channel_id ;
5153
+ assert ! ( nodes[ 0 ] . node. create_channel_with_id( nodes[ 0 ] . node. get_our_node_id( ) , channel_id, channel_value_satoshis, push_msat, 42 ) . is_err( ) ) ;
5154
+ }
5155
+
5156
+ #[ test]
5157
+ fn bolt2_open_channel_sending_node_checks2 ( ) {
5158
+ let nodes = create_network ( 2 ) ;
5159
+
5160
+ // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
5161
+ let channel_value_satoshis=2 ^24 ;
5162
+ let push_msat=10001 ;
5163
+ assert ! ( nodes[ 0 ] . node. create_channel( nodes[ 1 ] . node. get_our_node_id( ) , channel_value_satoshis, push_msat, 42 ) . is_err( ) ) ;
5164
+
5165
+ // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
5166
+ let channel_value_satoshis=10000 ;
5167
+ // Test when push_msat is equal to 1000 * funding_satoshis.
5168
+ let push_msat=1000 * channel_value_satoshis+1 ;
5169
+ assert ! ( nodes[ 0 ] . node. create_channel( nodes[ 1 ] . node. get_our_node_id( ) , channel_value_satoshis, push_msat, 42 ) . is_err( ) ) ;
5170
+
5171
+ // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
5172
+ let channel_value_satoshis=10000 ;
5173
+ let push_msat=10001 ;
5174
+ assert ! ( nodes[ 0 ] . node. create_channel( nodes[ 1 ] . node. get_our_node_id( ) , channel_value_satoshis, push_msat, 42 ) . is_ok( ) ) ; //Create a valid channel
5175
+ let node0_to_1_send_open_channel = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
5176
+ assert ! ( node0_to_1_send_open_channel. channel_reserve_satoshis>=node0_to_1_send_open_channel. dust_limit_satoshis) ;
5177
+
5178
+ // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
5179
+ // Only the least-significant bit of channel_flags is currently defined resulting in channel_flags only having one of two possible states 0 or 1
5180
+ assert ! ( node0_to_1_send_open_channel. channel_flags<=1 ) ;
5181
+
5182
+ // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
5183
+ let chain_hash=genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ;
5184
+ assert_eq ! ( node0_to_1_send_open_channel. chain_hash, chain_hash) ;
5185
+
5186
+ // BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
5187
+ let secp = Secp256k1 :: new ( ) ;
5188
+ assert ! ( PublicKey :: from_slice( & secp, & node0_to_1_send_open_channel. funding_pubkey. serialize( ) ) . is_ok( ) ) ;
5189
+ assert ! ( PublicKey :: from_slice( & secp, & node0_to_1_send_open_channel. revocation_basepoint. serialize( ) ) . is_ok( ) ) ;
5190
+ assert ! ( PublicKey :: from_slice( & secp, & node0_to_1_send_open_channel. htlc_basepoint. serialize( ) ) . is_ok( ) ) ;
5191
+ assert ! ( PublicKey :: from_slice( & secp, & node0_to_1_send_open_channel. payment_basepoint. serialize( ) ) . is_ok( ) ) ;
5192
+ assert ! ( PublicKey :: from_slice( & secp, & node0_to_1_send_open_channel. delayed_payment_basepoint. serialize( ) ) . is_ok( ) ) ;
5193
+ }
5194
+
5122
5195
#[ test]
5123
5196
fn test_shutdown_rebroadcast ( ) {
5124
5197
do_test_shutdown_rebroadcast ( 0 ) ;
0 commit comments