Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Store channel in monitor - simplified approach by joostjager · Pull Request #4190 · lightningdevkit/rust-lightning · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ _externalize_tests = ["inventory", "_test_utils"]
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
unsafe_revoked_tx_signing = []
safe_channels = []

std = []

Expand Down
71 changes: 67 additions & 4 deletions lightning/src/chain/channelmonitor.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
/// always `Some` otherwise.
pub channel_id: Option<ChannelId>,

/// The encoded channel data associated with this ChannelMonitor, if any.
pub encoded_channel: Option<Vec<u8>>,
}

impl ChannelMonitorUpdate {
Expand DownExpand Up@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
for update_step in self.updates.iter() {
update_step.write(w)?;
}
#[cfg(feature = "safe_channels")]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
(5, self.encoded_channel, option)
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(w, {
// 1 was previously used to store `counterparty_node_id`
(3, self.channel_id, option),
Expand All@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
}
}
let mut channel_id = None;
let mut encoded_channel = None;
read_tlv_fields!(r, {
// 1 was previously used to store `counterparty_node_id`
(3, channel_id, option),
(5, encoded_channel, option)
});
Ok(Self { update_id, updates, channel_id })
Ok(Self { update_id, updates, channel_id, encoded_channel })
}
}

Expand DownExpand Up@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
/// make deciding whether to do so simple, here we track whether this monitor was last written
/// prior to 0.1.
written_by_0_1_or_later: bool,

encoded_channel: Option<Vec<u8>>,
}

// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
Expand DownExpand Up@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
_ => channel_monitor.pending_monitor_events.clone(),
};

#[cfg(feature = "safe_channels")]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
(5, pending_monitor_events, required_vec),
(7, channel_monitor.funding_spend_seen, required),
(9, channel_monitor.counterparty_node_id, required),
(11, channel_monitor.confirmed_commitment_tx_counterparty_output, option),
(13, channel_monitor.spendable_txids_confirmed, required_vec),
(15, channel_monitor.counterparty_fulfilled_htlcs, required),
(17, channel_monitor.initial_counterparty_commitment_info, option),
(19, channel_monitor.channel_id, required),
(21, channel_monitor.balances_empty_height, option),
(23, channel_monitor.holder_pays_commitment_tx_fee, option),
(25, channel_monitor.payment_preimages, required),
(27, channel_monitor.first_negotiated_funding_txo, required),
(29, channel_monitor.initial_counterparty_commitment_tx, option),
(31, channel_monitor.funding.channel_parameters, required),
(32, channel_monitor.pending_funding, optional_vec),
(33, channel_monitor.htlcs_resolved_to_user, required),
(34, channel_monitor.alternative_funding_confirmed, option),
(35, channel_monitor.is_manual_broadcast, required),
(37, channel_monitor.funding_seen_onchain, required),
(39, channel_monitor.encoded_channel, option),
});
#[cfg(not(feature = "safe_channels"))]
write_tlv_fields!(writer, {
(1, channel_monitor.funding_spend_confirmed, option),
(3, channel_monitor.htlcs_resolved_on_chain, required_vec),
Expand DownExpand Up@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
alternative_funding_confirmed: None,

written_by_0_1_or_later: true,
encoded_channel: None,
})
}

Expand DownExpand Up@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
}

/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
self.inner.lock().unwrap().encoded_channel.clone()
}

/// Updates the encoded channel data associated with this ChannelMonitor.
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
self.inner.lock().unwrap().encoded_channel = Some(encoded);
}

/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
/// ChannelMonitor.
///
Expand DownExpand Up@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
}

if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
if ret.is_ok() {
if self.no_further_updates_allowed() && is_pre_close_update {
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
Err(())
} else {
// Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
// therefore do not update the monitor.
if let Some(encoded_channel) = updates.encoded_channel.as_ref() {
self.encoded_channel = Some(encoded_channel.clone());
}
Ok(())
}
} else { ret }
}

Expand DownExpand Up@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let mut alternative_funding_confirmed = None;
let mut is_manual_broadcast = RequiredWrapper(None);
let mut funding_seen_onchain = RequiredWrapper(None);
let mut encoded_channel = None;
read_tlv_fields!(reader, {
(1, funding_spend_confirmed, option),
(3, htlcs_resolved_on_chain, optional_vec),
Expand All@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
(34, alternative_funding_confirmed, option),
(35, is_manual_broadcast, (default_value, false)),
(37, funding_seen_onchain, (default_value, true)),
(39, encoded_channel, option),
});
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
Expand DownExpand Up@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
alternative_funding_confirmed,

written_by_0_1_or_later,
encoded_channel,
});

if counterparty_node_id.is_none() {
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2839,6 +2839,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn channel_holding_cell_serialize() {
do_channel_holding_cell_serialize(true, true);
Expand DownExpand Up@@ -3320,6 +3322,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_outbound_reload_without_init_mon() {
do_test_outbound_reload_without_init_mon(true);
Expand DownExpand Up@@ -3428,6 +3431,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo
assert!(nodes[1].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inbound_reload_without_init_mon() {
do_test_inbound_reload_without_init_mon(true, true);
Expand DownExpand Up@@ -3767,6 +3771,7 @@ fn do_test_inverted_mon_completion_order(
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_inverted_mon_completion_order() {
do_test_inverted_mon_completion_order(true, true);
Expand DownExpand Up@@ -3969,6 +3974,7 @@ fn do_test_durable_preimages_on_closed_channel(
}
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_durable_preimages_on_closed_channel() {
do_test_durable_preimages_on_closed_channel(true, true, true);
Expand DownExpand Up@@ -4093,6 +4099,7 @@ fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
send_payment(&nodes[1], &[&nodes[2]], 100_000);
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_reload_mon_update_completion_actions() {
do_test_reload_mon_update_completion_actions(true);
Expand DownExpand Up@@ -4459,6 +4466,7 @@ fn do_test_partial_claim_mon_update_compl_actions(reload_a: bool, reload_b: bool
assert!(!get_monitor!(nodes[3], chan_4_id).get_stored_preimages().contains_key(&payment_hash));
}

#[cfg(not(feature = "safe_channels"))]
#[test]
fn test_partial_claim_mon_update_compl_actions() {
do_test_partial_claim_mon_update_compl_actions(true, true);
Expand Down
24 changes: 21 additions & 3 deletions lightning/src/ln/channel.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2153,7 +2153,7 @@ where
// Not having a signing session implies they've already sent `splice_locked`,
// which must always come after the initial commitment signed is sent.
.unwrap_or(true);
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
funded_channel
.splice_initial_commitment_signed(msg, fee_estimator, logger)
.map(|monitor_update_opt| (None, monitor_update_opt))
Expand DownExpand Up@@ -6045,6 +6045,7 @@ where
should_broadcast: broadcast,
}],
channel_id: Some(self.channel_id()),
encoded_channel: None,
};
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
} else {
Expand DownExpand Up@@ -7276,6 +7277,7 @@ where
payment_info,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

if !self.context.channel_state.can_generate_new_commitment() {
Expand DownExpand Up@@ -7417,6 +7419,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
},
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
Expand DownExpand Up@@ -7892,14 +7895,15 @@ where
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);

self.context.latest_monitor_update_id += 1;
let monitor_update = ChannelMonitorUpdate {
let mut monitor_update = ChannelMonitorUpdate {
update_id: self.context.latest_monitor_update_id,
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
holder_commitment_tx,
counterparty_commitment_tx,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context
Expand All@@ -7909,6 +7913,7 @@ where
.received_commitment_signed();
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
Ok(self.push_ret_blockable_mon_update(monitor_update))
}

Expand DownExpand Up@@ -8165,6 +8170,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

self.context.expecting_peer_commitment_signed = false;
Expand DownExpand Up@@ -8217,6 +8223,7 @@ where
Vec::new(),
Vec::new(),
);
monitor_update.encoded_channel = Some(self.encode());
return Ok(self.push_ret_blockable_mon_update(monitor_update));
}

Expand DownExpand Up@@ -8270,6 +8277,7 @@ where
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
updates: Vec::new(),
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

let mut htlc_updates = Vec::new();
Expand DownExpand Up@@ -8346,7 +8354,7 @@ where
// `ChannelMonitorUpdate` to the user, making this one redundant, however
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
// We do not bother to track and include `payment_info` here, however.
let fulfill = self.get_update_fulfill_htlc(
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
htlc_id,
*payment_preimage,
None,
Expand All@@ -8360,6 +8368,8 @@ where
unreachable!()
};
update_fulfill_count += 1;

additional_monitor_update.encoded_channel = Some(self.encode());
monitor_update.updates.append(&mut additional_monitor_update.updates);
None
},
Expand DownExpand Up@@ -8418,6 +8428,8 @@ where
update_add_count, update_fulfill_count, update_fail_count);

self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());

monitor_update.encoded_channel = Some(self.encode());
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
} else {
(None, Vec::new())
Expand DownExpand Up@@ -8534,6 +8546,7 @@ where
secret: msg.per_commitment_secret,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: None,
};

// Update state now that we've passed all the can-fail calls...
Expand DownExpand Up@@ -8759,6 +8772,7 @@ where
};
macro_rules! return_with_htlcs_to_fail {
($htlcs_to_fail: expr) => {
monitor_update.encoded_channel = Some(self.encode());
if !release_monitor {
self.context
.blocked_monitor_updates
Expand DownExpand Up@@ -10384,6 +10398,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand DownExpand Up@@ -11153,6 +11168,7 @@ where
funding_txid: funding_txo.txid,
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
Expand DownExpand Up@@ -12712,6 +12728,7 @@ where
update_id: self.context.latest_monitor_update_id,
updates: vec![update],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.context.channel_state.set_awaiting_remote_revoke();
monitor_update
Expand DownExpand Up@@ -12958,6 +12975,7 @@ where
scriptpubkey: self.get_closing_scriptpubkey(),
}],
channel_id: Some(self.context.channel_id()),
encoded_channel: Some(self.encode()),
};
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
self.push_ret_blockable_mon_update(monitor_update)
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/channel_open_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2095,6 +2095,7 @@ pub fn test_batch_channel_open() {
)));
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_close_in_funding_batch() {
// This test ensures that if one of the channels
Expand DownExpand Up@@ -2183,6 +2184,7 @@ pub fn test_close_in_funding_batch() {
assert!(nodes[0].node.list_channels().is_empty());
}

#[cfg(not(feature = "safe_channels"))]
#[xtest(feature = "_externalize_tests")]
pub fn test_batch_funding_close_after_funding_signed() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
Loading
Loading