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

[WASM] Fixing, refactoring, and implementing P2P, IndexedDB, SwapLock, SavedSwap, MySwaps, MyOrdersStorage #1007

Merged
merged 43 commits into from
Sep 13, 2021

Conversation

sergeyboyko0791
Copy link

@sergeyboyko0791 sergeyboyko0791 commented Jul 7, 2021

  • Refactor IndexedDB API to make it easier to add new databases and tables
    • Move the low-level Indexed DB interface to the separate db_driver module
    • Remove channel logic from tx_history_db (tx_history_db has to communicate with Indexed DB directly)
    • Change type of index_value from &str to Json
    • Remove TxHistoryOps and native TxHistoryDb
    • Return tx_history_path to MmCoin
    • Add DbNamespaceId to MmCtx to allow running multiple MmCtx at the same time
    • Add the stringified rmd160 of the wallet public key to the names of IndexedDb databases.
  • Add and implement SwapLock in WASM
    • Add SwapsContext::swap_db
    • Add SwapLockTable
    • Add FileLockError, FileLockResult to common::file_lock
    • Integrate SwapLock to maker/taker swaps
  • Add P2P in-memory transport
    • Add NodeType::LightInMemory and NodeType::RelayInMemory variants
    • Refactor parsing relay addresses
    • Use in-memory transport in atomicdex_tests
  • Add the 'Build MM2 WASM Debug' step to the Linux CI script
  • Remove defined-in-js.js
  • Design and implement IdbCursor in Rust
  • Finish integrating swaps in WASM
  • Add and implement MyOrdersStorage

* Move the low-level Indexed DB interface to the separate 'db_driver' module
* Remove channel logic from tx_history_db (tx_history_db communicates with Indexed DB directly)
* Change type of index_value from &str to Json
* Move the logic of initializing database from 'CoinsContext::tx_history_db()' to 'indexed_db::db_lock::get_or_initialize_db'
* Remove 'TxHistoryOps' and native 'TxHistoryDb'
* Replace 'TxHistoryError', 'TxHistoryResult' to lp_coins
* Return 'tx_history_path' to MmCoin
* Add 'FileLockError', 'FileLockResult' to 'common::file_lock'
* Add 'SwapsContext::swap_db'
* Add 'SwapLockTable'
* Remove 'SwapLock::last_timestamp'
* Add 'NodeType::LightInMemory' and 'NodeType::RelayInMemory' variants
* Refactor parsing relay addresses
@sergeyboyko0791 sergeyboyko0791 linked an issue Jul 7, 2021 that may be closed by this pull request
@sergeyboyko0791 sergeyboyko0791 marked this pull request as draft July 7, 2021 13:23
@sergeyboyko0791 sergeyboyko0791 self-assigned this Jul 7, 2021
@sergeyboyko0791 sergeyboyko0791 marked this pull request as ready for review July 8, 2021 07:18
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

Looks great! I have a couple of questions 🙂
I will take one more look at this when we discuss them.

mm2src/mm2_tests.rs Outdated Show resolved Hide resolved
mm2src/mm2_libp2p/src/relay_address.rs Show resolved Hide resolved
* Refactor 'MarketMakerIt'
* Add 'common::log_tag' macro
* Configure the 'trade_test_rick_and_morty' and 'test_qrc20_history' tests to run in WASM
# Conflicts:
#	mm2src/mm2_tests.rs
* Move 'SavedSwap' to the separate 'lp_swap/saved_swap' module
* Add 'get_item_by_unique_index', 'replace_item_by_unique_index', 'delete_item_by_unique_index' methods to 'DbTable'
* Refactor 'for_tests::register_wasm_log'
* Add 'common::wasm_http' mod with 'FetchRequest'
* Add driver/cursor.rs, Idb<Single/Multi><Bound>KeyCursor
* Add indexed_cursor.rs, Db<Single/Multi><Bound>KeyCursor
* Add TableUpgrader::create_multi_index
* Add IdbTableImpl::cursor_builder
* Add DbTable::open_cursor
* Add 'spawn_highload_future' to dispatcher_legacy.rs
* Move common::file_lock to common::fs::file_lock
* Move slurp, safe_slurp, file_lock, remove_file, write, read_dir_async, read_dir, json_dir_entries to common::fs
* Use MyOrdersStorage everywhere instead of working with fs/Db manually
* Rename swap_db::MySwapsTable to swap_db::MySwapsFiltersTable
* Move fs functions from lp_swap::saved_swap to common::fs
* Add error_if, warn_if logging macros
* Remove OrdermatchContext::my_cancelled_orders
* Add 'OrdermatchContext::ordermatch_db'
* Add 'MyActive[Maker/Taker]OrdersTable', 'MyHistoryOrdersTable', 'MyFilteringHistoryOrdersTable'
* Kick-start active maker/taker orders on restart
@sergeyboyko0791 sergeyboyko0791 changed the title [WASM] Fixing, refactoring, and implementing P2P, IndexedDB, SwapLock [WASM] Fixing, refactoring, and implementing P2P, IndexedDB, SwapLock, SavedSwap, MySwaps, MyOrdersStorage Aug 11, 2021
* Enable 'order_status' RPC call in WASM
* Rename 'my_swaps_db::MySwaps' to 'my_swaps_storage::MySwapsStorage'
* Rename swap_db.rs to swap_wasm_db.rs
@sergeyboyko0791
Copy link
Author

@artemii235 @shamardy PR is ready for the review 🥳

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

Thanks for the great work!
I've reviewed this PR partially, might have more comments when we solve the questions below.

mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
mm2src/lp_ordermatch/my_orders_storage.rs Outdated Show resolved Hide resolved
mm2src/lp_ordermatch/my_orders_storage.rs Outdated Show resolved Hide resolved
mm2src/common/log.rs Outdated Show resolved Hide resolved
@artemii235
Copy link
Member

@sergeyboyko0791 Could you please also solve git conflicts?

* Remove excessive trait bounds of the `load_history_from_file_impl` function
* Refactor error messages
@sergeyboyko0791
Copy link
Author

sergeyboyko0791 commented Aug 30, 2021

This PR consists of critical changes in many different modules such as Swaps, Orders, Tx History.
Basically, the changes are concerned the process of saving, loading swap/order/tx_history files data to a database.

The test cases:

Transaction history (check in Native and WASM):

  1. The history saves and loads normally;
  2. If the tx history file is broken (for example, invalid JSON character), mm2 has to clear it and reload the transaction history again.

Swaps (in Native only):

  1. The swap data saves and loads normally ⚠️
    My Maker/Taker swaps file path: DB/<ID>/SWAPS/MY/<UUID>.json
  2. Not finished Maker/Taker swaps yet have to be kick-starterted on restart ⚠️
  3. Swap data is broadcast after the finish of a Maker/Taker swap;
  4. all_swaps_uuids_by_filters, import_swaps, my_recent_swaps RPC calls work correctly;
  5. my_swap_status returns correct info of already finished swaps.
  6. If two instances of MarketMaker are run with the same passphrase and with the same DB directory, not finished swaps must be kick-started on only one instance ⚠️

Orders (in Native only):

  1. My active orders save and load normally;
    My Maker/Taker orders file path: DB/<ID>/ORDERS/MY/<Maker/Taker>/<UUID>.json;
  2. Maker/Taker active orders have to be kick-started on mm2 restart;
  3. My Maker/Taker orders has to be saved to DB/<ID>ORDERS/MY/HISTORY/<UUID>.json if save_in_history is true when the orders were matched/canceled;
  4. update_maker_order RPC call works correctly;
  5. my_orders RPC call work correctly for Maker and Taker orders;
  6. orders_history_by_filter RPC call work correctly with many different parameter combinations. Especially for Maker and Taker orders;
  7. order_status returns correct info of already matched/canceled Maker and Taker orders.

artemii235
artemii235 previously approved these changes Sep 1, 2021
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

LGTM! I have left a couple of non-blocker possible refactoring ideas. We can discuss them later. Thanks!

//!
//! # Important
//!
//! Please make sure all keys of the index are specified
Copy link
Member

Choose a reason for hiding this comment

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

It seems that created indexes should be represented in the type system somehow to avoid invalid cursor creation. E.g

enum SwapIndexes {
    BaseRelStartedAt,
    ...
}

It might be a part of the TableSignature trait to pin indexes to a specific table: associated type or another way, just thinking out loud.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the advice! It makes sense and it's nice to have :)
We can consider integrating it later as you said or if we move it to a public crate

use serde_json::Value as Json;

#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct MyActiveMakerOrdersTable {
Copy link
Member

Choose a reason for hiding this comment

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

I have an intuitive feeling that it might be worth splitting a Table and it's item into separate structs. They can be pinned to each other using generics or associated types of the TableSignature trait. It's just an idea, I'm unsure whether it's possible to do so without major refactoring. Maybe it doesn't make much sense in general 🙂

Copy link
Author

Choose a reason for hiding this comment

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

Actually, I didn't understand why it's needed :D
The only reason I see is the confusing names of the structures.

Copy link
Member

Choose a reason for hiding this comment

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

The only reason I see is the confusing names of the structures.

Actually yes, it's confusing because a specific struct represents table and table's item at the same time 🙂

shamardy
shamardy previously approved these changes Sep 1, 2021
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

LGTM! Great Work 🔥

# Conflicts:
#	mm2src/lp_native_dex.rs
#	mm2src/lp_ordermatch.rs
#	mm2src/mm2_tests.rs
@artemii235 artemii235 dismissed stale reviews from shamardy and themself via 7c560ed September 13, 2021 11:48
@artemii235 artemii235 merged commit 42d76e6 into dev Sep 13, 2021
@artemii235 artemii235 deleted the wasm-swaps branch September 13, 2021 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Web browser support (WASM).
3 participants