Skip to content

Commit 423ed51

Browse files
committed
Replace unreachable unwraps with expects
We replace all `unwrap`s with `expect`s. Co-Authored-By: HAL 9000
1 parent fe692f3 commit 423ed51

30 files changed

Lines changed: 335 additions & 260 deletions

build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
fn main() {
99
#[cfg(feature = "uniffi")]
10-
uniffi::generate_scaffolding("bindings/ldk_node.udl").unwrap();
10+
uniffi::generate_scaffolding("bindings/ldk_node.udl")
11+
.expect("the checked-in UniFFI UDL should always generate scaffolding");
1112
}

src/balance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ impl LightningBalance {
232232
inbound_htlc_rounded_msat,
233233
} => {
234234
// unwrap safety: confirmed_balance_candidate_index is guaranteed to index into balance_candidates
235-
let balance = balance_candidates.get(confirmed_balance_candidate_index).unwrap();
235+
let balance = balance_candidates
236+
.get(confirmed_balance_candidate_index)
237+
.expect("LDK should provide a valid confirmed balance candidate index");
236238

237239
Self::ClaimableOnChannelClose {
238240
channel_id,

src/builder.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl ArcedNodeBuilder {
861861
pub fn set_chain_source_esplora(
862862
&self, server_url: String, sync_config: Option<EsploraSyncConfig>,
863863
) {
864-
self.inner.write().unwrap().set_chain_source_esplora(server_url, sync_config);
864+
self.inner.write().expect("lock").set_chain_source_esplora(server_url, sync_config);
865865
}
866866

867867
/// Configures the [`Node`] instance to source its chain data from the given Esplora server.
@@ -875,7 +875,7 @@ impl ArcedNodeBuilder {
875875
&self, server_url: String, headers: HashMap<String, String>,
876876
sync_config: Option<EsploraSyncConfig>,
877877
) {
878-
self.inner.write().unwrap().set_chain_source_esplora_with_headers(
878+
self.inner.write().expect("lock").set_chain_source_esplora_with_headers(
879879
server_url,
880880
headers,
881881
sync_config,
@@ -889,7 +889,7 @@ impl ArcedNodeBuilder {
889889
pub fn set_chain_source_electrum(
890890
&self, server_url: String, sync_config: Option<ElectrumSyncConfig>,
891891
) {
892-
self.inner.write().unwrap().set_chain_source_electrum(server_url, sync_config);
892+
self.inner.write().expect("lock").set_chain_source_electrum(server_url, sync_config);
893893
}
894894

895895
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
@@ -903,7 +903,7 @@ impl ArcedNodeBuilder {
903903
pub fn set_chain_source_bitcoind_rpc(
904904
&self, rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String,
905905
) {
906-
self.inner.write().unwrap().set_chain_source_bitcoind_rpc(
906+
self.inner.write().expect("lock").set_chain_source_bitcoind_rpc(
907907
rpc_host,
908908
rpc_port,
909909
rpc_user,
@@ -924,7 +924,7 @@ impl ArcedNodeBuilder {
924924
&self, rest_host: String, rest_port: u16, rpc_host: String, rpc_port: u16,
925925
rpc_user: String, rpc_password: String,
926926
) {
927-
self.inner.write().unwrap().set_chain_source_bitcoind_rest(
927+
self.inner.write().expect("lock").set_chain_source_bitcoind_rest(
928928
rest_host,
929929
rest_port,
930930
rpc_host,
@@ -937,20 +937,20 @@ impl ArcedNodeBuilder {
937937
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
938938
/// network.
939939
pub fn set_gossip_source_p2p(&self) {
940-
self.inner.write().unwrap().set_gossip_source_p2p();
940+
self.inner.write().expect("lock").set_gossip_source_p2p();
941941
}
942942

943943
/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
944944
/// server.
945945
pub fn set_gossip_source_rgs(&self, rgs_server_url: String) {
946-
self.inner.write().unwrap().set_gossip_source_rgs(rgs_server_url);
946+
self.inner.write().expect("lock").set_gossip_source_rgs(rgs_server_url);
947947
}
948948

949949
/// Configures the [`Node`] instance to source its external scores from the given URL.
950950
///
951951
/// The external scores are merged into the local scoring system to improve routing.
952952
pub fn set_pathfinding_scores_source(&self, url: String) {
953-
self.inner.write().unwrap().set_pathfinding_scores_source(url);
953+
self.inner.write().expect("lock").set_pathfinding_scores_source(url);
954954
}
955955

956956
/// Configures the [`Node`] instance to source inbound liquidity from the given
@@ -964,7 +964,7 @@ impl ArcedNodeBuilder {
964964
pub fn set_liquidity_source_lsps1(
965965
&self, node_id: PublicKey, address: SocketAddress, token: Option<String>,
966966
) {
967-
self.inner.write().unwrap().set_liquidity_source_lsps1(node_id, address, token);
967+
self.inner.write().expect("lock").set_liquidity_source_lsps1(node_id, address, token);
968968
}
969969

970970
/// Configures the [`Node`] instance to source just-in-time inbound liquidity from the given
@@ -978,7 +978,7 @@ impl ArcedNodeBuilder {
978978
pub fn set_liquidity_source_lsps2(
979979
&self, node_id: PublicKey, address: SocketAddress, token: Option<String>,
980980
) {
981-
self.inner.write().unwrap().set_liquidity_source_lsps2(node_id, address, token);
981+
self.inner.write().expect("lock").set_liquidity_source_lsps2(node_id, address, token);
982982
}
983983

984984
/// Configures the [`Node`] instance to provide an [LSPS2] service, issuing just-in-time
@@ -988,12 +988,12 @@ impl ArcedNodeBuilder {
988988
///
989989
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
990990
pub fn set_liquidity_provider_lsps2(&self, service_config: LSPS2ServiceConfig) {
991-
self.inner.write().unwrap().set_liquidity_provider_lsps2(service_config);
991+
self.inner.write().expect("lock").set_liquidity_provider_lsps2(service_config);
992992
}
993993

994994
/// Sets the used storage directory path.
995995
pub fn set_storage_dir_path(&self, storage_dir_path: String) {
996-
self.inner.write().unwrap().set_storage_dir_path(storage_dir_path);
996+
self.inner.write().expect("lock").set_storage_dir_path(storage_dir_path);
997997
}
998998

999999
/// Configures the [`Node`] instance to write logs to the filesystem.
@@ -1012,29 +1012,29 @@ impl ArcedNodeBuilder {
10121012
pub fn set_filesystem_logger(
10131013
&self, log_file_path: Option<String>, log_level: Option<LogLevel>,
10141014
) {
1015-
self.inner.write().unwrap().set_filesystem_logger(log_file_path, log_level);
1015+
self.inner.write().expect("lock").set_filesystem_logger(log_file_path, log_level);
10161016
}
10171017

10181018
/// Configures the [`Node`] instance to write logs to the [`log`](https://crates.io/crates/log) facade.
10191019
pub fn set_log_facade_logger(&self) {
1020-
self.inner.write().unwrap().set_log_facade_logger();
1020+
self.inner.write().expect("lock").set_log_facade_logger();
10211021
}
10221022

10231023
/// Configures the [`Node`] instance to write logs to the provided custom [`LogWriter`].
10241024
pub fn set_custom_logger(&self, log_writer: Arc<dyn LogWriter>) {
1025-
self.inner.write().unwrap().set_custom_logger(log_writer);
1025+
self.inner.write().expect("lock").set_custom_logger(log_writer);
10261026
}
10271027

10281028
/// Sets the Bitcoin network used.
10291029
pub fn set_network(&self, network: Network) {
1030-
self.inner.write().unwrap().set_network(network);
1030+
self.inner.write().expect("lock").set_network(network);
10311031
}
10321032

10331033
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
10341034
pub fn set_listening_addresses(
10351035
&self, listening_addresses: Vec<SocketAddress>,
10361036
) -> Result<(), BuildError> {
1037-
self.inner.write().unwrap().set_listening_addresses(listening_addresses).map(|_| ())
1037+
self.inner.write().expect("lock").set_listening_addresses(listening_addresses).map(|_| ())
10381038
}
10391039

10401040
/// Sets the IP address and TCP port which [`Node`] will announce to the gossip network that it accepts connections on.
@@ -1045,7 +1045,11 @@ impl ArcedNodeBuilder {
10451045
pub fn set_announcement_addresses(
10461046
&self, announcement_addresses: Vec<SocketAddress>,
10471047
) -> Result<(), BuildError> {
1048-
self.inner.write().unwrap().set_announcement_addresses(announcement_addresses).map(|_| ())
1048+
self.inner
1049+
.write()
1050+
.expect("lock")
1051+
.set_announcement_addresses(announcement_addresses)
1052+
.map(|_| ())
10491053
}
10501054

10511055
/// Configures the [`Node`] instance to use a Tor SOCKS proxy for outbound connections to peers with OnionV3 addresses.
@@ -1054,22 +1058,22 @@ impl ArcedNodeBuilder {
10541058
///
10551059
/// **Note**: If unset, connecting to peer OnionV3 addresses will fail.
10561060
pub fn set_tor_config(&self, tor_config: TorConfig) -> Result<(), BuildError> {
1057-
self.inner.write().unwrap().set_tor_config(tor_config).map(|_| ())
1061+
self.inner.write().expect("lock").set_tor_config(tor_config).map(|_| ())
10581062
}
10591063

10601064
/// Sets the node alias that will be used when broadcasting announcements to the gossip
10611065
/// network.
10621066
///
10631067
/// The provided alias must be a valid UTF-8 string and no longer than 32 bytes in total.
10641068
pub fn set_node_alias(&self, node_alias: String) -> Result<(), BuildError> {
1065-
self.inner.write().unwrap().set_node_alias(node_alias).map(|_| ())
1069+
self.inner.write().expect("lock").set_node_alias(node_alias).map(|_| ())
10661070
}
10671071

10681072
/// Sets the role of the node in an asynchronous payments context.
10691073
pub fn set_async_payments_role(
10701074
&self, role: Option<AsyncPaymentsRole>,
10711075
) -> Result<(), BuildError> {
1072-
self.inner.write().unwrap().set_async_payments_role(role).map(|_| ())
1076+
self.inner.write().expect("lock").set_async_payments_role(role).map(|_| ())
10731077
}
10741078

10751079
/// Configures the [`Node`] to resync chain data from genesis on first startup, recovering any
@@ -1078,21 +1082,21 @@ impl ArcedNodeBuilder {
10781082
/// This should only be set on first startup when importing an older wallet from a previously
10791083
/// used [`NodeEntropy`].
10801084
pub fn set_wallet_recovery_mode(&self) {
1081-
self.inner.write().unwrap().set_wallet_recovery_mode();
1085+
self.inner.write().expect("lock").set_wallet_recovery_mode();
10821086
}
10831087

10841088
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
10851089
/// previously configured.
10861090
pub fn build(&self, node_entropy: Arc<NodeEntropy>) -> Result<Arc<Node>, BuildError> {
1087-
self.inner.read().unwrap().build(*node_entropy).map(Arc::new)
1091+
self.inner.read().expect("lock").build(*node_entropy).map(Arc::new)
10881092
}
10891093

10901094
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
10911095
/// previously configured.
10921096
pub fn build_with_fs_store(
10931097
&self, node_entropy: Arc<NodeEntropy>,
10941098
) -> Result<Arc<Node>, BuildError> {
1095-
self.inner.read().unwrap().build_with_fs_store(*node_entropy).map(Arc::new)
1099+
self.inner.read().expect("lock").build_with_fs_store(*node_entropy).map(Arc::new)
10961100
}
10971101

10981102
/// Builds a [`Node`] instance with a [VSS] backend and according to the options
@@ -1118,7 +1122,7 @@ impl ArcedNodeBuilder {
11181122
) -> Result<Arc<Node>, BuildError> {
11191123
self.inner
11201124
.read()
1121-
.unwrap()
1125+
.expect("lock")
11221126
.build_with_vss_store(*node_entropy, vss_url, store_id, fixed_headers)
11231127
.map(Arc::new)
11241128
}
@@ -1151,7 +1155,7 @@ impl ArcedNodeBuilder {
11511155
) -> Result<Arc<Node>, BuildError> {
11521156
self.inner
11531157
.read()
1154-
.unwrap()
1158+
.expect("lock")
11551159
.build_with_vss_store_and_lnurl_auth(
11561160
*node_entropy,
11571161
vss_url,
@@ -1180,7 +1184,7 @@ impl ArcedNodeBuilder {
11801184
) -> Result<Arc<Node>, BuildError> {
11811185
self.inner
11821186
.read()
1183-
.unwrap()
1187+
.expect("lock")
11841188
.build_with_vss_store_and_fixed_headers(*node_entropy, vss_url, store_id, fixed_headers)
11851189
.map(Arc::new)
11861190
}
@@ -1203,7 +1207,7 @@ impl ArcedNodeBuilder {
12031207
let adapter = Arc::new(crate::ffi::VssHeaderProviderAdapter::new(header_provider));
12041208
self.inner
12051209
.read()
1206-
.unwrap()
1210+
.expect("lock")
12071211
.build_with_vss_store_and_header_provider(*node_entropy, vss_url, store_id, adapter)
12081212
.map(Arc::new)
12091213
}
@@ -1214,7 +1218,7 @@ impl ArcedNodeBuilder {
12141218
pub fn build_with_store<S: SyncAndAsyncKVStore + Send + Sync + 'static>(
12151219
&self, node_entropy: Arc<NodeEntropy>, kv_store: S,
12161220
) -> Result<Arc<Node>, BuildError> {
1217-
self.inner.read().unwrap().build_with_store(*node_entropy, kv_store).map(Arc::new)
1221+
self.inner.read().expect("lock").build_with_store(*node_entropy, kv_store).map(Arc::new)
12181222
}
12191223
}
12201224

@@ -1610,7 +1614,7 @@ fn build_with_store_internal(
16101614
// Restore external pathfinding scores from cache if possible.
16111615
match external_scores_res {
16121616
Ok(external_scores) => {
1613-
scorer.lock().unwrap().merge(external_scores, cur_time);
1617+
scorer.lock().expect("lock").merge(external_scores, cur_time);
16141618
log_trace!(logger, "External scores from cache merged successfully");
16151619
},
16161620
Err(e) => {
@@ -1763,7 +1767,7 @@ fn build_with_store_internal(
17631767

17641768
// Reset the RGS sync timestamp in case we somehow switch gossip sources
17651769
{
1766-
let mut locked_node_metrics = node_metrics.write().unwrap();
1770+
let mut locked_node_metrics = node_metrics.write().expect("lock");
17671771
locked_node_metrics.latest_rgs_snapshot_timestamp = None;
17681772
write_node_metrics(&*locked_node_metrics, &*kv_store, Arc::clone(&logger))
17691773
.map_err(|e| {
@@ -1775,7 +1779,7 @@ fn build_with_store_internal(
17751779
},
17761780
GossipSourceConfig::RapidGossipSync(rgs_server) => {
17771781
let latest_sync_timestamp =
1778-
node_metrics.read().unwrap().latest_rgs_snapshot_timestamp.unwrap_or(0);
1782+
node_metrics.read().expect("lock").latest_rgs_snapshot_timestamp.unwrap_or(0);
17791783
Arc::new(GossipSource::new_rgs(
17801784
rgs_server.clone(),
17811785
latest_sync_timestamp,

0 commit comments

Comments
 (0)