Skip to content

Commit 35c2b71

Browse files
Update monitor with preimage after channel close
If the channel is hitting the chain right as we receive a preimage, previous to this commit the relevant ChannelMonitor would never learn of this preimage.
1 parent 97e54ee commit 35c2b71

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use bitcoin::secp256k1;
3737
use chain;
3838
use chain::Watch;
3939
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
40-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent};
40+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, ChannelMonitorUpdateErr, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent};
4141
use chain::transaction::{OutPoint, TransactionData};
4242
use ln::channel::{Channel, ChannelError};
4343
use ln::features::{InitFeatures, NodeFeatures};
@@ -2150,13 +2150,28 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
21502150
});
21512151
},
21522152
HTLCSource::PreviousHopData(hop_data) => {
2153+
let hop_data_node_id = hop_data.counterparty_node_id;
2154+
let outpoint = hop_data.outpoint;
21532155
if let Err((counterparty_node_id, err)) = match self.claim_funds_from_hop(&mut channel_state_lock, hop_data, payment_preimage) {
21542156
Ok(()) => Ok(()),
21552157
Err(None) => {
2156-
// TODO: There is probably a channel monitor somewhere that needs to
2157-
// learn the preimage as the channel already hit the chain and that's
2158-
// why it's missing.
2159-
Ok(())
2158+
let preimage_update = ChannelMonitorUpdate {
2159+
update_id: std::u64::MAX,
2160+
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
2161+
payment_preimage: payment_preimage.clone(),
2162+
}],
2163+
};
2164+
match self.chain_monitor.update_channel(outpoint, preimage_update) {
2165+
Err(ChannelMonitorUpdateErr::PermanentFailure) => {
2166+
let err_msg = MsgHandleErrInternal::send_err_msg_no_close("Failed to update channel with preimage".to_owned(), outpoint.to_channel_id());
2167+
Err((hop_data_node_id, err_msg))
2168+
}
2169+
Err(ChannelMonitorUpdateErr::TemporaryFailure) => {
2170+
let err_msg = MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update channel with preimage".to_owned()), outpoint.to_channel_id());
2171+
Err((hop_data_node_id, err_msg))
2172+
}
2173+
Ok(()) => Ok(())
2174+
}
21602175
},
21612176
Err(Some(res)) => Err(res),
21622177
} {

0 commit comments

Comments
 (0)