@@ -1119,6 +1119,66 @@ pub(crate) struct ShutdownResult {
1119
1119
pub(crate) channel_funding_txo: Option<OutPoint>,
1120
1120
}
1121
1121
1122
+ #[derive(Debug, Copy, Clone)]
1123
+ enum HolderCommitmentPoint {
1124
+ PendingNext { transaction_number: u64, current: PublicKey },
1125
+ Available { transaction_number: u64, current: PublicKey, next: PublicKey },
1126
+ }
1127
+
1128
+ impl HolderCommitmentPoint {
1129
+ pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Self
1130
+ where SP::Target: SignerProvider
1131
+ {
1132
+ HolderCommitmentPoint::Available {
1133
+ transaction_number: INITIAL_COMMITMENT_NUMBER,
1134
+ current: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx),
1135
+ next: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx),
1136
+ }
1137
+ }
1138
+
1139
+ pub fn is_available(&self) -> bool {
1140
+ if let HolderCommitmentPoint::Available { .. } = self { true } else { false }
1141
+ }
1142
+
1143
+ pub fn transaction_number(&self) -> u64 {
1144
+ match self {
1145
+ HolderCommitmentPoint::PendingNext { transaction_number, .. } => *transaction_number,
1146
+ HolderCommitmentPoint::Available { transaction_number, .. } => *transaction_number,
1147
+ }
1148
+ }
1149
+
1150
+ pub fn current_point(&self) -> PublicKey {
1151
+ match self {
1152
+ HolderCommitmentPoint::PendingNext { current, .. } => *current,
1153
+ HolderCommitmentPoint::Available { current, .. } => *current,
1154
+ }
1155
+ }
1156
+
1157
+ pub fn next_point(&self) -> Option<PublicKey> {
1158
+ match self {
1159
+ HolderCommitmentPoint::PendingNext { .. } => None,
1160
+ HolderCommitmentPoint::Available { next, .. } => Some(*next),
1161
+ }
1162
+ }
1163
+
1164
+ pub fn advance<SP: Deref, L: Deref>(&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L)
1165
+ where SP::Target: SignerProvider, L::Target: Logger
1166
+ {
1167
+ if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
1168
+ *self = HolderCommitmentPoint::PendingNext {
1169
+ transaction_number: *transaction_number - 1,
1170
+ current: *next,
1171
+ };
1172
+ }
1173
+
1174
+ if let HolderCommitmentPoint::PendingNext { transaction_number, current } = self {
1175
+ let next = signer.as_ref().get_per_commitment_point(*transaction_number - 1, secp_ctx);
1176
+ log_trace!(logger, "Retrieved next per-commitment point {}", *transaction_number - 1);
1177
+ *self = HolderCommitmentPoint::Available { transaction_number: *transaction_number, current: *current, next };
1178
+ }
1179
+ }
1180
+ }
1181
+
1122
1182
/// If the majority of the channels funds are to the fundee and the initiator holds only just
1123
1183
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
1124
1184
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -1297,6 +1357,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1297
1357
// generation start at 0 and count up...this simplifies some parts of implementation at the
1298
1358
// cost of others, but should really just be changed.
1299
1359
1360
+ holder_commitment_point: HolderCommitmentPoint,
1300
1361
cur_holder_commitment_transaction_number: u64,
1301
1362
cur_counterparty_commitment_transaction_number: u64,
1302
1363
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
@@ -1739,6 +1800,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1739
1800
1740
1801
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
1741
1802
1803
+ let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1804
+ let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
1805
+
1742
1806
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
1743
1807
1744
1808
let channel_context = ChannelContext {
@@ -1764,10 +1828,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1764
1828
1765
1829
latest_monitor_update_id: 0,
1766
1830
1767
- holder_signer: ChannelSignerType::Ecdsa(holder_signer) ,
1831
+ holder_signer,
1768
1832
shutdown_scriptpubkey,
1769
1833
destination_script,
1770
1834
1835
+ holder_commitment_point,
1771
1836
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1772
1837
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1773
1838
value_to_self_msat,
@@ -1963,6 +2028,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1963
2028
1964
2029
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
1965
2030
2031
+ let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2032
+ let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
2033
+
1966
2034
Ok(Self {
1967
2035
user_id,
1968
2036
@@ -1986,10 +2054,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1986
2054
1987
2055
latest_monitor_update_id: 0,
1988
2056
1989
- holder_signer: ChannelSignerType::Ecdsa(holder_signer) ,
2057
+ holder_signer,
1990
2058
shutdown_scriptpubkey,
1991
2059
destination_script,
1992
2060
2061
+ holder_commitment_point,
1993
2062
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1994
2063
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1995
2064
value_to_self_msat,
@@ -8779,6 +8848,10 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8779
8848
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
8780
8849
}
8781
8850
8851
+ // `current_point` will become optional when async signing is implemented.
8852
+ let cur_holder_commitment_point = Some(self.context.holder_commitment_point.current_point());
8853
+ let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8854
+
8782
8855
write_tlv_fields!(writer, {
8783
8856
(0, self.context.announcement_sigs, option),
8784
8857
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8815,7 +8888,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8815
8888
(39, pending_outbound_blinding_points, optional_vec),
8816
8889
(41, holding_cell_blinding_points, optional_vec),
8817
8890
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8818
- // 45 and 47 are reserved for async signing
8891
+ (45, cur_holder_commitment_point, option),
8892
+ (47, next_holder_commitment_point, option),
8819
8893
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8820
8894
});
8821
8895
@@ -9126,6 +9200,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9126
9200
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
9127
9201
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
9128
9202
9203
+ let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9204
+ let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9205
+
9129
9206
read_tlv_fields!(reader, {
9130
9207
(0, announcement_sigs, option),
9131
9208
(1, minimum_depth, option),
@@ -9156,7 +9233,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9156
9233
(39, pending_outbound_blinding_points_opt, optional_vec),
9157
9234
(41, holding_cell_blinding_points_opt, optional_vec),
9158
9235
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9159
- // 45 and 47 are reserved for async signing
9236
+ (45, cur_holder_commitment_point_opt, option),
9237
+ (47, next_holder_commitment_point_opt, option),
9160
9238
(49, local_initiated_shutdown, option),
9161
9239
});
9162
9240
@@ -9268,6 +9346,24 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9268
9346
}
9269
9347
}
9270
9348
9349
+ // If we're restoring this channel for the first time after an upgrade, then we require that the
9350
+ // signer be available so that we can immediately populate the current commitment point. Channel
9351
+ // restoration will fail if this is not possible.
9352
+ let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9353
+ (Some(current), Some(next)) => HolderCommitmentPoint::Available {
9354
+ transaction_number: cur_holder_commitment_transaction_number, current, next
9355
+ },
9356
+ (Some(current), _) => HolderCommitmentPoint::Available {
9357
+ transaction_number: cur_holder_commitment_transaction_number, current,
9358
+ next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9359
+ },
9360
+ (_, _) => HolderCommitmentPoint::Available {
9361
+ transaction_number: cur_holder_commitment_transaction_number,
9362
+ current: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number, &secp_ctx),
9363
+ next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9364
+ },
9365
+ };
9366
+
9271
9367
Ok(Channel {
9272
9368
context: ChannelContext {
9273
9369
user_id,
@@ -9293,6 +9389,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9293
9389
shutdown_scriptpubkey,
9294
9390
destination_script,
9295
9391
9392
+ holder_commitment_point,
9296
9393
cur_holder_commitment_transaction_number,
9297
9394
cur_counterparty_commitment_transaction_number,
9298
9395
value_to_self_msat,
0 commit comments