Skip to content

Commit 28df2b7

Browse files
committed
Store unencoded channel in monitor
1 parent 1adb140 commit 28df2b7

File tree

3 files changed

+391
-118
lines changed

3 files changed

+391
-118
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::ln::chan_utils::{
5151
HTLCClaim, HTLCOutputInCommitment, HolderCommitmentTransaction,
5252
};
5353
#[cfg(feature = "safe_channels")]
54-
use crate::ln::channel::read_check_data;
54+
use crate::ln::channel::FundedChannelState;
5555
use crate::ln::channel::INITIAL_COMMITMENT_NUMBER;
5656
use crate::ln::channel_keys::{
5757
DelayedPaymentBasepoint, DelayedPaymentKey, HtlcBasepoint, HtlcKey, RevocationBasepoint,
@@ -114,11 +114,25 @@ pub struct ChannelMonitorUpdate {
114114
/// always `Some` otherwise.
115115
pub channel_id: Option<ChannelId>,
116116

117-
/// The encoded channel data associated with this ChannelMonitor, if any.
117+
/// The channel state associated with this ChannelMonitorUpdate, if any.
118118
#[cfg(feature = "safe_channels")]
119-
pub encoded_channel: Option<Vec<u8>>,
119+
pub encoded_channel: Option<UpdateChannelState>,
120120
}
121121

122+
/// The state of a channel to be stored alongside a ChannelMonitor. For closed channels, no state is stored.
123+
#[derive(Clone, Debug, PartialEq, Eq)]
124+
pub enum UpdateChannelState {
125+
/// Open channel in funded state.
126+
Funded(FundedChannelState),
127+
/// Closed channel.
128+
Closed,
129+
}
130+
131+
impl_writeable_tlv_based_enum!(UpdateChannelState,
132+
(1, Closed) => {},
133+
{0, Funded} => (),
134+
);
135+
122136
impl ChannelMonitorUpdate {
123137
pub(crate) fn internal_renegotiated_funding_data(
124138
&self,
@@ -1432,7 +1446,7 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
14321446
/// The serialized channel state as provided via the last `ChannelMonitorUpdate` or via a call to
14331447
/// [`ChannelMonitor::update_encoded_channel`].
14341448
#[cfg(feature = "safe_channels")]
1435-
encoded_channel: Option<Vec<u8>>,
1449+
encoded_channel: Option<UpdateChannelState>,
14361450
}
14371451

14381452
// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
@@ -1555,7 +1569,7 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
15551569
// Check that the encoded channel (if present) is consistent with the rest of the monitor. This sets an invariant
15561570
// for the safe_channels feature.
15571571
#[cfg(feature = "safe_channels")]
1558-
if let Some(ref encoded_channel) = channel_monitor.encoded_channel {
1572+
if let Some(UpdateChannelState::Funded(ref encoded_channel)) = channel_monitor.encoded_channel {
15591573
channel_monitor.check_encoded_channel_consistency(encoded_channel);
15601574
}
15611575
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
@@ -2182,14 +2196,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
21822196

21832197
/// Gets the encoded channel data, if any, associated with this ChannelMonitor.
21842198
#[cfg(feature = "safe_channels")]
2185-
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
2199+
pub fn get_encoded_channel(&self) -> Option<UpdateChannelState> {
21862200
self.inner.lock().unwrap().encoded_channel.clone()
21872201
}
21882202

21892203
/// Updates the encoded channel data associated with this ChannelMonitor. To clear the encoded channel data (for
2190-
/// example after shut down of a channel), pass an empty vector.
2204+
/// example after shut down of a channel), pass `None`.
21912205
#[cfg(feature = "safe_channels")]
2192-
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
2206+
pub fn update_encoded_channel(&self, encoded: UpdateChannelState) {
21932207
self.inner.lock().unwrap().update_encoded_channel(encoded);
21942208
}
21952209

@@ -2799,52 +2813,45 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
27992813

28002814
impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
28012815
#[cfg(feature = "safe_channels")]
2802-
fn check_encoded_channel_consistency(&self, encoded: &[u8]) {
2803-
let encoded_channel_reader = &mut &encoded[..];
2804-
let check_res = read_check_data(encoded_channel_reader);
2805-
if let Ok(check_data) = check_res {
2806-
debug_assert!(
2807-
check_data.cur_holder_commitment_transaction_number
2808-
<= self.get_cur_holder_commitment_number(),
2809-
"cur_holder_commitment_transaction_number - channel: {} vs monitor: {}",
2810-
check_data.cur_holder_commitment_transaction_number,
2811-
self.get_cur_holder_commitment_number()
2812-
);
2813-
debug_assert!(
2814-
check_data.revoked_counterparty_commitment_transaction_number
2815-
<= self.get_min_seen_secret(),
2816-
"revoked_counterparty_commitment_transaction_number - channel: {} vs monitor: {}",
2817-
check_data.revoked_counterparty_commitment_transaction_number,
2818-
self.get_min_seen_secret()
2819-
);
2820-
debug_assert!(
2821-
check_data.cur_counterparty_commitment_transaction_number
2822-
<= self.get_cur_counterparty_commitment_number(),
2823-
"cur_counterparty_commitment_transaction_number - channel: {} vs monitor: {}",
2824-
check_data.cur_counterparty_commitment_transaction_number,
2825-
self.get_cur_counterparty_commitment_number()
2826-
);
2827-
debug_assert!(
2828-
check_data.latest_monitor_update_id >= self.get_latest_update_id(),
2829-
"latest_monitor_update_id - channel: {} vs monitor: {}",
2830-
check_data.latest_monitor_update_id,
2831-
self.get_latest_update_id()
2832-
);
2833-
} else {
2834-
debug_assert!(false, "Failed to read check data from encoded channel");
2835-
}
2816+
fn check_encoded_channel_consistency(&self, encoded: &FundedChannelState) {
2817+
debug_assert!(
2818+
encoded.get_cur_holder_commitment_transaction_number()
2819+
<= self.get_cur_holder_commitment_number(),
2820+
"cur_holder_commitment_transaction_number - channel: {} vs monitor: {}",
2821+
encoded.get_cur_holder_commitment_transaction_number(),
2822+
self.get_cur_holder_commitment_number()
2823+
);
2824+
debug_assert!(
2825+
encoded.get_revoked_counterparty_commitment_transaction_number()
2826+
<= self.get_min_seen_secret(),
2827+
"revoked_counterparty_commitment_transaction_number - channel: {} vs monitor: {}",
2828+
encoded.get_revoked_counterparty_commitment_transaction_number(),
2829+
self.get_min_seen_secret()
2830+
);
2831+
debug_assert!(
2832+
encoded.get_cur_counterparty_commitment_transaction_number()
2833+
<= self.get_cur_counterparty_commitment_number(),
2834+
"cur_counterparty_commitment_transaction_number - channel: {} vs monitor: {}",
2835+
encoded.get_cur_counterparty_commitment_transaction_number(),
2836+
self.get_cur_counterparty_commitment_number()
2837+
);
2838+
debug_assert!(
2839+
encoded.latest_monitor_update_id >= self.get_latest_update_id(),
2840+
"latest_monitor_update_id - channel: {} vs monitor: {}",
2841+
encoded.latest_monitor_update_id,
2842+
self.get_latest_update_id()
2843+
);
28362844
}
28372845

28382846
#[cfg(feature = "safe_channels")]
2839-
fn update_encoded_channel(&mut self, encoded: Vec<u8>) {
2840-
if encoded.len() > 0 {
2847+
fn update_encoded_channel(&mut self, encoded: UpdateChannelState) {
2848+
if let UpdateChannelState::Funded(ref channel) = encoded {
28412849
// Check that the encoded channel is consistent with the rest of the monitor. This sets an invariant for the
28422850
// safe_channels feature.
2843-
self.check_encoded_channel_consistency(&encoded);
2844-
self.encoded_channel = Some(encoded);
2845-
} else {
2846-
self.encoded_channel = None;
2851+
self.check_encoded_channel_consistency(channel);
28472852
}
2853+
2854+
self.encoded_channel = Some(encoded);
28482855
}
28492856

28502857
/// Helper for get_claimable_balances which does the work for an individual HTLC, generating up

0 commit comments

Comments
 (0)