Skip to content

Commit

Permalink
Merge pull request #1050 from get10101/feat/reconfigure-channels-afte…
Browse files Browse the repository at this point in the history
…r-update

Update `ChannelConfig` of open channels
  • Loading branch information
luckysori authored Aug 2, 2023
2 parents de4437d + ea24f8a commit e9dfc4a
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 37 deletions.
3 changes: 1 addition & 2 deletions coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ impl Node {
}

pub fn update_ldk_settings(&self, ldk_config: UserConfig) {
tracing::info!(?ldk_config, "Updating LDK settings");
*self.inner.channel_config.write() = ldk_config;
self.inner.update_ldk_settings(ldk_config)
}

/// Returns true or false, whether we can find an usable channel with the provided trader.
Expand Down
12 changes: 6 additions & 6 deletions crates/ln-dlc-node/src/ln/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct EventHandler<S> {
peer_manager: Arc<PeerManager>,
fee_rate_estimator: Arc<FeeRateEstimator>,
event_sender: Option<watch::Sender<Option<Event>>>,
channel_config: Arc<parking_lot::RwLock<UserConfig>>,
ldk_config: Arc<parking_lot::RwLock<UserConfig>>,
}

impl<S> EventHandler<S>
Expand All @@ -83,7 +83,7 @@ where
peer_manager: Arc<PeerManager>,
fee_rate_estimator: Arc<FeeRateEstimator>,
event_sender: Option<watch::Sender<Option<Event>>>,
channel_config: Arc<parking_lot::RwLock<UserConfig>>,
ldk_config: Arc<parking_lot::RwLock<UserConfig>>,
) -> Self {
Self {
channel_manager,
Expand All @@ -97,7 +97,7 @@ where
peer_manager,
fee_rate_estimator,
event_sender,
channel_config,
ldk_config,
}
}

Expand Down Expand Up @@ -718,8 +718,8 @@ where
.upsert_channel(shadow_channel.clone())
.context("Failed to upsert shadow channel")?;

let mut channel_config = *self.channel_config.read();
channel_config.channel_handshake_config.announced_channel = false;
let mut ldk_config = *self.ldk_config.read();
ldk_config.channel_handshake_config.announced_channel = false;

let temp_channel_id = self
.channel_manager
Expand All @@ -728,7 +728,7 @@ where
channel_value,
0,
shadow_channel.user_channel_id.to_u128(),
Some(channel_config),
Some(ldk_config),
)
.map_err(|e| anyhow!("Failed to open just in time channel: {e:?}"))?;

Expand Down
6 changes: 3 additions & 3 deletions crates/ln-dlc-node/src/node/channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn build(
explora_client: Arc<EsploraSyncClient<Arc<TracingLogger>>>,
logger: Arc<TracingLogger>,
chain_monitor: Arc<ChainMonitor>,
channel_config: UserConfig,
ldk_config: UserConfig,
network: bitcoin::Network,
persister: Arc<FilesystemPersister>,
router: Arc<Router>,
Expand All @@ -64,7 +64,7 @@ pub(crate) fn build(
keys_manager.clone(),
keys_manager.clone(),
keys_manager,
channel_config,
ldk_config,
ChainParameters {
network,
best_block: BestBlock::new(block_hash, height),
Expand All @@ -89,7 +89,7 @@ pub(crate) fn build(
ln_dlc_wallet,
router,
logger,
channel_config,
ldk_config,
channel_monitor_mut_references,
);
let channel_manager = <(BlockHash, ChannelManager)>::read(&mut file, read_args)
Expand Down
6 changes: 3 additions & 3 deletions crates/ln-dlc-node/src/node/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ where
.expect("Mutex to not be poisoned")
.insert(intercept_scid, target_node);

let channel_config = self.channel_config.read();
let ldk_config = self.ldk_config.read();

let route_hint_hop = RouteHintHop {
src_node_id: self.info.pubkey,
short_channel_id: intercept_scid,
fees: RoutingFees {
base_msat: channel_config.channel_config.forwarding_fee_base_msat,
proportional_millionths: channel_config
base_msat: ldk_config.channel_config.forwarding_fee_base_msat,
proportional_millionths: ldk_config
.channel_config
.forwarding_fee_proportional_millionths,
},
Expand Down
6 changes: 3 additions & 3 deletions crates/ln-dlc-node/src/node/ln_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ where
initial_send_amount_sats: u64,
public_channel: bool,
) -> Result<[u8; 32]> {
let mut channel_config = *self.channel_config.read();
channel_config.channel_handshake_config.announced_channel = public_channel;
let mut ldk_config = *self.ldk_config.read();
ldk_config.channel_handshake_config.announced_channel = public_channel;

let temp_channel_id = self
.channel_manager
Expand All @@ -31,7 +31,7 @@ where
channel_amount_sat,
initial_send_amount_sats * 1000,
0,
Some(channel_config),
Some(ldk_config),
)
.map_err(|e| anyhow!("{e:?}"))
.with_context(|| format!("Could not create channel with {counterparty_node_id}"))?;
Expand Down
46 changes: 35 additions & 11 deletions crates/ln-dlc-node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::NetworkGraph;
use crate::PeerManager;
use anyhow::Context;
use anyhow::Result;
use bitcoin::hashes::hex::ToHex;
use bitcoin::secp256k1::PublicKey;
use bitcoin::Network;
use dlc_messages::message_handler::MessageHandler as DlcMessageHandler;
Expand Down Expand Up @@ -120,7 +121,7 @@ pub struct Node<S> {
oracle: Arc<P2PDOracleClient>,
pub dlc_message_handler: Arc<DlcMessageHandler>,
storage: Arc<S>,
pub channel_config: Arc<parking_lot::RwLock<UserConfig>>,
pub ldk_config: Arc<parking_lot::RwLock<UserConfig>>,
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
Expand Down Expand Up @@ -194,7 +195,7 @@ where
oracle: OracleInfo,
event_sender: watch::Sender<Option<Event>>,
) -> Result<Self> {
let channel_config = app_config();
let ldk_config = app_config();
Node::new(
alias,
network,
Expand All @@ -209,7 +210,7 @@ where
esplora_server_url,
seed,
ephemeral_randomness,
channel_config,
ldk_config,
LnDlcNodeSettings::default(),
oracle.into(),
Some(event_sender),
Expand Down Expand Up @@ -237,12 +238,12 @@ where
oracle: OracleInfo,
event_sender: watch::Sender<Option<Event>>,
) -> Result<Self> {
let mut channel_config = coordinator_config();
let mut ldk_config = coordinator_config();

// TODO: The config `force_announced_channel_preference` has been temporarily disabled
// for testing purposes, as otherwise the app is not able to open a channel to the
// coordinator. Remove this config, once not needed anymore.
channel_config
ldk_config
.channel_handshake_limits
.force_announced_channel_preference = false;
Self::new(
Expand All @@ -256,7 +257,7 @@ where
esplora_server_url,
seed,
ephemeral_randomness,
channel_config,
ldk_config,
settings,
oracle.into(),
Some(event_sender),
Expand All @@ -281,7 +282,7 @@ where
esplora_server_url: String,
seed: Bip39Seed,
ephemeral_randomness: [u8; 32],
channel_config: UserConfig,
ldk_config: UserConfig,
settings: LnDlcNodeSettings,
oracle_client: P2PDOracleClient,
event_sender: Option<watch::Sender<Option<Event>>>,
Expand All @@ -300,7 +301,7 @@ where
alias: alias.to_string(),
});

let channel_config = Arc::new(parking_lot::RwLock::new(channel_config));
let ldk_config = Arc::new(parking_lot::RwLock::new(ldk_config));

if !data_dir.exists() {
std::fs::create_dir_all(data_dir)
Expand Down Expand Up @@ -405,7 +406,7 @@ where
esplora_client.clone(),
logger.clone(),
chain_monitor.clone(),
*channel_config.read(),
*ldk_config.read(),
network,
persister.clone(),
router,
Expand Down Expand Up @@ -546,7 +547,7 @@ where
peer_manager.clone(),
fee_rate_estimator.clone(),
event_sender,
channel_config.clone(),
ldk_config.clone(),
);

// Connection manager
Expand Down Expand Up @@ -761,7 +762,7 @@ where
dlc_manager,
storage: node_storage,
fee_rate_estimator,
channel_config,
ldk_config,
_background_processor_handle: background_processor_handle,
_connection_manager_handle: connection_manager_handle,
_broadcast_node_announcement_handle: broadcast_node_announcement_handle,
Expand All @@ -770,6 +771,29 @@ where
settings,
})
}

pub fn update_ldk_settings(&self, ldk_config: UserConfig) {
tracing::debug!("Updating LDK settings");
*self.ldk_config.write() = ldk_config;

tracing::info!(?ldk_config, "Updated LDK settings");

for channel in self.list_channels() {
let channel_id = channel.channel_id;
let peer_id = channel.counterparty.node_id;
if let Err(e) = self.channel_manager.update_channel_config(
&peer_id,
&[channel_id],
&ldk_config.channel_config,
) {
tracing::error!(
channel_id = %channel_id.to_hex(),
%peer_id,
"Failed to apply new channel configuration: {e:?}"
);
}
}
}
}

impl Display for NodeInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async fn ln_force_close() {
// Mine enough blocks so that the payee's revocable output in the commitment transaction
// is spendable
let our_to_self_delay = coordinator
.channel_config
.ldk_config
.read()
.channel_handshake_config
.our_to_self_delay;
Expand Down
4 changes: 2 additions & 2 deletions crates/ln-dlc-node/src/tests/just_in_time_channel/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub(crate) async fn send_interceptable_payment(
let invoice_amount_msat = invoice.amount_milli_satoshis().unwrap();

let routing_fee_msat = calculate_routing_fee_msat(
coordinator.channel_config.read().channel_config,
coordinator.ldk_config.read().channel_config,
invoice_amount_sat,
);

Expand Down Expand Up @@ -301,7 +301,7 @@ fn does_inbound_htlc_fit_as_percent_of_channel(

let max_inbound_htlc_as_percent_of_channel = Decimal::from(
receiving_node
.channel_config
.ldk_config
.read()
.channel_handshake_config
.max_inbound_htlc_value_in_flight_percent_of_channel,
Expand Down
2 changes: 1 addition & 1 deletion crates/ln-dlc-node/src/tests/just_in_time_channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod channel_close;
mod create;
mod fail_intercepted_htlc;
mod multiple_payments;
mod payments;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::node::Node;
use crate::tests::calculate_routing_fee_msat;
use crate::tests::init_tracing;
use crate::tests::just_in_time_channel::create::send_interceptable_payment;
use crate::tests::setup_coordinator_payer_channel;
Expand Down Expand Up @@ -55,3 +56,59 @@ async fn just_in_time_channel_with_multiple_payments() {
assert_eq!(coordinator.channel_manager.list_channels().len(), 2);
}
}

#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn new_config_affects_routing_fees() {
init_tracing();

// Arrange

let payer = Node::start_test_app("payer").unwrap();
let coordinator = Node::start_test_coordinator("coordinator").unwrap();
let payee = Node::start_test_app("payee").unwrap();

payer.connect(coordinator.info).await.unwrap();
payee.connect(coordinator.info).await.unwrap();

let opening_invoice_amount = 10_000;
let expected_coordinator_payee_channel_value =
setup_coordinator_payer_channel(opening_invoice_amount, &coordinator, &payer).await;

send_interceptable_payment(
&payer,
&payee,
&coordinator,
opening_invoice_amount,
Some(expected_coordinator_payee_channel_value),
)
.await
.unwrap();

// Act

let coordinator_balance_before = coordinator.get_ldk_balance().available_msat();

let mut ldk_config_coordinator = *coordinator.ldk_config.read();

ldk_config_coordinator
.channel_config
.forwarding_fee_proportional_millionths *= 10;

coordinator.update_ldk_settings(ldk_config_coordinator);

let payment_amount_sat = 5_000;
send_interceptable_payment(&payer, &payee, &coordinator, payment_amount_sat, None)
.await
.unwrap();

// Assert

let coordinator_balance_after = coordinator.get_ldk_balance().available_msat();
let routing_fee_charged_msat = coordinator_balance_after - coordinator_balance_before;

let routing_fee_expected_msat =
calculate_routing_fee_msat(ldk_config_coordinator.channel_config, payment_amount_sat);

assert_eq!(routing_fee_charged_msat, routing_fee_expected_msat);
}
6 changes: 3 additions & 3 deletions crates/ln-dlc-node/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Node<InMemoryStore> {

fn start_test(
name: &str,
channel_config: UserConfig,
ldk_config: UserConfig,
esplora_origin: String,
oracle: OracleInfo,
storage: Arc<InMemoryStore>,
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Node<InMemoryStore> {
esplora_origin,
seed,
ephemeral_randomness,
channel_config,
ldk_config,
settings,
oracle.into(),
ldk_event_sender,
Expand Down Expand Up @@ -258,7 +258,7 @@ impl Node<InMemoryStore> {

let (does_manually_accept_inbound_channels, required_confirmations) =
block_in_place(|| {
let config = peer.channel_config.read();
let config = peer.ldk_config.read();

(
config.manually_accept_inbound_channels,
Expand Down
4 changes: 2 additions & 2 deletions crates/ln-dlc-node/src/tests/multi_hop_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn multi_hop_payment() {
let invoice_amount_msat = invoice.amount_milli_satoshis().unwrap();

let routing_fee_msat = calculate_routing_fee_msat(
coordinator.channel_config.read().channel_config,
coordinator.ldk_config.read().channel_config,
invoice_amount_sat,
);

Expand Down Expand Up @@ -104,7 +104,7 @@ async fn multi_hop_payment() {
/// coins as possible to their peer on channel creation.
fn min_outbound_liquidity_channel_creator(peer: &Node<InMemoryStore>, peer_balance: u64) -> u64 {
let min_reserve_millionths_creator = Decimal::from(
peer.channel_config
peer.ldk_config
.read()
.channel_handshake_config
.their_channel_reserve_proportional_millionths,
Expand Down

0 comments on commit e9dfc4a

Please sign in to comment.