Skip to content

Commit

Permalink
Account for new ChannelId type
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Sep 11, 2023
1 parent a33e59c commit 2db9c6e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 52 deletions.
36 changes: 13 additions & 23 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
hex_utils, ChannelId, ChannelManager, Config, Error, KeysManager, NetworkGraph, UserChannelId,
Wallet,
hex_utils, ChannelManager, Config, Error, KeysManager, NetworkGraph, UserChannelId, Wallet,
};

use crate::payment_store::{
Expand All @@ -17,7 +16,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget,
use lightning::events::Event as LdkEvent;
use lightning::events::PaymentPurpose;
use lightning::impl_writeable_tlv_based_enum;
use lightning::ln::PaymentHash;
use lightning::ln::{ChannelId, PaymentHash};
use lightning::routing::gossip::NodeId;
use lightning::util::errors::APIError;
use lightning::util::persist::KVStore;
Expand Down Expand Up @@ -573,7 +572,7 @@ where
});
}
}
LdkEvent::SpendableOutputs { outputs } => {
LdkEvent::SpendableOutputs { outputs, channel_id: _ } => {
// TODO: We should eventually remember the outputs and supply them to the wallet's coin selection, once BDK allows us to do so.
let destination_address = self.wallet.get_new_address().unwrap_or_else(|e| {
log_error!(self.logger, "Failed to get destination address: {}", e);
Expand Down Expand Up @@ -664,7 +663,7 @@ where
let nodes = read_only_network_graph.nodes();
let channels = self.channel_manager.list_channels();

let node_str = |channel_id: &Option<[u8; 32]>| {
let node_str = |channel_id: &Option<ChannelId>| {
channel_id
.and_then(|channel_id| channels.iter().find(|c| c.channel_id == channel_id))
.and_then(|channel| {
Expand All @@ -678,11 +677,9 @@ where
})
})
};
let channel_str = |channel_id: &Option<[u8; 32]>| {
let channel_str = |channel_id: &Option<ChannelId>| {
channel_id
.map(|channel_id| {
format!(" with channel {}", hex_utils::to_string(&channel_id))
})
.map(|channel_id| format!(" with channel {}", channel_id))
.unwrap_or_default()
};
let from_prev_str = format!(
Expand Down Expand Up @@ -725,16 +722,14 @@ where
log_info!(
self.logger,
"New channel {} with counterparty {} has been created and is pending confirmation on chain.",
hex_utils::to_string(&channel_id),
channel_id,
counterparty_node_id,
);
self.event_queue
.add_event(Event::ChannelPending {
channel_id: ChannelId(channel_id),
channel_id,
user_channel_id: UserChannelId(user_channel_id),
former_temporary_channel_id: ChannelId(
former_temporary_channel_id.unwrap(),
),
former_temporary_channel_id: former_temporary_channel_id.unwrap(),
counterparty_node_id,
funding_txo,
})
Expand All @@ -749,12 +744,12 @@ where
log_info!(
self.logger,
"Channel {} with counterparty {} ready to be used.",
hex_utils::to_string(&channel_id),
channel_id,
counterparty_node_id,
);
self.event_queue
.add_event(Event::ChannelReady {
channel_id: ChannelId(channel_id),
channel_id,
user_channel_id: UserChannelId(user_channel_id),
counterparty_node_id: Some(counterparty_node_id),
})
Expand All @@ -770,15 +765,10 @@ where
counterparty_node_id,
..
} => {
log_info!(
self.logger,
"Channel {} closed due to: {:?}",
hex_utils::to_string(&channel_id),
reason
);
log_info!(self.logger, "Channel {} closed due to: {:?}", channel_id, reason);
self.event_queue
.add_event(Event::ChannelClosed {
channel_id: ChannelId(channel_id),
channel_id,
user_channel_id: UserChannelId(user_channel_id),
counterparty_node_id,
})
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ use payment_store::PaymentStore;
pub use payment_store::{PaymentDetails, PaymentDirection, PaymentStatus};
use peer_store::{PeerInfo, PeerStore};
use types::{ChainMonitor, ChannelManager, KeysManager, NetworkGraph, PeerManager, Router, Scorer};
pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId};
pub use types::{ChannelDetails, PeerDetails, UserChannelId};
use wallet::Wallet;

use logger::{log_debug, log_error, log_info, log_trace, FilesystemLogger, Logger};

use lightning::chain::Confirm;
use lightning::ln::channelmanager::{self, PaymentId, RecipientOnionFields, Retry};
use lightning::ln::{PaymentHash, PaymentPreimage};
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage};
use lightning::sign::EntropySource;

use lightning::util::persist::KVStore;
Expand Down Expand Up @@ -1032,7 +1032,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
&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) {
match self.channel_manager.close_channel(&channel_id, &counterparty_node_id) {
Ok(_) => Ok(()),
Err(_) => Err(Error::ChannelClosingFailed),
}
Expand All @@ -1046,7 +1046,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
self.channel_manager
.update_channel_config(
&counterparty_node_id,
&[channel_id.0],
&[*channel_id],
&(*channel_config).clone().into(),
)
.map_err(|_| Error::ChannelConfigUpdateFailed)
Expand Down
25 changes: 2 additions & 23 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lightning::ln::channelmanager::ChannelDetails as LdkChannelDetails;
use lightning::ln::msgs::RoutingMessageHandler;
use lightning::ln::msgs::SocketAddress as LdkSocketAddress;
use lightning::ln::peer_handler::IgnoringMessageHandler;
use lightning::ln::ChannelId;
use lightning::routing::gossip;
use lightning::routing::router::DefaultRouter;
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
Expand Down Expand Up @@ -106,28 +107,6 @@ impl lightning::onion_message::MessageRouter for FakeMessageRouter {
}
}

/// The global identifier of a channel.
///
/// Note that this will start out to be a temporary ID until channel funding negotiation is
/// finalized, at which point it will change to be a permanent global ID tied to the on-chain
/// funding transaction.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ChannelId(pub [u8; 32]);

impl Writeable for ChannelId {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
Ok(self.0.write(writer)?)
}
}

impl Readable for ChannelId {
fn read<R: lightning::io::Read>(
reader: &mut R,
) -> Result<Self, lightning::ln::msgs::DecodeError> {
Ok(Self(Readable::read(reader)?))
}
}

/// A local, potentially user-provided, identifier of a channel.
///
/// By default, this will be randomly generated for the user to ensure local uniqueness.
Expand Down Expand Up @@ -224,7 +203,7 @@ pub struct ChannelDetails {
impl From<LdkChannelDetails> for ChannelDetails {
fn from(value: LdkChannelDetails) -> Self {
ChannelDetails {
channel_id: ChannelId(value.channel_id),
channel_id: value.channel_id,
counterparty_node_id: value.counterparty.node_id,
funding_txo: value.funding_txo.and_then(|o| Some(o.into_bitcoin_outpoint())),
channel_value_sats: value.channel_value_satoshis,
Expand Down
4 changes: 2 additions & 2 deletions src/uniffi_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use crate::UniffiCustomTypeConverter;
use crate::error::Error;
use crate::hex_utils;
use crate::io::SqliteStore;
use crate::{ChannelId, Node, SocketAddress, UserChannelId};
use crate::{Node, SocketAddress, UserChannelId};

use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{Address, Txid};
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
use lightning_invoice::{Bolt11Invoice, SignedRawBolt11Invoice};

use bip39::Mnemonic;
Expand Down

0 comments on commit 2db9c6e

Please sign in to comment.