Skip to content

Commit a95add5

Browse files
committed
Add HolderCommitmentPoint struct to ChannelContext
1 parent cfa24f5 commit a95add5

File tree

1 file changed

+105
-4
lines changed

1 file changed

+105
-4
lines changed

lightning/src/ln/channel.rs

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

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

1366+
holder_commitment_point: HolderCommitmentPoint,
13001367
cur_holder_commitment_transaction_number: u64,
13011368
cur_counterparty_commitment_transaction_number: u64,
13021369
value_to_self_msat: u64, // Excluding all pending_htlcs, fees, and anchor outputs
@@ -1739,6 +1806,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17391806

17401807
let value_to_self_msat = our_funding_satoshis * 1000 + msg_push_msat;
17411808

1809+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
1810+
let mut holder_commitment_point = HolderCommitmentPoint::new();
1811+
holder_commitment_point.advance(&holder_signer, &secp_ctx, &&logger);
1812+
17421813
// TODO(dual_funding): Checks for `funding_feerate_sat_per_1000_weight`?
17431814

17441815
let channel_context = ChannelContext {
@@ -1764,10 +1835,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17641835

17651836
latest_monitor_update_id: 0,
17661837

1767-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
1838+
holder_signer,
17681839
shutdown_scriptpubkey,
17691840
destination_script,
17701841

1842+
holder_commitment_point,
17711843
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
17721844
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
17731845
value_to_self_msat,
@@ -1965,6 +2037,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19652037

19662038
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
19672039

2040+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2041+
let mut holder_commitment_point = HolderCommitmentPoint::new();
2042+
holder_commitment_point.advance(&holder_signer, &secp_ctx, logger);
2043+
19682044
Ok(Self {
19692045
user_id,
19702046

@@ -1988,10 +2064,11 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19882064

19892065
latest_monitor_update_id: 0,
19902066

1991-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
2067+
holder_signer,
19922068
shutdown_scriptpubkey,
19932069
destination_script,
19942070

2071+
holder_commitment_point,
19952072
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19962073
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19972074
value_to_self_msat,
@@ -8785,6 +8862,9 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87858862
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
87868863
}
87878864

8865+
let cur_holder_commitment_point = self.context.holder_commitment_point.current_point();
8866+
let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8867+
87888868
write_tlv_fields!(writer, {
87898869
(0, self.context.announcement_sigs, option),
87908870
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8821,7 +8901,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88218901
(39, pending_outbound_blinding_points, optional_vec),
88228902
(41, holding_cell_blinding_points, optional_vec),
88238903
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8824-
// 45 and 47 are reserved for async signing
8904+
(45, cur_holder_commitment_point, option),
8905+
(47, next_holder_commitment_point, option),
88258906
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
88268907
});
88278908

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

9216+
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9217+
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9218+
91359219
read_tlv_fields!(reader, {
91369220
(0, announcement_sigs, option),
91379221
(1, minimum_depth, option),
@@ -9162,7 +9246,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91629246
(39, pending_outbound_blinding_points_opt, optional_vec),
91639247
(41, holding_cell_blinding_points_opt, optional_vec),
91649248
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9165-
// 45 and 47 are reserved for async signing
9249+
(45, cur_holder_commitment_point_opt, option),
9250+
(47, next_holder_commitment_point_opt, option),
91669251
(49, local_initiated_shutdown, option),
91679252
});
91689253

@@ -9274,6 +9359,21 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92749359
}
92759360
}
92769361

9362+
// If we're restoring this channel for the first time after an upgrade, then we require that the
9363+
// signer be available so that we can immediately populate the current commitment point. Channel
9364+
// restoration will fail if this is not possible.
9365+
let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9366+
(Some(current), Some(next)) => HolderCommitmentPoint::Available {
9367+
transaction_number: cur_holder_commitment_transaction_number, current, next
9368+
},
9369+
(Some(current), _) => HolderCommitmentPoint::PendingNext {
9370+
transaction_number: cur_holder_commitment_transaction_number, current
9371+
},
9372+
(_, _) => HolderCommitmentPoint::Uninitialized {
9373+
transaction_number: cur_holder_commitment_transaction_number
9374+
}
9375+
};
9376+
92779377
Ok(Channel {
92789378
context: ChannelContext {
92799379
user_id,
@@ -9299,6 +9399,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92999399
shutdown_scriptpubkey,
93009400
destination_script,
93019401

9402+
holder_commitment_point,
93029403
cur_holder_commitment_transaction_number,
93039404
cur_counterparty_commitment_transaction_number,
93049405
value_to_self_msat,

0 commit comments

Comments
 (0)