Skip to content

Commit cd1ef7d

Browse files
committed
store channel in monitor
1 parent dcad93c commit cd1ef7d

File tree

3 files changed

+197
-170
lines changed

3 files changed

+197
-170
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub struct ChannelMonitorUpdate {
111111
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
112112
/// always `Some` otherwise.
113113
pub channel_id: Option<ChannelId>,
114+
115+
pub encoded_channel: Option<Vec<u8>>,
114116
}
115117

116118
impl ChannelMonitorUpdate {
@@ -159,6 +161,7 @@ impl Writeable for ChannelMonitorUpdate {
159161
write_tlv_fields!(w, {
160162
// 1 was previously used to store `counterparty_node_id`
161163
(3, self.channel_id, option),
164+
(5, self.encoded_channel, option)
162165
});
163166
Ok(())
164167
}
@@ -176,11 +179,13 @@ impl Readable for ChannelMonitorUpdate {
176179
}
177180
}
178181
let mut channel_id = None;
182+
let mut encoded_channel = None;
179183
read_tlv_fields!(r, {
180184
// 1 was previously used to store `counterparty_node_id`
181185
(3, channel_id, option),
186+
(5, encoded_channel, option)
182187
});
183-
Ok(Self { update_id, updates, channel_id })
188+
Ok(Self { update_id, updates, channel_id, encoded_channel })
184189
}
185190
}
186191

@@ -1402,6 +1407,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
14021407
/// make deciding whether to do so simple, here we track whether this monitor was last written
14031408
/// prior to 0.1.
14041409
written_by_0_1_or_later: bool,
1410+
1411+
encoded_channel: Option<Vec<u8>>,
14051412
}
14061413

14071414
// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
@@ -1755,6 +1762,7 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
17551762
(34, channel_monitor.alternative_funding_confirmed, option),
17561763
(35, channel_monitor.is_manual_broadcast, required),
17571764
(37, channel_monitor.funding_seen_onchain, required),
1765+
(39, channel_monitor.encoded_channel, option),
17581766
});
17591767

17601768
Ok(())
@@ -1994,6 +2002,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19942002
alternative_funding_confirmed: None,
19952003

19962004
written_by_0_1_or_later: true,
2005+
encoded_channel: None,
19972006
})
19982007
}
19992008

@@ -2114,6 +2123,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
21142123
inner.update_monitor(updates, broadcaster, fee_estimator, &logger)
21152124
}
21162125

2126+
pub fn get_encoded_channel(&self) -> Option<Vec<u8>> {
2127+
self.inner.lock().unwrap().encoded_channel.clone()
2128+
}
2129+
2130+
pub fn update_encoded_channel(&self, encoded: Vec<u8>) {
2131+
self.inner.lock().unwrap().encoded_channel = Some(encoded);
2132+
}
2133+
21172134
/// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
21182135
/// ChannelMonitor.
21192136
///
@@ -4405,9 +4422,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
44054422
}
44064423
}
44074424

4408-
if ret.is_ok() && self.no_further_updates_allowed() && is_pre_close_update {
4409-
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
4410-
Err(())
4425+
if ret.is_ok() {
4426+
if self.no_further_updates_allowed() && is_pre_close_update {
4427+
log_error!(logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent");
4428+
Err(())
4429+
} else {
4430+
self.encoded_channel = updates.encoded_channel.clone();
4431+
Ok(())
4432+
}
44114433
} else { ret }
44124434
}
44134435

@@ -6645,6 +6667,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66456667
let mut alternative_funding_confirmed = None;
66466668
let mut is_manual_broadcast = RequiredWrapper(None);
66476669
let mut funding_seen_onchain = RequiredWrapper(None);
6670+
let mut encoded_channel = None;
66486671
read_tlv_fields!(reader, {
66496672
(1, funding_spend_confirmed, option),
66506673
(3, htlcs_resolved_on_chain, optional_vec),
@@ -6667,6 +6690,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66676690
(34, alternative_funding_confirmed, option),
66686691
(35, is_manual_broadcast, (default_value, false)),
66696692
(37, funding_seen_onchain, (default_value, true)),
6693+
(39, encoded_channel, option),
66706694
});
66716695
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
66726696
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
@@ -6844,6 +6868,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
68446868
alternative_funding_confirmed,
68456869

68466870
written_by_0_1_or_later,
6871+
encoded_channel,
68476872
});
68486873

68496874
if counterparty_node_id.is_none() {

lightning/src/ln/channel.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ where
21532153
// Not having a signing session implies they've already sent `splice_locked`,
21542154
// which must always come after the initial commitment signed is sent.
21552155
.unwrap_or(true);
2156-
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
2156+
let res: Result<(Option<ChannelMonitor<<<SP as Deref>::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError> = if has_negotiated_pending_splice && !session_received_commitment_signed {
21572157
funded_channel
21582158
.splice_initial_commitment_signed(msg, fee_estimator, logger)
21592159
.map(|monitor_update_opt| (None, monitor_update_opt))
@@ -6045,6 +6045,7 @@ where
60456045
should_broadcast: broadcast,
60466046
}],
60476047
channel_id: Some(self.channel_id()),
6048+
encoded_channel: None,
60486049
};
60496050
Some((self.get_counterparty_node_id(), funding_txo, self.channel_id(), update))
60506051
} else {
@@ -7276,6 +7277,7 @@ where
72767277
payment_info,
72777278
}],
72787279
channel_id: Some(self.context.channel_id()),
7280+
encoded_channel: None,
72797281
};
72807282

72817283
if !self.context.channel_state.can_generate_new_commitment() {
@@ -7417,6 +7419,7 @@ where
74177419
Vec::new(),
74187420
Vec::new(),
74197421
);
7422+
monitor_update.encoded_channel = Some(self.encode());
74207423
UpdateFulfillCommitFetch::NewClaim { monitor_update, htlc_value_msat }
74217424
},
74227425
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
@@ -7892,14 +7895,15 @@ where
78927895
&self.context.channel_id(), pending_splice_funding.get_funding_txo().unwrap().txid);
78937896

78947897
self.context.latest_monitor_update_id += 1;
7895-
let monitor_update = ChannelMonitorUpdate {
7898+
let mut monitor_update = ChannelMonitorUpdate {
78967899
update_id: self.context.latest_monitor_update_id,
78977900
updates: vec![ChannelMonitorUpdateStep::RenegotiatedFunding {
78987901
channel_parameters: pending_splice_funding.channel_transaction_parameters.clone(),
78997902
holder_commitment_tx,
79007903
counterparty_commitment_tx,
79017904
}],
79027905
channel_id: Some(self.context.channel_id()),
7906+
encoded_channel: None,
79037907
};
79047908

79057909
self.context
@@ -7909,6 +7913,7 @@ where
79097913
.received_commitment_signed();
79107914
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
79117915

7916+
monitor_update.encoded_channel = Some(self.encode());
79127917
Ok(self.push_ret_blockable_mon_update(monitor_update))
79137918
}
79147919

@@ -8165,6 +8170,7 @@ where
81658170
update_id: self.context.latest_monitor_update_id,
81668171
updates: vec![update],
81678172
channel_id: Some(self.context.channel_id()),
8173+
encoded_channel: None,
81688174
};
81698175

81708176
self.context.expecting_peer_commitment_signed = false;
@@ -8217,6 +8223,7 @@ where
82178223
Vec::new(),
82188224
Vec::new(),
82198225
);
8226+
monitor_update.encoded_channel = Some(self.encode());
82208227
return Ok(self.push_ret_blockable_mon_update(monitor_update));
82218228
}
82228229

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

82758283
let mut htlc_updates = Vec::new();
@@ -8346,7 +8354,7 @@ where
83468354
// `ChannelMonitorUpdate` to the user, making this one redundant, however
83478355
// there's no harm in including the extra `ChannelMonitorUpdateStep` here.
83488356
// We do not bother to track and include `payment_info` here, however.
8349-
let fulfill = self.get_update_fulfill_htlc(
8357+
let fulfill: UpdateFulfillFetch = self.get_update_fulfill_htlc(
83508358
htlc_id,
83518359
*payment_preimage,
83528360
None,
@@ -8360,6 +8368,8 @@ where
83608368
unreachable!()
83618369
};
83628370
update_fulfill_count += 1;
8371+
8372+
additional_monitor_update.encoded_channel = Some(self.encode());
83638373
monitor_update.updates.append(&mut additional_monitor_update.updates);
83648374
None
83658375
},
@@ -8418,6 +8428,8 @@ where
84188428
update_add_count, update_fulfill_count, update_fail_count);
84198429

84208430
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
8431+
8432+
monitor_update.encoded_channel = Some(self.encode());
84218433
(self.push_ret_blockable_mon_update(monitor_update), htlcs_to_fail)
84228434
} else {
84238435
(None, Vec::new())
@@ -8534,6 +8546,7 @@ where
85348546
secret: msg.per_commitment_secret,
85358547
}],
85368548
channel_id: Some(self.context.channel_id()),
8549+
encoded_channel: None,
85378550
};
85388551

85398552
// Update state now that we've passed all the can-fail calls...
@@ -8759,6 +8772,7 @@ where
87598772
};
87608773
macro_rules! return_with_htlcs_to_fail {
87618774
($htlcs_to_fail: expr) => {
8775+
monitor_update.encoded_channel = Some(self.encode());
87628776
if !release_monitor {
87638777
self.context
87648778
.blocked_monitor_updates
@@ -10384,6 +10398,7 @@ where
1038410398
scriptpubkey: self.get_closing_scriptpubkey(),
1038510399
}],
1038610400
channel_id: Some(self.context.channel_id()),
10401+
encoded_channel: Some(self.encode()),
1038710402
};
1038810403
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
1038910404
self.push_ret_blockable_mon_update(monitor_update)
@@ -11153,6 +11168,7 @@ where
1115311168
funding_txid: funding_txo.txid,
1115411169
}],
1115511170
channel_id: Some(self.context.channel_id()),
11171+
encoded_channel: Some(self.encode()),
1115611172
};
1115711173
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
1115811174
let monitor_update = self.push_ret_blockable_mon_update(monitor_update);
@@ -12712,6 +12728,7 @@ where
1271212728
update_id: self.context.latest_monitor_update_id,
1271312729
updates: vec![update],
1271412730
channel_id: Some(self.context.channel_id()),
12731+
encoded_channel: Some(self.encode()),
1271512732
};
1271612733
self.context.channel_state.set_awaiting_remote_revoke();
1271712734
monitor_update
@@ -12958,6 +12975,7 @@ where
1295812975
scriptpubkey: self.get_closing_scriptpubkey(),
1295912976
}],
1296012977
channel_id: Some(self.context.channel_id()),
12978+
encoded_channel: Some(self.encode()),
1296112979
};
1296212980
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
1296312981
self.push_ret_blockable_mon_update(monitor_update)

0 commit comments

Comments
 (0)