Skip to content

Commit 3fd4fdc

Browse files
committed
Make htlc_maximum_msat a required field.
1 parent 8e5cf75 commit 3fd4fdc

File tree

11 files changed

+180
-206
lines changed

11 files changed

+180
-206
lines changed

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bitcoin::BlockHash;
88
use bitcoin::secp256k1::PublicKey;
99

1010
use lightning::ln::msgs::{
11-
DecodeError, ErrorAction, LightningError, OptionalField, UnsignedChannelUpdate,
11+
DecodeError, ErrorAction, LightningError, UnsignedChannelUpdate,
1212
};
1313
use lightning::routing::gossip::NetworkGraph;
1414
use lightning::util::ser::{BigSize, Readable};
@@ -118,12 +118,7 @@ impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
118118
let default_htlc_minimum_msat: u64 = Readable::read(&mut read_cursor)?;
119119
let default_fee_base_msat: u32 = Readable::read(&mut read_cursor)?;
120120
let default_fee_proportional_millionths: u32 = Readable::read(&mut read_cursor)?;
121-
let tentative_default_htlc_maximum_msat: u64 = Readable::read(&mut read_cursor)?;
122-
let default_htlc_maximum_msat = if tentative_default_htlc_maximum_msat == u64::max_value() {
123-
OptionalField::Absent
124-
} else {
125-
OptionalField::Present(tentative_default_htlc_maximum_msat)
126-
};
121+
let default_htlc_maximum_msat: u64 = Readable::read(&mut read_cursor)?;
127122

128123
for _ in 0..update_count {
129124
let scid_delta: BigSize = Readable::read(read_cursor)?;
@@ -146,7 +141,7 @@ impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
146141
flags: standard_channel_flags,
147142
cltv_expiry_delta: default_cltv_expiry_delta,
148143
htlc_minimum_msat: default_htlc_minimum_msat,
149-
htlc_maximum_msat: default_htlc_maximum_msat.clone(),
144+
htlc_maximum_msat: default_htlc_maximum_msat,
150145
fee_base_msat: default_fee_base_msat,
151146
fee_proportional_millionths: default_fee_proportional_millionths,
152147
excess_data: vec![],
@@ -169,21 +164,14 @@ impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
169164
action: ErrorAction::IgnoreError,
170165
})?;
171166

172-
let htlc_maximum_msat =
173-
if let Some(htlc_maximum_msat) = directional_info.htlc_maximum_msat {
174-
OptionalField::Present(htlc_maximum_msat)
175-
} else {
176-
OptionalField::Absent
177-
};
178-
179167
UnsignedChannelUpdate {
180168
chain_hash,
181169
short_channel_id,
182170
timestamp: backdated_timestamp,
183171
flags: standard_channel_flags,
184172
cltv_expiry_delta: directional_info.cltv_expiry_delta,
185173
htlc_minimum_msat: directional_info.htlc_minimum_msat,
186-
htlc_maximum_msat,
174+
htlc_maximum_msat: directional_info.htlc_maximum_msat,
187175
fee_base_msat: directional_info.fees.base_msat,
188176
fee_proportional_millionths: directional_info.fees.proportional_millionths,
189177
excess_data: vec![],
@@ -211,13 +199,8 @@ impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
211199
}
212200

213201
if channel_flags & 0b_0000_0100 > 0 {
214-
let tentative_htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
215-
synthetic_update.htlc_maximum_msat = if tentative_htlc_maximum_msat == u64::max_value()
216-
{
217-
OptionalField::Absent
218-
} else {
219-
OptionalField::Present(tentative_htlc_maximum_msat)
220-
};
202+
let htlc_maximum_msat: u64 = Readable::read(read_cursor)?;
203+
synthetic_update.htlc_maximum_msat = htlc_maximum_msat;
221204
}
222205

223206
network_graph.update_channel_unsigned(&synthetic_update)?;

lightning/src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6440,7 +6440,7 @@ mod tests {
64406440
use ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator};
64416441
use ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS};
64426442
use ln::features::{InitFeatures, ChannelTypeFeatures};
6443-
use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate};
6443+
use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate, MAX_VALUE_MSAT};
64446444
use ln::script::ShutdownScript;
64456445
use ln::chan_utils;
64466446
use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight};
@@ -6854,7 +6854,7 @@ mod tests {
68546854
flags: 0,
68556855
cltv_expiry_delta: 100,
68566856
htlc_minimum_msat: 5,
6857-
htlc_maximum_msat: OptionalField::Absent,
6857+
htlc_maximum_msat: MAX_VALUE_MSAT,
68586858
fee_base_msat: 110,
68596859
fee_proportional_millionths: 11,
68606860
excess_data: Vec::new(),

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use routing::router::{PaymentParameters, Route, RouteHop, RoutePath, RouteParame
4848
use ln::msgs;
4949
use ln::msgs::NetAddress;
5050
use ln::onion_utils;
51-
use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VALUE_MSAT, OptionalField};
51+
use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VALUE_MSAT};
5252
use ln::wire::Encode;
5353
use chain::keysinterface::{Sign, KeysInterface, KeysManager, InMemorySigner, Recipient};
5454
use util::config::UserConfig;
@@ -2357,7 +2357,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23572357
flags: (!were_node_one) as u8 | ((!chan.is_live() as u8) << 1),
23582358
cltv_expiry_delta: chan.get_cltv_expiry_delta(),
23592359
htlc_minimum_msat: chan.get_counterparty_htlc_minimum_msat(),
2360-
htlc_maximum_msat: OptionalField::Present(chan.get_announced_htlc_max_msat()),
2360+
htlc_maximum_msat: chan.get_announced_htlc_max_msat(),
23612361
fee_base_msat: chan.get_outbound_forwarding_fee_base_msat(),
23622362
fee_proportional_millionths: chan.get_fee_proportional_millionths(),
23632363
excess_data: Vec::new(),

lightning/src/ln/functional_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputI
2626
use routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
2727
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
2828
use ln::msgs;
29-
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField, ErrorAction};
29+
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
3030
use util::enforcing_trait_impls::EnforcingSigner;
3131
use util::{byte_utils, test_utils};
3232
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason};
@@ -8308,19 +8308,19 @@ fn test_channel_update_has_correct_htlc_maximum_msat() {
83088308

83098309
// Assert that `node[0]`'s `ChannelUpdate` is capped at 50 percent of the `channel_value`, as
83108310
// that's the value of `node[1]`'s `holder_max_htlc_value_in_flight_msat`.
8311-
assert_eq!(node_0_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_50_percent_msat));
8311+
assert_eq!(node_0_chan_update.contents.htlc_maximum_msat, channel_value_50_percent_msat);
83128312
// Assert that `node[1]`'s `ChannelUpdate` is capped at 30 percent of the `channel_value`, as
83138313
// that's the value of `node[0]`'s `holder_max_htlc_value_in_flight_msat`.
8314-
assert_eq!(node_1_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_30_percent_msat));
8314+
assert_eq!(node_1_chan_update.contents.htlc_maximum_msat, channel_value_30_percent_msat);
83158315

83168316
// Assert that `node[2]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
83178317
// the value of `node[3]`'s `holder_max_htlc_value_in_flight_msat` (100%), exceeds 90% of the
83188318
// `channel_value`.
8319-
assert_eq!(node_2_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_90_percent_msat));
8319+
assert_eq!(node_2_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
83208320
// Assert that `node[3]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
83218321
// the value of `node[2]`'s `holder_max_htlc_value_in_flight_msat` (95%), exceeds 90% of the
83228322
// `channel_value`.
8323-
assert_eq!(node_3_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_90_percent_msat));
8323+
assert_eq!(node_3_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
83248324
}
83258325

83268326
#[test]

lightning/src/ln/msgs.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,8 @@ pub struct UnsignedChannelUpdate {
624624
pub cltv_expiry_delta: u16,
625625
/// The minimum HTLC size incoming to sender, in milli-satoshi
626626
pub htlc_minimum_msat: u64,
627-
/// Optionally, the maximum HTLC value incoming to sender, in milli-satoshi
628-
pub htlc_maximum_msat: OptionalField<u64>,
627+
/// The maximum HTLC value incoming to sender, in milli-satoshi. Used to be optional.
628+
pub htlc_maximum_msat: u64,
629629
/// The base HTLC fee charged by sender, in milli-satoshi
630630
pub fee_base_msat: u32,
631631
/// The amount to fee multiplier, in micro-satoshi
@@ -1491,14 +1491,12 @@ impl_writeable!(ChannelAnnouncement, {
14911491

14921492
impl Writeable for UnsignedChannelUpdate {
14931493
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1494-
let mut message_flags: u8 = 0;
1495-
if let OptionalField::Present(_) = self.htlc_maximum_msat {
1496-
message_flags = 1;
1497-
}
1494+
// `must_be_one` used to be `message_flags` but was deprecated in the spec.
1495+
const MUST_BE_ONE: u8 = 1;
14981496
self.chain_hash.write(w)?;
14991497
self.short_channel_id.write(w)?;
15001498
self.timestamp.write(w)?;
1501-
let all_flags = self.flags as u16 | ((message_flags as u16) << 8);
1499+
let all_flags = self.flags as u16 | ((MUST_BE_ONE as u16) << 8);
15021500
all_flags.write(w)?;
15031501
self.cltv_expiry_delta.write(w)?;
15041502
self.htlc_minimum_msat.write(w)?;
@@ -1512,22 +1510,20 @@ impl Writeable for UnsignedChannelUpdate {
15121510

15131511
impl Readable for UnsignedChannelUpdate {
15141512
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1515-
let has_htlc_maximum_msat;
15161513
Ok(Self {
15171514
chain_hash: Readable::read(r)?,
15181515
short_channel_id: Readable::read(r)?,
15191516
timestamp: Readable::read(r)?,
15201517
flags: {
15211518
let flags: u16 = Readable::read(r)?;
1522-
let message_flags = flags >> 8;
1523-
has_htlc_maximum_msat = (message_flags as i32 & 1) == 1;
1519+
// Note: we ignore `must_be_one`, formely `message_flags`, since it was deprecated by the spec.
15241520
flags as u8
15251521
},
15261522
cltv_expiry_delta: Readable::read(r)?,
15271523
htlc_minimum_msat: Readable::read(r)?,
15281524
fee_base_msat: Readable::read(r)?,
15291525
fee_proportional_millionths: Readable::read(r)?,
1530-
htlc_maximum_msat: if has_htlc_maximum_msat { Readable::read(r)? } else { OptionalField::Absent },
1526+
htlc_maximum_msat: Readable::read(r)?,
15311527
excess_data: read_to_end(r)?,
15321528
})
15331529
}
@@ -2069,7 +2065,7 @@ mod tests {
20692065
do_encoding_node_announcement(false, false, true, false, true, false, false);
20702066
}
20712067

2072-
fn do_encoding_channel_update(direction: bool, disable: bool, htlc_maximum_msat: bool, excess_data: bool) {
2068+
fn do_encoding_channel_update(direction: bool, disable: bool, excess_data: bool) {
20732069
let secp_ctx = Secp256k1::new();
20742070
let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
20752071
let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
@@ -2080,7 +2076,7 @@ mod tests {
20802076
flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 },
20812077
cltv_expiry_delta: 144,
20822078
htlc_minimum_msat: 1000000,
2083-
htlc_maximum_msat: if htlc_maximum_msat { OptionalField::Present(131355275467161) } else { OptionalField::Absent },
2079+
htlc_maximum_msat: 131355275467161,
20842080
fee_base_msat: 10000,
20852081
fee_proportional_millionths: 20,
20862082
excess_data: if excess_data { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
@@ -2093,11 +2089,7 @@ mod tests {
20932089
let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
20942090
target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
20952091
target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
2096-
if htlc_maximum_msat {
2097-
target_value.append(&mut hex::decode("01").unwrap());
2098-
} else {
2099-
target_value.append(&mut hex::decode("00").unwrap());
2100-
}
2092+
target_value.append(&mut hex::decode("01").unwrap());
21012093
target_value.append(&mut hex::decode("00").unwrap());
21022094
if direction {
21032095
let flag = target_value.last_mut().unwrap();
@@ -2108,9 +2100,7 @@ mod tests {
21082100
*flag = *flag | 1 << 1;
21092101
}
21102102
target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
2111-
if htlc_maximum_msat {
2112-
target_value.append(&mut hex::decode("0000777788889999").unwrap());
2113-
}
2103+
target_value.append(&mut hex::decode("0000777788889999").unwrap());
21142104
if excess_data {
21152105
target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
21162106
}
@@ -2119,16 +2109,16 @@ mod tests {
21192109

21202110
#[test]
21212111
fn encoding_channel_update() {
2122-
do_encoding_channel_update(false, false, false, false);
2123-
do_encoding_channel_update(false, false, false, true);
2124-
do_encoding_channel_update(true, false, false, false);
2125-
do_encoding_channel_update(true, false, false, true);
2126-
do_encoding_channel_update(false, true, false, false);
2127-
do_encoding_channel_update(false, true, false, true);
2128-
do_encoding_channel_update(false, false, true, false);
2129-
do_encoding_channel_update(false, false, true, true);
2130-
do_encoding_channel_update(true, true, true, false);
2131-
do_encoding_channel_update(true, true, true, true);
2112+
do_encoding_channel_update(false, false, false);
2113+
do_encoding_channel_update(false, false, true);
2114+
do_encoding_channel_update(true, false, false);
2115+
do_encoding_channel_update(true, false, true);
2116+
do_encoding_channel_update(false, true, false);
2117+
do_encoding_channel_update(false, true, true);
2118+
do_encoding_channel_update(false, false, false);
2119+
do_encoding_channel_update(false, false, true);
2120+
do_encoding_channel_update(true, true, false);
2121+
do_encoding_channel_update(true, true, true);
21322122
}
21332123

21342124
fn do_encoding_open_channel(random_bit: bool, shutdown: bool, incl_chan_type: bool) {

lightning/src/ln/onion_route_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use routing::gossip::{NetworkUpdate, RoutingFees, NodeId};
2020
use routing::router::{get_route, PaymentParameters, Route, RouteHint, RouteHintHop};
2121
use ln::features::{InitFeatures, InvoiceFeatures, NodeFeatures};
2222
use ln::msgs;
23-
use ln::msgs::{ChannelMessageHandler, ChannelUpdate, OptionalField};
23+
use ln::msgs::{ChannelMessageHandler, ChannelUpdate};
2424
use ln::wire::Encode;
2525
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
2626
use util::ser::{Writeable, Writer};
@@ -225,7 +225,7 @@ impl msgs::ChannelUpdate {
225225
flags: 0,
226226
cltv_expiry_delta: 0,
227227
htlc_minimum_msat: 0,
228-
htlc_maximum_msat: OptionalField::Absent,
228+
htlc_maximum_msat: msgs::MAX_VALUE_MSAT,
229229
fee_base_msat: 0,
230230
fee_proportional_millionths: 0,
231231
excess_data: vec![],

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use routing::gossip::RoutingFees;
1919
use routing::router::{PaymentParameters, RouteHint, RouteHintHop};
2020
use ln::features::{InitFeatures, InvoiceFeatures, ChannelTypeFeatures};
2121
use ln::msgs;
22-
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField, ChannelUpdate, ErrorAction};
22+
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction};
2323
use ln::wire::Encode;
2424
use util::enforcing_trait_impls::EnforcingSigner;
2525
use util::events::{ClosureReason, Event, MessageSendEvent, MessageSendEventsProvider};
@@ -523,7 +523,7 @@ fn test_scid_alias_returned() {
523523
flags: 1,
524524
cltv_expiry_delta: accept_forward_cfg.channel_options.cltv_expiry_delta,
525525
htlc_minimum_msat: 1_000,
526-
htlc_maximum_msat: OptionalField::Present(1_000_000), // Defaults to 10% of the channel value
526+
htlc_maximum_msat: 1_000_000, // Defaults to 10% of the channel value
527527
fee_base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat,
528528
fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths,
529529
excess_data: Vec::new(),

0 commit comments

Comments
 (0)