Skip to content

Commit c08aa34

Browse files
committed
Add HolderCommitmentPoint struct to ChannelContext
1 parent df01208 commit c08aa34

File tree

1 file changed

+110
-4
lines changed

1 file changed

+110
-4
lines changed

lightning/src/ln/channel.rs

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,75 @@ pub(crate) struct ShutdownResult {
11191119
pub(crate) channel_funding_txo: Option<OutPoint>,
11201120
}
11211121

1122+
/// Tracks the transaction number, along with current and next commitment points.
1123+
/// This consolidates the logic to advance our commitment number and request new
1124+
/// commitment points from our signer.
1125+
#[derive(Debug, Copy, Clone)]
1126+
enum HolderCommitmentPoint {
1127+
// TODO: add a variant for before our first commitment point is retrieved
1128+
/// We've advanced our commitment number and are waiting on the next commitment point.
1129+
/// Until the `get_per_commitment_point` signer method becomes async, this variant
1130+
/// will not be used.
1131+
PendingNext { transaction_number: u64, current: PublicKey },
1132+
/// Our current commitment point is ready, we've cached our next point,
1133+
/// and we are not pending a new one.
1134+
Available { transaction_number: u64, current: PublicKey, next: PublicKey },
1135+
}
1136+
1137+
impl HolderCommitmentPoint {
1138+
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Self
1139+
where SP::Target: SignerProvider
1140+
{
1141+
HolderCommitmentPoint::Available {
1142+
transaction_number: INITIAL_COMMITMENT_NUMBER,
1143+
current: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx),
1144+
next: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx),
1145+
}
1146+
}
1147+
1148+
pub fn is_available(&self) -> bool {
1149+
if let HolderCommitmentPoint::Available { .. } = self { true } else { false }
1150+
}
1151+
1152+
pub fn transaction_number(&self) -> u64 {
1153+
match self {
1154+
HolderCommitmentPoint::PendingNext { transaction_number, .. } => *transaction_number,
1155+
HolderCommitmentPoint::Available { transaction_number, .. } => *transaction_number,
1156+
}
1157+
}
1158+
1159+
pub fn current_point(&self) -> PublicKey {
1160+
match self {
1161+
HolderCommitmentPoint::PendingNext { current, .. } => *current,
1162+
HolderCommitmentPoint::Available { current, .. } => *current,
1163+
}
1164+
}
1165+
1166+
pub fn next_point(&self) -> Option<PublicKey> {
1167+
match self {
1168+
HolderCommitmentPoint::PendingNext { .. } => None,
1169+
HolderCommitmentPoint::Available { next, .. } => Some(*next),
1170+
}
1171+
}
1172+
1173+
pub fn advance<SP: Deref, L: Deref>(&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L)
1174+
where SP::Target: SignerProvider, L::Target: Logger
1175+
{
1176+
if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
1177+
*self = HolderCommitmentPoint::PendingNext {
1178+
transaction_number: *transaction_number - 1,
1179+
current: *next,
1180+
};
1181+
}
1182+
1183+
if let HolderCommitmentPoint::PendingNext { transaction_number, current } = self {
1184+
let next = signer.as_ref().get_per_commitment_point(*transaction_number - 1, secp_ctx);
1185+
log_trace!(logger, "Retrieved next per-commitment point {}", *transaction_number - 1);
1186+
*self = HolderCommitmentPoint::Available { transaction_number: *transaction_number, current: *current, next };
1187+
}
1188+
}
1189+
}
1190+
11221191
/// If the majority of the channels funds are to the fundee and the initiator holds only just
11231192
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
11241193
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -1297,6 +1366,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
12971366
// generation start at 0 and count up...this simplifies some parts of implementation at the
12981367
// cost of others, but should really just be changed.
12991368

1369+
holder_commitment_point: HolderCommitmentPoint,
13001370
cur_holder_commitment_transaction_number: u64,
13011371
cur_counterparty_commitment_transaction_number: u64,
13021372
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
@@ -1739,6 +1809,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17391809

17401810
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
17411811

1812+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1813+
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
1814+
17421815
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
17431816

17441817
let channel_context = ChannelContext {
@@ -1764,10 +1837,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17641837

17651838
latest_monitor_update_id: 0,
17661839

1767-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
1840+
holder_signer,
17681841
shutdown_scriptpubkey,
17691842
destination_script,
17701843

1844+
holder_commitment_point,
17711845
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
17721846
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
17731847
value_to_self_msat,
@@ -1963,6 +2037,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19632037

19642038
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
19652039

2040+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2041+
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
2042+
19662043
Ok(Self {
19672044
user_id,
19682045

@@ -1986,10 +2063,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19862063

19872064
latest_monitor_update_id: 0,
19882065

1989-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
2066+
holder_signer,
19902067
shutdown_scriptpubkey,
19912068
destination_script,
19922069

2070+
holder_commitment_point,
19932071
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19942072
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19952073
value_to_self_msat,
@@ -8779,6 +8857,10 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87798857
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
87808858
}
87818859

8860+
// `current_point` will become optional when async signing is implemented.
8861+
let cur_holder_commitment_point = Some(self.context.holder_commitment_point.current_point());
8862+
let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8863+
87828864
write_tlv_fields!(writer, {
87838865
(0, self.context.announcement_sigs, option),
87848866
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8815,7 +8897,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88158897
(39, pending_outbound_blinding_points, optional_vec),
88168898
(41, holding_cell_blinding_points, optional_vec),
88178899
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8818-
// 45 and 47 are reserved for async signing
8900+
(45, cur_holder_commitment_point, option),
8901+
(47, next_holder_commitment_point, option),
88198902
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
88208903
});
88218904

@@ -9126,6 +9209,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91269209
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
91279210
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
91289211

9212+
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9213+
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9214+
91299215
read_tlv_fields!(reader, {
91309216
(0, announcement_sigs, option),
91319217
(1, minimum_depth, option),
@@ -9156,7 +9242,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91569242
(39, pending_outbound_blinding_points_opt, optional_vec),
91579243
(41, holding_cell_blinding_points_opt, optional_vec),
91589244
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9159-
// 45 and 47 are reserved for async signing
9245+
(45, cur_holder_commitment_point_opt, option),
9246+
(47, next_holder_commitment_point_opt, option),
91609247
(49, local_initiated_shutdown, option),
91619248
});
91629249

@@ -9268,6 +9355,24 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92689355
}
92699356
}
92709357

9358+
// If we're restoring this channel for the first time after an upgrade, then we require that the
9359+
// signer be available so that we can immediately populate the current commitment point. Channel
9360+
// restoration will fail if this is not possible.
9361+
let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9362+
(Some(current), Some(next)) => HolderCommitmentPoint::Available {
9363+
transaction_number: cur_holder_commitment_transaction_number, current, next
9364+
},
9365+
(Some(current), _) => HolderCommitmentPoint::Available {
9366+
transaction_number: cur_holder_commitment_transaction_number, current,
9367+
next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9368+
},
9369+
(_, _) => HolderCommitmentPoint::Available {
9370+
transaction_number: cur_holder_commitment_transaction_number,
9371+
current: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number, &secp_ctx),
9372+
next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9373+
},
9374+
};
9375+
92719376
Ok(Channel {
92729377
context: ChannelContext {
92739378
user_id,
@@ -9293,6 +9398,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92939398
shutdown_scriptpubkey,
92949399
destination_script,
92959400

9401+
holder_commitment_point,
92969402
cur_holder_commitment_transaction_number,
92979403
cur_counterparty_commitment_transaction_number,
92989404
value_to_self_msat,

0 commit comments

Comments
 (0)