Skip to content

Commit 9c2a050

Browse files
committed
Return an error if we fail to advance our commitment number
1 parent 516e5e6 commit 9c2a050

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

lightning/src/ln/channel.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,9 @@ impl HolderCommitmentPoint {
10081008
}
10091009

10101010
/// If we are not pending the next commitment point, this method advances the commitment number
1011-
/// and requests the next commitment point from the signer.
1011+
/// and requests the next commitment point from the signer. Returns `Ok` if we were at
1012+
/// `Available` and were able to advance our commitment number (even if we are still pending
1013+
/// the next commitment point).
10121014
///
10131015
/// If our signer is not ready to provide the next commitment point, we will
10141016
/// only advance to `PendingNext`, and should be tried again later in `signer_unblocked`
@@ -1020,7 +1022,9 @@ impl HolderCommitmentPoint {
10201022
/// This method is used for the following transitions:
10211023
/// - `Available` -> `PendingNext`
10221024
/// - `Available` -> `PendingNext` -> `Available` (in one fell swoop)
1023-
pub fn advance<SP: Deref, L: Deref>(&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L)
1025+
pub fn advance<SP: Deref, L: Deref>(
1026+
&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L
1027+
) -> Result<(), ()>
10241028
where SP::Target: SignerProvider, L::Target: Logger
10251029
{
10261030
if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
@@ -1029,7 +1033,9 @@ impl HolderCommitmentPoint {
10291033
current: *next,
10301034
};
10311035
self.try_resolve_pending(signer, secp_ctx, logger);
1036+
return Ok(());
10321037
}
1038+
Err(())
10331039
}
10341040
}
10351041

@@ -4618,7 +4624,18 @@ impl<SP: Deref> Channel<SP> where
46184624
channel_id: Some(self.context.channel_id()),
46194625
};
46204626

4621-
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
4627+
if self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger).is_err() {
4628+
// We only fail to advance our commitment point/number if we're currently
4629+
// waiting for our signer to unblock and provide a commitment point.
4630+
// During post-funding channel operation, we only advance our point upon
4631+
// receiving a commitment_signed, and our counterparty cannot send us
4632+
// another commitment signed until we've provided a new commitment point
4633+
// in revoke_and_ack, which requires unblocking our signer and completing
4634+
// the advance to the next point. This should be unreachable since
4635+
// a new commitment_signed should fail at our signature checks above.
4636+
debug_assert!(false, "We should be ready to advance our commitment point by the time we receive commitment_signed");
4637+
return Err(ChannelError::close("Failed to advance our commitment point".to_owned()));
4638+
}
46224639
self.context.expecting_peer_commitment_signed = false;
46234640
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
46244641
// build_commitment_no_status_check() next which will reset this to RAAFirst.
@@ -7799,7 +7816,14 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
77997816
} else {
78007817
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
78017818
}
7802-
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
7819+
if self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger).is_err() {
7820+
// We only fail to advance our commitment point/number if we're currently
7821+
// waiting for our signer to unblock and provide a commitment point.
7822+
// We cannot send open_channel before this has occurred, so if we
7823+
// err here by the time we receive funding_signed, something has gone wrong.
7824+
debug_assert!(false, "We should be ready to advance our commitment point by the time we receive funding_signed");
7825+
return Err((self, ChannelError::close("Failed to advance holder commitment point".to_owned())));
7826+
}
78037827
self.context.cur_counterparty_commitment_transaction_number -= 1;
78047828

78057829
log_info!(logger, "Received funding_signed from peer for channel {}", &self.context.channel_id());
@@ -8066,7 +8090,14 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80668090
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
80678091
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
80688092
self.context.cur_counterparty_commitment_transaction_number -= 1;
8069-
self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
8093+
if self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger).is_err() {
8094+
// We only fail to advance our commitment point/number if we're currently
8095+
// waiting for our signer to unblock and provide a commitment point.
8096+
// We cannot send accept_channel before this has occurred, so if we
8097+
// err here by the time we receive funding_created, something has gone wrong.
8098+
debug_assert!(false, "We should be ready to advance our commitment point by the time we receive funding_created");
8099+
return Err((self, ChannelError::close("Failed to advance holder commitment point".to_owned())));
8100+
}
80708101

80718102
let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
80728103

0 commit comments

Comments
 (0)