Skip to content

Commit 9a3e171

Browse files
committed
rebase to new master
1 parent 97488ad commit 9a3e171

File tree

7 files changed

+83
-22
lines changed

7 files changed

+83
-22
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ use lightning::chain::transaction::OutPoint;
1616
use lightning::util::reset_rng_state;
1717
use lightning::util::logger::Logger;
1818
use lightning::util::ser::{Readable, Reader};
19+
use lightning::util::configurations::UserConfigurations;
20+
1921

2022
mod utils;
2123

2224
use utils::test_logger;
23-
2425
use secp256k1::key::{PublicKey, SecretKey};
2526
use secp256k1::Secp256k1;
2627

@@ -193,7 +194,7 @@ pub fn do_test(data: &[u8]) {
193194
let mut channel = if get_slice!(1)[0] != 0 {
194195
let chan_value = slice_to_be24(get_slice!(3));
195196

196-
let mut chan = match Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, slice_to_be24(get_slice!(3)), get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger)) {
197+
let mut chan = match Channel::new_outbound(&fee_est, chan_keys!(), their_pubkey, chan_value, slice_to_be24(get_slice!(3)), get_slice!(1)[0] == 0, slice_to_be64(get_slice!(8)), Arc::clone(&logger), &UserConfigurations::new()) {
197198
Ok(chan) => chan,
198199
Err(_) => return,
199200
};
@@ -218,7 +219,7 @@ pub fn do_test(data: &[u8]) {
218219
} else {
219220
decode_msg!(msgs::OpenChannel, 2*32+6*8+4+2*2+6*33+1)
220221
};
221-
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger)) {
222+
let mut chan = match Channel::new_from_req(&fee_est, chan_keys!(), their_pubkey, &open_chan, slice_to_be64(get_slice!(8)), false, get_slice!(1)[0] == 0, Arc::clone(&logger),&UserConfigurations::new()) {
222223
Ok(chan) => chan,
223224
Err(_) => return,
224225
};

src/ln/channel.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use util::{transaction_utils,rng};
2525
use util::sha2::Sha256;
2626
use util::logger::Logger;
2727
use util::errors::APIError;
28+
use util::configurations::UserConfigurations;
2829

2930
use std;
3031
use std::default::Default;
@@ -258,11 +259,14 @@ const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 |
258259

259260
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
260261

262+
261263
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
262264
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
263265
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
264266
// inbound channel.
265267
pub struct Channel {
268+
269+
config : UserConfigurations,
266270
user_id: u64,
267271

268272
channel_id: [u8; 32],
@@ -402,7 +406,7 @@ impl Channel {
402406
}
403407

404408
// Constructors:
405-
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Result<Channel, APIError> {
409+
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>, configurations: &UserConfigurations) -> Result<Channel, APIError> {
406410
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
407411
return Err(APIError::APIMisuseError{err: "funding value > 2^24"});
408412
}
@@ -429,7 +433,7 @@ impl Channel {
429433

430434
Ok(Channel {
431435
user_id: user_id,
432-
436+
config : configurations.clone(),
433437
channel_id: rng::rand_u832(),
434438
channel_state: ChannelState::OurInitSent as u32,
435439
channel_outbound: true,
@@ -499,7 +503,7 @@ impl Channel {
499503
/// Assumes chain_hash has already been checked and corresponds with what we expect!
500504
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
501505
/// that we're rejecting the new channel.
502-
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
506+
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, require_announce: bool, allow_announce: bool, logger: Arc<Logger>, configurations : &UserConfigurations) -> Result<Channel, HandleError> {
503507
macro_rules! return_error_message {
504508
( $msg: expr ) => {
505509
return Err(HandleError{err: $msg, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: $msg.to_string() }})});
@@ -538,6 +542,26 @@ impl Channel {
538542
if msg.max_accepted_htlcs > 483 {
539543
return_error_message!("max_accpted_htlcs > 483");
540544
}
545+
//optional parameter checking
546+
// MAY fail the channel if
547+
if msg.funding_satoshis < configurations.channel_limits.funding_satoshis {
548+
return_error_message!("funding satoshis is less than the user specified limit");
549+
}
550+
if msg.htlc_minimum_msat > configurations.channel_limits.htlc_minimum_msat {
551+
return_error_message!("htlc minimum msat is higher than the user specified limit");
552+
}
553+
if msg.max_htlc_value_in_flight_msat < configurations.channel_limits.max_htlc_value_in_flight_msat {
554+
return_error_message!("max htlc value in flight msat is less than the user specified limit");
555+
}
556+
if msg.channel_reserve_satoshis > configurations.channel_limits.channel_reserve_satoshis {
557+
return_error_message!("channel reserve satoshis is higher than the user specified limit");
558+
}
559+
if msg.max_accepted_htlcs < configurations.channel_limits.max_accepted_htlcs {
560+
return_error_message!("max accepted htlcs is less than the user specified limit");
561+
}
562+
if msg.dust_limit_satoshis < configurations.channel_limits.dust_limit_satoshis {
563+
return_error_message!("dust limit satoshis is less than the user specified limit");
564+
}
541565

542566
// Convert things into internal flags and prep our state:
543567

@@ -588,7 +612,7 @@ impl Channel {
588612

589613
let mut chan = Channel {
590614
user_id: user_id,
591-
615+
config: (*configurations).clone(),
592616
channel_id: msg.temporary_channel_id,
593617
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
594618
channel_outbound: false,
@@ -1244,15 +1268,6 @@ impl Channel {
12441268
return_error_message!("max_accpted_htlcs > 483");
12451269
}
12461270

1247-
// TODO: Optional additional constraints mentioned in the spec
1248-
// MAY fail the channel if
1249-
// funding_satoshi is too small
1250-
// htlc_minimum_msat too large
1251-
// max_htlc_value_in_flight_msat too small
1252-
// channel_reserve_satoshis too large
1253-
// max_accepted_htlcs too small
1254-
// dust_limit_satoshis too small
1255-
12561271
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
12571272

12581273
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
@@ -2866,6 +2881,7 @@ mod tests {
28662881

28672882
#[test]
28682883
fn outbound_commitment_test() {
2884+
use util::configurations::UserConfigurations;
28692885
// Test vectors from BOLT 3 Appendix C:
28702886
let feeest = TestFeeEstimator{fee_est: 15000};
28712887
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
@@ -2887,7 +2903,7 @@ mod tests {
28872903
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);
28882904

28892905
let their_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
2890-
let mut chan = Channel::new_outbound(&feeest, chan_keys, their_node_id, 10000000, 100000, false, 42, Arc::clone(&logger)).unwrap(); // Nothing uses their network key in this test
2906+
let mut chan = Channel::new_outbound(&feeest, chan_keys, their_node_id, 10000000, 100000, false, 42, Arc::clone(&logger), &UserConfigurations::new()).unwrap(); // Nothing uses their network key in this test
28912907
chan.their_to_self_delay = 144;
28922908
chan.our_dust_limit_satoshis = 546;
28932909

src/ln/channelmanager.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use ln::channelmonitor::ManyChannelMonitor;
1717
use ln::router::{Route,RouteHop};
1818
use ln::msgs;
1919
use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable};
20+
use util::configurations::UserConfigurations;
2021
use util::{byte_utils, events, internal_traits, rng};
2122
use util::sha2::Sha256;
2223
use util::chacha20poly1305rfc::ChaCha20;
@@ -245,6 +246,7 @@ const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assum
245246
/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
246247
/// to individual Channels.
247248
pub struct ChannelManager {
249+
configuration : UserConfigurations,
248250
genesis_hash: Sha256dHash,
249251
fee_estimator: Arc<FeeEstimator>,
250252
monitor: Arc<ManyChannelMonitor>,
@@ -310,6 +312,7 @@ impl ChannelManager {
310312
let secp_ctx = Secp256k1::new();
311313

312314
let res = Arc::new(ChannelManager {
315+
configuration : UserConfigurations::new(),
313316
genesis_hash: genesis_block(network).header.bitcoin_hash(),
314317
fee_estimator: feeest.clone(),
315318
monitor: monitor.clone(),
@@ -368,7 +371,7 @@ impl ChannelManager {
368371
}
369372
};
370373

371-
let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, push_msat, self.announce_channels_publicly, user_id, Arc::clone(&self.logger))?;
374+
let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, push_msat, self.announce_channels_publicly, user_id, Arc::clone(&self.logger), &self.configuration)?;
372375
let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator);
373376
let mut channel_state = self.channel_state.lock().unwrap();
374377
match channel_state.by_id.insert(channel.channel_id(), channel) {
@@ -1455,7 +1458,7 @@ impl ChannelManager {
14551458
}
14561459
};
14571460

1458-
let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, false, self.announce_channels_publicly, Arc::clone(&self.logger)).map_err(|e| MsgHandleErrInternal::from_no_close(e))?;
1461+
let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, false, self.announce_channels_publicly, Arc::clone(&self.logger), &self.configuration).map_err(|e| MsgHandleErrInternal::from_no_close(e))?;
14591462
let accept_msg = channel.get_accept_channel();
14601463
channel_state.by_id.insert(channel.channel_id(), channel);
14611464
Ok(accept_msg)
@@ -1481,8 +1484,7 @@ impl ChannelManager {
14811484
pending_events.push(events::Event::FundingGenerationReady {
14821485
temporary_channel_id: msg.temporary_channel_id,
14831486
channel_value_satoshis: value,
1484-
output_script: output_script,
1485-
user_channel_id: user_id,
1487+
output_script: output_script, user_channel_id: user_id,
14861488
});
14871489
Ok(())
14881490
}

src/ln/msgs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl MsgDecodable for OpenChannel {
705705
let mut temp_channel_id = [0; 32];
706706
temp_channel_id[..].copy_from_slice(&v[32..64]);
707707
Ok(OpenChannel {
708-
chain_hash: deserialize(&v[0..32]).unwrap(),
708+
chain_hash: deserialize(&v[0..32]).unwrap(), //panic should not be possible as we are deseriliazing a hash
709709
temporary_channel_id: temp_channel_id,
710710
funding_satoshis: byte_utils::slice_to_be64(&v[64..72]),
711711
push_msat: byte_utils::slice_to_be64(&v[72..80]),

src/ln/router.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ impl std::fmt::Display for ChannelInfo {
7070
}
7171
}
7272

73+
74+
7375
struct NodeInfo {
7476
#[cfg(feature = "non_bitcoin_chain_hash_routing")]
7577
channels: Vec<(u64, Sha256dHash)>,

src/util/configurations.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#[derive(Copy, Clone)]
2+
pub struct UserConfigurations{
3+
pub channel_limits : ChannelLimits,
4+
}
5+
6+
impl UserConfigurations {
7+
pub fn new() -> Self{
8+
UserConfigurations {
9+
channel_limits : ChannelLimits::new(),
10+
}
11+
}
12+
}
13+
14+
#[derive(Copy, Clone)]
15+
pub struct ChannelLimits
16+
{
17+
pub funding_satoshis :u64,
18+
pub htlc_minimum_msat : u64,
19+
pub max_htlc_value_in_flight_msat : u64,
20+
pub channel_reserve_satoshis : u64,
21+
pub max_accepted_htlcs : u16,
22+
pub dust_limit_satoshis : u64,
23+
}
24+
25+
impl ChannelLimits {
26+
//creating max and min possible values because if they are not set, means we should not check them.
27+
pub fn new() -> Self{
28+
ChannelLimits {
29+
funding_satoshis : 0,
30+
htlc_minimum_msat : <u64>::max_value(),
31+
max_htlc_value_in_flight_msat : 0,
32+
channel_reserve_satoshis : <u64>::max_value(),
33+
max_accepted_htlcs : 0,
34+
dust_limit_satoshis : 0,
35+
}
36+
}
37+
}

src/util/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ pub mod ser;
2929
pub(crate) mod ser;
3030

3131
pub mod logger;
32+
pub use self::configurations::{UserConfigurations, ChannelLimits};
33+
pub mod configurations;
34+

0 commit comments

Comments
 (0)