Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call stop() on Node drop #84

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ interface Node {
[Throws=NodeError]
void connect(PublicKey node_id, SocketAddr address, boolean permanently);
[Throws=NodeError]
void disconnect([ByRef]PublicKey node_id);
void disconnect(PublicKey node_id);
[Throws=NodeError]
void connect_open_channel(PublicKey node_id, SocketAddr address, u64 channel_amount_sats, u64? push_to_counterparty_msat, boolean announce_channel);
[Throws=NodeError]
void close_channel([ByRef]ChannelId channel_id, [ByRef]PublicKey counterparty_node_id);
void close_channel([ByRef]ChannelId channel_id, PublicKey counterparty_node_id);
[Throws=NodeError]
void sync_wallets();
[Throws=NodeError]
PaymentHash send_payment([ByRef]Invoice invoice);
[Throws=NodeError]
PaymentHash send_payment_using_amount([ByRef]Invoice invoice, u64 amount_msat);
[Throws=NodeError]
PaymentHash send_spontaneous_payment(u64 amount_msat, [ByRef]PublicKey node_id);
PaymentHash send_spontaneous_payment(u64 amount_msat, PublicKey node_id);
[Throws=NodeError]
Invoice receive_payment(u64 amount_msat, [ByRef]string description, u32 expiry_secs);
[Throws=NodeError]
Expand Down
20 changes: 13 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ impl Node {
///
/// Will also remove the peer from the peer store, i.e., after this has been called we won't
/// try to reconnect on restart.
pub fn disconnect(&self, counterparty_node_id: &PublicKey) -> Result<(), Error> {
pub fn disconnect(&self, counterparty_node_id: PublicKey) -> Result<(), Error> {
let rt_lock = self.runtime.read().unwrap();
if rt_lock.is_none() {
return Err(Error::NotRunning);
Expand All @@ -968,7 +968,7 @@ impl Node {
}
}

self.peer_manager.disconnect_by_node_id(*counterparty_node_id);
self.peer_manager.disconnect_by_node_id(counterparty_node_id);
Ok(())
}

Expand Down Expand Up @@ -1126,10 +1126,10 @@ impl Node {

/// Close a previously opened channel.
pub fn close_channel(
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
&self, channel_id: &ChannelId, counterparty_node_id: PublicKey,
) -> Result<(), Error> {
self.peer_store.remove_peer(counterparty_node_id)?;
match self.channel_manager.close_channel(&channel_id.0, counterparty_node_id) {
self.peer_store.remove_peer(&counterparty_node_id)?;
match self.channel_manager.close_channel(&channel_id.0, &counterparty_node_id) {
Ok(_) => Ok(()),
Err(_) => Err(Error::ChannelClosingFailed),
}
Expand Down Expand Up @@ -1291,7 +1291,7 @@ impl Node {

/// Send a spontaneous, aka. "keysend", payment
pub fn send_spontaneous_payment(
&self, amount_msat: u64, node_id: &PublicKey,
&self, amount_msat: u64, node_id: PublicKey,
) -> Result<PaymentHash, Error> {
let rt_lock = self.runtime.read().unwrap();
if rt_lock.is_none() {
Expand All @@ -1303,7 +1303,7 @@ impl Node {

let route_params = RouteParameters {
payment_params: PaymentParameters::from_node_id(
*node_id,
node_id,
self.config.default_cltv_expiry_delta,
),
final_value_msat: amount_msat,
Expand Down Expand Up @@ -1440,6 +1440,12 @@ impl Node {
}
}

impl Drop for Node {
fn drop(&mut self) {
let _ = self.stop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the node is already stopped, this will return Error::NotRunning. I guess that is ok though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's why I'm ignoring the result here.

}
}

async fn connect_peer_if_necessary(
pubkey: PublicKey, peer_addr: SocketAddr, peer_manager: Arc<PeerManager>,
logger: Arc<FilesystemLogger>,
Expand Down
2 changes: 1 addition & 1 deletion src/test/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fn channel_full_cycle() {
assert_eq!(node_b.payment(&payment_hash).unwrap().direction, PaymentDirection::Inbound);
assert_eq!(node_b.payment(&payment_hash).unwrap().amount_msat, Some(determined_amount_msat));

node_b.close_channel(&channel_id, &node_a.node_id()).unwrap();
node_b.close_channel(&channel_id, node_a.node_id()).unwrap();
expect_event!(node_a, ChannelClosed);
expect_event!(node_b, ChannelClosed);

Expand Down