Skip to content

Commit 4260230

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

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,
@@ -1963,6 +2035,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19632035

19642036
let temporary_channel_id = temporary_channel_id.unwrap_or_else(|| ChannelId::temporary_from_entropy_source(entropy_source));
19652037

2038+
let holder_signer = ChannelSignerType::Ecdsa(holder_signer);
2039+
let mut holder_commitment_point = HolderCommitmentPoint::new();
2040+
holder_commitment_point.advance(&holder_signer, &secp_ctx, logger);
2041+
19662042
Ok(Self {
19672043
user_id,
19682044

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

19872063
latest_monitor_update_id: 0,
19882064

1989-
holder_signer: ChannelSignerType::Ecdsa(holder_signer),
2065+
holder_signer,
19902066
shutdown_scriptpubkey,
19912067
destination_script,
19922068

2069+
holder_commitment_point,
19932070
cur_holder_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19942071
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
19952072
value_to_self_msat,
@@ -8779,6 +8856,9 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
87798856
monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
87808857
}
87818858

8859+
let cur_holder_commitment_point = self.context.holder_commitment_point.current_point();
8860+
let next_holder_commitment_point = self.context.holder_commitment_point.next_point();
8861+
87828862
write_tlv_fields!(writer, {
87838863
(0, self.context.announcement_sigs, option),
87848864
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8815,7 +8895,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
88158895
(39, pending_outbound_blinding_points, optional_vec),
88168896
(41, holding_cell_blinding_points, optional_vec),
88178897
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
8818-
// 45 and 47 are reserved for async signing
8898+
(45, cur_holder_commitment_point, option),
8899+
(47, next_holder_commitment_point, option),
88198900
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
88208901
});
88218902

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

9210+
let mut cur_holder_commitment_point_opt: Option<PublicKey> = None;
9211+
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
9212+
91299213
read_tlv_fields!(reader, {
91309214
(0, announcement_sigs, option),
91319215
(1, minimum_depth, option),
@@ -9156,7 +9240,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
91569240
(39, pending_outbound_blinding_points_opt, optional_vec),
91579241
(41, holding_cell_blinding_points_opt, optional_vec),
91589242
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
9159-
// 45 and 47 are reserved for async signing
9243+
(45, cur_holder_commitment_point_opt, option),
9244+
(47, next_holder_commitment_point_opt, option),
91609245
(49, local_initiated_shutdown, option),
91619246
});
91629247

@@ -9268,6 +9353,21 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92689353
}
92699354
}
92709355

9356+
// If we're restoring this channel for the first time after an upgrade, then we require that the
9357+
// signer be available so that we can immediately populate the current commitment point. Channel
9358+
// restoration will fail if this is not possible.
9359+
let holder_commitment_point = match (cur_holder_commitment_point_opt, next_holder_commitment_point_opt) {
9360+
(Some(current), Some(next)) => HolderCommitmentPoint::Available {
9361+
transaction_number: cur_holder_commitment_transaction_number, current, next
9362+
},
9363+
(Some(current), _) => HolderCommitmentPoint::PendingNext {
9364+
transaction_number: cur_holder_commitment_transaction_number, current
9365+
},
9366+
(_, _) => HolderCommitmentPoint::Uninitialized {
9367+
transaction_number: cur_holder_commitment_transaction_number
9368+
}
9369+
};
9370+
92719371
Ok(Channel {
92729372
context: ChannelContext {
92739373
user_id,
@@ -9293,6 +9393,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92939393
shutdown_scriptpubkey,
92949394
destination_script,
92959395

9396+
holder_commitment_point,
92969397
cur_holder_commitment_transaction_number,
92979398
cur_counterparty_commitment_transaction_number,
92989399
value_to_self_msat,

0 commit comments

Comments
 (0)