@@ -4255,22 +4255,28 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
42554255
42564256#[ cfg( test) ]
42574257mod tests {
4258+ use bitcoin:: BitcoinHash ;
42584259 use bitcoin:: util:: bip143;
42594260 use bitcoin:: consensus:: encode:: serialize;
42604261 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;
42624264 use bitcoin:: blockdata:: opcodes;
4265+ use bitcoin:: network:: constants:: Network ;
42634266 use bitcoin_hashes:: hex:: FromHex ;
42644267 use hex;
42654268 use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
42664269 use ln:: channel:: { Channel , ChannelKeys , InboundHTLCOutput , OutboundHTLCOutput , InboundHTLCState , OutboundHTLCState , HTLCOutputInCommitment , TxCreationKeys } ;
42674270 use ln:: channel:: MAX_FUNDING_SATOSHIS ;
4271+ use ln:: features:: InitFeatures ;
4272+ use ln:: msgs:: { OptionalField , DataLossProtect } ;
42684273 use ln:: chan_utils;
42694274 use ln:: chan_utils:: { LocalCommitmentTransaction , ChannelPublicKeys } ;
42704275 use chain:: chaininterface:: { FeeEstimator , ConfirmationTarget } ;
42714276 use chain:: keysinterface:: { InMemoryChannelKeys , KeysInterface } ;
42724277 use chain:: transaction:: OutPoint ;
42734278 use util:: config:: UserConfig ;
4279+ use util:: enforcing_trait_impls:: EnforcingChannelKeys ;
42744280 use util:: test_utils;
42754281 use util:: logger:: Logger ;
42764282 use secp256k1:: { Secp256k1 , Message , Signature , All } ;
@@ -4280,6 +4286,7 @@ mod tests {
42804286 use bitcoin_hashes:: hash160:: Hash as Hash160 ;
42814287 use bitcoin_hashes:: Hash ;
42824288 use std:: sync:: Arc ;
4289+ use rand:: { thread_rng, Rng } ;
42834290
42844291 struct TestFeeEstimator {
42854292 fee_est : u64
@@ -4327,6 +4334,70 @@ mod tests {
43274334 PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & hex:: decode ( hex) . unwrap ( ) [ ..] ) . unwrap ( ) )
43284335 }
43294336
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+
43304401 #[ test]
43314402 fn outbound_commitment_test ( ) {
43324403 // Test vectors from BOLT 3 Appendix C:
0 commit comments