Skip to content

Commit 5b3d6ea

Browse files
committed
Handle fallible release_commitment_secret
1 parent 6ed398d commit 5b3d6ea

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

lightning/src/ln/channel.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5500,32 +5500,42 @@ impl<SP: Deref> Channel<SP> where
55005500
fn get_last_revoke_and_ack<L: Deref>(&mut self, logger: &L) -> Option<msgs::RevokeAndACK> where L::Target: Logger {
55015501
debug_assert!(self.context.holder_commitment_point.transaction_number() <= INITIAL_COMMITMENT_NUMBER - 2);
55025502
self.context.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
5503-
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2);
5504-
if let HolderCommitmentPoint::Available { transaction_number: _, current, next: _ } = self.context.holder_commitment_point {
5503+
let per_commitment_secret = self.context.holder_signer.as_ref()
5504+
.release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2).ok();
5505+
if let (HolderCommitmentPoint::Available { current, .. }, Some(per_commitment_secret)) =
5506+
(self.context.holder_commitment_point, per_commitment_secret) {
55055507
self.context.signer_pending_revoke_and_ack = false;
5506-
Some(msgs::RevokeAndACK {
5508+
return Some(msgs::RevokeAndACK {
55075509
channel_id: self.context.channel_id,
55085510
per_commitment_secret,
55095511
next_per_commitment_point: current,
55105512
#[cfg(taproot)]
55115513
next_local_nonce: None,
55125514
})
5513-
} else {
5514-
#[cfg(not(async_signing))] {
5515-
panic!("Holder commitment point must be Available when generating revoke_and_ack");
5516-
}
5517-
#[cfg(async_signing)] {
5518-
// Technically if we're at HolderCommitmentPoint::PendingNext,
5519-
// we have a commitment point ready to send in an RAA, however we
5520-
// choose to wait since if we send RAA now, we could get another
5521-
// CS before we have any commitment point available. Blocking our
5522-
// RAA here is a convenient way to make sure that post-funding
5523-
// we're only ever waiting on one commitment point at a time.
5524-
log_trace!(logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment point is not available",
5525-
&self.context.channel_id(), self.context.holder_commitment_point.transaction_number());
5526-
self.context.signer_pending_revoke_and_ack = true;
5527-
None
5528-
}
5515+
}
5516+
if !self.context.holder_commitment_point.is_available() {
5517+
log_trace!(logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment point is not available",
5518+
&self.context.channel_id(), self.context.holder_commitment_point.transaction_number());
5519+
}
5520+
if per_commitment_secret.is_none() {
5521+
log_trace!(logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment secret for {} is not available",
5522+
&self.context.channel_id(), self.context.holder_commitment_point.transaction_number(),
5523+
self.context.holder_commitment_point.transaction_number() + 2);
5524+
}
5525+
#[cfg(not(async_signing))] {
5526+
panic!("Holder commitment point and per commitment secret must be available when generating revoke_and_ack");
5527+
}
5528+
#[cfg(async_signing)] {
5529+
// Technically if we're at HolderCommitmentPoint::PendingNext,
5530+
// we have a commitment point ready to send in an RAA, however we
5531+
// choose to wait since if we send RAA now, we could get another
5532+
// CS before we have any commitment point available. Blocking our
5533+
// RAA here is a convenient way to make sure that post-funding
5534+
// we're only ever waiting on one commitment point at a time.
5535+
log_trace!(logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment point is not available",
5536+
&self.context.channel_id(), self.context.holder_commitment_point.transaction_number());
5537+
self.context.signer_pending_revoke_and_ack = true;
5538+
None
55295539
}
55305540
}
55315541

lightning/src/ln/functional_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ fn test_fee_spike_violation_fails_htlc() {
14741474

14751475
let pubkeys = chan_signer.as_ref().pubkeys();
14761476
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
1477-
chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1477+
chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER).unwrap(),
14781478
chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx).unwrap(),
14791479
chan_signer.as_ref().pubkeys().funding_pubkey)
14801480
};
@@ -7860,15 +7860,15 @@ fn test_counterparty_raa_skip_no_crash() {
78607860

78617861
// Make signer believe we got a counterparty signature, so that it allows the revocation
78627862
keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7863-
per_commitment_secret = keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
7863+
per_commitment_secret = keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER).unwrap();
78647864

78657865
// Must revoke without gaps
78667866
keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7867-
keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
7867+
keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1).unwrap();
78687868

78697869
keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
78707870
next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
7871-
&SecretKey::from_slice(&keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
7871+
&SecretKey::from_slice(&keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2).unwrap()).unwrap());
78727872
}
78737873

78747874
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),

lightning/src/sign/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ pub trait ChannelSigner {
752752
///
753753
/// Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
754754
// TODO: return a Result so we can signal a validation error
755-
fn release_commitment_secret(&self, idx: u64) -> [u8; 32];
755+
fn release_commitment_secret(&self, idx: u64) -> Result<[u8; 32], ()>;
756756

757757
/// Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
758758
///
@@ -1361,8 +1361,8 @@ impl ChannelSigner for InMemorySigner {
13611361
Ok(PublicKey::from_secret_key(secp_ctx, &commitment_secret))
13621362
}
13631363

1364-
fn release_commitment_secret(&self, idx: u64) -> [u8; 32] {
1365-
chan_utils::build_commitment_secret(&self.commitment_seed, idx)
1364+
fn release_commitment_secret(&self, idx: u64) -> Result<[u8; 32], ()> {
1365+
Ok(chan_utils::build_commitment_secret(&self.commitment_seed, idx))
13661366
}
13671367

13681368
fn validate_holder_commitment(

lightning/src/util/test_channel_signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl ChannelSigner for TestChannelSigner {
172172
self.inner.get_per_commitment_point(idx, secp_ctx)
173173
}
174174

175-
fn release_commitment_secret(&self, idx: u64) -> [u8; 32] {
175+
fn release_commitment_secret(&self, idx: u64) -> Result<[u8; 32], ()> {
176176
{
177177
let mut state = self.state.lock().unwrap();
178178
assert!(idx == state.last_holder_revoked_commitment || idx == state.last_holder_revoked_commitment - 1, "can only revoke the current or next unrevoked commitment - trying {}, last revoked {}", idx, state.last_holder_revoked_commitment);

0 commit comments

Comments
 (0)