Skip to content

Commit 52a5285

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

File tree

1 file changed

+101
-4
lines changed

1 file changed

+101
-4
lines changed

lightning/src/ln/channel.rs

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

1122+
#[derive(Debug, Copy, Clone)]
1123+
enum HolderCommitmentPoint {
1124+
PendingNext { transaction_number: u64, current: PublicKey },
1125+
Available { transaction_number: u64, current: PublicKey, next: PublicKey },
1126+
}
1127+
1128+
impl HolderCommitmentPoint {
1129+
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Self
1130+
where SP::Target: SignerProvider
1131+
{
1132+
HolderCommitmentPoint::Available {
1133+
transaction_number: INITIAL_COMMITMENT_NUMBER,
1134+
current: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER, secp_ctx),
1135+
next: signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, secp_ctx),
1136+
}
1137+
}
1138+
1139+
pub fn is_available(&self) -> bool {
1140+
if let HolderCommitmentPoint::Available { .. } = self { true } else { false }
1141+
}
1142+
1143+
pub fn transaction_number(&self) -> u64 {
1144+
match self {
1145+
HolderCommitmentPoint::PendingNext { transaction_number, .. } => *transaction_number,
1146+
HolderCommitmentPoint::Available { transaction_number, .. } => *transaction_number,
1147+
}
1148+
}
1149+
1150+
pub fn current_point(&self) -> PublicKey {
1151+
match self {
1152+
HolderCommitmentPoint::PendingNext { current, .. } => *current,
1153+
HolderCommitmentPoint::Available { current, .. } => *current,
1154+
}
1155+
}
1156+
1157+
pub fn next_point(&self) -> Option<PublicKey> {
1158+
match self {
1159+
HolderCommitmentPoint::PendingNext { .. } => None,
1160+
HolderCommitmentPoint::Available { next, .. } => Some(*next),
1161+
}
1162+
}
1163+
1164+
pub fn advance<SP: Deref, L: Deref>(&mut self, signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>, logger: &L)
1165+
where SP::Target: SignerProvider, L::Target: Logger
1166+
{
1167+
if let HolderCommitmentPoint::Available { transaction_number, next, .. } = self {
1168+
*self = HolderCommitmentPoint::PendingNext {
1169+
transaction_number: *transaction_number - 1,
1170+
current: *next,
1171+
};
1172+
}
1173+
1174+
if let HolderCommitmentPoint::PendingNext { transaction_number, current } = self {
1175+
let next = signer.as_ref().get_per_commitment_point(*transaction_number - 1, secp_ctx);
1176+
log_trace!(logger, "Retrieved next per-commitment point {}", *transaction_number - 1);
1177+
*self = HolderCommitmentPoint::Available { transaction_number: *transaction_number, current: *current, next };
1178+
}
1179+
}
1180+
}
1181+
11221182
/// If the majority of the channels funds are to the fundee and the initiator holds only just
11231183
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
11241184
/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -1297,6 +1357,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
12971357
// generation start at 0 and count up...this simplifies some parts of implementation at the
12981358
// cost of others, but should really just be changed.
12991359

1360+
holder_commitment_point: HolderCommitmentPoint,
13001361
cur_holder_commitment_transaction_number: u64,
13011362
cur_counterparty_commitment_transaction_number: u64,
13021363
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
@@ -1739,6 +1800,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17391800

17401801
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
17411802

1803+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1804+
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
1805+
17421806
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
17431807

17441808
let channel_context = ChannelContext {
@@ -1764,10 +1828,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17641828

17651829
latest_monitor_update_id: 0,
17661830

1767-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
1831+
holder_signer,
17681832
shutdown_scriptpubkey,
17691833
destination_script,
17701834

1835+
holder_commitment_point,
17711836
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
17721837
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
17731838
value_to_self_msat,
@@ -1963,6 +2028,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19632028

19642029
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
19652030

2031+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2032+
let holder_commitment_point = HolderCommitmentPoint::new(&holder_signer, &secp_ctx);
2033+
19662034
Ok(Self {
19672035
user_id,
19682036

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

19872055
latest_monitor_update_id: 0,
19882056

1989-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
2057+
holder_signer,
19902058
shutdown_scriptpubkey,
19912059
destination_script,
19922060

2061+
holder_commitment_point,
19932062
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19942063
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19952064
value_to_self_msat,
@@ -8779,6 +8848,10 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87798848
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
87808849
}
87818850

8851+
// `current_point` will become optional when async signing is implemented.
8852+
let cur_holder_commitment_point = Some(self.context.holder_commitment_point.current_point());
8853+
let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8854+
87828855
write_tlv_fields!(writer, {
87838856
(0, self.context.announcement_sigs, option),
87848857
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8815,7 +8888,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88158888
(39, pending_outbound_blinding_points, optional_vec),
88168889
(41, holding_cell_blinding_points, optional_vec),
88178890
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8818-
// 45 and 47 are reserved for async signing
8891+
(45, cur_holder_commitment_point, option),
8892+
(47, next_holder_commitment_point, option),
88198893
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
88208894
});
88218895

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

9203+
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9204+
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9205+
91299206
read_tlv_fields!(reader, {
91309207
(0, announcement_sigs, option),
91319208
(1, minimum_depth, option),
@@ -9156,7 +9233,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91569233
(39, pending_outbound_blinding_points_opt, optional_vec),
91579234
(41, holding_cell_blinding_points_opt, optional_vec),
91589235
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9159-
// 45 and 47 are reserved for async signing
9236+
(45, cur_holder_commitment_point_opt, option),
9237+
(47, next_holder_commitment_point_opt, option),
91609238
(49, local_initiated_shutdown, option),
91619239
});
91629240

@@ -9268,6 +9346,24 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92689346
}
92699347
}
92709348

9349+
// If we're restoring this channel for the first time after an upgrade, then we require that the
9350+
// signer be available so that we can immediately populate the current commitment point. Channel
9351+
// restoration will fail if this is not possible.
9352+
let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9353+
(Some(current), Some(next)) => HolderCommitmentPoint::Available {
9354+
transaction_number: cur_holder_commitment_transaction_number, current, next
9355+
},
9356+
(Some(current), _) => HolderCommitmentPoint::Available {
9357+
transaction_number: cur_holder_commitment_transaction_number, current,
9358+
next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9359+
},
9360+
(_, _) => HolderCommitmentPoint::Available {
9361+
transaction_number: cur_holder_commitment_transaction_number,
9362+
current: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number, &secp_ctx),
9363+
next: holder_signer.get_per_commitment_point(cur_holder_commitment_transaction_number - 1, &secp_ctx),
9364+
},
9365+
};
9366+
92719367
Ok(Channel {
92729368
context: ChannelContext {
92739369
user_id,
@@ -9293,6 +9389,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92939389
shutdown_scriptpubkey,
92949390
destination_script,
92959391

9392+
holder_commitment_point,
92969393
cur_holder_commitment_transaction_number,
92979394
cur_counterparty_commitment_transaction_number,
92989395
value_to_self_msat,

0 commit comments

Comments
 (0)