@@ -1310,32 +1310,31 @@ impl HolderCommitmentPoint {
1310
1310
}
1311
1311
}
1312
1312
1313
- /// If we are not pending the next commitment point, this method advances the commitment number
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).
1313
+ /// Returns the next [`HolderCommitmentPoint`] if it is available (i.e., was previously obtained
1314
+ /// from `signer` and cached), leaving the callee unchanged.
1316
1315
///
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`.
1316
+ /// Otherwise, returns an `Err` indicating that the signer wasn't previously ready and that the
1317
+ /// caller must invoke `try_resolve_pending` once it is.
1320
1318
///
1321
- /// If our signer is ready to provide the next commitment point, the next call to `advance` will
1322
- /// succeed.
1319
+ /// Attempts to resolve the next point on the *returned* [`HolderCommitmentPoint`], if `signer`
1320
+ /// is ready, allowing *it* to be advanced later. Otherwise, `try_resolve_pending` must be
1321
+ /// called on it, typically via [`Channel::signer_maybe_unblocked`].
1323
1322
pub fn advance<SP: Deref, L: Deref>(
1324
- &mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L,
1325
- ) -> Result<() , ()>
1323
+ &self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L,
1324
+ ) -> Result<HolderCommitmentPoint , ()>
1326
1325
where
1327
1326
SP::Target: SignerProvider,
1328
1327
L::Target: Logger,
1329
1328
{
1330
1329
if let Some(next_point) = self.next_point {
1331
- *self = Self {
1330
+ let mut advanced_point = Self {
1332
1331
transaction_number: self.transaction_number - 1,
1333
1332
point: next_point,
1334
1333
next_point: None,
1335
1334
};
1336
1335
1337
- self .try_resolve_pending(signer, secp_ctx, logger);
1338
- return Ok(() );
1336
+ advanced_point .try_resolve_pending(signer, secp_ctx, logger);
1337
+ return Ok(advanced_point );
1339
1338
}
1340
1339
Err(())
1341
1340
}
@@ -1851,6 +1850,7 @@ where
1851
1850
pending_funding: vec![],
1852
1851
context: chan.context,
1853
1852
interactive_tx_signing_session: chan.interactive_tx_signing_session,
1853
+ previous_holder_commitment_point: None,
1854
1854
next_holder_commitment_point: initial_holder_commitment_point,
1855
1855
#[cfg(splicing)]
1856
1856
pending_splice: None,
@@ -2766,9 +2766,9 @@ where
2766
2766
2767
2767
#[rustfmt::skip]
2768
2768
fn initial_commitment_signed<L: Deref>(
2769
- &mut self, channel_id: ChannelId, counterparty_signature: Signature, holder_commitment_point: &mut HolderCommitmentPoint,
2769
+ &mut self, channel_id: ChannelId, counterparty_signature: Signature, holder_commitment_point: &HolderCommitmentPoint,
2770
2770
best_block: BestBlock, signer_provider: &SP, logger: &L,
2771
- ) -> Result<(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, CommitmentTransaction), ChannelError>
2771
+ ) -> Result<(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, CommitmentTransaction, HolderCommitmentPoint ), ChannelError>
2772
2772
where
2773
2773
L::Target: Logger
2774
2774
{
@@ -2824,14 +2824,16 @@ where
2824
2824
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2825
2825
}
2826
2826
}
2827
- if holder_commitment_point.advance(&context.holder_signer, &context.secp_ctx, logger).is_err() {
2828
- // We only fail to advance our commitment point/number if we're currently
2829
- // waiting for our signer to unblock and provide a commitment point.
2830
- // We cannot send accept_channel/open_channel before this has occurred, so if we
2831
- // err here by the time we receive funding_created/funding_signed, something has gone wrong.
2832
- debug_assert!(false, "We should be ready to advance our commitment point by the time we receive {}", self.received_msg());
2833
- return Err(ChannelError::close("Failed to advance holder commitment point".to_owned()));
2834
- }
2827
+ let advanced_holder_commitment_point = holder_commitment_point
2828
+ .advance(&context.holder_signer, &context.secp_ctx, logger)
2829
+ .map_err(|()| {
2830
+ // We only fail to advance our commitment point/number if we're currently
2831
+ // waiting for our signer to unblock and provide a commitment point.
2832
+ // We cannot send accept_channel/open_channel before this has occurred, so if we
2833
+ // err here by the time we receive funding_created/funding_signed, something has gone wrong.
2834
+ debug_assert!(false, "We should be ready to advance our commitment point by the time we receive {}", self.received_msg());
2835
+ ChannelError::close("Failed to advance holder commitment point".to_owned())
2836
+ })?;
2835
2837
2836
2838
let context = self.context();
2837
2839
let funding = self.funding();
@@ -2852,7 +2854,7 @@ where
2852
2854
2853
2855
self.context_mut().cur_counterparty_commitment_transaction_number -= 1;
2854
2856
2855
- Ok((channel_monitor, counterparty_initial_commitment_tx))
2857
+ Ok((channel_monitor, counterparty_initial_commitment_tx, advanced_holder_commitment_point ))
2856
2858
}
2857
2859
2858
2860
fn is_v2_established(&self) -> bool;
@@ -6066,6 +6068,9 @@ where
6066
6068
/// This field is cleared once our counterparty sends a `channel_ready`.
6067
6069
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
6068
6070
6071
+ /// The commitment point used for the previous commitment transaction.
6072
+ previous_holder_commitment_point: Option<HolderCommitmentPoint>,
6073
+
6069
6074
/// The commitment point used for the next holder commitment transaction.
6070
6075
next_holder_commitment_point: HolderCommitmentPoint,
6071
6076
@@ -6947,12 +6952,13 @@ where
6947
6952
return Err(ChannelError::Close((msg.to_owned(), reason)));
6948
6953
}
6949
6954
6950
- let next_holder_commitment_point = &mut self.next_holder_commitment_point.clone();
6951
- self.context.assert_no_commitment_advancement(next_holder_commitment_point .transaction_number(), "initial commitment_signed");
6955
+ let holder_commitment_point = self.next_holder_commitment_point.clone();
6956
+ self.context.assert_no_commitment_advancement(holder_commitment_point .transaction_number(), "initial commitment_signed");
6952
6957
6953
- let (channel_monitor, _) = self.initial_commitment_signed(
6954
- self.context.channel_id(), msg.signature, next_holder_commitment_point, best_block, signer_provider, logger)?;
6955
- self.next_holder_commitment_point = *next_holder_commitment_point;
6958
+ let (channel_monitor, _, next_holder_commitment_point) = self.initial_commitment_signed(
6959
+ self.context.channel_id(), msg.signature, &holder_commitment_point, best_block, signer_provider, logger)?;
6960
+ self.previous_holder_commitment_point = Some(holder_commitment_point);
6961
+ self.next_holder_commitment_point = next_holder_commitment_point;
6956
6962
6957
6963
log_info!(logger, "Received initial commitment_signed from peer for channel {}", &self.context.channel_id());
6958
6964
@@ -7083,14 +7089,10 @@ where
7083
7089
));
7084
7090
}
7085
7091
7092
+ let holder_commitment_point = &self.next_holder_commitment_point;
7086
7093
let update = self
7087
7094
.context
7088
- .validate_commitment_signed(
7089
- &self.funding,
7090
- &self.next_holder_commitment_point,
7091
- msg,
7092
- logger,
7093
- )
7095
+ .validate_commitment_signed(&self.funding, holder_commitment_point, msg, logger)
7094
7096
.map(|(commitment_tx, htlcs_included)| {
7095
7097
let (nondust_htlc_sources, dust_htlcs) =
7096
7098
Self::get_commitment_htlc_data(&htlcs_included);
@@ -7212,23 +7214,24 @@ where
7212
7214
where
7213
7215
L::Target: Logger,
7214
7216
{
7215
- if self
7217
+ let next_holder_commitment_point = self
7216
7218
.next_holder_commitment_point
7217
7219
.advance(&self.context.holder_signer, &self.context.secp_ctx, logger)
7218
- .is_err()
7219
- {
7220
- // We only fail to advance our commitment point/number if we're currently
7221
- // waiting for our signer to unblock and provide a commitment point.
7222
- // During post-funding channel operation, we only advance our point upon
7223
- // receiving a commitment_signed, and our counterparty cannot send us
7224
- // another commitment signed until we've provided a new commitment point
7225
- // in revoke_and_ack, which requires unblocking our signer and completing
7226
- // the advance to the next point. This should be unreachable since
7227
- // a new commitment_signed should fail at our signature checks in
7228
- // validate_commitment_signed.
7229
- debug_assert!(false, "We should be ready to advance our commitment point by the time we receive commitment_signed");
7230
- return Err(ChannelError::close("Failed to advance our commitment point".to_owned()));
7231
- }
7220
+ .map_err(|()| {
7221
+ // We only fail to advance our commitment point/number if we're currently
7222
+ // waiting for our signer to unblock and provide a commitment point.
7223
+ // During post-funding channel operation, we only advance our point upon
7224
+ // receiving a commitment_signed, and our counterparty cannot send us
7225
+ // another commitment signed until we've provided a new commitment point
7226
+ // in revoke_and_ack, which requires unblocking our signer and completing
7227
+ // the advance to the next point. This should be unreachable since
7228
+ // a new commitment_signed should fail at our signature checks in
7229
+ // validate_commitment_signed.
7230
+ debug_assert!(false, "We should be ready to advance our commitment point by the time we receive commitment_signed");
7231
+ ChannelError::close("Failed to advance our commitment point".to_owned())
7232
+ })?;
7233
+ self.previous_holder_commitment_point = Some(self.next_holder_commitment_point);
7234
+ self.next_holder_commitment_point = next_holder_commitment_point;
7232
7235
7233
7236
// Update state now that we've passed all the can-fail calls...
7234
7237
let mut need_commitment = false;
@@ -12042,15 +12045,15 @@ where
12042
12045
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated(_)) {
12043
12046
return Err((self, ChannelError::close("Received funding_signed in strange state!".to_owned())));
12044
12047
}
12045
- let mut initial_holder_commitment_point = match self.unfunded_context.initial_holder_commitment_point {
12048
+ let initial_holder_commitment_point = match self.unfunded_context.initial_holder_commitment_point {
12046
12049
Some(point) => point,
12047
12050
None => return Err((self, ChannelError::close("Received funding_signed before our first commitment point was available".to_owned()))),
12048
12051
};
12049
12052
self.context.assert_no_commitment_advancement(initial_holder_commitment_point.transaction_number(), "funding_signed");
12050
12053
12051
- let (channel_monitor, _) = match self.initial_commitment_signed(
12054
+ let (channel_monitor, _, next_holder_commitment_point ) = match self.initial_commitment_signed(
12052
12055
self.context.channel_id(), msg.signature,
12053
- &mut initial_holder_commitment_point, best_block, signer_provider, logger
12056
+ &initial_holder_commitment_point, best_block, signer_provider, logger
12054
12057
) {
12055
12058
Ok(channel_monitor) => channel_monitor,
12056
12059
Err(err) => return Err((self, err)),
@@ -12063,7 +12066,8 @@ where
12063
12066
pending_funding: vec![],
12064
12067
context: self.context,
12065
12068
interactive_tx_signing_session: None,
12066
- next_holder_commitment_point: initial_holder_commitment_point,
12069
+ previous_holder_commitment_point: Some(initial_holder_commitment_point),
12070
+ next_holder_commitment_point,
12067
12071
#[cfg(splicing)]
12068
12072
pending_splice: None,
12069
12073
};
@@ -12318,7 +12322,7 @@ where
12318
12322
// channel.
12319
12323
return Err((self, ChannelError::close("Received funding_created after we got the channel!".to_owned())));
12320
12324
}
12321
- let mut initial_holder_commitment_point = match self.unfunded_context.initial_holder_commitment_point {
12325
+ let initial_holder_commitment_point = match self.unfunded_context.initial_holder_commitment_point {
12322
12326
Some(point) => point,
12323
12327
None => return Err((self, ChannelError::close("Received funding_created before our first commitment point was available".to_owned()))),
12324
12328
};
@@ -12327,9 +12331,9 @@ where
12327
12331
let funding_txo = OutPoint { txid: msg.funding_txid, index: msg.funding_output_index };
12328
12332
self.funding.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
12329
12333
12330
- let (channel_monitor, counterparty_initial_commitment_tx) = match self.initial_commitment_signed(
12334
+ let (channel_monitor, counterparty_initial_commitment_tx, next_holder_commitment_point ) = match self.initial_commitment_signed(
12331
12335
ChannelId::v1_from_funding_outpoint(funding_txo), msg.signature,
12332
- &mut initial_holder_commitment_point, best_block, signer_provider, logger
12336
+ &initial_holder_commitment_point, best_block, signer_provider, logger
12333
12337
) {
12334
12338
Ok(channel_monitor) => channel_monitor,
12335
12339
Err(err) => return Err((self, err)),
@@ -12349,7 +12353,8 @@ where
12349
12353
pending_funding: vec![],
12350
12354
context: self.context,
12351
12355
interactive_tx_signing_session: None,
12352
- next_holder_commitment_point: initial_holder_commitment_point,
12356
+ previous_holder_commitment_point: Some(initial_holder_commitment_point),
12357
+ next_holder_commitment_point,
12353
12358
#[cfg(splicing)]
12354
12359
pending_splice: None,
12355
12360
};
@@ -13868,6 +13873,21 @@ where
13868
13873
},
13869
13874
};
13870
13875
13876
+ let previous_holder_commitment_point = {
13877
+ let previous_holder_commitment_transaction_number =
13878
+ next_holder_commitment_point.transaction_number() + 1;
13879
+ let previous_point = holder_signer
13880
+ .get_per_commitment_point(previous_holder_commitment_transaction_number, &secp_ctx)
13881
+ .expect(
13882
+ "Must be able to derive the previous commitment point upon channel restoration",
13883
+ );
13884
+ Some(HolderCommitmentPoint {
13885
+ transaction_number: previous_holder_commitment_transaction_number,
13886
+ point: previous_point,
13887
+ next_point: Some(next_holder_commitment_point.point()),
13888
+ })
13889
+ };
13890
+
13871
13891
Ok(FundedChannel {
13872
13892
funding: FundingScope {
13873
13893
value_to_self_msat,
@@ -14005,6 +14025,7 @@ where
14005
14025
is_holder_quiescence_initiator: None,
14006
14026
},
14007
14027
interactive_tx_signing_session,
14028
+ previous_holder_commitment_point,
14008
14029
next_holder_commitment_point,
14009
14030
#[cfg(splicing)]
14010
14031
pending_splice: None,
0 commit comments