Skip to content

Commit

Permalink
Drop Network newtype again
Browse files Browse the repository at this point in the history
.. as UniFFI 0.26.0 added support for `non_exhaustive` enums.
  • Loading branch information
tnull committed Jan 24, 2024
1 parent af4a240 commit 9075a24
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 63 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ LDK Node is a self-custodial Lightning node in library form. Its central goal is
The primary abstraction of the library is the [`Node`][api_docs_node], which can be retrieved by setting up and configuring a [`Builder`][api_docs_builder] to your liking and calling one of the `build` methods. `Node` can then be controlled via commands such as `start`, `stop`, `connect_open_channel`, `send_payment`, etc.

```rust
use ldk_node::{Builder, Network};
use ldk_node::Builder;
use ldk_node::lightning_invoice::Bolt11Invoice;
use ldk_node::lightning::ln::msgs::SocketAddress;
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::bitcoin::Network;
use std::str::FromStr;

fn main() {
Expand Down
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ enum PaymentStatus {
"Failed",
};

[NonExhaustive]
enum Network {
"Bitcoin",
"Testnet",
Expand Down
6 changes: 3 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::peer_store::PeerStore;
use crate::sweep::OutputSweeper;
use crate::tx_broadcaster::TransactionBroadcaster;
use crate::types::{
ChainMonitor, ChannelManager, FakeMessageRouter, GossipSync, KeysManager, Network,
NetworkGraph, OnionMessenger, PeerManager,
ChainMonitor, ChannelManager, FakeMessageRouter, GossipSync, KeysManager, NetworkGraph,
OnionMessenger, PeerManager,
};
use crate::wallet::Wallet;
use crate::LogLevel;
Expand Down Expand Up @@ -49,7 +49,7 @@ use bdk::template::Bip84;

use bip39::Mnemonic;

use bitcoin::BlockHash;
use bitcoin::{BlockHash, Network};

#[cfg(any(vss, vss_test))]
use bitcoin::bip32::ChildNumber;
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
//! [`send_payment`], etc.:
//!
//! ```no_run
//! use ldk_node::{Builder, Network};
//! use ldk_node::Network;
//! use ldk_node::lightning_invoice::Bolt11Invoice;
//! use ldk_node::lightning::ln::msgs::SocketAddress;
//! use ldk_node::bitcoin::Network;
//! use ldk_node::bitcoin::secp256k1::PublicKey;
//! use std::str::FromStr;
//!
Expand Down Expand Up @@ -122,7 +123,7 @@ use types::{
Broadcaster, ChainMonitor, ChannelManager, FeeEstimator, KeysManager, NetworkGraph,
PeerManager, Router, Scorer, Sweeper, Wallet,
};
pub use types::{ChannelDetails, Network, PeerDetails, UserChannelId};
pub use types::{ChannelDetails, PeerDetails, UserChannelId};

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

Expand All @@ -148,7 +149,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::PublicKey;

use bitcoin::{Address, Txid};
use bitcoin::{Address, Network, Txid};

use rand::Rng;

Expand Down Expand Up @@ -1503,7 +1504,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
fn receive_payment_inner(
&self, amount_msat: Option<u64>, description: &str, expiry_secs: u32,
) -> Result<Bolt11Invoice, Error> {
let currency = Currency::from(bitcoin::Network::from(self.config.network));
let currency = Currency::from(self.config.network);
let keys_manager = Arc::clone(&self.keys_manager);
let invoice = match lightning_invoice::utils::create_invoice_from_channelmanager(
&self.channel_manager,
Expand Down Expand Up @@ -1556,7 +1557,8 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
///
/// For example, you could retrieve all stored outbound payments as follows:
/// ```
/// # use ldk_node::{Builder, Config, Network, PaymentDirection};
/// # use ldk_node::{Builder, Config, PaymentDirection};
/// # use ldk_node::bitcoin::Network;
/// # let mut config = Config::default();
/// # config.network = Network::Regtest;
/// # config.storage_dir_path = "/tmp/ldk_node_test/".to_string();
Expand Down
54 changes: 0 additions & 54 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use lightning_transaction_sync::EsploraSyncClient;
use bitcoin::secp256k1::{self, PublicKey, Secp256k1};
use bitcoin::OutPoint;

use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, Mutex, RwLock};

pub(crate) type ChainMonitor<K> = chainmonitor::ChainMonitor<
Expand Down Expand Up @@ -139,58 +137,6 @@ pub(crate) type Sweeper<K> = OutputSweeper<
Arc<FilesystemLogger>,
>;

/// The cryptocurrency network to act on.
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug)]
pub enum Network {
/// Mainnet Bitcoin.
Bitcoin,
/// Bitcoin's testnet network.
Testnet,
/// Bitcoin's signet network.
Signet,
/// Bitcoin's regtest network.
Regtest,
}

impl TryFrom<bitcoin::Network> for Network {
type Error = ();

fn try_from(network: bitcoin::Network) -> Result<Self, Self::Error> {
match network {
bitcoin::Network::Bitcoin => Ok(Self::Bitcoin),
bitcoin::Network::Testnet => Ok(Self::Testnet),
bitcoin::Network::Signet => Ok(Self::Signet),
bitcoin::Network::Regtest => Ok(Self::Regtest),
_ => Err(()),
}
}
}

impl From<Network> for bitcoin::Network {
fn from(network: Network) -> Self {
match network {
Network::Bitcoin => bitcoin::Network::Bitcoin,
Network::Testnet => bitcoin::Network::Testnet,
Network::Signet => bitcoin::Network::Signet,
Network::Regtest => bitcoin::Network::Regtest,
}
}
}

impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
bitcoin::Network::from(*self).fmt(f)
}
}

impl FromStr for Network {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
bitcoin::Network::from_str(s).map_err(|_| ())?.try_into()
}
}

/// 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

0 comments on commit 9075a24

Please sign in to comment.