Skip to content

Commit

Permalink
chore(ctx): replace gstuff constructible with oncelock (#2267)
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced authored Dec 10, 2024
1 parent cb37bd4 commit ac926dd
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 106 deletions.
4 changes: 2 additions & 2 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::*;
use crate::IguanaPrivKey;
use common::{block_on, block_on_f01};
use common::block_on;
use mm2_core::mm_ctx::MmCtxBuilder;

cfg_native!(
use crate::eth::for_tests::{eth_coin_for_test, eth_coin_from_keypair};
use crate::DexFee;

use common::now_sec;
use common::{now_sec, block_on_f01};
use ethkey::{Generator, Random};
use mm2_test_helpers::for_tests::{ETH_MAINNET_CHAIN_ID, ETH_MAINNET_NODE, ETH_SEPOLIA_CHAIN_ID, ETH_SEPOLIA_NODES,
ETH_SEPOLIA_TOKEN_CONTRACT};
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/hd_wallet/storage/sqlite_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl HDWalletStorageInternalOps for HDWalletSqliteStorage {
where
Self: Sized,
{
let shared = ctx.shared_sqlite_conn.as_option().or_mm_err(|| {
let shared = ctx.shared_sqlite_conn.get().or_mm_err(|| {
HDWalletStorageError::Internal("'MmCtx::shared_sqlite_conn' is not initialized".to_owned())
})?;
let storage = HDWalletSqliteStorage {
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/lightning/ln_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub async fn init_db(ctx: &MmArc, ticker: String) -> EnableLightningResult<Sqlit
let db = SqliteLightningDB::new(
ticker,
ctx.sqlite_connection
.get()
.ok_or(MmError::new(EnableLightningError::DbError(
"sqlite_connection is not initialized".into(),
)))?
Expand Down
5 changes: 3 additions & 2 deletions mm2src/coins/nft/nft_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,15 @@ impl NftCtx {
/// If an `NftCtx` instance doesn't already exist in the MM context, it gets created and cached for subsequent use.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn from_ctx(ctx: &MmArc) -> Result<Arc<NftCtx>, String> {
Ok(try_s!(from_ctx(&ctx.nft_ctx, move || {
from_ctx(&ctx.nft_ctx, move || {
let async_sqlite_connection = ctx
.async_sqlite_connection
.get()
.ok_or("async_sqlite_connection is not initialized".to_owned())?;
Ok(NftCtx {
nft_cache_db: async_sqlite_connection.clone(),
})
})))
})
}

#[cfg(target_arch = "wasm32")]
Expand Down
11 changes: 6 additions & 5 deletions mm2src/coins/tx_history_storage/sql_tx_history_storage_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,12 @@ pub struct SqliteTxHistoryStorage(Arc<Mutex<Connection>>);

impl SqliteTxHistoryStorage {
pub fn new(ctx: &MmArc) -> Result<Self, MmError<CreateTxHistoryStorageError>> {
let sqlite_connection = ctx
.sqlite_connection
.ok_or(MmError::new(CreateTxHistoryStorageError::Internal(
"sqlite_connection is not initialized".to_owned(),
)))?;
let sqlite_connection =
ctx.sqlite_connection
.get()
.ok_or(MmError::new(CreateTxHistoryStorageError::Internal(
"sqlite_connection is not initialized".to_owned(),
)))?;
Ok(SqliteTxHistoryStorage(sqlite_connection.clone()))
}
}
Expand Down
9 changes: 6 additions & 3 deletions mm2src/coins/utxo/utxo_block_header_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Debug for BlockHeaderStorage {
impl BlockHeaderStorage {
#[cfg(all(not(test), not(target_arch = "wasm32")))]
pub(crate) fn new_from_ctx(ctx: MmArc, ticker: String) -> Result<Self, BlockHeaderStorageError> {
let sqlite_connection = ctx.sqlite_connection.ok_or(BlockHeaderStorageError::Internal(
let sqlite_connection = ctx.sqlite_connection.get().ok_or(BlockHeaderStorageError::Internal(
"sqlite_connection is not initialized".to_owned(),
))?;
Ok(BlockHeaderStorage {
Expand All @@ -50,8 +50,11 @@ impl BlockHeaderStorage {
use db_common::sqlite::rusqlite::Connection;
use std::sync::{Arc, Mutex};

let conn = Arc::new(Mutex::new(Connection::open_in_memory().unwrap()));
let conn = ctx.sqlite_connection.clone_or(conn);
let conn = ctx
.sqlite_connection
.get()
.cloned()
.unwrap_or_else(|| Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));

Ok(BlockHeaderStorage {
inner: Box::new(SqliteBlockHeadersStorage { ticker, conn }),
Expand Down
20 changes: 11 additions & 9 deletions mm2src/coins/z_coin/storage/blockdb/blockdb_sql_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ impl BlockDbImpl {
async_blocking(move || {
let conn = Connection::open(path).map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
let conn = Arc::new(Mutex::new(conn));
let conn_clone = conn.clone();
let conn_clone = conn_clone.lock().unwrap();
run_optimization_pragmas(&conn_clone).map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
conn_clone
let conn_lock = conn.lock().unwrap();
run_optimization_pragmas(&conn_lock).map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
conn_lock
.execute(
"CREATE TABLE IF NOT EXISTS compactblocks (
height INTEGER PRIMARY KEY,
Expand All @@ -61,6 +60,7 @@ impl BlockDbImpl {
[],
)
.map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
drop(conn_lock);

Ok(Self { db: conn, ticker })
})
Expand All @@ -73,11 +73,12 @@ impl BlockDbImpl {
async_blocking(move || {
let conn = ctx
.sqlite_connection
.clone_or(Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));
let conn_clone = conn.clone();
let conn_clone = conn_clone.lock().unwrap();
run_optimization_pragmas(&conn_clone).map_err(|err| ZcoinStorageError::DbError(err.to_string()))?;
conn_clone
.get()
.cloned()
.unwrap_or_else(|| Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));
let conn_lock = conn.lock().unwrap();
run_optimization_pragmas(&conn_lock).map_err(|err| ZcoinStorageError::DbError(err.to_string()))?;
conn_lock
.execute(
"CREATE TABLE IF NOT EXISTS compactblocks (
height INTEGER PRIMARY KEY,
Expand All @@ -86,6 +87,7 @@ impl BlockDbImpl {
[],
)
.map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;
drop(conn_lock);

Ok(BlockDbImpl { db: conn, ticker })
})
Expand Down
8 changes: 5 additions & 3 deletions mm2src/crypto/src/crypto_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,12 @@ impl CryptoCtx {
*ctx_field = Some(result.clone());
drop(ctx_field);

ctx.rmd160.pin(rmd160).map_to_mm(CryptoInitError::Internal)?;
ctx.rmd160
.set(rmd160)
.map_to_mm(|_| CryptoInitError::Internal("Already Initialized".to_string()))?;
ctx.shared_db_id
.pin(shared_db_id)
.map_to_mm(CryptoInitError::Internal)?;
.set(shared_db_id)
.map_to_mm(|_| CryptoInitError::Internal("Already Initialized".to_string()))?;

info!("Public key hash: {rmd160}");
info!("Shared Database ID: {shared_db_id}");
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_bin_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn mm2_status() -> MainStatus {
Err(_) => return MainStatus::NoRpc,
};

if ctx.rpc_started.copy_or(false) {
if *ctx.rpc_started.get().unwrap_or(&false) {
MainStatus::RpcIsUp
} else {
MainStatus::NoRpc
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_bin_lib/src/mm2_wasm_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub async fn mm2_rpc(payload: JsValue) -> Result<JsValue, JsValue> {
Err(_) => return Err(Mm2RpcErr::NotRunning.into()),
};

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

serialize_to_js(&response).map_err(|e| {
Expand Down
3 changes: 1 addition & 2 deletions mm2src/mm2_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ common = { path = "../common" }
db_common = { path = "../db_common" }
derive_more = "0.99"
futures = { version = "0.3", package = "futures", features = ["compat", "async-await", "thread-pool"] }
gstuff = { version = "0.7", features = ["nightly"] }
hex = "0.4.2"
lazy_static = "1.4"
libp2p = { git = "https://github.com/KomodoPlatform/rust-libp2p.git", tag = "k-0.52.4", default-features = false, features = ["identify"] }
Expand All @@ -31,13 +32,11 @@ shared_ref_counter = { path = "../common/shared_ref_counter" }
uuid = { version = "1.2.2", features = ["fast-rng", "serde", "v4"] }

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

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

0 comments on commit ac926dd

Please sign in to comment.