Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ctx): replace gstuff constructible with oncelock #2267

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 8 additions & 4 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,11 +50,15 @@ 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_or_init(|| Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));

Ok(BlockHeaderStorage {
inner: Box::new(SqliteBlockHeadersStorage { ticker, conn }),
inner: Box::new(SqliteBlockHeadersStorage {
ticker,
conn: conn.clone(),
}),
})
}

Expand Down
7 changes: 5 additions & 2 deletions mm2src/coins/z_coin/storage/blockdb/blockdb_sql_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl BlockDbImpl {
async_blocking(move || {
let conn = ctx
.sqlite_connection
.clone_or(Arc::new(Mutex::new(Connection::open_in_memory().unwrap())));
.get_or_init(|| 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()))?;
Expand All @@ -87,7 +87,10 @@ impl BlockDbImpl {
)
.map_to_mm(|err| ZcoinStorageError::DbError(err.to_string()))?;

Ok(BlockDbImpl { db: conn, ticker })
Ok(BlockDbImpl {
db: conn.clone(),
ticker,
})
})
.await
}
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
Loading