Skip to content

Commit 2c9c81f

Browse files
committed
fix unit test
1 parent 4f79ebe commit 2c9c81f

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,12 +2343,12 @@ impl Channel {
23432343
if msg.next_remote_commitment_number > 0 {
23442344
if let Some(ref data_loss) = msg.data_loss_protect {
23452345
//check if provided signature is a valid signature from us
2346-
if chan_utils::build_commitment_secret(self.local_keys.commitment_seed, msg.next_remote_commitment_number-1) != data_loss.your_last_per_commitment_secret{
2346+
if chan_utils::build_commitment_secret(self.local_keys.commitment_seed, INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1) != data_loss.your_last_per_commitment_secret{
23472347
return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided"));
23482348
}
23492349
//check if we have fallen beind
23502350
//We should not broadcast commitment transaction or continue
2351-
if msg.next_remote_commitment_number > self.cur_local_commitment_transaction_number{
2351+
if msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER- self.cur_local_commitment_transaction_number{
23522352
return Err(ChannelError::CloseNoPublish("We have fallen behind and we cannot catch up, need to close channel but not publish commitment"));
23532353
}
23542354
}

src/ln/functional_tests.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5590,6 +5590,8 @@ fn test_onchain_to_onchain_claim() {
55905590

55915591
#[test]
55925592
fn test_onchain_claim_after_reestablish_fail(){
5593+
//This test tries to claim after reestablishes fails and dataloss protect is on
5594+
//This means that peer A is behind and must not cleam as per bolt spec
55935595
let mut nodes = create_network(2);
55945596

55955597
// Create some initial channels
@@ -5639,18 +5641,21 @@ fn test_onchain_claim_after_reestablish_fail(){
56395641
let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
56405642

56415643
nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
5642-
assert!(!nodes[1].node.get_and_clear_pending_msg_events().is_empty()); //this should not be empty as node B must notify A is is behind
5644+
assert!(!nodes[1].node.get_and_clear_pending_msg_events().is_empty()); //this should not be empty
56435645

5644-
let me =nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
5645-
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5646+
nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]); //throws an error do not publish error as expected
5647+
assert!(!nodes[0].node.get_and_clear_pending_msg_events().is_empty()); //this should not be empty
56465648

56475649
let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
56485650
assert_eq!(commitment_tx[0].input.len(), 1);
56495651
assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
56505652

5653+
// let A try and claim and fail as block limit has not passed
5654+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5655+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()] }, 1);
5656+
assert!(!nodes[0].node.claim_funds(payment_preimage));
56515657

56525658
// let B claim
5653-
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
56545659
assert!(nodes[1].node.claim_funds(payment_preimage));
56555660
check_added_monitors!(nodes[1], 1);
56565661
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()] }, 1);
@@ -5663,18 +5668,6 @@ fn test_onchain_claim_after_reestablish_fail(){
56635668
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
56645669
_ => panic!("Unexepected event"),
56655670
}
5666-
5667-
// Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
5668-
/*let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
5669-
check_spends!(node_txn[0], commitment_tx[0].clone());
5670-
assert_eq!(node_txn[0], node_txn[2]);
5671-
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5672-
check_spends!(node_txn[1], chan_1.3.clone());
5673-
5674-
let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1);
5675-
assert_eq!(spend_txn.len(), 2);
5676-
assert_eq!(spend_txn[0], spend_txn[1]);
5677-
check_spends!(spend_txn[0], node_txn[0].clone());*/
56785671
}
56795672

56805673
#[test]

0 commit comments

Comments
 (0)