Skip to content

Commit 33eb336

Browse files
committed
Revert "chore(ctx): replace gstuff constructible with oncelock (#2267)"
This reverts commit ac926dd
1 parent cfb1817 commit 33eb336

File tree

22 files changed

+111
-152
lines changed

22 files changed

+111
-152
lines changed

mm2src/coins/eth/eth_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use super::*;
22
use crate::IguanaPrivKey;
3-
use common::block_on;
3+
use common::{block_on, block_on_f01};
44
use mm2_core::mm_ctx::MmCtxBuilder;
55

66
cfg_native!(
77
use crate::eth::for_tests::{eth_coin_for_test, eth_coin_from_keypair};
88
use crate::DexFee;
99

10-
use common::{now_sec, block_on_f01};
10+
use common::now_sec;
1111
use ethkey::{Generator, Random};
1212
use mm2_test_helpers::for_tests::{ETH_MAINNET_CHAIN_ID, ETH_MAINNET_NODE, ETH_SEPOLIA_CHAIN_ID, ETH_SEPOLIA_NODES,
1313
ETH_SEPOLIA_TOKEN_CONTRACT};

mm2src/coins/hd_wallet/storage/sqlite_storage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl HDWalletStorageInternalOps for HDWalletSqliteStorage {
101101
where
102102
Self: Sized,
103103
{
104-
let shared = ctx.shared_sqlite_conn.get().or_mm_err(|| {
104+
let shared = ctx.shared_sqlite_conn.as_option().or_mm_err(|| {
105105
HDWalletStorageError::Internal("'MmCtx::shared_sqlite_conn' is not initialized".to_owned())
106106
})?;
107107
let storage = HDWalletSqliteStorage {

mm2src/coins/lightning/ln_utils.rs

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub async fn init_db(ctx: &MmArc, ticker: String) -> EnableLightningResult<Sqlit
8787
let db = SqliteLightningDB::new(
8888
ticker,
8989
ctx.sqlite_connection
90-
.get()
9190
.ok_or(MmError::new(EnableLightningError::DbError(
9291
"sqlite_connection is not initialized".into(),
9392
)))?

mm2src/coins/nft/nft_structs.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -734,15 +734,14 @@ impl NftCtx {
734734
/// If an `NftCtx` instance doesn't already exist in the MM context, it gets created and cached for subsequent use.
735735
#[cfg(not(target_arch = "wasm32"))]
736736
pub(crate) fn from_ctx(ctx: &MmArc) -> Result<Arc<NftCtx>, String> {
737-
from_ctx(&ctx.nft_ctx, move || {
737+
Ok(try_s!(from_ctx(&ctx.nft_ctx, move || {
738738
let async_sqlite_connection = ctx
739739
.async_sqlite_connection
740-
.get()
741740
.ok_or("async_sqlite_connection is not initialized".to_owned())?;
742741
Ok(NftCtx {
743742
nft_cache_db: async_sqlite_connection.clone(),
744743
})
745-
})
744+
})))
746745
}
747746

748747
#[cfg(target_arch = "wasm32")]

mm2src/coins/tx_history_storage/sql_tx_history_storage_v2.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,11 @@ pub struct SqliteTxHistoryStorage(Arc<Mutex<Connection>>);
376376

377377
impl SqliteTxHistoryStorage {
378378
pub fn new(ctx: &MmArc) -> Result<Self, MmError<CreateTxHistoryStorageError>> {
379-
let sqlite_connection =
380-
ctx.sqlite_connection
381-
.get()
382-
.ok_or(MmError::new(CreateTxHistoryStorageError::Internal(
383-
"sqlite_connection is not initialized".to_owned(),
384-
)))?;
379+
let sqlite_connection = ctx
380+
.sqlite_connection
381+
.ok_or(MmError::new(CreateTxHistoryStorageError::Internal(
382+
"sqlite_connection is not initialized".to_owned(),
383+
)))?;
385384
Ok(SqliteTxHistoryStorage(sqlite_connection.clone()))
386385
}
387386
}

mm2src/coins/utxo/utxo_block_header_storage/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Debug for BlockHeaderStorage {
2727
impl BlockHeaderStorage {
2828
#[cfg(all(not(test), not(target_arch = "wasm32")))]
2929
pub(crate) fn new_from_ctx(ctx: MmArc, ticker: String) -> Result<Self, BlockHeaderStorageError> {
30-
let sqlite_connection = ctx.sqlite_connection.get().ok_or(BlockHeaderStorageError::Internal(
30+
let sqlite_connection = ctx.sqlite_connection.ok_or(BlockHeaderStorageError::Internal(
3131
"sqlite_connection is not initialized".to_owned(),
3232
))?;
3333
Ok(BlockHeaderStorage {
@@ -50,11 +50,8 @@ impl BlockHeaderStorage {
5050
use db_common::sqlite::rusqlite::Connection;
5151
use std::sync::{Arc, Mutex};
5252

53-
let conn = ctx
54-
.sqlite_connection
55-
.get()
56-
.cloned()
57-
.unwrap_or_else(|| Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));
53+
let conn = Arc::new(Mutex::new(Connection::open_in_memory().unwrap()));
54+
let conn = ctx.sqlite_connection.clone_or(conn);
5855

5956
Ok(BlockHeaderStorage {
6057
inner: Box::new(SqliteBlockHeadersStorage { ticker, conn }),

mm2src/coins/z_coin/storage/blockdb/blockdb_sql_storage.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ impl BlockDbImpl {
4949
async_blocking(move || {
5050
let conn = Connection::open(path).map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
5151
let conn = Arc::new(Mutex::new(conn));
52-
let conn_lock = conn.lock().unwrap();
53-
run_optimization_pragmas(&conn_lock).map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
54-
conn_lock
52+
let conn_clone = conn.clone();
53+
let conn_clone = conn_clone.lock().unwrap();
54+
run_optimization_pragmas(&conn_clone).map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
55+
conn_clone
5556
.execute(
5657
"CREATE TABLE IF NOT EXISTS compactblocks (
5758
height INTEGER PRIMARY KEY,
@@ -60,7 +61,6 @@ impl BlockDbImpl {
6061
[],
6162
)
6263
.map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
63-
drop(conn_lock);
6464

6565
Ok(Self { db: conn, ticker })
6666
})
@@ -73,12 +73,11 @@ impl BlockDbImpl {
7373
async_blocking(move || {
7474
let conn = ctx
7575
.sqlite_connection
76-
.get()
77-
.cloned()
78-
.unwrap_or_else(|| Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));
79-
let conn_lock = conn.lock().unwrap();
80-
run_optimization_pragmas(&conn_lock).map_err(|err| ZcoinStorageError::DbError(err.to_string()))?;
81-
conn_lock
76+
.clone_or(Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));
77+
let conn_clone = conn.clone();
78+
let conn_clone = conn_clone.lock().unwrap();
79+
run_optimization_pragmas(&conn_clone).map_err(|err| ZcoinStorageError::DbError(err.to_string()))?;
80+
conn_clone
8281
.execute(
8382
"CREATE TABLE IF NOT EXISTS compactblocks (
8483
height INTEGER PRIMARY KEY,
@@ -87,7 +86,6 @@ impl BlockDbImpl {
8786
[],
8887
)
8988
.map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
90-
drop(conn_lock);
9189

9290
Ok(BlockDbImpl { db: conn, ticker })
9391
})

mm2src/crypto/src/crypto_ctx.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,10 @@ impl CryptoCtx {
316316
*ctx_field = Some(result.clone());
317317
drop(ctx_field);
318318

319-
ctx.rmd160
320-
.set(rmd160)
321-
.map_to_mm(|_| CryptoInitError::Internal("Already Initialized".to_string()))?;
319+
ctx.rmd160.pin(rmd160).map_to_mm(CryptoInitError::Internal)?;
322320
ctx.shared_db_id
323-
.set(shared_db_id)
324-
.map_to_mm(|_| CryptoInitError::Internal("Already Initialized".to_string()))?;
321+
.pin(shared_db_id)
322+
.map_to_mm(CryptoInitError::Internal)?;
325323

326324
info!("Public key hash: {rmd160}");
327325
info!("Shared Database ID: {shared_db_id}");

mm2src/mm2_bin_lib/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn mm2_status() -> MainStatus {
4141
Err(_) => return MainStatus::NoRpc,
4242
};
4343

44-
if *ctx.rpc_started.get().unwrap_or(&false) {
44+
if ctx.rpc_started.copy_or(false) {
4545
MainStatus::RpcIsUp
4646
} else {
4747
MainStatus::NoRpc

mm2src/mm2_bin_lib/src/mm2_wasm_lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub async fn mm2_rpc(payload: JsValue) -> Result<JsValue, JsValue> {
216216
Err(_) => return Err(Mm2RpcErr::NotRunning.into()),
217217
};
218218

219-
let wasm_rpc = ctx.wasm_rpc.get().ok_or(JsValue::from(Mm2RpcErr::NotRunning))?;
219+
let wasm_rpc = ctx.wasm_rpc.ok_or(JsValue::from(Mm2RpcErr::NotRunning))?;
220220
let response: Mm2RpcResponse = wasm_rpc.request(request_json).await.into();
221221

222222
serialize_to_js(&response).map_err(|e| {

mm2src/mm2_core/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ common = { path = "../common" }
1515
db_common = { path = "../db_common" }
1616
derive_more = "0.99"
1717
futures = { version = "0.3", package = "futures", features = ["compat", "async-await", "thread-pool"] }
18-
gstuff = { version = "0.7", features = ["nightly"] }
1918
hex = "0.4.2"
2019
lazy_static = "1.4"
2120
libp2p = { git = "https://github.com/KomodoPlatform/rust-libp2p.git", tag = "k-0.52.4", default-features = false, features = ["identify"] }
@@ -32,11 +31,13 @@ shared_ref_counter = { path = "../common/shared_ref_counter" }
3231
uuid = { version = "1.2.2", features = ["fast-rng", "serde", "v4"] }
3332

3433
[target.'cfg(target_arch = "wasm32")'.dependencies]
34+
gstuff = { version = "0.7", features = ["nightly"] }
3535
instant = { version = "0.1.12", features = ["wasm-bindgen"] }
3636
mm2_rpc = { path = "../mm2_rpc", features = [ "rpc_facilities" ] }
3737
wasm-bindgen-test = { version = "0.3.2" }
3838

3939
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
4040
rustls = { version = "0.21", default-features = false }
41+
gstuff = { version = "0.7", features = ["nightly"] }
4142
instant = "0.1.12"
4243
tokio = { version = "1.20", features = ["io-util", "rt-multi-thread", "net"] }

0 commit comments

Comments
 (0)