Skip to content

Commit 2814c10

Browse files
Add htcl min/max to CounterpartyForwardingInfo
1 parent c6e2540 commit 2814c10

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,6 +4310,11 @@ impl<Signer: Sign> Channel<Signer> {
43104310
self.counterparty_htlc_minimum_msat
43114311
}
43124312

4313+
/// Allowed in any state (including after shutdown), but will return none before TheirInitSent
4314+
pub fn get_counterparty_htlc_max_msat(&self) -> Option<u64> {
4315+
self.get_htlc_max_msat(self.counterparty_max_htlc_value_in_flight_msat)
4316+
}
4317+
43134318
/// Allowed in any state (including after shutdown), but will return none before TheirInitSent
43144319
fn get_htlc_max_msat(&self, inflight_value_of_party: u64) -> Option<u64> {
43154320
if self.channel_state >= ChannelState::TheirInitSent as u32 {
@@ -5389,10 +5394,16 @@ impl<Signer: Sign> Channel<Signer> {
53895394
if msg.contents.htlc_minimum_msat >= self.channel_value_satoshis * 1000 {
53905395
return Err(ChannelError::Close("Minimum htlc value is greater than channel value".to_string()));
53915396
}
5397+
let htlc_maximum_msat = match msg.contents.htlc_maximum_msat {
5398+
OptionalField::Present(htcl_max) => Some(htcl_max),
5399+
OptionalField::Absent => None
5400+
};
53925401
self.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
53935402
fee_base_msat: msg.contents.fee_base_msat,
53945403
fee_proportional_millionths: msg.contents.fee_proportional_millionths,
5395-
cltv_expiry_delta: msg.contents.cltv_expiry_delta
5404+
cltv_expiry_delta: msg.contents.cltv_expiry_delta,
5405+
htlc_minimum_msat: msg.contents.htlc_minimum_msat,
5406+
htlc_maximum_msat,
53965407
});
53975408

53985409
Ok(())
@@ -5779,12 +5790,16 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
57795790
// Note that this field is ignored by 0.0.99+ as the TLV Optional variant is used instead.
57805791
self.minimum_depth.unwrap_or(0).write(writer)?;
57815792

5793+
let mut htlc_maximum_msat = None;
57825794
match &self.counterparty_forwarding_info {
57835795
Some(info) => {
57845796
1u8.write(writer)?;
57855797
info.fee_base_msat.write(writer)?;
57865798
info.fee_proportional_millionths.write(writer)?;
57875799
info.cltv_expiry_delta.write(writer)?;
5800+
// Note that this field is ignored by 0.0.99+ as the TLV Optional variant is used instead.
5801+
info.htlc_maximum_msat.unwrap_or(0).write(writer)?;
5802+
htlc_maximum_msat = info.htlc_maximum_msat;
57885803
},
57895804
None => 0u8.write(writer)?
57905805
}
@@ -5847,6 +5862,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
58475862
(17, self.announcement_sigs_state, required),
58485863
(19, self.latest_inbound_scid_alias, option),
58495864
(21, self.outbound_scid_alias, required),
5865+
(23, htlc_maximum_msat, option),
58505866
});
58515867

58525868
Ok(())
@@ -6045,13 +6061,29 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
60456061
let _dummy: u32 = Readable::read(reader)?;
60466062
}
60476063

6048-
let counterparty_forwarding_info = match <u8 as Readable>::read(reader)? {
6049-
0 => None,
6050-
1 => Some(CounterpartyForwardingInfo {
6051-
fee_base_msat: Readable::read(reader)?,
6052-
fee_proportional_millionths: Readable::read(reader)?,
6053-
cltv_expiry_delta: Readable::read(reader)?,
6054-
}),
6064+
// Read fields for `CounterpartyForwardingInfo`. As the `htlc_maximum_msat` may be
6065+
// updated after the TLV Optional variant has been read depending on if they're used,
6066+
// we construct the `CounterpartyForwardingInfo` struct after TLV Optional variants
6067+
// have been read.
6068+
let mut fee_base_msat: u32 = 0;
6069+
let mut fee_proportional_millionths: u32 = 0;
6070+
let mut cltv_expiry_delta: u16 = 0;
6071+
let mut htlc_maximum_msat = None;
6072+
let has_forwarding_info = match <u8 as Readable>::read(reader)? {
6073+
0 => false,
6074+
1 => {
6075+
fee_base_msat = Readable::read(reader)?;
6076+
fee_proportional_millionths = Readable::read(reader)?;
6077+
cltv_expiry_delta = Readable::read(reader)?;
6078+
if ver == 1 {
6079+
// Read the old serialization from version 0.0.98.
6080+
htlc_maximum_msat = Some(Readable::read(reader)?);
6081+
} else {
6082+
// Read the 8 bytes of backwards-compatibility data.
6083+
let _dummy: u64 = Readable::read(reader)?;
6084+
}
6085+
true
6086+
},
60556087
_ => return Err(DecodeError::InvalidValue),
60566088
};
60576089

@@ -6121,8 +6153,22 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
61216153
(17, announcement_sigs_state, option),
61226154
(19, latest_inbound_scid_alias, option),
61236155
(21, outbound_scid_alias, option),
6156+
(23, htlc_maximum_msat, option),
61246157
});
61256158

6159+
// Construct the `CounterpartyForwardingInfo` after the TLV Optional variant for
6160+
// `htlc_maximum_msat` has been read.
6161+
let counterparty_forwarding_info = match has_forwarding_info {
6162+
false => None,
6163+
true => Some(CounterpartyForwardingInfo {
6164+
fee_base_msat,
6165+
fee_proportional_millionths,
6166+
cltv_expiry_delta,
6167+
htlc_minimum_msat: counterparty_htlc_minimum_msat,
6168+
htlc_maximum_msat
6169+
})
6170+
};
6171+
61266172
if let Some(preimages) = preimages_opt {
61276173
let mut iter = preimages.into_iter();
61286174
for htlc in pending_outbound_htlcs.iter_mut() {

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,10 @@ pub struct CounterpartyForwardingInfo {
11591159
/// such that the outgoing HTLC is forwardable to this counterparty. See `msgs::ChannelUpdate`'s
11601160
/// `cltv_expiry_delta` for more details.
11611161
pub cltv_expiry_delta: u16,
1162+
/// The smallest value HTLC (in msat) the remote peer will accept, for this channel.
1163+
pub htlc_minimum_msat: u64,
1164+
/// The largest value HTLC (in msat) the remote peer currently will accept, for this channel.
1165+
pub htlc_maximum_msat: Option<u64>,
11621166
}
11631167

11641168
/// Channel parameters which apply to our counterparty. These are split out from [`ChannelDetails`]
@@ -6054,6 +6058,8 @@ impl_writeable_tlv_based!(CounterpartyForwardingInfo, {
60546058
(2, fee_base_msat, required),
60556059
(4, fee_proportional_millionths, required),
60566060
(6, cltv_expiry_delta, required),
6061+
(8, htlc_minimum_msat, required),
6062+
(10, htlc_maximum_msat, option),
60576063
});
60586064

60596065
impl_writeable_tlv_based!(ChannelCounterparty, {

0 commit comments

Comments
 (0)