@@ -1249,114 +1249,91 @@ pub(crate) struct ShutdownResult {
1249
1249
/// This consolidates the logic to advance our commitment number and request new
1250
1250
/// commitment points from our signer.
1251
1251
#[derive(Debug, Copy, Clone)]
1252
- enum HolderCommitmentPoint {
1253
- /// We've advanced our commitment number and are waiting on the next commitment point.
1254
- ///
1255
- /// We should retry advancing to `Available` via `try_resolve_pending` once our
1256
- /// signer is ready to provide the next commitment point.
1257
- PendingNext { transaction_number: u64, current: PublicKey },
1258
- /// Our current commitment point is ready and we've cached our next point.
1259
- Available { transaction_number: u64, current: PublicKey, next: PublicKey },
1252
+ struct HolderCommitmentPoint {
1253
+ transaction_number: u64,
1254
+ point: PublicKey,
1255
+ next_point: Option<PublicKey>,
1260
1256
}
1261
1257
1262
1258
impl HolderCommitmentPoint {
1263
1259
#[rustfmt::skip]
1264
1260
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
1265
1261
where SP::Target: SignerProvider
1266
1262
{
1267
- let current = signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx).ok()?;
1268
- let next = signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx).ok();
1269
- let point = if let Some(next) = next {
1270
- HolderCommitmentPoint::Available { transaction_number: INITIAL_COMMITMENT_NUMBER, current, next }
1271
- } else {
1272
- HolderCommitmentPoint::PendingNext { transaction_number: INITIAL_COMMITMENT_NUMBER, current }
1273
- };
1274
- Some(point)
1263
+ Some(HolderCommitmentPoint {
1264
+ transaction_number: INITIAL_COMMITMENT_NUMBER,
1265
+ point: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx).ok()?,
1266
+ next_point: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx).ok(),
1267
+ })
1275
1268
}
1276
1269
1277
- #[rustfmt::skip]
1278
- pub fn is_available(&self) -> bool {
1279
- if let HolderCommitmentPoint::Available { .. } = self { true } else { false }
1270
+ pub fn can_advance(&self) -> bool {
1271
+ self.next_point.is_some()
1280
1272
}
1281
1273
1282
1274
pub fn transaction_number(&self) -> u64 {
1283
- match self {
1284
- HolderCommitmentPoint::PendingNext { transaction_number, .. } => *transaction_number,
1285
- HolderCommitmentPoint::Available { transaction_number, .. } => *transaction_number,
1286
- }
1275
+ self.transaction_number
1287
1276
}
1288
1277
1289
- pub fn current_point(&self) -> PublicKey {
1290
- match self {
1291
- HolderCommitmentPoint::PendingNext { current, .. } => *current,
1292
- HolderCommitmentPoint::Available { current, .. } => *current,
1293
- }
1278
+ pub fn point(&self) -> PublicKey {
1279
+ self.point
1294
1280
}
1295
1281
1296
1282
pub fn next_point(&self) -> Option<PublicKey> {
1297
- match self {
1298
- HolderCommitmentPoint::PendingNext { .. } => None,
1299
- HolderCommitmentPoint::Available { next, .. } => Some(*next),
1300
- }
1283
+ self.next_point
1301
1284
}
1302
1285
1303
- /// If we are pending the next commitment point, this method tries asking the signer again,
1304
- /// and transitions to the next state if successful.
1305
- ///
1306
- /// This method is used for the following transitions:
1307
- /// - `PendingNext` -> `Available`
1286
+ /// If we are pending the next commitment point, this method tries asking the signer again.
1308
1287
pub fn try_resolve_pending<SP: Deref, L: Deref>(
1309
1288
&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L,
1310
1289
) where
1311
1290
SP::Target: SignerProvider,
1312
1291
L::Target: Logger,
1313
1292
{
1314
- if let HolderCommitmentPoint::PendingNext { transaction_number, current } = self {
1315
- let next = signer.as_ref().get_per_commitment_point(*transaction_number - 1, secp_ctx);
1293
+ if !self.can_advance() {
1294
+ let next =
1295
+ signer.as_ref().get_per_commitment_point(self.transaction_number - 1, secp_ctx);
1316
1296
if let Ok(next) = next {
1317
1297
log_trace!(
1318
1298
logger,
1319
1299
"Retrieved next per-commitment point {}",
1320
- * transaction_number - 1
1300
+ self. transaction_number - 1
1321
1301
);
1322
- *self = HolderCommitmentPoint::Available {
1323
- transaction_number: *transaction_number,
1324
- current: *current,
1325
- next,
1326
- };
1302
+ self.next_point = Some(next);
1327
1303
} else {
1328
- log_trace!(logger, "Next per-commitment point {} is pending", transaction_number);
1304
+ log_trace!(
1305
+ logger,
1306
+ "Next per-commitment point {} is pending",
1307
+ self.transaction_number
1308
+ );
1329
1309
}
1330
1310
}
1331
1311
}
1332
1312
1333
1313
/// If we are not pending the next commitment point, this method advances the commitment number
1334
- /// and requests the next commitment point from the signer. Returns `Ok` if we were at
1335
- /// `Available` and were able to advance our commitment number (even if we are still pending
1336
- /// the next commitment point).
1337
- ///
1338
- /// If our signer is not ready to provide the next commitment point, we will
1339
- /// only advance to `PendingNext`, and should be tried again later in `signer_unblocked`
1340
- /// via `try_resolve_pending`.
1314
+ /// and requests the next commitment point from the signer. Returns `Ok` if we were able to
1315
+ /// advance our commitment number (even if we are still pending the next commitment point).
1341
1316
///
1342
- /// If our signer is ready to provide the next commitment point, we will advance all the
1343
- /// way to `Available`.
1317
+ /// If our signer is not ready to provide the next commitment point, we will advance but won't
1318
+ /// be able to advance again immediately. Instead, this hould be tried again later in
1319
+ /// `signer_unblocked` via `try_resolve_pending`.
1344
1320
///
1345
- /// This method is used for the following transitions:
1346
- /// - `Available` -> `PendingNext`
1347
- /// - `Available` -> `PendingNext` -> `Available` (in one fell swoop)
1321
+ /// If our signer is ready to provide the next commitment point, the next call to `advance` will
1322
+ /// succeed.
1348
1323
pub fn advance<SP: Deref, L: Deref>(
1349
1324
&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L,
1350
1325
) -> Result<(), ()>
1351
1326
where
1352
1327
SP::Target: SignerProvider,
1353
1328
L::Target: Logger,
1354
1329
{
1355
- if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
1356
- *self = HolderCommitmentPoint::PendingNext {
1357
- transaction_number: *transaction_number - 1,
1358
- current: *next,
1330
+ if let Some(next_point) = self.next_point {
1331
+ *self = Self {
1332
+ transaction_number: self.transaction_number - 1,
1333
+ point: next_point,
1334
+ next_point: None,
1359
1335
};
1336
+
1360
1337
self.try_resolve_pending(signer, secp_ctx, logger);
1361
1338
return Ok(());
1362
1339
}
@@ -2771,7 +2748,7 @@ where
2771
2748
let funding_script = self.funding().get_funding_redeemscript();
2772
2749
2773
2750
let commitment_data = self.context().build_commitment_transaction(self.funding(),
2774
- holder_commitment_point.transaction_number(), &holder_commitment_point.current_point (),
2751
+ holder_commitment_point.transaction_number(), &holder_commitment_point.point (),
2775
2752
true, false, logger);
2776
2753
let initial_commitment_tx = commitment_data.tx;
2777
2754
let trusted_tx = initial_commitment_tx.trust();
@@ -4218,7 +4195,7 @@ where
4218
4195
let funding_script = funding.get_funding_redeemscript();
4219
4196
4220
4197
let commitment_data = self.build_commitment_transaction(funding,
4221
- holder_commitment_point.transaction_number(), &holder_commitment_point.current_point (),
4198
+ holder_commitment_point.transaction_number(), &holder_commitment_point.point (),
4222
4199
true, false, logger);
4223
4200
let commitment_txid = {
4224
4201
let trusted_tx = commitment_data.tx.trust();
@@ -8412,7 +8389,7 @@ where
8412
8389
/// blocked.
8413
8390
#[rustfmt::skip]
8414
8391
pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
8415
- if !self.holder_commitment_point.is_available () {
8392
+ if !self.holder_commitment_point.can_advance () {
8416
8393
log_trace!(logger, "Attempting to update holder per-commitment point...");
8417
8394
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
8418
8395
}
@@ -8513,18 +8490,19 @@ where
8513
8490
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
8514
8491
let per_commitment_secret = self.context.holder_signer.as_ref()
8515
8492
.release_commitment_secret(self.holder_commitment_point.transaction_number() + 2).ok();
8516
- if let (HolderCommitmentPoint::Available { current, .. }, Some(per_commitment_secret)) =
8517
- (self.holder_commitment_point, per_commitment_secret) {
8518
- self.context.signer_pending_revoke_and_ack = false;
8519
- return Some(msgs::RevokeAndACK {
8520
- channel_id: self.context.channel_id,
8521
- per_commitment_secret,
8522
- next_per_commitment_point: current,
8523
- #[cfg(taproot)]
8524
- next_local_nonce: None,
8525
- })
8493
+ if let Some(per_commitment_secret) = per_commitment_secret {
8494
+ if self.holder_commitment_point.can_advance() {
8495
+ self.context.signer_pending_revoke_and_ack = false;
8496
+ return Some(msgs::RevokeAndACK {
8497
+ channel_id: self.context.channel_id,
8498
+ per_commitment_secret,
8499
+ next_per_commitment_point: self.holder_commitment_point.point(),
8500
+ #[cfg(taproot)]
8501
+ next_local_nonce: None,
8502
+ })
8503
+ }
8526
8504
}
8527
- if !self.holder_commitment_point.is_available () {
8505
+ if !self.holder_commitment_point.can_advance () {
8528
8506
log_trace!(logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment point is not available",
8529
8507
&self.context.channel_id(), self.holder_commitment_point.transaction_number());
8530
8508
}
@@ -8533,7 +8511,7 @@ where
8533
8511
&self.context.channel_id(), self.holder_commitment_point.transaction_number(),
8534
8512
self.holder_commitment_point.transaction_number() + 2);
8535
8513
}
8536
- // Technically if we're at HolderCommitmentPoint::PendingNext ,
8514
+ // Technically if HolderCommitmentPoint::can_advance is false ,
8537
8515
// we have a commitment point ready to send in an RAA, however we
8538
8516
// choose to wait since if we send RAA now, we could get another
8539
8517
// CS before we have any commitment point available. Blocking our
@@ -9873,11 +9851,11 @@ where
9873
9851
fn get_channel_ready<L: Deref>(
9874
9852
&mut self, logger: &L
9875
9853
) -> Option<msgs::ChannelReady> where L::Target: Logger {
9876
- if let HolderCommitmentPoint::Available { current, .. } = self.holder_commitment_point {
9854
+ if self.holder_commitment_point.can_advance() {
9877
9855
self.context.signer_pending_channel_ready = false;
9878
9856
Some(msgs::ChannelReady {
9879
9857
channel_id: self.context.channel_id(),
9880
- next_per_commitment_point: current ,
9858
+ next_per_commitment_point: self.holder_commitment_point.point() ,
9881
9859
short_channel_id_alias: Some(self.context.outbound_scid_alias),
9882
9860
})
9883
9861
} else {
@@ -11986,9 +11964,9 @@ where
11986
11964
}
11987
11965
11988
11966
let first_per_commitment_point = match self.unfunded_context.holder_commitment_point {
11989
- Some(holder_commitment_point) if holder_commitment_point.is_available () => {
11967
+ Some(holder_commitment_point) if holder_commitment_point.can_advance () => {
11990
11968
self.signer_pending_open_channel = false;
11991
- holder_commitment_point.current_point ()
11969
+ holder_commitment_point.point ()
11992
11970
},
11993
11971
_ => {
11994
11972
log_trace!(_logger, "Unable to generate open_channel message, waiting for commitment point");
@@ -12100,7 +12078,7 @@ where
12100
12078
self.unfunded_context.holder_commitment_point = HolderCommitmentPoint::new(&self.context.holder_signer, &self.context.secp_ctx);
12101
12079
}
12102
12080
if let Some(ref mut point) = self.unfunded_context.holder_commitment_point {
12103
- if !point.is_available () {
12081
+ if !point.can_advance () {
12104
12082
point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
12105
12083
}
12106
12084
}
@@ -12260,9 +12238,9 @@ where
12260
12238
&mut self, _logger: &L
12261
12239
) -> Option<msgs::AcceptChannel> where L::Target: Logger {
12262
12240
let first_per_commitment_point = match self.unfunded_context.holder_commitment_point {
12263
- Some(holder_commitment_point) if holder_commitment_point.is_available () => {
12241
+ Some(holder_commitment_point) if holder_commitment_point.can_advance () => {
12264
12242
self.signer_pending_accept_channel = false;
12265
- holder_commitment_point.current_point ()
12243
+ holder_commitment_point.point ()
12266
12244
},
12267
12245
_ => {
12268
12246
log_trace!(_logger, "Unable to generate accept_channel message, waiting for commitment point");
@@ -12384,7 +12362,7 @@ where
12384
12362
self.unfunded_context.holder_commitment_point = HolderCommitmentPoint::new(&self.context.holder_signer, &self.context.secp_ctx);
12385
12363
}
12386
12364
if let Some(ref mut point) = self.unfunded_context.holder_commitment_point {
12387
- if !point.is_available () {
12365
+ if !point.can_advance () {
12388
12366
point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
12389
12367
}
12390
12368
}
@@ -13222,9 +13200,9 @@ where
13222
13200
}
13223
13201
let is_manual_broadcast = Some(self.context.is_manual_broadcast);
13224
13202
13225
- // `current_point ` will become optional when async signing is implemented.
13226
- let cur_holder_commitment_point = Some(self.holder_commitment_point.current_point ());
13227
- let next_holder_commitment_point = self.holder_commitment_point.next_point();
13203
+ // `HolderCommitmentPoint::point ` will become optional when async signing is implemented.
13204
+ let holder_commitment_point = Some(self.holder_commitment_point.point ());
13205
+ let holder_commitment_point_next_advance = self.holder_commitment_point.next_point();
13228
13206
13229
13207
write_tlv_fields!(writer, {
13230
13208
(0, self.context.announcement_sigs, option),
@@ -13262,8 +13240,8 @@ where
13262
13240
(39, pending_outbound_blinding_points, optional_vec),
13263
13241
(41, holding_cell_blinding_points, optional_vec),
13264
13242
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
13265
- (45, cur_holder_commitment_point , option),
13266
- (47, next_holder_commitment_point , option),
13243
+ (45, holder_commitment_point , option),
13244
+ (47, holder_commitment_point_next_advance , option),
13267
13245
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
13268
13246
(51, is_manual_broadcast, option), // Added in 0.0.124
13269
13247
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
@@ -13320,7 +13298,7 @@ where
13320
13298
};
13321
13299
let destination_script = Readable::read(reader)?;
13322
13300
13323
- let cur_holder_commitment_transaction_number = Readable::read(reader)?;
13301
+ let holder_commitment_transaction_number = Readable::read(reader)?;
13324
13302
let cur_counterparty_commitment_transaction_number = Readable::read(reader)?;
13325
13303
let value_to_self_msat = Readable::read(reader)?;
13326
13304
@@ -13623,8 +13601,8 @@ where
13623
13601
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
13624
13602
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
13625
13603
13626
- let mut cur_holder_commitment_point_opt : Option<PublicKey> = None;
13627
- let mut next_holder_commitment_point_opt : Option<PublicKey> = None;
13604
+ let mut holder_commitment_point_opt : Option<PublicKey> = None;
13605
+ let mut holder_commitment_point_next_advance_opt : Option<PublicKey> = None;
13628
13606
let mut is_manual_broadcast = None;
13629
13607
13630
13608
let mut pending_funding = Some(Vec::new());
@@ -13664,8 +13642,8 @@ where
13664
13642
(39, pending_outbound_blinding_points_opt, optional_vec),
13665
13643
(41, holding_cell_blinding_points_opt, optional_vec),
13666
13644
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
13667
- (45, cur_holder_commitment_point_opt , option),
13668
- (47, next_holder_commitment_point_opt , option),
13645
+ (45, holder_commitment_point_opt , option),
13646
+ (47, holder_commitment_point_next_advance_opt , option),
13669
13647
(49, local_initiated_shutdown, option),
13670
13648
(51, is_manual_broadcast, option),
13671
13649
(53, funding_tx_broadcast_safe_event_emitted, option),
@@ -13853,37 +13831,31 @@ where
13853
13831
// If we're restoring this channel for the first time after an upgrade, then we require that the
13854
13832
// signer be available so that we can immediately populate the current commitment point. Channel
13855
13833
// restoration will fail if this is not possible.
13856
- let holder_commitment_point = match (
13857
- cur_holder_commitment_point_opt,
13858
- next_holder_commitment_point_opt,
13859
- ) {
13860
- (Some(current), Some(next)) => HolderCommitmentPoint::Available {
13861
- transaction_number: cur_holder_commitment_transaction_number,
13862
- current,
13863
- next,
13864
- },
13865
- (Some(current), _) => HolderCommitmentPoint::PendingNext {
13866
- transaction_number: cur_holder_commitment_transaction_number,
13867
- current,
13868
- },
13869
- (_, _) => {
13870
- let current = holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number, &secp_ctx)
13871
- .expect("Must be able to derive the current commitment point upon channel restoration");
13872
- let next = holder_signer
13873
- .get_per_commitment_point(
13874
- cur_holder_commitment_transaction_number - 1,
13875
- &secp_ctx,
13876
- )
13877
- .expect(
13878
- "Must be able to derive the next commitment point upon channel restoration",
13879
- );
13880
- HolderCommitmentPoint::Available {
13881
- transaction_number: cur_holder_commitment_transaction_number,
13882
- current,
13883
- next,
13884
- }
13885
- },
13886
- };
13834
+ let holder_commitment_point =
13835
+ match (holder_commitment_point_opt, holder_commitment_point_next_advance_opt) {
13836
+ (Some(point), next_point) => HolderCommitmentPoint {
13837
+ transaction_number: holder_commitment_transaction_number,
13838
+ point,
13839
+ next_point,
13840
+ },
13841
+ (_, _) => {
13842
+ let point = holder_signer.get_per_commitment_point(holder_commitment_transaction_number, &secp_ctx)
13843
+ .expect("Must be able to derive the current commitment point upon channel restoration");
13844
+ let next_point = holder_signer
13845
+ .get_per_commitment_point(
13846
+ holder_commitment_transaction_number - 1,
13847
+ &secp_ctx,
13848
+ )
13849
+ .expect(
13850
+ "Must be able to derive the next commitment point upon channel restoration",
13851
+ );
13852
+ HolderCommitmentPoint {
13853
+ transaction_number: holder_commitment_transaction_number,
13854
+ point,
13855
+ next_point: Some(next_point),
13856
+ }
13857
+ },
13858
+ };
13887
13859
13888
13860
Ok(FundedChannel {
13889
13861
funding: FundingScope {
0 commit comments