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): swap UTXO PoC using Storable State Machine. #1958

Merged
merged 33 commits into from
Sep 21, 2023

Conversation

artemii235
Copy link
Member

@artemii235 artemii235 commented Sep 4, 2023

#1895

What's done:

  • Implemented Storable State Machine abstraction with a goal to have as fewer changes to existing state machines as possible.
  • Implemented successful swap of UTXO to UTXO coin. Tested using dockerized komodod daemons.
  • Swap V2 message exchange is done using Protobuf. Fns like broadcast_swap_v2_message can (and likely should) be used for other protocols utilizing protobuf, but I preferred to keep them in lp_swap for now to not overlap with P2P upgrade PR.
  • Did few other minor refactorings.

Important notes:

  • It covers only success case now, no cancellation/refund is currently available.
  • It uses dummy in-memory storage, swap data is not persisted for now.
  • Swap status is not currently available through RPC API.
  • Additional work is required in terms of swap negotiation steps, required validation of messages, etc.
  • Seednodes should be updated to support/rebroadcast new swap protocol messages.

Test plan:

  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. Following messages should be logged:
    05 04:28:54, mm2_main::mm2::lp_swap::maker_swap_v2:203] INFO Maker swap 36f2e374-9f79-415b-87ff- f1b1622cf7fd has successfully started
    
    05 04:28:54, mm2_main::mm2::lp_swap::taker_swap_v2:197] INFO Taker swap 36f2e374-9f79-415b-87ff-f1b1622cf7fd has successfully started
    
  6. Swap transactions info is available only via logs now, e.g.
    05 04:28:57, mm2_main::mm2::lp_swap::maker_swap_v2:433] INFO Sent maker payment MYCOIN tx d5e3c9abd522eaf68e5ba699547632e27fd77985d1b3d56228b24ec7544a4dcd during swap 36f2e374-9f79-415b-87ff-f1b1622cf7fd
    
  7. Swap should complete on both nodes, confirmed by log messages like
    05 04:49:39, mm2_main::mm2::lp_swap::taker_swap_v2:922] INFO Swap 3d4c27a6-1fc5-486c-a340-f9b3889f119a has been completed
    

@artemii235 artemii235 changed the title feat(trading-proto-upgrade) swap UTXO/UTXO PoC using Storable State Machine. feat(trading-proto-upgrade): swap UTXO/UTXO PoC using Storable State Machine. Sep 4, 2023
@artemii235 artemii235 changed the title feat(trading-proto-upgrade): swap UTXO/UTXO PoC using Storable State Machine. feat(trading-proto-upgrade): swap UTXO PoC using Storable State Machine. Sep 4, 2023
Copy link
Member

@laruh laruh 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!
Left comments to add doc coms and asked one question

mm2src/coins/coin_errors.rs Show resolved Hide resolved
mm2src/coins/coin_errors.rs Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
pub type GenAndSignDexFeeSpendResult = MmResult<TxPreimageWithSig, TxGenError>;
pub type ValidateDexFeeResult = MmResult<(), ValidateDexFeeError>;
pub type GenTakerPaymentSpendResult = MmResult<TxPreimageWithSig, TxGenError>;
pub type ValidateDexFeeResult = MmResult<(), ValidateTakerPaymentError>;
pub type ValidateDexFeeSpendPreimageResult = MmResult<(), ValidateDexFeeSpendPreimageError>;

pub type IguanaPrivKey = Secp256k1Secret;
Copy link
Member

Choose a reason for hiding this comment

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

doc comment

mm2src/coins/lp_coins.rs Show resolved Hide resolved
mm2src/mm2_state_machine/src/state_machine.rs Show resolved Hide resolved
mm2src/mm2_state_machine/src/state_machine.rs Show resolved Hide resolved
mm2src/mm2_test_helpers/src/for_tests.rs Show resolved Hide resolved
mm2src/mm2_test_helpers/src/for_tests.rs Show resolved Hide resolved
@artemii235
Copy link
Member Author

@laruh I added as many doc comments as I could 🙂 Can you review the PR again, please?

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.

Huge work!
First review iteration, Will do another last review iteration before the end of the week.
For this review, only minor issues and opening some points for discussion.

mm2src/mm2_state_machine/src/storable_state_machine.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/taker_swap_v2.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs Show resolved Hide resolved
@dimxy
Copy link
Collaborator

dimxy commented Sep 13, 2023

I looked over the whole swap v2 code for better understanding.
I like very much the state machine implementation based on meta programming!

Do I understand it correct that in swap v2 we eliminate step1 (taker sends dex fee) and now taker sends all amounts in his tx: dex fee, premium, trading amount as step1?
While this makes less steps for swaps but does this not create a bigger grief problem for the taker, if maker refuses to continue? Sorry if this was discussed before

@artemii235
Copy link
Member Author

Hey @dimxy, thanks for your feedback! 🙂

Do I understand it correct that in swap v2 we eliminate step1 (taker sends dex fee) and now taker sends all amounts in his tx: dex fee, premium, trading amount as step1?

Yes, it's correct.

While this makes less steps for swaps but does this not create a bigger grief problem for the taker, if maker refuses to continue? Sorry if this was discussed before

Yes, it does, but we are looking for ways to solve this problem or at least reduce attack surface.

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

@KomodoPlatform/qa: This comment #1958 (comment) has a test plan to test only the success case of utxo/utxo swap using the new trading protocol.

@smk762
Copy link

smk762 commented Sep 21, 2023

@kivqa I've setup 3 seednodes for use in testing this PR. Add the following to your MM2.json:

    "use_trading_proto_v2": true,
    "netid": 8762,
    "seednodes": ["test.seed.c.komodo.earth", "seed15.komodo.earth", "seed16.komodo.earth"],

@smk762
Copy link

smk762 commented Sep 21, 2023

Taker

21 12:12:28, mm2_main::mm2::lp_swap::taker_swap_v2:277] INFO Taker swap 816d7c93-d34c-4183-b354-0a093934a6f4 has successfully started
21 12:12:32, mm2_main::mm2::lp_swap::taker_swap_v2:466] INFO Sent combined taker payment MARTY tx 546cc5618c1560f62bc8ab6725016aa44c8e92845cea520b669eddfe7da5125d during swap 816d7c93-d34c-4183-b354-0a093934a6f4
21 12:13:07, mm2_main::mm2::lp_swap::taker_swap_v2:777] INFO Found taker payment spend MARTY tx 1f8be55222242a4b524003d2332d1fa17b0bb985c29dff9cc5f7a9a2e0aa0aa0 during swap 816d7c93-d34c-4183-b354-0a093934a6f4
21 12:13:08, mm2_main::mm2::lp_swap::taker_swap_v2:881] INFO Spent maker payment DOC tx 3455d9c083306946b9bb6b70ea127d9b98175c791e92a2c7afc2d4d40ad02459 during swap 816d7c93-d34c-4183-b354-0a093934a6f4
21 12:13:08, mm2_main::mm2::lp_swap::taker_swap_v2:1060] INFO Swap 816d7c93-d34c-4183-b354-0a093934a6f4 has been completed

Maker

21 12:12:27, mm2_main::mm2::lp_swap::maker_swap_v2:302] INFO Maker swap 816d7c93-d34c-4183-b354-0a093934a6f4 has successfully started
21 12:12:33, mm2_main::mm2::lp_swap::maker_swap_v2:573] INFO Sent maker payment DOC tx 13f6f6e9990465de598521847a422a4d7aef42377d3f9d3c4d2303d3bdfaa7c8 during swap 816d7c93-d34c-4183-b354-0a093934a6f4
21 12:12:57, mm2_main::mm2::lp_swap::maker_swap_v2:845] INFO Spent taker payment MARTY tx 1f8be55222242a4b524003d2332d1fa17b0bb985c29dff9cc5f7a9a2e0aa0aa0 during swap 816d7c93-d34c-4183-b354-0a093934a6f4
21 12:12:57, mm2_main::mm2::lp_swap::maker_swap_v2:1025] INFO Swap 816d7c93-d34c-4183-b354-0a093934a6f4 has been completed

Seednode

21 12:12:26, atomicdex_gossipsub::behaviour:649] INFO SUBSCRIPTION: Adding peer: PeerId("12D3KooWLE7LaprLmg9SjC4QdEfJhd3MyiUMfSQM1Y5NH9UrqQB9") to topic: TopicHash { hash: "swapv2/816d7c93-d34c-4183-b354-0a093934a6f4" }

@shamardy shamardy merged commit 6370fa5 into dev Sep 21, 2023
29 of 35 checks passed
@shamardy shamardy deleted the swap-proto-upgrade-iteration-2 branch September 21, 2023 12:27
@shamardy
Copy link
Collaborator

Added this PR to v1.1.0-beta since it's a WIP PR and doesn't require any changes for GUIs yet.

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