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

feat(core): multi-pubkey DB support #2093

Open
wants to merge 207 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 183 commits
Commits
Show all changes
207 commits
Select commit Hold shift + click to select a range
aa0909f
WIP: resolve db paths by account keys
onur-ozkan Mar 28, 2024
f051600
WIP: handle db ids optionally
onur-ozkan Mar 29, 2024
c8a434e
use dummy values to fix build errors for development
onur-ozkan Apr 1, 2024
3e0061f
fix conflicts after merge
borngraced Apr 9, 2024
c6146d0
more wip changes and start coin pubkey for indexeddb storage
borngraced Apr 9, 2024
eff374c
Z coin or default pubkey for initializing blocksdb
borngraced Apr 9, 2024
fef8a4e
zcoin - allow db_id for walletdb
borngraced Apr 10, 2024
6247766
wip — SwapsContext
borngraced Apr 10, 2024
fa0f5ab
changes to maker and taker swap db_id
borngraced Apr 10, 2024
947ab8d
complete zcoin dbs multi pubkey support for WASM target
borngraced Apr 10, 2024
ae95d4e
fix clippy/fmt
borngraced Apr 10, 2024
c0d13e7
impl account_db_id for wasm target
borngraced Apr 10, 2024
71c4e9c
complete some todos in swap mod
borngraced Apr 10, 2024
be3dc81
minor changes to DbIdentifier constructor(new)
borngraced Apr 10, 2024
ffbc262
changes to swaps db
borngraced Apr 11, 2024
07c1168
handle recv_swap_msg with db_id
borngraced Apr 11, 2024
1ec96bc
add todos for StateMachineStorage for TakerSwapStorage
borngraced Apr 11, 2024
3ccfa00
add todos for StateMachineStorage for TakerSwapStorage
borngraced Apr 11, 2024
29763e2
get_swap_repr
borngraced Apr 11, 2024
cf749eb
mark account_db_id as todo for native target
borngraced Apr 11, 2024
368441c
impl find_unique_active_account_ids and use for swap_kick_starts
borngraced Apr 11, 2024
7095605
make swap_v2 multi_key
borngraced Apr 11, 2024
8823be2
WIP - start nft multi key support
borngraced Apr 11, 2024
42a22b1
WIP - my_swaps_storage
borngraced Apr 12, 2024
23f0566
fix field assignment outside of initializer
borngraced Apr 12, 2024
65a07d2
WIP - uxto_block_header_storage
borngraced Apr 12, 2024
71ae0b1
wip sqlite
borngraced Apr 12, 2024
61c49bb
merge find_unique_account_ids and find_unique_active_account_ids
borngraced Apr 13, 2024
d184c1e
Merge branch 'dev' into multi-pubkey-dbs
borngraced Apr 13, 2024
bd114fc
refactor find_unique_account_ids
borngraced Apr 14, 2024
f58c80a
debug with logger
borngraced Apr 14, 2024
a99c8a6
wasm clipy
borngraced Apr 14, 2024
5298f2b
WIP nft
borngraced Apr 15, 2024
d9d8e53
use a single find_unique_nft_account_ids for all targets
borngraced Apr 16, 2024
1538f81
make account_db_id return None by default
borngraced Apr 16, 2024
c0355fa
WIP shared, async, sqlite connection manager impl
borngraced Apr 16, 2024
a27ec3e
WIP sql_ctx
borngraced Apr 16, 2024
3407835
WIP sqlite connection manager
borngraced Apr 17, 2024
c27206c
make account_id optional and return None by default
borngraced Apr 17, 2024
ca13b41
WIP rollback sql_ctx
borngraced Apr 17, 2024
3f053bd
WIP rollback sql_ctx
borngraced Apr 17, 2024
f0e8016
rollback sql_ctx
borngraced Apr 17, 2024
f5e5245
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Apr 17, 2024
f8d08cf
fix lint
borngraced Apr 17, 2024
2d96232
find_unique_nft_account_ids
borngraced Apr 17, 2024
d2152f8
improve find_unique_nft_account_ids_active and update get_nft_list to…
borngraced Apr 18, 2024
04d2896
update get_nft_transfers to fetch transfers concurrently for multi db_id
borngraced Apr 18, 2024
879438b
use try_join_all for efficiency
borngraced Apr 18, 2024
234a2aa
finish implement async_sqlite_storage_vw for multi key support
borngraced Apr 19, 2024
e2dacfc
finish refactoring nft ctx for multi key support db
borngraced Apr 19, 2024
a00c2c7
replace async_sqlite_connection with v2
borngraced Apr 19, 2024
04e1d97
rename AsyncConnectionArc to AsyncSqliteConnectionArc
borngraced Apr 19, 2024
f904927
WIP sync_sqlite_connection_v2
borngraced Apr 19, 2024
ea778d2
WIP utxo_block_header storage refacting
borngraced Apr 19, 2024
8fa9cec
WIP utxo_block_header storage refacting test fn
borngraced Apr 19, 2024
86649f5
WIP simplify new_from_ctx test fn
borngraced Apr 19, 2024
0699843
WIP sqlite_connection_v2
borngraced Apr 19, 2024
3480e26
fmt
borngraced Apr 19, 2024
b9375e0
finish sqlite refactoring and rename sqlite_connection_v2 to sql_conn…
borngraced Apr 20, 2024
4c3a61c
cargo fmt
borngraced Apr 20, 2024
12537c0
initialize only default pubkey in lp_init
borngraced Apr 20, 2024
77b43f5
cargo clippy
borngraced Apr 20, 2024
fbe1fe2
stats_nodes multi key db
borngraced Apr 21, 2024
795f718
fix find_unique_account_ids todos
borngraced Apr 21, 2024
42f5db0
fix merge conflicts
borngraced Apr 21, 2024
88754f7
minor changes
borngraced Apr 22, 2024
cc3a02f
cargo fmt
borngraced Apr 22, 2024
ee8d9d0
wip changes
borngraced Apr 23, 2024
acd6383
wip changes db_id
borngraced Apr 23, 2024
4d04128
organize lp_coin mods/imports
borngraced Apr 23, 2024
e508b59
wip lp_ordermatch db update
borngraced Apr 23, 2024
d7c4a83
orders_kick_start
borngraced Apr 24, 2024
3a7fa66
wasm shareddb storage
borngraced Apr 24, 2024
4e7ae81
clippy & fmt
borngraced Apr 24, 2024
69d9886
wip lp_ordermatch
borngraced Apr 25, 2024
8796922
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Apr 25, 2024
875c9df
cleanup lp_ordermatch.rs
borngraced Apr 25, 2024
4f7824d
merge dev branch and fix conflicts
borngraced Apr 26, 2024
4bba3fb
new sql initialization TODOs
borngraced Apr 26, 2024
637286d
todo for db_id in orders_kick_start
borngraced Apr 26, 2024
7cd60f8
minor changes/improvements
borngraced Apr 29, 2024
b03a0aa
improve and refactor async ocnnection management logic
borngraced Apr 30, 2024
98cb993
wip AccountContext
borngraced Apr 30, 2024
061d1ff
minor changes
borngraced Apr 30, 2024
398b93b
sae dev state
borngraced May 1, 2024
e2877a8
save dev state
borngraced May 1, 2024
577e18b
remove SwapsContext db_id param
borngraced May 1, 2024
9eaeb2e
fix tests
borngraced May 1, 2024
9882ccc
update ordermatch
borngraced May 1, 2024
8a2f4a7
debug db_id f or TakerOrder
borngraced May 1, 2024
4594ff6
fix ordermatch test bug and improve db_id getter
borngraced May 2, 2024
9ff52de
fix wasm test
borngraced May 2, 2024
49b9260
check recent swaps test
borngraced May 2, 2024
6db0236
debug check_my_reecent_swaps rpc
borngraced May 2, 2024
f970a12
remove debug line
borngraced May 2, 2024
094a659
update swaplock with db_id
borngraced May 3, 2024
92b94ac
cargo fmt
borngraced May 3, 2024
6d51390
sql_connection_pool
borngraced May 6, 2024
a4c27b9
fix wasm clippy and add doc comments
borngraced May 6, 2024
1c08ef5
more doc comments
borngraced May 6, 2024
a6d0b51
add hd wallet todos
borngraced May 6, 2024
35b102a
add todos for AccountContext
borngraced May 6, 2024
474d641
impl AsyncSqliteConnPool
borngraced May 6, 2024
0686d0e
code organization and improvements
borngraced May 6, 2024
ca440df
merge dev and fix conflicts
borngraced May 7, 2024
719862b
minor changes
borngraced May 7, 2024
8921e4a
merge dev and fix conflicts
borngraced May 7, 2024
6d6169f
fix hd_wallet todos
borngraced May 7, 2024
6954d62
add more todos
borngraced May 8, 2024
79e7c89
add nft unit test for multikeydb
borngraced May 9, 2024
20a4486
make mm_ctx_with_custom_async_db non wasm32 target
borngraced May 9, 2024
7a2ebaf
refactor db_lock and use tokio sync for handling lock
borngraced May 10, 2024
fdb4f55
fix hd_wallet
borngraced May 11, 2024
6c93752
fix wasm clippy 0
borngraced May 11, 2024
db397e5
fix wasm clippy
borngraced May 12, 2024
393e825
cargo fmt
borngraced May 12, 2024
6e515ae
cargo fmt
borngraced May 12, 2024
22319bc
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced May 14, 2024
b036c62
implement account db_id for tendermint coin
borngraced May 14, 2024
9dcb662
implement account db_id for tendermint coin
borngraced May 14, 2024
0ac682d
use coin db_id in init_db and a minor change to account_db_ids loop
borngraced May 14, 2024
c409ecc
cleanup test mod in hd_wallet/storage/mod
borngraced May 14, 2024
23bb803
drop connections as early as possible
borngraced May 14, 2024
8281609
refactor db_lock and substitute Mutex for RwLock
borngraced May 15, 2024
115a873
revert db_lock connection list lock handling to mutex
borngraced May 15, 2024
9062bee
improve my_orders_storage
borngraced May 16, 2024
b370bec
implement DbMigrationWatcher
borngraced May 16, 2024
46f8712
handle shared_db_id and minor fixes
borngraced May 17, 2024
5bde012
polish sql_connection_pool and fix review notes
borngraced May 17, 2024
e036383
cargo fmt
borngraced May 17, 2024
fab32e2
abstract db migration from tendermint activation fn and fix other rev…
borngraced May 20, 2024
789ce8d
minor changes to DbMigrationWatcher
borngraced May 20, 2024
ac5cc96
update coins_needed_for_kick_start for new db activation
borngraced May 20, 2024
d8caebd
minor fix
borngraced May 20, 2024
76efdf3
comment out gui_storage_dispatcher and related
borngraced May 21, 2024
24774a4
merge with dev and fix conflicts
borngraced May 21, 2024
6a0d0b6
comment out gui_storage_accounts_functionality unit tests
borngraced May 21, 2024
fbfe747
fix review notes
borngraced May 22, 2024
097a63e
merge with dev and fix conflicts
borngraced May 27, 2024
8a6d0cd
fix error handling
borngraced May 27, 2024
f685f72
fix hd_wallet todos
borngraced May 27, 2024
df3cf1e
remove unneeded comments
borngraced May 27, 2024
c028061
mark use_wacher as true
borngraced May 28, 2024
309a5ac
fix sqlite deadlock and improve api design
borngraced May 29, 2024
3cfd616
cargo clippy
borngraced May 29, 2024
0b5275c
remove todos after review
borngraced May 30, 2024
a53bd38
fix build error
borngraced May 30, 2024
ff42791
revert hd_wallet storage changes
borngraced Jun 4, 2024
4b99dba
impl account_db_id for eth and make account_db_id async
borngraced Jun 6, 2024
2d427d9
impl account_db_id for utxo coins
borngraced Jun 7, 2024
b516435
impl account_db_id for utxo and eth
borngraced Jun 10, 2024
497a006
fix clippy
borngraced Jun 10, 2024
df10264
fix native unit tests and other fixes
borngraced Jun 10, 2024
d32f8f6
fix clippy
borngraced Jun 10, 2024
966607d
fix bugs and improve code
borngraced Jun 11, 2024
19930d3
rename account_shared_db_id to shared_db_id
borngraced Jun 12, 2024
527efbc
use HDACcount shared_db_id for tx_history
borngraced Jun 12, 2024
bb41f41
cleanups and improve eth impl
borngraced Jun 12, 2024
916ce6a
fix clear nft todo
borngraced Jun 12, 2024
0f7c075
add docs and very minor improvs
borngraced Jun 13, 2024
bf12b01
minor changes
borngraced Jun 18, 2024
41b61c1
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Jun 18, 2024
54714eb
use device rmd160 for hd_wallet storage
borngraced Jun 19, 2024
7699f55
implement account_db_id for pirate and fix othe review notes
borngraced Jun 20, 2024
e9ef701
use appropriate pubkey in UtxoCoinBuilder::build
borngraced Jun 20, 2024
1b3931b
save shared_db_id as string and use lock
borngraced Jun 24, 2024
4962351
use path_to_db_root in path_to_dbdir
borngraced Jun 24, 2024
f449cdd
make getter fns non async
borngraced Jun 24, 2024
a4ac369
fix other review notes
borngraced Jun 24, 2024
99e6ca6
improve migration watcher -handling
borngraced Jun 24, 2024
a94c791
fix TendermintKeyPair derivation and minor fixes
borngraced Jun 25, 2024
33ba505
hd wallet and some review notes fixes
borngraced Jun 26, 2024
8a01341
fix clippy
borngraced Jun 26, 2024
b19d2ba
minor improvements and fixes
borngraced Jun 27, 2024
f35f371
fix review notes
borngraced Jun 27, 2024
0856b54
merge with dev and fix clippy
borngraced Jun 28, 2024
9a1f89d
cargo fmt
borngraced Jun 28, 2024
5fdee3e
Fix swap restart issue with multi-pubkey DBs
borngraced Jul 1, 2024
9a8494e
fix review notes
borngraced Jul 1, 2024
7c12cce
revert Cargo.toml
borngraced Jul 1, 2024
0b4d808
fix shared_db_id derivation and review notes
borngraced Jul 4, 2024
90f89b7
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Jul 5, 2024
8f19481
proper shared_db handling and improvements
borngraced Jul 5, 2024
e643ab3
merge and fix merge conflict
borngraced Jul 8, 2024
a78704d
fix review notes
borngraced Jul 9, 2024
bdd79af
minor fix
borngraced Jul 9, 2024
7e3d3a6
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Jul 12, 2024
5470b25
fix review notes
borngraced Jul 19, 2024
d57e394
merge with dev and fix conflicts
borngraced Jul 19, 2024
b690e93
fix clippy
borngraced Jul 22, 2024
1675a53
fix review notes and remove left over
borngraced Jul 24, 2024
5e02fa4
use display_eth_address
borngraced Jul 28, 2024
5f72e83
minor changes and improvements
borngraced Jul 29, 2024
a0cf3cb
cargo fmt
borngraced Jul 29, 2024
0c817e8
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Jul 30, 2024
34f9d27
allow swap continuation with correct db_id on swap restart for swap_v2
borngraced Jul 30, 2024
79f3792
improve swap_v2 swap kickstart and eth pubkey deriv
borngraced Jul 30, 2024
117aa44
Fix: Improve db_id validation for taker order processing
borngraced Aug 1, 2024
2f3086c
cleanups
borngraced Aug 2, 2024
c6b3648
merge with dev and fix conflicts
borngraced Aug 5, 2024
06ce83b
save dev state
borngraced Aug 6, 2024
eb9d74a
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
borngraced Aug 6, 2024
5cbfd89
fix clippy
borngraced Aug 6, 2024
cea464b
fix orders_kick_start
borngraced Aug 7, 2024
07a478c
fix minor review notes
borngraced Aug 7, 2024
342fab8
Merge remote-tracking branch 'origin/dev' into multi-pubkey-dbs
shamardy Sep 14, 2024
07177cf
post merge formating
shamardy Sep 14, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ incremental = true
codegen-units = 256

[profile.release.package.mocktopus]
opt-level = 1 # TODO: MIR fails on optimizing this dependency, remove that..
opt-level = 1 # TODO: MIR fails on optimizing this dependency, remove that..
173 changes: 97 additions & 76 deletions mm2src/coins/eth.rs

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions mm2src/coins/eth/v2_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::nft::nft_structs::Chain;
use common::executor::AbortedError;
use crypto::{trezor::TrezorError, Bip32Error, CryptoCtxError, HwError};
use enum_derives::EnumFromTrait;
#[cfg(not(target_arch = "wasm32"))] use futures::SinkExt;
use instant::Instant;
use mm2_err_handle::common_errors::WithInternal;
#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -662,6 +663,9 @@ pub(crate) async fn build_address_and_priv_key_policy(
.map_to_mm(|e| EthActivationV2Error::InternalError(e.to_string()))?;
let bip39_secp_priv_key = global_hd_ctx.root_priv_key().clone();

#[cfg(not(target_arch = "wasm32"))]
run_db_migration_for_new_eth_pubkey(ctx, dhash160(activated_key.public().as_bytes()).to_string()).await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also use the same db_id as eth_account_db_id stated in my first comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


let hd_wallet_rmd160 = *ctx.rmd160();
let hd_wallet_storage = HDWalletCoinStorage::init_with_rmd160(ctx, ticker.to_string(), hd_wallet_rmd160)
.await
Expand All @@ -676,6 +680,7 @@ pub(crate) async fn build_address_and_priv_key_policy(
enabled_address: *path_to_address,
gap_limit,
};

let derivation_method = DerivationMethod::HDWallet(hd_wallet);
Ok((
EthPrivKeyPolicy::HDWallet {
Expand Down Expand Up @@ -916,3 +921,39 @@ fn compress_public_key(uncompressed: H520) -> MmResult<H264, EthActivationV2Erro
let compressed = public_key.serialize();
Ok(H264::from(compressed))
}

#[cfg(not(target_arch = "wasm32"))]
async fn run_db_migration_for_new_eth_pubkey(ctx: &MmArc, db_id: String) -> MmResult<(), EthActivationV2Error> {
info!("Public key hash: {db_id:?}");

let db_migration_sender = ctx
.db_migration_watcher
.as_option()
.expect("Db migration watcher isn't intialized yet!")
.get_sender();
let mut db_migration_sender = db_migration_sender.lock().await;
db_migration_sender
.send(db_id)
.await
.map_to_mm(|err| EthActivationV2Error::InternalError(err.to_string()))?;

Ok(())
}

pub(super) async fn eth_shared_db_id(coin: &EthCoin, ctx: &MmArc) -> Option<String> {
// Use the hd_wallet_rmd160 as the db_id in HD mode since it's unique to a device and not tied to a single address
coin.derivation_method()
.hd_wallet()
.map(|_| ctx.default_shared_db_id().to_string())
}

pub(super) async fn eth_account_db_id(coin: &EthCoin) -> Option<String> {
if let Some(hd_wallet) = coin.derivation_method().hd_wallet() {
return hd_wallet
.get_enabled_address()
.await
.map(|addr| hex::encode(dhash160(addr.pubkey().as_bytes())));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the address without the 0x prefix as the eth_account_db_id which will be an H160 pubkey hash similar to other db_ids.

Copy link
Collaborator

@shamardy shamardy Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use same pubkey hash as utxo and tendermint for external EVM wallets btw but it will be a bit more complex to do so. We request from the user to sign a message and extract the public key from it, it's actually implemented for metamask here for uncompressed format

let (hash, sig) = metamask_session
.sign_typed_data_v4(eth_account_str.clone(), adex_eip712_request(domain, request))
.await?;
let sig = sig.strip_prefix("0x").unwrap_or(&sig);
let signature = Signature::from_str(sig)
.map_to_mm(|_| MetamaskError::Internal(format!("'{sig}' signature is invalid")))?;
let pubkey = recover_pubkey(hash, signature).mm_err(|_| {
let error = format!("Couldn't recover a public key from the signature: '{sig}'");
MetamaskError::Internal(error)
})?;
let recovered_address = address_from_pubkey_uncompressed(pubkey);
if eth_account != recovered_address {
let error =
format!("Recovered address '{recovered_address:?}' should be the same as '{eth_account:?}'");
return MmError::err(MetamaskError::Internal(error));
}

But we use compressed format for getting pubkey hash for utxo and tendermint so we should do the same for EVM. I will check if I can get the same public key from metamask as the one from Keplr for the same seed this way.

Drawback of this is that for external wallets, we have to let the user sign a message at the start (while coin is activating) to get the public key.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

None
borngraced marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion mm2src/coins/hd_wallet/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::ops::Deref;
#[cfg(target_arch = "wasm32")] mod wasm_storage;

#[cfg(any(test, target_arch = "wasm32"))] mod mock_storage;

#[cfg(any(test, target_arch = "wasm32"))]
pub(crate) use mock_storage::HDWalletMockStorage;

Expand Down Expand Up @@ -220,11 +221,11 @@ impl Default for HDWalletCoinStorage {

impl HDWalletCoinStorage {
pub async fn init(ctx: &MmArc, coin: String) -> HDWalletStorageResult<HDWalletCoinStorage> {
let inner = Box::new(HDWalletStorageInstance::init(ctx).await?);
let crypto_ctx = CryptoCtx::from_ctx(ctx)?;
let hd_wallet_rmd160 = crypto_ctx
.hw_wallet_rmd160()
.or_mm_err(|| HDWalletStorageError::HDWalletUnavailable)?;
let inner = Box::new(HDWalletStorageInstance::init(ctx).await?);
Ok(HDWalletCoinStorage {
coin,
hd_wallet_rmd160,
Expand Down
7 changes: 4 additions & 3 deletions mm2src/coins/hd_wallet/storage/sqlite_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ impl HDWalletStorageInternalOps for HDWalletSqliteStorage {
where
Self: Sized,
{
let shared = ctx.shared_sqlite_conn.as_option().or_mm_err(|| {
let shared = ctx.shared_sqlite_conn_opt().or_mm_err(|| {
HDWalletStorageError::Internal("'MmCtx::shared_sqlite_conn' is not initialized".to_owned())
})?;
let storage = HDWalletSqliteStorage {
conn: SqliteConnShared::downgrade(shared),
conn: SqliteConnShared::downgrade(&shared),
};
storage.init_tables().await?;
Ok(storage)
Expand Down Expand Up @@ -279,7 +279,8 @@ pub(crate) async fn get_all_storage_items(ctx: &MmArc) -> Vec<HDAccountStorageIt
const SELECT_ALL_ACCOUNTS: &str =
"SELECT account_id, account_xpub, external_addresses_number, internal_addresses_number FROM hd_account";

let conn = ctx.shared_sqlite_conn();
let conn = ctx.shared_sqlite_conn_opt().unwrap();
let conn = conn.lock().unwrap();
let mut statement = conn.prepare(SELECT_ALL_ACCOUNTS).unwrap();
statement
.query_map([], |row: &Row<'_>| HDAccountStorageItem::try_from(row))
Expand Down
29 changes: 16 additions & 13 deletions mm2src/coins/hd_wallet/storage/wasm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const WALLET_ID_INDEX: &str = "wallet_id";
/// * account_id - HD account id
const WALLET_ACCOUNT_ID_INDEX: &str = "wallet_account_id";

type HDWalletDbLocked<'a> = DbLocked<'a, HDWalletDb>;
type HDWalletDbLocked = DbLocked<HDWalletDb>;

impl From<DbTransactionError> for HDWalletStorageError {
fn from(e: DbTransactionError) -> Self {
Expand Down Expand Up @@ -53,17 +53,17 @@ impl From<CursorError> for HDWalletStorageError {
let stringified_error = e.to_string();
match e {
// We don't expect that the `String` and `u32` types serialization to fail.
CursorError::ErrorSerializingIndexFieldValue {..}
CursorError::ErrorSerializingIndexFieldValue { .. }
// We don't expect that the `String` and `u32` types deserialization to fail.
| CursorError::ErrorDeserializingIndexValue {..}
| CursorError::ErrorOpeningCursor {..}
| CursorError::AdvanceError {..}
| CursorError::InvalidKeyRange {..}
| CursorError::TypeMismatch {..}
| CursorError::IncorrectNumberOfKeysPerIndex {..}
| CursorError::ErrorDeserializingIndexValue { .. }
| CursorError::ErrorOpeningCursor { .. }
| CursorError::AdvanceError { .. }
| CursorError::InvalidKeyRange { .. }
| CursorError::TypeMismatch { .. }
| CursorError::IncorrectNumberOfKeysPerIndex { .. }
| CursorError::UnexpectedState(..)
| CursorError::IncorrectUsage {..} => HDWalletStorageError::Internal(stringified_error),
CursorError::ErrorDeserializingItem {..} => HDWalletStorageError::ErrorDeserializing(stringified_error),
| CursorError::IncorrectUsage { .. } => HDWalletStorageError::Internal(stringified_error),
CursorError::ErrorDeserializingItem { .. } => HDWalletStorageError::ErrorDeserializing(stringified_error),
}
}
}
Expand Down Expand Up @@ -270,8 +270,10 @@ impl HDWalletIndexedDbStorage {
.or_mm_err(|| HDWalletStorageError::Internal("'HDWalletIndexedDbStorage::db' doesn't exist".to_owned()))
}

async fn lock_db_mutex(db: &SharedDb<HDWalletDb>) -> HDWalletStorageResult<HDWalletDbLocked<'_>> {
db.get_or_initialize().await.mm_err(HDWalletStorageError::from)
async fn lock_db_mutex(db: &SharedDb<HDWalletDb>) -> HDWalletStorageResult<HDWalletDbLocked> {
db.get_or_initialize_shared(None)
.await
.mm_err(HDWalletStorageError::from)
}

async fn find_account(
Expand Down Expand Up @@ -314,9 +316,10 @@ impl HDWalletIndexedDbStorage {
}

/// This function is used in `hd_wallet_storage::tests`.
#[cfg(any(test, target_arch = "wasm32"))]
pub(super) async fn get_all_storage_items(ctx: &MmArc) -> Vec<HDAccountStorageItem> {
let coins_ctx = CoinsContext::from_ctx(ctx).unwrap();
let db = coins_ctx.hd_wallet_db.get_or_initialize().await.unwrap();
let db = coins_ctx.hd_wallet_db.get_or_initialize_shared(None).await.unwrap();
let transaction = db.inner.transaction().await.unwrap();
let table = transaction.table::<HDAccountTable>().await.unwrap();
table
Expand Down
16 changes: 6 additions & 10 deletions mm2src/coins/lightning/ln_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub type ChannelManager = SimpleArcChannelManager<ChainMonitor, Platform, Platfo
pub type Router = DefaultRouter<Arc<NetworkGraph>, Arc<LogState>, Arc<Scorer>>;

#[inline]
fn ln_data_dir(ctx: &MmArc, ticker: &str) -> PathBuf { ctx.dbdir().join("LIGHTNING").join(ticker) }
fn ln_data_dir(ctx: &MmArc, ticker: &str) -> PathBuf { ctx.dbdir(None).join("LIGHTNING").join(ticker) }

#[inline]
fn ln_data_backup_dir(ctx: &MmArc, path: Option<String>, ticker: &str) -> Option<PathBuf> {
Expand Down Expand Up @@ -68,15 +68,11 @@ pub async fn init_persister(
Ok(persister)
}

pub async fn init_db(ctx: &MmArc, ticker: String) -> EnableLightningResult<SqliteLightningDB> {
let db = SqliteLightningDB::new(
ticker,
ctx.sqlite_connection
.ok_or(MmError::new(EnableLightningError::DbError(
"sqlite_connection is not initialized".into(),
)))?
.clone(),
)?;
pub async fn init_db(ctx: &MmArc, ticker: String, db_id: Option<&str>) -> EnableLightningResult<SqliteLightningDB> {
let db = ctx.sqlite_conn_opt(db_id).or_mm_err(|| {
EnableLightningError::DbError("'MmCtx::sqlite_connection' is not found or initialized".to_owned())
})?;
let db = SqliteLightningDB::new(ticker, db)?;

if !db.is_db_initialized().await? {
db.init_db().await?;
Expand Down
Loading
Loading