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(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs #2015

Merged
merged 47 commits into from
Dec 21, 2023

Conversation

artemii235
Copy link
Member

@artemii235 artemii235 commented Nov 27, 2023

#1895

What's done:

  • Implemented WASM storage for upgraded swaps.
  • Implemented old swaps data migration.
  • Swaps kickstart on MM2 reload.
  • Swaps file lock, preventing the same swap from starting from different processes.
  • my_swap_status, my_recent_swaps V2 RPCs.
  • V1 RPCs can also return upgraded swaps data.
  • Added BoolAsInt struct because boolean can be used as IndexedDb indexes.
  • Implemented refund states for both swap types.

Important notes:

  • Seednodes should be updated to support/rebroadcast new swap protocol messages.

Test plan:

Prerequisites for all test cases:

  1. Use only test coins or very small amounts of assets with real market value!
  2. Run two nodes (seed and light) with "use_trading_proto_v2": true in MM2.conf.
  3. Select coins to trade, both must have "protocol": { "type": "UTXO" }.
  4. Initiate trading using setprice and buy/sell calls.
  5. Each case should be checked under both native and WASM targets.

RPC

  1. Start swaps.
  2. "my_swap_status" and "my_recent_swaps" of both RPC versions must return swap data.

Kickstart

  1. Start swaps.
  2. Stop MM node.
  3. Restart node.
  4. coins_needed_for_kick_start must return tickers used in swap.
  5. Reactivate coins.
  6. Swap must kickstart and complete.

Coin deactivation prevention

  1. Start swaps.
  2. Try to disable coin used in active swap.
  3. The RPC must return an error containing UUID of the ongoing swap.

Old swaps data migration

  1. Use the node that already has old swaps in DB.
  2. Backup database.
  3. Run updated MM2.
  4. Open MM2.db (SQLite), check that all completed swaps in "my_swaps" table have "is_finished:1".
  5. Restart MM. Ensure that old completed swaps are not kickstarted.

artemii235 and others added 30 commits October 30, 2023 13:35
…ration-4

# Conflicts:
#	mm2src/mm2_main/src/lp_swap/swap_wasm_db.rs
@artemii235
Copy link
Member Author

@shamardy I've fixed your review notes 🙂

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.

Thank you for the fixes! Next review Iteration!

loop {
match state_machine
.maker_coin
.can_refund_htlc(state_machine.maker_payment_locktime())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should add the case of immediate refund using the taker refund secret here. I am sure you have this on your rader and plan to implement it in next sprints but this is just a reminder :)
Also, recover_funds_of_swap for swaps v2 is probably needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I have this on radar, will do in the next iteration 🙂

mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs Outdated Show resolved Hide resolved
pub(super) fn init_additional_context_impl(ctx: &MmArc, swap_info: ActiveSwapV2Info) {
subscribe_to_topic(ctx, swap_v2_topic(&swap_info.uuid));
let swap_ctx = SwapsContext::from_ctx(ctx).expect("SwapsContext::from_ctx should not fail");
swap_ctx.init_msg_v2_store(swap_info.uuid, bits256::default());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am just wondering why was accept_only_from needed in legacy swap implementation?

Copy link
Member Author

Choose a reason for hiding this comment

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

This part is not finished yet: we should limit processing of the swap messages only to the node, with which we matched our order.

Comment on lines +841 to +842
maker_coin_swap_contract: None,
taker_coin_swap_contract: None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are None here but retrieved in the mirror implementation for taker swap similar to p2p_keypair.

Copy link
Member Author

Choose a reason for hiding this comment

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

These are placeholders for the ETH/ERC-20 support in upgraded swaps.

shamardy
shamardy previously approved these changes Dec 5, 2023
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.

🔥

@shamardy
Copy link
Collaborator

shamardy commented Dec 5, 2023

@borngraced did you finish review for this PR?

.reverse()
.open_cursor("migration")
.await?
// TODO refactor when "closure invoked recursively or after being dropped" is fixed
Copy link
Member

Choose a reason for hiding this comment

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

we started working on this already.
#2019

borngraced
borngraced previously approved these changes Dec 6, 2023
Copy link
Member

@borngraced borngraced left a comment

Choose a reason for hiding this comment

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

great work 🔥

@shamardy
Copy link
Collaborator

shamardy commented Dec 6, 2023

@artemii235 I will merge this PR to dev once 2.0.0-beta is released.

Comment on lines +274 to +287
pub(super) async fn get_current_migration(
migration_table: &DbTable<'_, SwapsMigrationTable>,
) -> MmResult<u32, SavedSwapError> {
let migrations = migration_table
.cursor_builder()
.bound("migration", 0, u32::MAX)
.reverse()
.open_cursor("migration")
.await?
// TODO refactor when "closure invoked recursively or after being dropped" is fixed
.collect()
.await?;

Ok(migrations.first().map(|(_, m)| m.migration).unwrap_or_default())
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 a fix for for this issue now..So I think this pr can be hold till #2028 is merged then you can query first directly instead of collecting all.

@artemii235 @shamardy

Copy link
Member Author

Choose a reason for hiding this comment

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

Perfect, thanks!

…ration-4

# Conflicts:
#	mm2src/mm2_main/src/lp_native_dex.rs
@artemii235 artemii235 dismissed stale reviews from borngraced and shamardy via 8313bc4 December 13, 2023 07:00
Copy link

@ca333 ca333 left a comment

Choose a reason for hiding this comment

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

secure code reviewed

@shamardy shamardy merged commit e61c41e into dev Dec 21, 2023
24 of 31 checks passed
@shamardy shamardy deleted the swap-proto-upgrade-iteration-4 branch December 21, 2023 11:58
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Dec 21, 2023
* dev: (22 commits)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  fix(config): accept a string as rpcport value (KomodoPlatform#2026)
  feat(nft): move db lock, add tx fee and confirmations (KomodoPlatform#1989)
  chore(network): update seednodes for netid 8762 (KomodoPlatform#2024)
  chore(network): add todo on peer storage behaviour (KomodoPlatform#2025)
  chore(network): exclude `168.119.236.249` from the seednode list (KomodoPlatform#2021)
  feat(network): deprecate 7777 network (KomodoPlatform#2020)
  chore(release): bump mm2 version to 2.0.0-beta (KomodoPlatform#2018)
  feat(UTXO swaps): kmd burn plan impl (KomodoPlatform#2006)
  chore(docs): fix the link to simple market maker in README.md (KomodoPlatform#2011)
  refactor(cli): cli dependency updates and warn on bad config perm (KomodoPlatform#1956)
  chore(containers and docs): update docs and container images (KomodoPlatform#2003)
  ...

# Conflicts:
#	mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs
#	mm2src/mm2_test_helpers/src/for_tests.rs
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Jan 23, 2024
* dev: (24 commits)
  chore(release): bump mm2 version to 2.1.0-beta (KomodoPlatform#2044)
  feat(trezor): add segwit support for withdraw with trezor (KomodoPlatform#1984)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  fix(config): accept a string as rpcport value (KomodoPlatform#2026)
  feat(nft): move db lock, add tx fee and confirmations (KomodoPlatform#1989)
  chore(network): update seednodes for netid 8762 (KomodoPlatform#2024)
  chore(network): add todo on peer storage behaviour (KomodoPlatform#2025)
  chore(network): exclude `168.119.236.249` from the seednode list (KomodoPlatform#2021)
  feat(network): deprecate 7777 network (KomodoPlatform#2020)
  chore(release): bump mm2 version to 2.0.0-beta (KomodoPlatform#2018)
  feat(UTXO swaps): kmd burn plan impl (KomodoPlatform#2006)
  chore(docs): fix the link to simple market maker in README.md (KomodoPlatform#2011)
  ...
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Feb 18, 2024
* evm-hd-wallet: (27 commits)
  Fix todo comments
  Fix HDAddressOps::Address trait bounds
  fix(indexeddb): fix IDB cursor.continue_() call after drop (KomodoPlatform#2028)
  security bump for `h2` (KomodoPlatform#2062)
  fix(makerbot): allow more than one prices url in makerbot (KomodoPlatform#2027)
  fix(wasm worker env): refactor direct usage of `window` (KomodoPlatform#1953)
  feat(nft): nft abi in withdraw_nft RPC, clear_nft_db RPC (KomodoPlatform#2039)
  refactor(utxo): refactor utxo output script creation (KomodoPlatform#1960)
  feat(ETH): balance event streaming for ETH (KomodoPlatform#2041)
  chore(release): bump mm2 version to 2.1.0-beta (KomodoPlatform#2044)
  feat(trezor): add segwit support for withdraw with trezor (KomodoPlatform#1984)
  chore(config): remove vscode launchjson (KomodoPlatform#2040)
  feat(trading-proto-upgrade): wasm DB, kickstart, refund states, v2 RPCs (KomodoPlatform#2015)
  feat(UTXO): balance event streaming for Electrum clients (KomodoPlatform#2013)
  feat(tx): add new sign_raw_transaction rpc for UTXO and EVM coins (KomodoPlatform#1930)
  fix(p2p): handle encode_and_sign errors (KomodoPlatform#2038)
  chore(release): add changelog entries for v2.0.0-beta (KomodoPlatform#2037)
  chore(network): write network information to stdout (KomodoPlatform#2034)
  fix(price_endpoints): add cached url (KomodoPlatform#2032)
  deps(network): sync with upstream yamux (KomodoPlatform#2030)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants