@@ -4255,22 +4255,28 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
4255
4255
4256
4256
#[ cfg( test) ]
4257
4257
mod tests {
4258
+ use bitcoin:: BitcoinHash ;
4258
4259
use bitcoin:: util:: bip143;
4259
4260
use bitcoin:: consensus:: encode:: serialize;
4260
4261
use bitcoin:: blockdata:: script:: { Script , Builder } ;
4261
- use bitcoin:: blockdata:: transaction:: Transaction ;
4262
+ use bitcoin:: blockdata:: transaction:: { Transaction , TxOut } ;
4263
+ use bitcoin:: blockdata:: constants:: genesis_block;
4262
4264
use bitcoin:: blockdata:: opcodes;
4265
+ use bitcoin:: network:: constants:: Network ;
4263
4266
use bitcoin_hashes:: hex:: FromHex ;
4264
4267
use hex;
4265
4268
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
4266
4269
use ln:: channel:: { Channel , ChannelKeys , InboundHTLCOutput , OutboundHTLCOutput , InboundHTLCState , OutboundHTLCState , HTLCOutputInCommitment , TxCreationKeys } ;
4267
4270
use ln:: channel:: MAX_FUNDING_SATOSHIS ;
4271
+ use ln:: features:: InitFeatures ;
4272
+ use ln:: msgs:: { OptionalField , DataLossProtect } ;
4268
4273
use ln:: chan_utils;
4269
4274
use ln:: chan_utils:: { LocalCommitmentTransaction , ChannelPublicKeys } ;
4270
4275
use chain:: chaininterface:: { FeeEstimator , ConfirmationTarget } ;
4271
4276
use chain:: keysinterface:: { InMemoryChannelKeys , KeysInterface } ;
4272
4277
use chain:: transaction:: OutPoint ;
4273
4278
use util:: config:: UserConfig ;
4279
+ use util:: enforcing_trait_impls:: EnforcingChannelKeys ;
4274
4280
use util:: test_utils;
4275
4281
use util:: logger:: Logger ;
4276
4282
use secp256k1:: { Secp256k1 , Message , Signature , All } ;
@@ -4280,6 +4286,7 @@ mod tests {
4280
4286
use bitcoin_hashes:: hash160:: Hash as Hash160 ;
4281
4287
use bitcoin_hashes:: Hash ;
4282
4288
use std:: sync:: Arc ;
4289
+ use rand:: { thread_rng, Rng } ;
4283
4290
4284
4291
struct TestFeeEstimator {
4285
4292
fee_est : u64
@@ -4327,6 +4334,70 @@ mod tests {
4327
4334
PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & hex:: decode ( hex) . unwrap ( ) [ ..] ) . unwrap ( ) )
4328
4335
}
4329
4336
4337
+ #[ test]
4338
+ fn channel_reestablish_no_updates ( ) {
4339
+ let feeest = TestFeeEstimator { fee_est : 15000 } ;
4340
+ let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
4341
+ let secp_ctx = Secp256k1 :: new ( ) ;
4342
+ let mut seed = [ 0 ; 32 ] ;
4343
+ let mut rng = thread_rng ( ) ;
4344
+ rng. fill_bytes ( & mut seed) ;
4345
+ let network = Network :: Testnet ;
4346
+ let keys_provider = test_utils:: TestKeysInterface :: new ( & seed, network, logger. clone ( ) as Arc < Logger > ) ;
4347
+
4348
+ // Go through the flow of opening a channel between two nodes.
4349
+
4350
+ // Create Node A's channel
4351
+ let node_a_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
4352
+ let config = UserConfig :: default ( ) ;
4353
+ let mut node_a_chan = Channel :: < EnforcingChannelKeys > :: new_outbound ( & & feeest, & & keys_provider, node_a_node_id, 10000000 , 100000 , 42 , Arc :: clone ( & logger) , & config) . unwrap ( ) ;
4354
+
4355
+ // Create Node B's channel by receiving Node A's open_channel message
4356
+ let open_channel_msg = node_a_chan. get_open_channel ( genesis_block ( network) . header . bitcoin_hash ( ) , & & feeest) ;
4357
+ let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
4358
+ let mut node_b_chan = Channel :: < EnforcingChannelKeys > :: new_from_req ( & & feeest, & & keys_provider, node_b_node_id, InitFeatures :: supported ( ) , & open_channel_msg, 7 , logger, & config) . unwrap ( ) ;
4359
+
4360
+ // Node B --> Node A: accept channel
4361
+ let accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
4362
+ node_a_chan. accept_channel ( & accept_channel_msg, & config, InitFeatures :: supported ( ) ) . unwrap ( ) ;
4363
+
4364
+ // Node A --> Node B: funding created
4365
+ let output_script = node_a_chan. get_funding_redeemscript ( ) ;
4366
+ let tx = Transaction { version : 1 , lock_time : 0 , input : Vec :: new ( ) , output : vec ! [ TxOut {
4367
+ value: 10000000 , script_pubkey: output_script. clone( ) ,
4368
+ } ] } ;
4369
+ let funding_outpoint = OutPoint :: new ( tx. txid ( ) , 0 ) ;
4370
+ let ( funding_created_msg, _) = node_a_chan. get_outbound_funding_created ( funding_outpoint) . unwrap ( ) ;
4371
+ let ( funding_signed_msg, _) = node_b_chan. funding_created ( & funding_created_msg) . unwrap ( ) ;
4372
+
4373
+ // Node B --> Node A: funding signed
4374
+ let _ = node_a_chan. funding_signed ( & funding_signed_msg) ;
4375
+
4376
+ // Now disconnect the two nodes and check that the commitment point in
4377
+ // Node B's channel_reestablish message is sane.
4378
+ node_b_chan. remove_uncommitted_htlcs_and_mark_paused ( ) ;
4379
+ let expected_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & node_b_chan. build_local_commitment_secret ( node_b_chan. cur_local_commitment_transaction_number + 1 ) ) ;
4380
+ let msg = node_b_chan. get_channel_reestablish ( ) ;
4381
+ match msg. data_loss_protect {
4382
+ OptionalField :: Present ( DataLossProtect { my_current_per_commitment_point, .. } ) => {
4383
+ assert_eq ! ( expected_commitment_point, my_current_per_commitment_point) ;
4384
+ } ,
4385
+ _ => panic ! ( )
4386
+ }
4387
+
4388
+ // Check that the commitment point in Node A's channel_reestablish message
4389
+ // is sane.
4390
+ node_a_chan. remove_uncommitted_htlcs_and_mark_paused ( ) ;
4391
+ let expected_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & node_a_chan. build_local_commitment_secret ( node_a_chan. cur_local_commitment_transaction_number + 1 ) ) ;
4392
+ let msg = node_a_chan. get_channel_reestablish ( ) ;
4393
+ match msg. data_loss_protect {
4394
+ OptionalField :: Present ( DataLossProtect { my_current_per_commitment_point, .. } ) => {
4395
+ assert_eq ! ( expected_commitment_point, my_current_per_commitment_point) ;
4396
+ } ,
4397
+ _ => panic ! ( )
4398
+ }
4399
+ }
4400
+
4330
4401
#[ test]
4331
4402
fn outbound_commitment_test ( ) {
4332
4403
// Test vectors from BOLT 3 Appendix C:
0 commit comments