Skip to content

Commit 183c0be

Browse files
committed
Use struct for HolderCommitmentPoint
The only difference between the two variants is the next point, which can be stored using an Option for simplicity. The naming of the Available variant is also confusing as it refers to the next commitment point. But HolderCommitmentPoint is typically used to represent the next point, which is actually stored in the current field. Drop the "current" nomenclature to avoid confusion.
1 parent 3b939c0 commit 183c0be

File tree

1 file changed

+98
-126
lines changed

1 file changed

+98
-126
lines changed

lightning/src/ln/channel.rs

Lines changed: 98 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,114 +1249,91 @@ pub(crate) struct ShutdownResult {
12491249
/// This consolidates the logic to advance our commitment number and request new
12501250
/// commitment points from our signer.
12511251
#[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>,
12601256
}
12611257

12621258
impl HolderCommitmentPoint {
12631259
#[rustfmt::skip]
12641260
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
12651261
where SP::Target: SignerProvider
12661262
{
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+
})
12751268
}
12761269

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()
12801272
}
12811273

12821274
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
12871276
}
12881277

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
12941280
}
12951281

12961282
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
13011284
}
13021285

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.
13081287
pub fn try_resolve_pending<SP: Deref, L: Deref>(
13091288
&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L,
13101289
) where
13111290
SP::Target: SignerProvider,
13121291
L::Target: Logger,
13131292
{
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);
13161296
if let Ok(next) = next {
13171297
log_trace!(
13181298
logger,
13191299
"Retrieved next per-commitment point {}",
1320-
*transaction_number - 1
1300+
self.transaction_number - 1
13211301
);
1322-
*self = HolderCommitmentPoint::Available {
1323-
transaction_number: *transaction_number,
1324-
current: *current,
1325-
next,
1326-
};
1302+
self.next_point = Some(next);
13271303
} 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+
);
13291309
}
13301310
}
13311311
}
13321312

13331313
/// 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).
13411316
///
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`.
13441320
///
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.
13481323
pub fn advance<SP: Deref, L: Deref>(
13491324
&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L,
13501325
) -> Result<(), ()>
13511326
where
13521327
SP::Target: SignerProvider,
13531328
L::Target: Logger,
13541329
{
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,
13591335
};
1336+
13601337
self.try_resolve_pending(signer, secp_ctx, logger);
13611338
return Ok(());
13621339
}
@@ -2771,7 +2748,7 @@ where
27712748
let funding_script = self.funding().get_funding_redeemscript();
27722749

27732750
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(),
27752752
true, false, logger);
27762753
let initial_commitment_tx = commitment_data.tx;
27772754
let trusted_tx = initial_commitment_tx.trust();
@@ -4218,7 +4195,7 @@ where
42184195
let funding_script = funding.get_funding_redeemscript();
42194196

42204197
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(),
42224199
true, false, logger);
42234200
let commitment_txid = {
42244201
let trusted_tx = commitment_data.tx.trust();
@@ -8412,7 +8389,7 @@ where
84128389
/// blocked.
84138390
#[rustfmt::skip]
84148391
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() {
84168393
log_trace!(logger, "Attempting to update holder per-commitment point...");
84178394
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
84188395
}
@@ -8513,18 +8490,19 @@ where
85138490
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
85148491
let per_commitment_secret = self.context.holder_signer.as_ref()
85158492
.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+
}
85268504
}
8527-
if !self.holder_commitment_point.is_available() {
8505+
if !self.holder_commitment_point.can_advance() {
85288506
log_trace!(logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment point is not available",
85298507
&self.context.channel_id(), self.holder_commitment_point.transaction_number());
85308508
}
@@ -8533,7 +8511,7 @@ where
85338511
&self.context.channel_id(), self.holder_commitment_point.transaction_number(),
85348512
self.holder_commitment_point.transaction_number() + 2);
85358513
}
8536-
// Technically if we're at HolderCommitmentPoint::PendingNext,
8514+
// Technically if HolderCommitmentPoint::can_advance is false,
85378515
// we have a commitment point ready to send in an RAA, however we
85388516
// choose to wait since if we send RAA now, we could get another
85398517
// CS before we have any commitment point available. Blocking our
@@ -9873,11 +9851,11 @@ where
98739851
fn get_channel_ready<L: Deref>(
98749852
&mut self, logger: &L
98759853
) -> 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() {
98779855
self.context.signer_pending_channel_ready = false;
98789856
Some(msgs::ChannelReady {
98799857
channel_id: self.context.channel_id(),
9880-
next_per_commitment_point: current,
9858+
next_per_commitment_point: self.holder_commitment_point.point(),
98819859
short_channel_id_alias: Some(self.context.outbound_scid_alias),
98829860
})
98839861
} else {
@@ -11986,9 +11964,9 @@ where
1198611964
}
1198711965

1198811966
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() => {
1199011968
self.signer_pending_open_channel = false;
11991-
holder_commitment_point.current_point()
11969+
holder_commitment_point.point()
1199211970
},
1199311971
_ => {
1199411972
log_trace!(_logger, "Unable to generate open_channel message, waiting for commitment point");
@@ -12100,7 +12078,7 @@ where
1210012078
self.unfunded_context.holder_commitment_point = HolderCommitmentPoint::new(&self.context.holder_signer, &self.context.secp_ctx);
1210112079
}
1210212080
if let Some(ref mut point) = self.unfunded_context.holder_commitment_point {
12103-
if !point.is_available() {
12081+
if !point.can_advance() {
1210412082
point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
1210512083
}
1210612084
}
@@ -12260,9 +12238,9 @@ where
1226012238
&mut self, _logger: &L
1226112239
) -> Option<msgs::AcceptChannel> where L::Target: Logger {
1226212240
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() => {
1226412242
self.signer_pending_accept_channel = false;
12265-
holder_commitment_point.current_point()
12243+
holder_commitment_point.point()
1226612244
},
1226712245
_ => {
1226812246
log_trace!(_logger, "Unable to generate accept_channel message, waiting for commitment point");
@@ -12384,7 +12362,7 @@ where
1238412362
self.unfunded_context.holder_commitment_point = HolderCommitmentPoint::new(&self.context.holder_signer, &self.context.secp_ctx);
1238512363
}
1238612364
if let Some(ref mut point) = self.unfunded_context.holder_commitment_point {
12387-
if !point.is_available() {
12365+
if !point.can_advance() {
1238812366
point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
1238912367
}
1239012368
}
@@ -13222,9 +13200,9 @@ where
1322213200
}
1322313201
let is_manual_broadcast = Some(self.context.is_manual_broadcast);
1322413202

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();
1322813206

1322913207
write_tlv_fields!(writer, {
1323013208
(0, self.context.announcement_sigs, option),
@@ -13262,8 +13240,8 @@ where
1326213240
(39, pending_outbound_blinding_points, optional_vec),
1326313241
(41, holding_cell_blinding_points, optional_vec),
1326413242
(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),
1326713245
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
1326813246
(51, is_manual_broadcast, option), // Added in 0.0.124
1326913247
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
@@ -13320,7 +13298,7 @@ where
1332013298
};
1332113299
let destination_script = Readable::read(reader)?;
1332213300

13323-
let cur_holder_commitment_transaction_number = Readable::read(reader)?;
13301+
let holder_commitment_transaction_number = Readable::read(reader)?;
1332413302
let cur_counterparty_commitment_transaction_number = Readable::read(reader)?;
1332513303
let value_to_self_msat = Readable::read(reader)?;
1332613304

@@ -13623,8 +13601,8 @@ where
1362313601
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
1362413602
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
1362513603

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;
1362813606
let mut is_manual_broadcast = None;
1362913607

1363013608
let mut pending_funding = Some(Vec::new());
@@ -13664,8 +13642,8 @@ where
1366413642
(39, pending_outbound_blinding_points_opt, optional_vec),
1366513643
(41, holding_cell_blinding_points_opt, optional_vec),
1366613644
(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),
1366913647
(49, local_initiated_shutdown, option),
1367013648
(51, is_manual_broadcast, option),
1367113649
(53, funding_tx_broadcast_safe_event_emitted, option),
@@ -13853,37 +13831,31 @@ where
1385313831
// If we're restoring this channel for the first time after an upgrade, then we require that the
1385413832
// signer be available so that we can immediately populate the current commitment point. Channel
1385513833
// 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+
};
1388713859

1388813860
Ok(FundedChannel {
1388913861
funding: FundingScope {

0 commit comments

Comments
 (0)