Skip to content

Commit 9fbafd4

Browse files
authored
Merge pull request #1430 from vincenzopalazzo/macros/channel_reestablish_v2
send warning when we receive a old commitment transaction
2 parents 2324012 + e6300da commit 9fbafd4

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

lightning/src/ln/channel.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3756,6 +3756,15 @@ impl<Signer: Sign> Channel<Signer> {
37563756
}
37573757
}
37583758

3759+
// Before we change the state of the channel, we check if the peer is sending a very old
3760+
// commitment transaction number, if yes we send a warning message.
3761+
let our_commitment_transaction = INITIAL_COMMITMENT_NUMBER - self.cur_holder_commitment_transaction_number - 1;
3762+
if msg.next_remote_commitment_number + 1 < our_commitment_transaction {
3763+
return Err(
3764+
ChannelError::Warn(format!("Peer attempted to reestablish channel with a very old local commitment transaction: {} (received) vs {} (expected)", msg.next_remote_commitment_number, our_commitment_transaction))
3765+
);
3766+
}
3767+
37593768
// Go ahead and unmark PeerDisconnected as various calls we may make check for it (and all
37603769
// remaining cases either succeed or ErrorMessage-fail).
37613770
self.channel_state &= !(ChannelState::PeerDisconnected as u32);

lightning/src/ln/functional_tests.rs

+40-14
Original file line numberDiff line numberDiff line change
@@ -7345,7 +7345,7 @@ fn test_data_loss_protect() {
73457345
logger = test_utils::TestLogger::with_id(format!("node {}", 0));
73467346
let mut chain_monitor = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut io::Cursor::new(previous_chain_monitor_state.0), keys_manager).unwrap().1;
73477347
chain_source = test_utils::TestChainSource::new(Network::Testnet);
7348-
tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))};
7348+
tx_broadcaster = test_utils::TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new())) };
73497349
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
73507350
persister = test_utils::TestPersister::new();
73517351
monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
@@ -7402,22 +7402,48 @@ fn test_data_loss_protect() {
74027402
}
74037403

74047404
// Check we close channel detecting A is fallen-behind
7405+
// Check that we sent the warning message when we detected that A has fallen behind,
7406+
// and give the possibility for A to recover from the warning.
74057407
nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
7406-
check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Peer attempted to reestablish channel with a very old local commitment transaction".to_string() });
7407-
assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Peer attempted to reestablish channel with a very old local commitment transaction");
7408-
check_added_monitors!(nodes[1], 1);
7408+
let warn_msg = "Peer attempted to reestablish channel with a very old local commitment transaction".to_owned();
7409+
assert!(check_warn_msg!(nodes[1], nodes[0].node.get_our_node_id(), chan.2).contains(&warn_msg));
74097410

74107411
// Check A is able to claim to_remote output
7411-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
7412-
assert_eq!(node_txn.len(), 1);
7413-
check_spends!(node_txn[0], chan.3);
7414-
assert_eq!(node_txn[0].output.len(), 2);
7415-
mine_transaction(&nodes[0], &node_txn[0]);
7416-
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7417-
check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can\'t do any automated broadcasting".to_string() });
7418-
let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
7419-
assert_eq!(spend_txn.len(), 1);
7420-
check_spends!(spend_txn[0], node_txn[0]);
7412+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
7413+
// The node B should not broadcast the transaction to force close the channel!
7414+
assert!(node_txn.is_empty());
7415+
// B should now detect that there is something wrong and should force close the channel.
7416+
let exp_err = "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can\'t do any automated broadcasting";
7417+
check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: exp_err.to_string() });
7418+
7419+
// after the warning message sent by B, we should not able to
7420+
// use the channel, or reconnect with success to the channel.
7421+
assert!(nodes[0].node.list_usable_channels().is_empty());
7422+
nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
7423+
nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
7424+
let retry_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
7425+
7426+
nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &retry_reestablish[0]);
7427+
let mut err_msgs_0 = Vec::with_capacity(1);
7428+
for msg in nodes[0].node.get_and_clear_pending_msg_events() {
7429+
if let MessageSendEvent::HandleError { ref action, .. } = msg {
7430+
match action {
7431+
&ErrorAction::SendErrorMessage { ref msg } => {
7432+
assert_eq!(msg.data, "Failed to find corresponding channel");
7433+
err_msgs_0.push(msg.clone());
7434+
},
7435+
_ => panic!("Unexpected event!"),
7436+
}
7437+
} else {
7438+
panic!("Unexpected event!");
7439+
}
7440+
}
7441+
assert_eq!(err_msgs_0.len(), 1);
7442+
nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &err_msgs_0[0]);
7443+
assert!(nodes[1].node.list_usable_channels().is_empty());
7444+
check_added_monitors!(nodes[1], 1);
7445+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: "Failed to find corresponding channel".to_owned() });
7446+
check_closed_broadcast!(nodes[1], false);
74217447
}
74227448

74237449
#[test]

0 commit comments

Comments
 (0)