From: Gleb Naumenko Date: Sun, 28 Jun 2020 11:43:10 +0000 (+0300) Subject: Add htlc_maximum_msat field X-Git-Tag: v0.0.12~46^2~2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=8b4f6e88611d280fa5758b7934c67539dec0fc23;p=rust-lightning Add htlc_maximum_msat field --- diff --git a/fuzz/src/bin/gen_target.sh b/fuzz/src/bin/gen_target.sh index 9b5629e7c..fb1f839a5 100755 --- a/fuzz/src/bin/gen_target.sh +++ b/fuzz/src/bin/gen_target.sh @@ -31,12 +31,12 @@ GEN_TEST msg_update_fee msg_targets:: GEN_TEST msg_update_fulfill_htlc msg_targets:: GEN_TEST msg_channel_announcement msg_targets:: -GEN_TEST msg_channel_update msg_targets:: GEN_TEST msg_node_announcement msg_targets:: GEN_TEST msg_update_add_htlc msg_targets:: GEN_TEST msg_error_message msg_targets:: -GEN_TEST msg_onion_hop_data msg_targets:: +GEN_TEST msg_channel_update msg_targets:: +GEN_TEST msg_onion_hop_data msg_targets:: GEN_TEST msg_ping msg_targets:: GEN_TEST msg_pong msg_targets:: diff --git a/fuzz/src/msg_targets/gen_target.sh b/fuzz/src/msg_targets/gen_target.sh index 0121e4eb8..b9381fc3a 100755 --- a/fuzz/src/msg_targets/gen_target.sh +++ b/fuzz/src/msg_targets/gen_target.sh @@ -29,11 +29,11 @@ GEN_TEST UpdateFee test_msg "" GEN_TEST UpdateFulfillHTLC test_msg "" GEN_TEST ChannelAnnouncement test_msg_exact "" -GEN_TEST ChannelUpdate test_msg_exact "" GEN_TEST NodeAnnouncement test_msg_exact "" GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33" GEN_TEST ErrorMessage test_msg_hole ", 32, 2" +GEN_TEST ChannelUpdate test_msg_hole ", 108, 1" GEN_TEST Init test_msg_simple "" GEN_TEST OnionHopData test_msg_simple "" diff --git a/fuzz/src/msg_targets/mod.rs b/fuzz/src/msg_targets/mod.rs index 69fc4e742..8c5dd6971 100644 --- a/fuzz/src/msg_targets/mod.rs +++ b/fuzz/src/msg_targets/mod.rs @@ -16,10 +16,10 @@ pub mod msg_update_fail_malformed_htlc; pub mod msg_update_fee; pub mod msg_update_fulfill_htlc; pub mod msg_channel_announcement; -pub mod msg_channel_update; pub mod msg_node_announcement; pub mod msg_update_add_htlc; pub mod msg_error_message; +pub mod msg_channel_update; pub mod msg_init; pub mod msg_onion_hop_data; pub mod msg_ping; diff --git a/fuzz/src/msg_targets/msg_channel_update.rs b/fuzz/src/msg_targets/msg_channel_update.rs index d0326bfa4..c428c1c76 100644 --- a/fuzz/src/msg_targets/msg_channel_update.rs +++ b/fuzz/src/msg_targets/msg_channel_update.rs @@ -8,11 +8,11 @@ use utils::test_logger; #[inline] pub fn msg_channel_update_test(data: &[u8], _out: Out) { - test_msg_exact!(msgs::ChannelUpdate, data); + test_msg_hole!(msgs::ChannelUpdate, data, 108, 1); } #[no_mangle] pub extern "C" fn msg_channel_update_run(data: *const u8, datalen: usize) { let data = unsafe { std::slice::from_raw_parts(data, datalen) }; - test_msg_exact!(msgs::ChannelUpdate, data); + test_msg_hole!(msgs::ChannelUpdate, data, 108, 1); } diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index 3a6b5bdf4..31e552f59 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -172,12 +172,12 @@ pub fn do_test(data: &[u8], out: Out) { let _ = net_graph_msg_handler.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4)); }, 2 => { - let _ = net_graph_msg_handler.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128)); + let _ = net_graph_msg_handler.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 136)); }, 3 => { match get_slice!(1)[0] { 0 => { - net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)}); + net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 136)}); }, 1 => { let short_channel_id = slice_to_be64(get_slice!(8)); diff --git a/fuzz/targets.h b/fuzz/targets.h index 430f614fb..fe94a3913 100644 --- a/fuzz/targets.h +++ b/fuzz/targets.h @@ -22,10 +22,10 @@ void msg_update_fail_malformed_htlc_run(const unsigned char* data, size_t data_l void msg_update_fee_run(const unsigned char* data, size_t data_len); void msg_update_fulfill_htlc_run(const unsigned char* data, size_t data_len); void msg_channel_announcement_run(const unsigned char* data, size_t data_len); -void msg_channel_update_run(const unsigned char* data, size_t data_len); void msg_node_announcement_run(const unsigned char* data, size_t data_len); void msg_update_add_htlc_run(const unsigned char* data, size_t data_len); void msg_error_message_run(const unsigned char* data, size_t data_len); +void msg_channel_update_run(const unsigned char* data, size_t data_len); void msg_onion_hop_data_run(const unsigned char* data, size_t data_len); void msg_ping_run(const unsigned char* data, size_t data_len); void msg_pong_run(const unsigned char* data, size_t data_len); diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index a0879d4e7..7a4c76e28 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -3135,6 +3135,18 @@ impl Channel { self.our_htlc_minimum_msat } + /// Allowed in any state (including after shutdown) + pub fn get_announced_htlc_max_msat(&self) -> u64 { + return cmp::min( + // Upper bound by capacity. We make it a bit less than full capacity to prevent attempts + // to use full capacity. This is an effort to reduce routing failures, because in many cases + // channel might have been used to route very small values (either by honest users or as DoS). + self.channel_value_satoshis * 9 / 10, + + Channel::::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis) + ); + } + /// Allowed in any state (including after shutdown) pub fn get_their_htlc_minimum_msat(&self) -> u64 { self.our_htlc_minimum_msat diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index d591515c4..5466b3d23 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -34,7 +34,7 @@ use ln::features::{InitFeatures, NodeFeatures}; use routing::router::{Route, RouteHop}; use ln::msgs; use ln::onion_utils; -use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError}; +use ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, OptionalField}; use chain::keysinterface::{ChannelKeys, KeysInterface, KeysManager, InMemoryChannelKeys}; use util::config::UserConfig; use util::{byte_utils, events}; @@ -1186,7 +1186,8 @@ impl res.extend_from_slice(&byte_utils::be32_to_array(msg.cltv_expiry)); } else if code == 0x1000 | 20 { - res.extend_from_slice(&byte_utils::be16_to_array(chan_update.contents.flags)); + // TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791 + res.extend_from_slice(&byte_utils::be16_to_array(0)); } res.extend_from_slice(&chan_update.encode_with_len()[..]); } @@ -1212,9 +1213,10 @@ impl chain_hash: self.genesis_hash, short_channel_id: short_channel_id, timestamp: chan.get_update_time_counter(), - flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1), + flags: (!were_node_one) as u8 | ((!chan.is_live() as u8) << 1), cltv_expiry_delta: CLTV_EXPIRY_DELTA, htlc_minimum_msat: chan.get_our_htlc_minimum_msat(), + htlc_maximum_msat: OptionalField::Present(chan.get_announced_htlc_max_msat()), fee_base_msat: chan.get_our_fee_base_msat(&self.fee_estimator), fee_proportional_millionths: chan.get_fee_proportional_millionths(), excess_data: Vec::new(), @@ -2494,7 +2496,8 @@ impl let reason = if let Ok(upd) = self.get_channel_update(chan) { onion_utils::build_first_hop_failure_packet(incoming_shared_secret, error_code, &{ let mut res = Vec::with_capacity(8 + 128); - res.extend_from_slice(&byte_utils::be16_to_array(upd.contents.flags)); + // TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791 + res.extend_from_slice(&byte_utils::be16_to_array(0)); res.extend_from_slice(&upd.encode_with_len()[..]); res }[..]) diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 4d2b155ce..9526189e7 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -15,7 +15,7 @@ use ln::{chan_utils, onion_utils}; use routing::router::{Route, RouteHop, get_route}; use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use ln::msgs; -use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, ErrorAction}; +use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, ErrorAction, OptionalField}; use util::enforcing_trait_impls::EnforcingChannelKeys; use util::{byte_utils, test_utils}; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; @@ -6058,6 +6058,7 @@ impl msgs::ChannelUpdate { flags: 0, cltv_expiry_delta: 0, htlc_minimum_msat: 0, + htlc_maximum_msat: OptionalField::Absent, fee_base_msat: 0, fee_proportional_millionths: 0, excess_data: vec![], diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index bd5d23504..554cf7290 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -427,9 +427,10 @@ pub(crate) struct UnsignedChannelUpdate { pub(crate) chain_hash: BlockHash, pub(crate) short_channel_id: u64, pub(crate) timestamp: u32, - pub(crate) flags: u16, + pub(crate) flags: u8, pub(crate) cltv_expiry_delta: u16, pub(crate) htlc_minimum_msat: u64, + pub(crate) htlc_maximum_msat: OptionalField, pub(crate) fee_base_msat: u32, pub(crate) fee_proportional_millionths: u32, pub(crate) excess_data: Vec, @@ -517,7 +518,7 @@ pub enum HTLCFailChannelUpdate { /// As we wish to serialize these differently from Options (Options get a tag byte, but /// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a /// separate enum type for them. -#[derive(Clone, PartialEq)] +#[derive(Clone, PartialEq, Debug)] pub enum OptionalField { /// Optional field is included in message Present(T), @@ -742,6 +743,26 @@ impl Readable for OptionalField