Skip to content

Commit 0d60b0c

Browse files
committed
rebase to new master
1 parent 0112394 commit 0d60b0c

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

src/ln/channel.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use util::ser::Writeable;
2626
use util::sha2::Sha256;
2727
use util::logger::Logger;
2828
use util::errors::APIError;
29+
use util::configurations::UserConfigurations;
2930

3031
use std;
3132
use std::default::Default;
@@ -259,11 +260,14 @@ const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 |
259260

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

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

269273
channel_id: [u8; 32],
@@ -403,7 +407,7 @@ impl Channel {
403407
}
404408

405409
// Constructors:
406-
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> {
410+
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> {
407411
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
408412
return Err(APIError::APIMisuseError{err: "funding value > 2^24"});
409413
}
@@ -430,7 +434,7 @@ impl Channel {
430434

431435
Ok(Channel {
432436
user_id: user_id,
433-
437+
config : configurations.clone(),
434438
channel_id: rng::rand_u832(),
435439
channel_state: ChannelState::OurInitSent as u32,
436440
channel_outbound: true,
@@ -500,7 +504,7 @@ impl Channel {
500504
/// Assumes chain_hash has already been checked and corresponds with what we expect!
501505
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
502506
/// that we're rejecting the new channel.
503-
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> {
507+
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> {
504508
macro_rules! return_error_message {
505509
( $msg: expr ) => {
506510
return Err(HandleError{err: $msg, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: $msg.to_string() }})});
@@ -539,6 +543,26 @@ impl Channel {
539543
if msg.max_accepted_htlcs > 483 {
540544
return_error_message!("max_accpted_htlcs > 483");
541545
}
546+
//optional parameter checking
547+
// MAY fail the channel if
548+
if msg.funding_satoshis < configurations.channel_limits.funding_satoshis {
549+
return_error_message!("funding satoshis is less than the user specified limit");
550+
}
551+
if msg.htlc_minimum_msat > configurations.channel_limits.htlc_minimum_msat {
552+
return_error_message!("htlc minimum msat is higher than the user specified limit");
553+
}
554+
if msg.max_htlc_value_in_flight_msat < configurations.channel_limits.max_htlc_value_in_flight_msat {
555+
return_error_message!("max htlc value in flight msat is less than the user specified limit");
556+
}
557+
if msg.channel_reserve_satoshis > configurations.channel_limits.channel_reserve_satoshis {
558+
return_error_message!("channel reserve satoshis is higher than the user specified limit");
559+
}
560+
if msg.max_accepted_htlcs < configurations.channel_limits.max_accepted_htlcs {
561+
return_error_message!("max accepted htlcs is less than the user specified limit");
562+
}
563+
if msg.dust_limit_satoshis < configurations.channel_limits.dust_limit_satoshis {
564+
return_error_message!("dust limit satoshis is less than the user specified limit");
565+
}
542566

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

@@ -589,7 +613,7 @@ impl Channel {
589613

590614
let mut chan = Channel {
591615
user_id: user_id,
592-
616+
config: (*configurations).clone(),
593617
channel_id: msg.temporary_channel_id,
594618
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
595619
channel_outbound: false,
@@ -1245,15 +1269,6 @@ impl Channel {
12451269
return_error_message!("max_accpted_htlcs > 483");
12461270
}
12471271

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

12591274
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
@@ -2872,6 +2887,7 @@ mod tests {
28722887

28732888
#[test]
28742889
fn outbound_commitment_test() {
2890+
use util::configurations::UserConfigurations;
28752891
// Test vectors from BOLT 3 Appendix C:
28762892
let feeest = TestFeeEstimator{fee_est: 15000};
28772893
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
@@ -2893,7 +2909,7 @@ mod tests {
28932909
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);
28942910

28952911
let their_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
2896-
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
2912+
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
28972913
chan.their_to_self_delay = 144;
28982914
chan.our_dust_limit_satoshis = 546;
28992915

src/ln/channelmanager.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use ln::channelmonitor::ManyChannelMonitor;
2727
use ln::router::{Route,RouteHop};
2828
use ln::msgs;
2929
use ln::msgs::{HandleError,ChannelMessageHandler};
30+
use util::configurations::UserConfigurations;
3031
use util::{byte_utils, events, internal_traits, rng};
3132
use util::sha2::Sha256;
3233
use util::ser::{Readable, Writeable};
@@ -229,6 +230,7 @@ const ERR: () = "You need at least 32 bit pointers (well, usize, but we'll assum
229230
/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
230231
/// to individual Channels.
231232
pub struct ChannelManager {
233+
configuration : UserConfigurations,
232234
genesis_hash: Sha256dHash,
233235
fee_estimator: Arc<FeeEstimator>,
234236
monitor: Arc<ManyChannelMonitor>,
@@ -301,6 +303,7 @@ impl ChannelManager {
301303
let secp_ctx = Secp256k1::new();
302304

303305
let res = Arc::new(ChannelManager {
306+
configuration : UserConfigurations::new(),
304307
genesis_hash: genesis_block(network).header.bitcoin_hash(),
305308
fee_estimator: feeest.clone(),
306309
monitor: monitor.clone(),
@@ -362,7 +365,7 @@ impl ChannelManager {
362365
}
363366
};
364367

365-
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))?;
368+
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)?;
366369
let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator);
367370
let mut channel_state = self.channel_state.lock().unwrap();
368371
match channel_state.by_id.insert(channel.channel_id(), channel) {
@@ -1457,7 +1460,7 @@ impl ChannelManager {
14571460
}
14581461
};
14591462

1460-
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))?;
1463+
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))?;
14611464
let accept_msg = channel.get_accept_channel();
14621465
channel_state.by_id.insert(channel.channel_id(), channel);
14631466
Ok(accept_msg)
@@ -1483,8 +1486,7 @@ impl ChannelManager {
14831486
pending_events.push(events::Event::FundingGenerationReady {
14841487
temporary_channel_id: msg.temporary_channel_id,
14851488
channel_value_satoshis: value,
1486-
output_script: output_script,
1487-
user_channel_id: user_id,
1489+
output_script: output_script, user_channel_id: user_id,
14881490
});
14891491
Ok(())
14901492
}

src/ln/router.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ impl std::fmt::Display for ChannelInfo {
7777
}
7878
}
7979

80+
81+
8082
struct NodeInfo {
8183
#[cfg(feature = "non_bitcoin_chain_hash_routing")]
8284
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
@@ -28,3 +28,6 @@ pub use self::rng::reset_rng_state;
2828

2929
#[cfg(test)]
3030
pub(crate) mod test_utils;
31+
32+
pub use self::configurations::{UserConfigurations, ChannelLimits};
33+
pub mod configurations;

0 commit comments

Comments
 (0)