From 1675a53752aaa5d3a01a5f88566592b42264f990 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Wed, 24 Jul 2024 18:51:28 +0100 Subject: [PATCH] fix review notes and remove left over --- mm2src/coins/eth/v2_activation.rs | 13 ++++++++----- mm2src/mm2_db/src/indexed_db/indexed_db.rs | 18 +++--------------- mm2src/mm2_main/src/lp_native_dex.rs | 15 ++++++++++++--- mm2src/mm2_main/src/lp_ordermatch.rs | 8 ++++++++ 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/mm2src/coins/eth/v2_activation.rs b/mm2src/coins/eth/v2_activation.rs index 34421d0fd5..6a7328931a 100644 --- a/mm2src/coins/eth/v2_activation.rs +++ b/mm2src/coins/eth/v2_activation.rs @@ -742,7 +742,9 @@ pub(crate) async fn build_address_and_priv_key_policy( #[cfg(not(target_arch = "wasm32"))] { - let pubkey = dhash160(activated_key.public().as_bytes()).to_string(); + // Skip the first byte of the uncompressed public key before converting to the eth address. + let pubkey = Public::from_slice(&activated_key.public().as_bytes()[1..]); + let pubkey = public_to_address(&pubkey).to_string(); run_db_migration_for_new_pubkey(ctx, pubkey) .await .map_to_mm(EthActivationV2Error::InternalError)?; @@ -1013,10 +1015,11 @@ pub(super) async fn eth_shared_db_id(coin: &EthCoin, ctx: &MmArc) -> Option Option { match coin.derivation_method() { - DerivationMethod::HDWallet(hd_wallet) => hd_wallet - .get_enabled_address() - .await - .map(|addr| dhash160(addr.pubkey().as_bytes()).to_string()), + DerivationMethod::HDWallet(hd_wallet) => hd_wallet.get_enabled_address().await.map(|addr| { + // Skip the first byte of the uncompressed public key before converting to the eth address. + let pubkey = Public::from_slice(&addr.pubkey().as_bytes()[1..]); + public_to_address(&pubkey).to_string() + }), _ => None, } } diff --git a/mm2src/mm2_db/src/indexed_db/indexed_db.rs b/mm2src/mm2_db/src/indexed_db/indexed_db.rs index 75f2b6a821..21672fbbf8 100644 --- a/mm2src/mm2_db/src/indexed_db/indexed_db.rs +++ b/mm2src/mm2_db/src/indexed_db/indexed_db.rs @@ -111,28 +111,20 @@ impl DbIdentifier { } pub fn display_db_id(&self) -> String { self.db_id.clone().unwrap_or_else(|| "KOMODEFI".to_string()) } - - pub fn db_id(&self) -> String { - self.db_id - .clone() - .unwrap_or_else(|| hex::encode(H160::default().as_slice())) - } } pub struct IndexedDbBuilder { pub db_name: String, pub db_version: u32, pub tables: HashMap, - pub db_id: String, } impl IndexedDbBuilder { - pub fn new(db_id: DbIdentifier) -> IndexedDbBuilder { + pub fn new(db_ident: DbIdentifier) -> IndexedDbBuilder { IndexedDbBuilder { - db_name: db_id.to_string(), + db_name: db_ident.to_string(), db_version: 1, tables: HashMap::new(), - db_id: db_id.db_id(), } } @@ -148,13 +140,12 @@ impl IndexedDbBuilder { } pub async fn build(self) -> InitDbResult { - let db_id = self.db_id.clone(); let (init_tx, init_rx) = oneshot::channel(); let (event_tx, event_rx) = mpsc::unbounded(); self.init_and_spawn(init_tx, event_rx); init_rx.await.expect("The init channel must not be closed")?; - Ok(IndexedDb { event_tx, db_id }) + Ok(IndexedDb { event_tx }) } fn init_and_spawn( @@ -190,7 +181,6 @@ impl IndexedDbBuilder { pub struct IndexedDb { event_tx: DbEventTx, - db_id: String, } async fn send_event_recv_response( @@ -248,8 +238,6 @@ impl IndexedDb { // ignore if the receiver is closed result_tx.send(Ok(transaction_event_tx)).ok(); } - - pub fn get_db_id(&self) -> String { self.db_id.to_string() } } pub struct DbTransaction<'transaction> { diff --git a/mm2src/mm2_main/src/lp_native_dex.rs b/mm2src/mm2_main/src/lp_native_dex.rs index 959ab37f81..4eea14dc37 100644 --- a/mm2src/mm2_main/src/lp_native_dex.rs +++ b/mm2src/mm2_main/src/lp_native_dex.rs @@ -460,7 +460,7 @@ async fn init_db_migration_watcher_loop(ctx: MmArc) { let mut migrations = HashSet::new(); let mut receiver = ctx .init_db_migration_watcher() - .expect("db_m igration_watcher initialization failed"); + .expect("db_migration_watcher initialization failed"); while let Some(db_id) = receiver.next().await { if migrations.contains(&db_id) { @@ -468,13 +468,13 @@ async fn init_db_migration_watcher_loop(ctx: MmArc) { continue; } - // run db migration for db_id if new activated pubkey is unique. + // run db migration for new db_id. if let Err(err) = run_db_migration_impl(&ctx, Some(&db_id), None).await { error!("{err:?}"); continue; }; - // insert new db_id to migration list + // insert new db_id to migrated list migrations.insert(db_id.to_owned()); // Fetch and extend ctx.coins_needed_for_kick_start from new intialized db. @@ -500,6 +500,15 @@ async fn run_db_migration_impl(ctx: &MmArc, db_id: Option<&str>, shared_db_id: O } pub async fn lp_init_continue(ctx: MmArc) -> MmInitResult<()> { + #[cfg(not(target_arch = "wasm32"))] + { + let dbdir = ctx.dbdir(None); + fs::create_dir_all(&dbdir).map_to_mm(|e| MmInitError::ErrorCreatingDbDir { + path: dbdir.clone(), + error: e.to_string(), + })?; + } + init_ordermatch_context(&ctx)?; init_p2p(ctx.clone()).await?; diff --git a/mm2src/mm2_main/src/lp_ordermatch.rs b/mm2src/mm2_main/src/lp_ordermatch.rs index 18ce367371..6f0586ea29 100644 --- a/mm2src/mm2_main/src/lp_ordermatch.rs +++ b/mm2src/mm2_main/src/lp_ordermatch.rs @@ -5428,6 +5428,10 @@ pub async fn orders_kick_start(ctx: &MmArc, db_id: Option<&str>) -> Result) -> Result