From f2b7ccaa868a2b955b56164ba63da8744ffb6d40 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Tue, 17 Mar 2020 19:54:16 -0400 Subject: [PATCH] Verify commitment point on ChannelReestablish (no updates case). Adds a test for PR #537. --- lightning/src/ln/channel.rs | 73 ++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 81762923f..a9565af9e 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4255,22 +4255,28 @@ impl ReadableArgs> for Channel = Arc::new(test_utils::TestLogger::new()); + let secp_ctx = Secp256k1::new(); + let mut seed = [0; 32]; + let mut rng = thread_rng(); + rng.fill_bytes(&mut seed); + let network = Network::Testnet; + let keys_provider = test_utils::TestKeysInterface::new(&seed, network, logger.clone() as Arc); + + // Go through the flow of opening a channel between two nodes. + + // Create Node A's channel + let node_a_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()); + let config = UserConfig::default(); + let mut node_a_chan = Channel::::new_outbound(&&feeest, &&keys_provider, node_a_node_id, 10000000, 100000, 42, Arc::clone(&logger), &config).unwrap(); + + // Create Node B's channel by receiving Node A's open_channel message + let open_channel_msg = node_a_chan.get_open_channel(genesis_block(network).header.bitcoin_hash(), &&feeest); + let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap()); + let mut node_b_chan = Channel::::new_from_req(&&feeest, &&keys_provider, node_b_node_id, InitFeatures::supported(), &open_channel_msg, 7, logger, &config).unwrap(); + + // Node B --> Node A: accept channel + let accept_channel_msg = node_b_chan.get_accept_channel(); + node_a_chan.accept_channel(&accept_channel_msg, &config, InitFeatures::supported()).unwrap(); + + // Node A --> Node B: funding created + let output_script = node_a_chan.get_funding_redeemscript(); + let tx = Transaction { version: 1, lock_time: 0, input: Vec::new(), output: vec![TxOut { + value: 10000000, script_pubkey: output_script.clone(), + }]}; + let funding_outpoint = OutPoint::new(tx.txid(), 0); + let (funding_created_msg, _) = node_a_chan.get_outbound_funding_created(funding_outpoint).unwrap(); + let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg).unwrap(); + + // Node B --> Node A: funding signed + let _ = node_a_chan.funding_signed(&funding_signed_msg); + + // Now disconnect the two nodes and check that the commitment point in + // Node B's channel_reestablish message is sane. + node_b_chan.remove_uncommitted_htlcs_and_mark_paused(); + 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)); + let msg = node_b_chan.get_channel_reestablish(); + match msg.data_loss_protect { + OptionalField::Present(DataLossProtect { my_current_per_commitment_point, .. }) => { + assert_eq!(expected_commitment_point, my_current_per_commitment_point); + }, + _ => panic!() + } + + // Check that the commitment point in Node A's channel_reestablish message + // is sane. + node_a_chan.remove_uncommitted_htlcs_and_mark_paused(); + 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)); + let msg = node_a_chan.get_channel_reestablish(); + match msg.data_loss_protect { + OptionalField::Present(DataLossProtect { my_current_per_commitment_point, .. }) => { + assert_eq!(expected_commitment_point, my_current_per_commitment_point); + }, + _ => panic!() + } + } + #[test] fn outbound_commitment_test() { // Test vectors from BOLT 3 Appendix C: -- 2.39.5