Skip to content

Commit 13adf0e

Browse files
committed
Add htlc_maximum_msat field
1 parent 168302f commit 13adf0e

File tree

8 files changed

+86
-10
lines changed

8 files changed

+86
-10
lines changed

fuzz/src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
172172
let _ = net_graph_msg_handler.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4));
173173
},
174174
2 => {
175-
let _ = net_graph_msg_handler.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128));
175+
let _ = net_graph_msg_handler.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 136));
176176
},
177177
3 => {
178178
match get_slice!(1)[0] {

lightning/src/ln/channel.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31273127
self.our_htlc_minimum_msat
31283128
}
31293129

3130+
/// Allowed in any state (including after shutdown)
3131+
pub fn get_announced_htlc_max_msat(&self) -> u64 {
3132+
return cmp::min(
3133+
// Upper bound by capacity. We make it a bit less than full capacity to prevent attempts
3134+
// to use full capacity. This is an effort to reduce routing failures, because in many cases
3135+
// channel might have been used to route very small values (either by honest users or as DoS).
3136+
self.channel_value_satoshis * 9 / 10,
3137+
3138+
Channel::<ChanSigner>::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis)
3139+
);
3140+
}
3141+
31303142
/// Allowed in any state (including after shutdown)
31313143
pub fn get_their_htlc_minimum_msat(&self) -> u64 {
31323144
self.our_htlc_minimum_msat

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use ln::features::{InitFeatures, NodeFeatures};
3434
use routing::router::{Route, RouteHop};
3535
use ln::msgs;
3636
use ln::onion_utils;
37-
use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
37+
use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, OptionalField};
3838
use chain::keysinterface::{ChannelKeys, KeysInterface, KeysManager, InMemoryChannelKeys};
3939
use util::config::UserConfig;
4040
use util::{byte_utils, events};
@@ -1211,9 +1211,10 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
12111211
chain_hash: self.genesis_hash,
12121212
short_channel_id: short_channel_id,
12131213
timestamp: chan.get_update_time_counter(),
1214-
flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1),
1214+
flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1) | (1 << 8),
12151215
cltv_expiry_delta: CLTV_EXPIRY_DELTA,
12161216
htlc_minimum_msat: chan.get_our_htlc_minimum_msat(),
1217+
htlc_maximum_msat: OptionalField::Present(chan.get_announced_htlc_max_msat()),
12171218
fee_base_msat: chan.get_our_fee_base_msat(&self.fee_estimator),
12181219
fee_proportional_millionths: chan.get_fee_proportional_millionths(),
12191220
excess_data: Vec::new(),

lightning/src/ln/functional_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ln::{chan_utils, onion_utils};
1515
use routing::router::{Route, RouteHop, get_route};
1616
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1717
use ln::msgs;
18-
use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, ErrorAction};
18+
use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, ErrorAction, OptionalField};
1919
use util::enforcing_trait_impls::EnforcingChannelKeys;
2020
use util::{byte_utils, test_utils};
2121
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
@@ -6055,6 +6055,7 @@ impl msgs::ChannelUpdate {
60556055
flags: 0,
60566056
cltv_expiry_delta: 0,
60576057
htlc_minimum_msat: 0,
6058+
htlc_maximum_msat: OptionalField::Absent,
60586059
fee_base_msat: 0,
60596060
fee_proportional_millionths: 0,
60606061
excess_data: vec![],

lightning/src/ln/msgs.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ pub(crate) struct UnsignedChannelUpdate {
430430
pub(crate) flags: u16,
431431
pub(crate) cltv_expiry_delta: u16,
432432
pub(crate) htlc_minimum_msat: u64,
433+
pub(crate) htlc_maximum_msat: OptionalField<u64>,
433434
pub(crate) fee_base_msat: u32,
434435
pub(crate) fee_proportional_millionths: u32,
435436
pub(crate) excess_data: Vec<u8>,
@@ -517,7 +518,7 @@ pub enum HTLCFailChannelUpdate {
517518
/// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
518519
/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
519520
/// separate enum type for them.
520-
#[derive(Clone, PartialEq)]
521+
#[derive(Clone, PartialEq, Debug)]
521522
pub enum OptionalField<T> {
522523
/// Optional field is included in message
523524
Present(T),
@@ -742,6 +743,26 @@ impl Readable for OptionalField<Script> {
742743
}
743744
}
744745

746+
impl Writeable for OptionalField<u64> {
747+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
748+
match *self {
749+
OptionalField::Present(ref value) => {
750+
value.write(w)?;
751+
},
752+
OptionalField::Absent => {}
753+
}
754+
Ok(())
755+
}
756+
}
757+
758+
impl Readable for OptionalField<u64> {
759+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
760+
let value: u64 = Readable::read(r)?;
761+
Ok(OptionalField::Present(value))
762+
}
763+
}
764+
765+
745766
impl_writeable_len_match!(AcceptChannel, {
746767
{AcceptChannel{ shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 270 + 2 + script.len()},
747768
{_, 270}
@@ -1180,7 +1201,7 @@ impl_writeable_len_match!(ChannelAnnouncement, {
11801201

11811202
impl Writeable for UnsignedChannelUpdate {
11821203
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1183-
w.size_hint(64 + self.excess_data.len());
1204+
w.size_hint(64 + self.excess_data.len() + if let OptionalField::Present(_) = self.htlc_maximum_msat { 8 } else { 0 } );
11841205
self.chain_hash.write(w)?;
11851206
self.short_channel_id.write(w)?;
11861207
self.timestamp.write(w)?;
@@ -1189,22 +1210,30 @@ impl Writeable for UnsignedChannelUpdate {
11891210
self.htlc_minimum_msat.write(w)?;
11901211
self.fee_base_msat.write(w)?;
11911212
self.fee_proportional_millionths.write(w)?;
1213+
self.htlc_maximum_msat.write(w)?;
11921214
w.write_all(&self.excess_data[..])?;
11931215
Ok(())
11941216
}
11951217
}
11961218

11971219
impl Readable for UnsignedChannelUpdate {
11981220
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1221+
let has_htlc_maximum_msat;
11991222
Ok(Self {
12001223
chain_hash: Readable::read(r)?,
12011224
short_channel_id: Readable::read(r)?,
12021225
timestamp: Readable::read(r)?,
1203-
flags: Readable::read(r)?,
1226+
flags: {
1227+
let flags = Readable::read(r)?;
1228+
let message_flags = flags >> 8;
1229+
has_htlc_maximum_msat = (message_flags as i32 & 1) == 1;
1230+
flags
1231+
},
12041232
cltv_expiry_delta: Readable::read(r)?,
12051233
htlc_minimum_msat: Readable::read(r)?,
12061234
fee_base_msat: Readable::read(r)?,
12071235
fee_proportional_millionths: Readable::read(r)?,
1236+
htlc_maximum_msat: if has_htlc_maximum_msat { Readable::read(r)? } else { OptionalField::Absent },
12081237
excess_data: {
12091238
let mut excess_data = vec![];
12101239
r.read_to_end(&mut excess_data)?;
@@ -1608,6 +1637,7 @@ mod tests {
16081637
flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
16091638
cltv_expiry_delta: 144,
16101639
htlc_minimum_msat: 1000000,
1640+
htlc_maximum_msat: if htlc_maximum_msat { OptionalField::Present(1) } else { OptionalField::Absent },
16111641
fee_base_msat: 10000,
16121642
fee_proportional_millionths: 20,
16131643
excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
@@ -1636,6 +1666,7 @@ mod tests {
16361666
}
16371667
target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
16381668
if htlc_maximum_msat {
1669+
target_value.append(&mut hex::decode("0000000000000001").unwrap());
16391670
target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
16401671
}
16411672
assert_eq!(encoded_value, target_value);

lightning/src/routing/network_graph.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bitcoin::blockdata::opcodes;
1111

1212
use chain::chaininterface::{ChainError, ChainWatchInterface};
1313
use ln::features::{ChannelFeatures, NodeFeatures};
14-
use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress};
14+
use ln::msgs::{DecodeError, ErrorAction, LightningError, RoutingMessageHandler, NetAddress, OptionalField};
1515
use ln::msgs;
1616
use util::ser::{Writeable, Readable, Writer};
1717
use util::logger::Logger;
@@ -214,6 +214,8 @@ pub struct DirectionalChannelInfo {
214214
pub cltv_expiry_delta: u16,
215215
/// The minimum value, which must be relayed to the next hop via the channel
216216
pub htlc_minimum_msat: u64,
217+
/// The maximum value which may be relayed to the next hop via the channel.
218+
pub htlc_maximum_msat: Option<u64>,
217219
/// Fees charged when the channel is used for routing
218220
pub fees: RoutingFees,
219221
/// Most recent update for the channel received from the network
@@ -235,6 +237,7 @@ impl_writeable!(DirectionalChannelInfo, 0, {
235237
enabled,
236238
cltv_expiry_delta,
237239
htlc_minimum_msat,
240+
htlc_maximum_msat,
238241
fees,
239242
last_update_message
240243
});
@@ -680,6 +683,7 @@ impl NetworkGraph {
680683
last_update: msg.contents.timestamp,
681684
cltv_expiry_delta: msg.contents.cltv_expiry_delta,
682685
htlc_minimum_msat: msg.contents.htlc_minimum_msat,
686+
htlc_maximum_msat: if let OptionalField::Present(max_value) = msg.contents.htlc_maximum_msat { Some(max_value) } else { None },
683687
fees: RoutingFees {
684688
base_msat: msg.contents.fee_base_msat,
685689
proportional_millionths: msg.contents.fee_proportional_millionths,
@@ -773,7 +777,7 @@ mod tests {
773777
use chain::chaininterface;
774778
use ln::features::{ChannelFeatures, NodeFeatures};
775779
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
776-
use ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
780+
use ln::msgs::{OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
777781
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate};
778782
use util::test_utils;
779783
use util::logger::Logger;
@@ -1155,6 +1159,7 @@ mod tests {
11551159
flags: 0,
11561160
cltv_expiry_delta: 144,
11571161
htlc_minimum_msat: 1000000,
1162+
htlc_maximum_msat: OptionalField::Absent,
11581163
fee_base_msat: 10000,
11591164
fee_proportional_millionths: 20,
11601165
excess_data: Vec::new()
@@ -1289,6 +1294,7 @@ mod tests {
12891294
flags: 0,
12901295
cltv_expiry_delta: 144,
12911296
htlc_minimum_msat: 1000000,
1297+
htlc_maximum_msat: OptionalField::Absent,
12921298
fee_base_msat: 10000,
12931299
fee_proportional_millionths: 20,
12941300
excess_data: Vec::new()
@@ -1416,6 +1422,7 @@ mod tests {
14161422
flags: 0,
14171423
cltv_expiry_delta: 144,
14181424
htlc_minimum_msat: 1000000,
1425+
htlc_maximum_msat: OptionalField::Absent,
14191426
fee_base_msat: 10000,
14201427
fee_proportional_millionths: 20,
14211428
excess_data: Vec::new()
@@ -1452,6 +1459,7 @@ mod tests {
14521459
flags: 0,
14531460
cltv_expiry_delta: 144,
14541461
htlc_minimum_msat: 1000000,
1462+
htlc_maximum_msat: OptionalField::Absent,
14551463
fee_base_msat: 10000,
14561464
fee_proportional_millionths: 20,
14571465
excess_data: [1; 3].to_vec()

0 commit comments

Comments
 (0)