@@ -1119,6 +1119,72 @@ 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
+ Uninitialized { transaction_number: u64 },
1125
+ PendingNext { transaction_number: u64, current: PublicKey },
1126
+ Available { transaction_number: u64, current: PublicKey, next: PublicKey },
1127
+ }
1128
+
1129
+ impl HolderCommitmentPoint where {
1130
+ pub fn new() -> Self {
1131
+ HolderCommitmentPoint::Uninitialized { transaction_number: INITIAL_COMMITMENT_NUMBER }
1132
+ }
1133
+
1134
+ pub fn is_available(&self) -> bool {
1135
+ if let HolderCommitmentPoint::Available { .. } = self { true } else { false }
1136
+ }
1137
+
1138
+ pub fn transaction_number(&self) -> u64 {
1139
+ match self {
1140
+ HolderCommitmentPoint::Uninitialized { transaction_number } => *transaction_number,
1141
+ HolderCommitmentPoint::PendingNext { transaction_number, .. } => *transaction_number,
1142
+ HolderCommitmentPoint::Available { transaction_number, .. } => *transaction_number,
1143
+ }
1144
+ }
1145
+
1146
+ pub fn current_point(&self) -> Option<PublicKey> {
1147
+ match self {
1148
+ HolderCommitmentPoint::Uninitialized { .. } => None,
1149
+ HolderCommitmentPoint::PendingNext { current, .. } => Some(*current),
1150
+ HolderCommitmentPoint::Available { current, .. } => Some(*current),
1151
+ }
1152
+ }
1153
+
1154
+ pub fn next_point(&self) -> Option<PublicKey> {
1155
+ match self {
1156
+ HolderCommitmentPoint::Uninitialized { .. } => None,
1157
+ HolderCommitmentPoint::PendingNext { .. } => None,
1158
+ HolderCommitmentPoint::Available { next, .. } => Some(*next),
1159
+ }
1160
+ }
1161
+
1162
+ pub fn advance<SP: Deref, L: Deref>(&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L)
1163
+ where SP::Target: SignerProvider, L::Target: Logger
1164
+ {
1165
+ if let HolderCommitmentPoint::Uninitialized { transaction_number } = self {
1166
+ let current = signer.as_ref().get_per_commitment_point(*transaction_number, secp_ctx); // TODO
1167
+ log_trace!(logger, "Retrieved current per-commitment point {}", transaction_number);
1168
+ *self = HolderCommitmentPoint::PendingNext { transaction_number: *transaction_number, current };
1169
+ // TODO: handle error case when get_per_commitment_point becomes async
1170
+ }
1171
+
1172
+ if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
1173
+ *self = HolderCommitmentPoint::PendingNext {
1174
+ transaction_number: *transaction_number - 1,
1175
+ current: *next,
1176
+ };
1177
+ }
1178
+
1179
+ if let HolderCommitmentPoint::PendingNext { transaction_number, current } = self {
1180
+ let next = signer.as_ref().get_per_commitment_point(*transaction_number - 1, secp_ctx); // TODO
1181
+ log_trace!(logger, "Retrieved next per-commitment point {}", *transaction_number - 1);
1182
+ *self = HolderCommitmentPoint::Available { transaction_number: *transaction_number, current: *current, next };
1183
+ // TODO: handle error case when get_per_commitment_point becomes async
1184
+ }
1185
+ }
1186
+ }
1187
+
1122
1188
/// If the majority of the channels funds are to the fundee and the initiator holds only just
1123
1189
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
1124
1190
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -1297,6 +1363,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1297
1363
// generation start at 0 and count up...this simplifies some parts of implementation at the
1298
1364
// cost of others, but should really just be changed.
1299
1365
1366
+ holder_commitment_point: HolderCommitmentPoint,
1300
1367
cur_holder_commitment_transaction_number: u64,
1301
1368
cur_counterparty_commitment_transaction_number: u64,
1302
1369
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
@@ -1739,6 +1806,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1739
1806
1740
1807
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
1741
1808
1809
+ let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1810
+ let mut holder_commitment_point = HolderCommitmentPoint::new();
1811
+ holder_commitment_point.advance(&holder_signer, &secp_ctx, &&logger);
1812
+
1742
1813
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
1743
1814
1744
1815
let channel_context = ChannelContext {
@@ -1764,10 +1835,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1764
1835
1765
1836
latest_monitor_update_id: 0,
1766
1837
1767
- holder_signer: ChannelSignerType::Ecdsa(holder_signer) ,
1838
+ holder_signer,
1768
1839
shutdown_scriptpubkey,
1769
1840
destination_script,
1770
1841
1842
+ holder_commitment_point,
1771
1843
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1772
1844
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1773
1845
value_to_self_msat,
@@ -1965,6 +2037,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1965
2037
1966
2038
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
1967
2039
2040
+ let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2041
+ let mut holder_commitment_point = HolderCommitmentPoint::new();
2042
+ holder_commitment_point.advance(&holder_signer, &secp_ctx, logger);
2043
+
1968
2044
Ok(Self {
1969
2045
user_id,
1970
2046
@@ -1988,10 +2064,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1988
2064
1989
2065
latest_monitor_update_id: 0,
1990
2066
1991
- holder_signer: ChannelSignerType::Ecdsa(holder_signer) ,
2067
+ holder_signer,
1992
2068
shutdown_scriptpubkey,
1993
2069
destination_script,
1994
2070
2071
+ holder_commitment_point,
1995
2072
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1996
2073
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
1997
2074
value_to_self_msat,
@@ -8785,6 +8862,9 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8785
8862
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
8786
8863
}
8787
8864
8865
+ let cur_holder_commitment_point = self.context.holder_commitment_point.current_point();
8866
+ let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8867
+
8788
8868
write_tlv_fields!(writer, {
8789
8869
(0, self.context.announcement_sigs, option),
8790
8870
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8821,7 +8901,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8821
8901
(39, pending_outbound_blinding_points, optional_vec),
8822
8902
(41, holding_cell_blinding_points, optional_vec),
8823
8903
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8824
- // 45 and 47 are reserved for async signing
8904
+ (45, cur_holder_commitment_point, option),
8905
+ (47, next_holder_commitment_point, option),
8825
8906
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
8826
8907
});
8827
8908
@@ -9132,6 +9213,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9132
9213
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
9133
9214
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
9134
9215
9216
+ let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9217
+ let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9218
+
9135
9219
read_tlv_fields!(reader, {
9136
9220
(0, announcement_sigs, option),
9137
9221
(1, minimum_depth, option),
@@ -9162,7 +9246,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9162
9246
(39, pending_outbound_blinding_points_opt, optional_vec),
9163
9247
(41, holding_cell_blinding_points_opt, optional_vec),
9164
9248
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9165
- // 45 and 47 are reserved for async signing
9249
+ (45, cur_holder_commitment_point_opt, option),
9250
+ (47, next_holder_commitment_point_opt, option),
9166
9251
(49, local_initiated_shutdown, option),
9167
9252
});
9168
9253
@@ -9274,6 +9359,21 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9274
9359
}
9275
9360
}
9276
9361
9362
+ // If we're restoring this channel for the first time after an upgrade, then we require that the
9363
+ // signer be available so that we can immediately populate the current commitment point. Channel
9364
+ // restoration will fail if this is not possible.
9365
+ let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9366
+ (Some(current), Some(next)) => HolderCommitmentPoint::Available {
9367
+ transaction_number: cur_holder_commitment_transaction_number, current, next
9368
+ },
9369
+ (Some(current), _) => HolderCommitmentPoint::PendingNext {
9370
+ transaction_number: cur_holder_commitment_transaction_number, current
9371
+ },
9372
+ (_, _) => HolderCommitmentPoint::Uninitialized {
9373
+ transaction_number: cur_holder_commitment_transaction_number
9374
+ }
9375
+ };
9376
+
9277
9377
Ok(Channel {
9278
9378
context: ChannelContext {
9279
9379
user_id,
@@ -9299,6 +9399,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9299
9399
shutdown_scriptpubkey,
9300
9400
destination_script,
9301
9401
9402
+ holder_commitment_point,
9302
9403
cur_holder_commitment_transaction_number,
9303
9404
cur_counterparty_commitment_transaction_number,
9304
9405
value_to_self_msat,
0 commit comments