Skip to content

Commit 96889fd

Browse files
committed
Avoid storing config object in Channel
1 parent c5ec561 commit 96889fd

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

src/ln/channel.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +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,ChannelLimits,ChannelOptions};
29+
use util::configurations::UserConfigurations;
3030

3131
use std;
3232
use std::default::Default;
@@ -266,14 +266,13 @@ const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
266266
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
267267
// inbound channel.
268268
pub(super) struct Channel {
269-
config : UserConfigurations,
270-
271269
user_id: u64,
272270

273271
channel_id: [u8; 32],
274272
channel_state: u32,
275273
channel_outbound: bool,
276274
secp_ctx: Secp256k1<secp256k1::All>,
275+
announce_publicly: bool,
277276
channel_value_satoshis: u64,
278277

279278
local_keys: ChannelKeys,
@@ -406,7 +405,7 @@ impl Channel {
406405
}
407406

408407
// Constructors:
409-
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, logger: Arc<Logger>, configurations: &UserConfigurations) -> Result<Channel, APIError> {
408+
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, logger: Arc<Logger>, config: &UserConfigurations) -> Result<Channel, APIError> {
410409
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
411410
return Err(APIError::APIMisuseError{err: "funding value > 2^24"});
412411
}
@@ -433,14 +432,12 @@ impl Channel {
433432

434433
Ok(Channel {
435434
user_id: user_id,
436-
config: UserConfigurations{
437-
channel_options: configurations.channel_options.clone(),
438-
channel_limits : Arc::clone(&configurations.channel_limits),},
439435

440436
channel_id: rng::rand_u832(),
441437
channel_state: ChannelState::OurInitSent as u32,
442438
channel_outbound: true,
443439
secp_ctx: secp_ctx,
440+
announce_publicly: config.channel_options.announced_channel,
444441
channel_value_satoshis: channel_value_satoshis,
445442

446443
local_keys: chan_keys,
@@ -511,7 +508,6 @@ impl Channel {
511508
return Err(HandleError{err: $msg, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: $msg.to_string() }})});
512509
}
513510
}
514-
let mut local_config = (*configurations).channel_options.clone();
515511

516512
// Check sanity of message fields:
517513
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
@@ -569,16 +565,14 @@ impl Channel {
569565
// Convert things into internal flags and prep our state:
570566

571567
let their_announce = if (msg.channel_flags & 1) == 1 { true } else { false };
572-
if local_config.force_announced_channel_preference{
573-
if local_config.announced_channel && !their_announce {
568+
if configurations.channel_options.force_announced_channel_preference{
569+
if configurations.channel_options.announced_channel && !their_announce {
574570
return_error_message!("Peer tried to open unannounced channel, but we require public ones");
575571
}
576-
if !local_config.announced_channel && their_announce {
572+
if !configurations.channel_options.announced_channel && their_announce {
577573
return_error_message!("Peer tried to open announced channel, but we require private ones");
578574
}
579575
}
580-
//we either accept their preference or the preferences match
581-
local_config.announced_channel = their_announce;
582576

583577
let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
584578

@@ -619,14 +613,12 @@ impl Channel {
619613

620614
let mut chan = Channel {
621615
user_id: user_id,
622-
config: UserConfigurations{
623-
channel_options: local_config,
624-
channel_limits : Arc::clone(&configurations.channel_limits),},
625616

626617
channel_id: msg.temporary_channel_id,
627618
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
628619
channel_outbound: false,
629620
secp_ctx: secp_ctx,
621+
announce_publicly: their_announce,
630622

631623
local_keys: chan_keys,
632624
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
@@ -1233,7 +1225,7 @@ impl Channel {
12331225

12341226
// Message handlers:
12351227

1236-
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
1228+
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfigurations) -> Result<(), HandleError> {
12371229
macro_rules! return_error_message {
12381230
( $msg: expr ) => {
12391231
return Err(HandleError{err: $msg, action: Some(msgs::ErrorAction::SendErrorMessage{ msg: msgs::ErrorMessage { channel_id: msg.temporary_channel_id, data: $msg.to_string() }})});
@@ -1277,7 +1269,7 @@ impl Channel {
12771269
return_error_message!("max_accpted_htlcs > 483");
12781270
}
12791271
//Optional user definined limits
1280-
if msg.minimum_depth > self.config.channel_limits.minimum_depth {
1272+
if msg.minimum_depth > config.channel_limits.minimum_depth {
12811273
return_error_message!("We consider the minimum depth to be unreasonably large");
12821274
}
12831275
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
@@ -2268,7 +2260,7 @@ impl Channel {
22682260
}
22692261

22702262
pub fn should_announce(&self) -> bool {
2271-
self.config.channel_options.announced_channel
2263+
self.announce_publicly
22722264
}
22732265

22742266
/// Gets the fee we'd want to charge for adding an HTLC output to this Channel
@@ -2454,7 +2446,7 @@ impl Channel {
24542446
delayed_payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key),
24552447
htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
24562448
first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
2457-
channel_flags: if self.config.channel_options.announced_channel {1} else {0},
2449+
channel_flags: if self.announce_publicly {1} else {0},
24582450
shutdown_scriptpubkey: None,
24592451
}
24602452
}
@@ -2558,7 +2550,7 @@ impl Channel {
25582550
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
25592551
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
25602552
pub fn get_channel_announcement(&self, our_node_id: PublicKey, chain_hash: Sha256dHash) -> Result<(msgs::UnsignedChannelAnnouncement, Signature), HandleError> {
2561-
if !self.config.channel_options.announced_channel {
2553+
if !self.announce_publicly {
25622554
return Err(HandleError{err: "Channel is not available for public announcements", action: Some(msgs::ErrorAction::IgnoreError)});
25632555
}
25642556
if self.channel_state & (ChannelState::ChannelFunded as u32) == 0 {

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ impl ChannelManager {
14691469
//TODO: see issue #153, need a consistent behavior on obnoxious behavior from random node
14701470
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.temporary_channel_id));
14711471
}
1472-
chan.accept_channel(&msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
1472+
chan.accept_channel(&msg, &self.configuration).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
14731473
(chan.get_value_satoshis(), chan.get_funding_redeemscript().to_v0_p2wsh(), chan.get_user_id())
14741474
},
14751475
//TODO: same as above

0 commit comments

Comments
 (0)