Skip to content

Commit 8ebaab1

Browse files
committed
Route lock access through the shared helpers
Switch the codebase over to the new lock helper names so lock poisoning behavior is centralized and the call sites stay shorter and more consistent. Co-Authored-By: HAL 9000
1 parent bab1327 commit 8ebaab1

24 files changed

Lines changed: 292 additions & 248 deletions

src/builder.rs

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ use crate::types::{
8080
GossipSync, Graph, KeysManager, MessageRouter, OnionMessenger, PaymentStore, PeerManager,
8181
PendingPaymentStore, SyncAndAsyncKVStore,
8282
};
83+
use crate::util::locks::{MutexExt, RwLockExt};
8384
use crate::wallet::persist::KVStoreWalletPersister;
8485
use crate::wallet::Wallet;
8586
use crate::{Node, NodeMetrics};
@@ -861,7 +862,7 @@ impl ArcedNodeBuilder {
861862
pub fn set_chain_source_esplora(
862863
&self, server_url: String, sync_config: Option<EsploraSyncConfig>,
863864
) {
864-
self.inner.write().unwrap().set_chain_source_esplora(server_url, sync_config);
865+
self.inner.wlck().set_chain_source_esplora(server_url, sync_config);
865866
}
866867

867868
/// Configures the [`Node`] instance to source its chain data from the given Esplora server.
@@ -875,11 +876,7 @@ impl ArcedNodeBuilder {
875876
&self, server_url: String, headers: HashMap<String, String>,
876877
sync_config: Option<EsploraSyncConfig>,
877878
) {
878-
self.inner.write().unwrap().set_chain_source_esplora_with_headers(
879-
server_url,
880-
headers,
881-
sync_config,
882-
);
879+
self.inner.wlck().set_chain_source_esplora_with_headers(server_url, headers, sync_config);
883880
}
884881

885882
/// Configures the [`Node`] instance to source its chain data from the given Electrum server.
@@ -889,7 +886,7 @@ impl ArcedNodeBuilder {
889886
pub fn set_chain_source_electrum(
890887
&self, server_url: String, sync_config: Option<ElectrumSyncConfig>,
891888
) {
892-
self.inner.write().unwrap().set_chain_source_electrum(server_url, sync_config);
889+
self.inner.wlck().set_chain_source_electrum(server_url, sync_config);
893890
}
894891

895892
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
@@ -903,12 +900,7 @@ impl ArcedNodeBuilder {
903900
pub fn set_chain_source_bitcoind_rpc(
904901
&self, rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String,
905902
) {
906-
self.inner.write().unwrap().set_chain_source_bitcoind_rpc(
907-
rpc_host,
908-
rpc_port,
909-
rpc_user,
910-
rpc_password,
911-
);
903+
self.inner.wlck().set_chain_source_bitcoind_rpc(rpc_host, rpc_port, rpc_user, rpc_password);
912904
}
913905

914906
/// Configures the [`Node`] instance to synchronize chain data from a Bitcoin Core REST endpoint.
@@ -924,7 +916,7 @@ impl ArcedNodeBuilder {
924916
&self, rest_host: String, rest_port: u16, rpc_host: String, rpc_port: u16,
925917
rpc_user: String, rpc_password: String,
926918
) {
927-
self.inner.write().unwrap().set_chain_source_bitcoind_rest(
919+
self.inner.wlck().set_chain_source_bitcoind_rest(
928920
rest_host,
929921
rest_port,
930922
rpc_host,
@@ -937,20 +929,20 @@ impl ArcedNodeBuilder {
937929
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
938930
/// network.
939931
pub fn set_gossip_source_p2p(&self) {
940-
self.inner.write().unwrap().set_gossip_source_p2p();
932+
self.inner.wlck().set_gossip_source_p2p();
941933
}
942934

943935
/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
944936
/// server.
945937
pub fn set_gossip_source_rgs(&self, rgs_server_url: String) {
946-
self.inner.write().unwrap().set_gossip_source_rgs(rgs_server_url);
938+
self.inner.wlck().set_gossip_source_rgs(rgs_server_url);
947939
}
948940

949941
/// Configures the [`Node`] instance to source its external scores from the given URL.
950942
///
951943
/// The external scores are merged into the local scoring system to improve routing.
952944
pub fn set_pathfinding_scores_source(&self, url: String) {
953-
self.inner.write().unwrap().set_pathfinding_scores_source(url);
945+
self.inner.wlck().set_pathfinding_scores_source(url);
954946
}
955947

956948
/// Configures the [`Node`] instance to source inbound liquidity from the given
@@ -964,7 +956,7 @@ impl ArcedNodeBuilder {
964956
pub fn set_liquidity_source_lsps1(
965957
&self, node_id: PublicKey, address: SocketAddress, token: Option<String>,
966958
) {
967-
self.inner.write().unwrap().set_liquidity_source_lsps1(node_id, address, token);
959+
self.inner.wlck().set_liquidity_source_lsps1(node_id, address, token);
968960
}
969961

970962
/// Configures the [`Node`] instance to source just-in-time inbound liquidity from the given
@@ -978,7 +970,7 @@ impl ArcedNodeBuilder {
978970
pub fn set_liquidity_source_lsps2(
979971
&self, node_id: PublicKey, address: SocketAddress, token: Option<String>,
980972
) {
981-
self.inner.write().unwrap().set_liquidity_source_lsps2(node_id, address, token);
973+
self.inner.wlck().set_liquidity_source_lsps2(node_id, address, token);
982974
}
983975

984976
/// Configures the [`Node`] instance to provide an [LSPS2] service, issuing just-in-time
@@ -988,12 +980,12 @@ impl ArcedNodeBuilder {
988980
///
989981
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
990982
pub fn set_liquidity_provider_lsps2(&self, service_config: LSPS2ServiceConfig) {
991-
self.inner.write().unwrap().set_liquidity_provider_lsps2(service_config);
983+
self.inner.wlck().set_liquidity_provider_lsps2(service_config);
992984
}
993985

994986
/// Sets the used storage directory path.
995987
pub fn set_storage_dir_path(&self, storage_dir_path: String) {
996-
self.inner.write().unwrap().set_storage_dir_path(storage_dir_path);
988+
self.inner.wlck().set_storage_dir_path(storage_dir_path);
997989
}
998990

999991
/// Configures the [`Node`] instance to write logs to the filesystem.
@@ -1012,29 +1004,29 @@ impl ArcedNodeBuilder {
10121004
pub fn set_filesystem_logger(
10131005
&self, log_file_path: Option<String>, log_level: Option<LogLevel>,
10141006
) {
1015-
self.inner.write().unwrap().set_filesystem_logger(log_file_path, log_level);
1007+
self.inner.wlck().set_filesystem_logger(log_file_path, log_level);
10161008
}
10171009

10181010
/// Configures the [`Node`] instance to write logs to the [`log`](https://crates.io/crates/log) facade.
10191011
pub fn set_log_facade_logger(&self) {
1020-
self.inner.write().unwrap().set_log_facade_logger();
1012+
self.inner.wlck().set_log_facade_logger();
10211013
}
10221014

10231015
/// Configures the [`Node`] instance to write logs to the provided custom [`LogWriter`].
10241016
pub fn set_custom_logger(&self, log_writer: Arc<dyn LogWriter>) {
1025-
self.inner.write().unwrap().set_custom_logger(log_writer);
1017+
self.inner.wlck().set_custom_logger(log_writer);
10261018
}
10271019

10281020
/// Sets the Bitcoin network used.
10291021
pub fn set_network(&self, network: Network) {
1030-
self.inner.write().unwrap().set_network(network);
1022+
self.inner.wlck().set_network(network);
10311023
}
10321024

10331025
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
10341026
pub fn set_listening_addresses(
10351027
&self, listening_addresses: Vec<SocketAddress>,
10361028
) -> Result<(), BuildError> {
1037-
self.inner.write().unwrap().set_listening_addresses(listening_addresses).map(|_| ())
1029+
self.inner.wlck().set_listening_addresses(listening_addresses).map(|_| ())
10381030
}
10391031

10401032
/// Sets the IP address and TCP port which [`Node`] will announce to the gossip network that it accepts connections on.
@@ -1045,7 +1037,7 @@ impl ArcedNodeBuilder {
10451037
pub fn set_announcement_addresses(
10461038
&self, announcement_addresses: Vec<SocketAddress>,
10471039
) -> Result<(), BuildError> {
1048-
self.inner.write().unwrap().set_announcement_addresses(announcement_addresses).map(|_| ())
1040+
self.inner.wlck().set_announcement_addresses(announcement_addresses).map(|_| ())
10491041
}
10501042

10511043
/// Configures the [`Node`] instance to use a Tor SOCKS proxy for outbound connections to peers with OnionV3 addresses.
@@ -1054,22 +1046,22 @@ impl ArcedNodeBuilder {
10541046
///
10551047
/// **Note**: If unset, connecting to peer OnionV3 addresses will fail.
10561048
pub fn set_tor_config(&self, tor_config: TorConfig) -> Result<(), BuildError> {
1057-
self.inner.write().unwrap().set_tor_config(tor_config).map(|_| ())
1049+
self.inner.wlck().set_tor_config(tor_config).map(|_| ())
10581050
}
10591051

10601052
/// Sets the node alias that will be used when broadcasting announcements to the gossip
10611053
/// network.
10621054
///
10631055
/// The provided alias must be a valid UTF-8 string and no longer than 32 bytes in total.
10641056
pub fn set_node_alias(&self, node_alias: String) -> Result<(), BuildError> {
1065-
self.inner.write().unwrap().set_node_alias(node_alias).map(|_| ())
1057+
self.inner.wlck().set_node_alias(node_alias).map(|_| ())
10661058
}
10671059

10681060
/// Sets the role of the node in an asynchronous payments context.
10691061
pub fn set_async_payments_role(
10701062
&self, role: Option<AsyncPaymentsRole>,
10711063
) -> Result<(), BuildError> {
1072-
self.inner.write().unwrap().set_async_payments_role(role).map(|_| ())
1064+
self.inner.wlck().set_async_payments_role(role).map(|_| ())
10731065
}
10741066

10751067
/// Configures the [`Node`] to resync chain data from genesis on first startup, recovering any
@@ -1078,21 +1070,21 @@ impl ArcedNodeBuilder {
10781070
/// This should only be set on first startup when importing an older wallet from a previously
10791071
/// used [`NodeEntropy`].
10801072
pub fn set_wallet_recovery_mode(&self) {
1081-
self.inner.write().unwrap().set_wallet_recovery_mode();
1073+
self.inner.wlck().set_wallet_recovery_mode();
10821074
}
10831075

10841076
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
10851077
/// previously configured.
10861078
pub fn build(&self, node_entropy: Arc<NodeEntropy>) -> Result<Arc<Node>, BuildError> {
1087-
self.inner.read().unwrap().build(*node_entropy).map(Arc::new)
1079+
self.inner.rlck().build(*node_entropy).map(Arc::new)
10881080
}
10891081

10901082
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
10911083
/// previously configured.
10921084
pub fn build_with_fs_store(
10931085
&self, node_entropy: Arc<NodeEntropy>,
10941086
) -> Result<Arc<Node>, BuildError> {
1095-
self.inner.read().unwrap().build_with_fs_store(*node_entropy).map(Arc::new)
1087+
self.inner.rlck().build_with_fs_store(*node_entropy).map(Arc::new)
10961088
}
10971089

10981090
/// Builds a [`Node`] instance with a [VSS] backend and according to the options
@@ -1117,8 +1109,7 @@ impl ArcedNodeBuilder {
11171109
fixed_headers: HashMap<String, String>,
11181110
) -> Result<Arc<Node>, BuildError> {
11191111
self.inner
1120-
.read()
1121-
.unwrap()
1112+
.rlck()
11221113
.build_with_vss_store(*node_entropy, vss_url, store_id, fixed_headers)
11231114
.map(Arc::new)
11241115
}
@@ -1150,8 +1141,7 @@ impl ArcedNodeBuilder {
11501141
lnurl_auth_server_url: String, fixed_headers: HashMap<String, String>,
11511142
) -> Result<Arc<Node>, BuildError> {
11521143
self.inner
1153-
.read()
1154-
.unwrap()
1144+
.rlck()
11551145
.build_with_vss_store_and_lnurl_auth(
11561146
*node_entropy,
11571147
vss_url,
@@ -1179,8 +1169,7 @@ impl ArcedNodeBuilder {
11791169
fixed_headers: HashMap<String, String>,
11801170
) -> Result<Arc<Node>, BuildError> {
11811171
self.inner
1182-
.read()
1183-
.unwrap()
1172+
.rlck()
11841173
.build_with_vss_store_and_fixed_headers(*node_entropy, vss_url, store_id, fixed_headers)
11851174
.map(Arc::new)
11861175
}
@@ -1202,8 +1191,7 @@ impl ArcedNodeBuilder {
12021191
) -> Result<Arc<Node>, BuildError> {
12031192
let adapter = Arc::new(crate::ffi::VssHeaderProviderAdapter::new(header_provider));
12041193
self.inner
1205-
.read()
1206-
.unwrap()
1194+
.rlck()
12071195
.build_with_vss_store_and_header_provider(*node_entropy, vss_url, store_id, adapter)
12081196
.map(Arc::new)
12091197
}
@@ -1214,7 +1202,7 @@ impl ArcedNodeBuilder {
12141202
pub fn build_with_store<S: SyncAndAsyncKVStore + Send + Sync + 'static>(
12151203
&self, node_entropy: Arc<NodeEntropy>, kv_store: S,
12161204
) -> Result<Arc<Node>, BuildError> {
1217-
self.inner.read().unwrap().build_with_store(*node_entropy, kv_store).map(Arc::new)
1205+
self.inner.rlck().build_with_store(*node_entropy, kv_store).map(Arc::new)
12181206
}
12191207
}
12201208

@@ -1610,7 +1598,7 @@ fn build_with_store_internal(
16101598
// Restore external pathfinding scores from cache if possible.
16111599
match external_scores_res {
16121600
Ok(external_scores) => {
1613-
scorer.lock().unwrap().merge(external_scores, cur_time);
1601+
scorer.lck().merge(external_scores, cur_time);
16141602
log_trace!(logger, "External scores from cache merged successfully");
16151603
},
16161604
Err(e) => {
@@ -1763,7 +1751,7 @@ fn build_with_store_internal(
17631751

17641752
// Reset the RGS sync timestamp in case we somehow switch gossip sources
17651753
{
1766-
let mut locked_node_metrics = node_metrics.write().unwrap();
1754+
let mut locked_node_metrics = node_metrics.wlck();
17671755
locked_node_metrics.latest_rgs_snapshot_timestamp = None;
17681756
write_node_metrics(&*locked_node_metrics, &*kv_store, Arc::clone(&logger))
17691757
.map_err(|e| {
@@ -1775,7 +1763,7 @@ fn build_with_store_internal(
17751763
},
17761764
GossipSourceConfig::RapidGossipSync(rgs_server) => {
17771765
let latest_sync_timestamp =
1778-
node_metrics.read().unwrap().latest_rgs_snapshot_timestamp.unwrap_or(0);
1766+
node_metrics.rlck().latest_rgs_snapshot_timestamp.unwrap_or(0);
17791767
Arc::new(GossipSource::new_rgs(
17801768
rgs_server.clone(),
17811769
latest_sync_timestamp,

0 commit comments

Comments
 (0)