Skip to content

Commit 4d7ec22

Browse files
committed
Add htlc_maximum_msat field
1 parent 64d2fe0 commit 4d7ec22

File tree

8 files changed

+90
-7
lines changed

8 files changed

+90
-7
lines changed

lightning/src/ln/channel.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
358358
// get_remote_channel_reserve_satoshis(channel_value_sats: u64): u64
359359
their_htlc_minimum_msat: u64,
360360
our_htlc_minimum_msat: u64,
361+
our_htlc_maximum_msat: Option<u64>,
361362
their_to_self_delay: u16,
362363
our_to_self_delay: u16,
363364
#[cfg(test)]
@@ -535,6 +536,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
535536
local_channel_reserve_satoshis: 0,
536537
their_htlc_minimum_msat: 0,
537538
our_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
539+
our_htlc_maximum_msat: config.own_channel_config.our_htlc_maximum_msat,
538540
their_to_self_delay: 0,
539541
our_to_self_delay: config.own_channel_config.our_to_self_delay,
540542
their_max_accepted_htlcs: 0,
@@ -758,6 +760,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
758760
local_channel_reserve_satoshis: msg.channel_reserve_satoshis,
759761
their_htlc_minimum_msat: msg.htlc_minimum_msat,
760762
our_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
763+
our_htlc_maximum_msat: config.own_channel_config.our_htlc_maximum_msat,
761764
their_to_self_delay: msg.to_self_delay,
762765
our_to_self_delay: config.own_channel_config.our_to_self_delay,
763766
their_max_accepted_htlcs: msg.max_accepted_htlcs,
@@ -3127,6 +3130,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31273130
self.our_htlc_minimum_msat
31283131
}
31293132

3133+
/// Allowed in any state (including after shutdown)
3134+
pub fn get_our_htlc_maximum_msat(&self) -> Option<u64> {
3135+
self.our_htlc_maximum_msat
3136+
}
3137+
31303138
/// Allowed in any state (including after shutdown)
31313139
pub fn get_their_htlc_minimum_msat(&self) -> u64 {
31323140
self.our_htlc_minimum_msat
@@ -4195,6 +4203,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
41954203
self.local_channel_reserve_satoshis.write(writer)?;
41964204
self.their_htlc_minimum_msat.write(writer)?;
41974205
self.our_htlc_minimum_msat.write(writer)?;
4206+
self.our_htlc_maximum_msat.write(writer)?;
41984207
self.their_to_self_delay.write(writer)?;
41994208
self.our_to_self_delay.write(writer)?;
42004209
self.their_max_accepted_htlcs.write(writer)?;
@@ -4351,6 +4360,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
43514360
let local_channel_reserve_satoshis = Readable::read(reader)?;
43524361
let their_htlc_minimum_msat = Readable::read(reader)?;
43534362
let our_htlc_minimum_msat = Readable::read(reader)?;
4363+
let our_htlc_maximum_msat = Readable::read(reader)?;
43544364
let their_to_self_delay = Readable::read(reader)?;
43554365
let our_to_self_delay = Readable::read(reader)?;
43564366
let their_max_accepted_htlcs = Readable::read(reader)?;
@@ -4430,6 +4440,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
44304440
local_channel_reserve_satoshis,
44314441
their_htlc_minimum_msat,
44324442
our_htlc_minimum_msat,
4443+
our_htlc_maximum_msat,
44334444
their_to_self_delay,
44344445
our_to_self_delay,
44354446
their_max_accepted_htlcs,

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 1 deletion
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};
@@ -1214,6 +1214,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
12141214
flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1),
12151215
cltv_expiry_delta: CLTV_EXPIRY_DELTA,
12161216
htlc_minimum_msat: chan.get_our_htlc_minimum_msat(),
1217+
htlc_maximum_msat: if chan.get_our_htlc_maximum_msat().is_some() { OptionalField::Present(chan.get_our_htlc_maximum_msat().unwrap()) } else { OptionalField::Absent },
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: 32 additions & 2 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}
@@ -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: 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() }

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)