@@ -1119,6 +1119,75 @@ pub(crate) struct ShutdownResult {
1119
1119
pub(crate) channel_funding_txo: Option<OutPoint>,
1120
1120
}
1121
1121
1122
+ /// Tracks the transaction number, along with current and next commitment points.
1123
+ /// This consolidates the logic to advance our commitment number and request new
1124
+ /// commitment points from our signer.
1125
+ #[derive(Debug, Copy, Clone)]
1126
+ enum HolderCommitmentPoint {
1127
+ // TODO: add a variant for before our first commitment point is retrieved
1128
+ /// We've advanced our commitment number and are waiting on the next commitment point.
1129
+ /// Until the `get_per_commitment_point` signer method becomes async, this variant
1130
+ /// will not be used.
1131
+ PendingNext { transaction_number: u64, current: PublicKey },
1132
+ /// Our current commitment point is ready, we've cached our next point,
1133
+ /// and we are not pending a new one.
1134
+ Available { transaction_number: u64, current: PublicKey, next: PublicKey },
1135
+ }
1136
+
1137
+ impl HolderCommitmentPoint {
1138
+ pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Self
1139
+ where SP::Target: SignerProvider
1140
+ {
1141
+ HolderCommitmentPoint::Available {
1142
+ transaction_number: INITIAL_COMMITMENT_NUMBER,
1143
+ current: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx),
1144
+ next: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx),
1145
+ }
1146
+ }
1147
+
1148
+ pub fn is_available(&self) -> bool {
1149
+ if let HolderCommitmentPoint::Available { .. } = self { true } else { false }
1150
+ }
1151
+
1152
+ pub fn transaction_number(&self) -> u64 {
1153
+ match self {
1154
+ HolderCommitmentPoint::PendingNext { transaction_number, .. } => *transaction_number,
1155
+ HolderCommitmentPoint::Available { transaction_number, .. } => *transaction_number,
1156
+ }
1157
+ }
1158
+
1159
+ pub fn current_point(&self) -> PublicKey {
1160
+ match self {
1161
+ HolderCommitmentPoint::PendingNext { current, .. } => *current,
1162
+ HolderCommitmentPoint::Available { current, .. } => *current,
1163
+ }
1164
+ }
1165
+
1166
+ pub fn next_point(&self) -> Option<PublicKey> {
1167
+ match self {
1168
+ HolderCommitmentPoint::PendingNext { .. } => None,
1169
+ HolderCommitmentPoint::Available { next, .. } => Some(*next),
1170
+ }
1171
+ }
1172
+
1173
+ pub fn advance<SP: Deref, L: Deref>(&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L)
1174
+ where SP::Target: SignerProvider, L::Target: Logger
1175
+ {
1176
+ if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
1177
+ *self = HolderCommitmentPoint::PendingNext {
1178
+ transaction_number: *transaction_number - 1,
1179
+ current: *next,
1180
+ };
1181
+ }
1182
+
1183
+ if let HolderCommitmentPoint::PendingNext { transaction_number, current } = self {
1184
+ let next = signer.as_ref().get_per_commitment_point(*transaction_number - 1, secp_ctx);
1185
+ log_trace!(logger, "Retrieved next per-commitment point {}", *transaction_number - 1);
1186
+ *self = HolderCommitmentPoint::Available { transaction_number: *transaction_number, current: *current, next };
1187
+ }
1188
+ }
1189
+ }
1190
+
1122
1191
/// If the majority of the channels funds are to the fundee and the initiator holds only just
1123
1192
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
1124
1193
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -1297,6 +1366,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1297
1366
// generation start at 0 and count up...this simplifies some parts of implementation at the
1298
1367
// cost of others, but should really just be changed.
1299
1368
1369
+ holder_commitment_point: HolderCommitmentPoint,
1300
1370
cur_holder_commitment_transaction_number: u64,
1301
1371
cur_counterparty_commitment_transaction_number: u64,
1302
1372
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
@@ -1739,6 +1809,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1739
1809
1740
1810
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
1741
1811
1812
+ let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1813
+ let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
1814
+
1742
1815
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
1743
1816
1744
1817
let channel_context = ChannelContext {
@@ -1764,10 +1837,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1764
1837
1765
1838
latest_monitor_update_id: 0,
1766
1839
1767
- holder_signer: ChannelSignerType::Ecdsa(holder_signer) ,
1840
+ holder_signer,
1768
1841
shutdown_scriptpubkey,
1769
1842
destination_script,
1770
1843
1844
+ holder_commitment_point,
1771
1845
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1772
1846
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1773
1847
value_to_self_msat,
@@ -1963,6 +2037,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1963
2037
1964
2038
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
1965
2039
2040
+ let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2041
+ let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
2042
+
1966
2043
Ok(Self {
1967
2044
user_id,
1968
2045
@@ -1986,10 +2063,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1986
2063
1987
2064
latest_monitor_update_id: 0,
1988
2065
1989
- holder_signer: ChannelSignerType::Ecdsa(holder_signer) ,
2066
+ holder_signer,
1990
2067
shutdown_scriptpubkey,
1991
2068
destination_script,
1992
2069
2070
+ holder_commitment_point,
1993
2071
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1994
2072
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1995
2073
value_to_self_msat,
@@ -8779,6 +8857,10 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8779
8857
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
8780
8858
}
8781
8859
8860
+ // `current_point` will become optional when async signing is implemented.
8861
+ let cur_holder_commitment_point = Some(self.context.holder_commitment_point.current_point());
8862
+ let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8863
+
8782
8864
write_tlv_fields!(writer, {
8783
8865
(0, self.context.announcement_sigs, option),
8784
8866
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8815,7 +8897,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8815
8897
(39, pending_outbound_blinding_points, optional_vec),
8816
8898
(41, holding_cell_blinding_points, optional_vec),
8817
8899
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8818
- // 45 and 47 are reserved for async signing
8900
+ (45, cur_holder_commitment_point, option),
8901
+ (47, next_holder_commitment_point, option),
8819
8902
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8820
8903
});
8821
8904
@@ -9126,6 +9209,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9126
9209
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
9127
9210
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
9128
9211
9212
+ let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9213
+ let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9214
+
9129
9215
read_tlv_fields!(reader, {
9130
9216
(0, announcement_sigs, option),
9131
9217
(1, minimum_depth, option),
@@ -9156,7 +9242,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9156
9242
(39, pending_outbound_blinding_points_opt, optional_vec),
9157
9243
(41, holding_cell_blinding_points_opt, optional_vec),
9158
9244
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9159
- // 45 and 47 are reserved for async signing
9245
+ (45, cur_holder_commitment_point_opt, option),
9246
+ (47, next_holder_commitment_point_opt, option),
9160
9247
(49, local_initiated_shutdown, option),
9161
9248
});
9162
9249
@@ -9268,6 +9355,24 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9268
9355
}
9269
9356
}
9270
9357
9358
+ // If we're restoring this channel for the first time after an upgrade, then we require that the
9359
+ // signer be available so that we can immediately populate the current commitment point. Channel
9360
+ // restoration will fail if this is not possible.
9361
+ let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9362
+ (Some(current), Some(next)) => HolderCommitmentPoint::Available {
9363
+ transaction_number: cur_holder_commitment_transaction_number, current, next
9364
+ },
9365
+ (Some(current), _) => HolderCommitmentPoint::Available {
9366
+ transaction_number: cur_holder_commitment_transaction_number, current,
9367
+ next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9368
+ },
9369
+ (_, _) => HolderCommitmentPoint::Available {
9370
+ transaction_number: cur_holder_commitment_transaction_number,
9371
+ current: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number, &secp_ctx),
9372
+ next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9373
+ },
9374
+ };
9375
+
9271
9376
Ok(Channel {
9272
9377
context: ChannelContext {
9273
9378
user_id,
@@ -9293,6 +9398,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9293
9398
shutdown_scriptpubkey,
9294
9399
destination_script,
9295
9400
9401
+ holder_commitment_point,
9296
9402
cur_holder_commitment_transaction_number,
9297
9403
cur_counterparty_commitment_transaction_number,
9298
9404
value_to_self_msat,
0 commit comments