Skip to content

Commit

Permalink
feat: Update ChannelConfig of open channels
Browse files Browse the repository at this point in the history
In patch 2ddbee0 we mentioned that we wanted to be able to update the
`ChannelConfig` for open channels (specifically to be able to update
the routing fees charged).
  • Loading branch information
luckysori committed Aug 2, 2023
1 parent c63a776 commit ea24f8a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 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.ldk_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
24 changes: 24 additions & 0 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 @@ -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
57 changes: 57 additions & 0 deletions crates/ln-dlc-node/src/tests/just_in_time_channel/payments.rs
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);
}

0 comments on commit ea24f8a

Please sign in to comment.