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

Arrr integration WIP. #1204

Merged
merged 34 commits into from
Feb 25, 2022
Merged

Arrr integration WIP. #1204

merged 34 commits into from
Feb 25, 2022

Conversation

artemii235
Copy link
Member

Generate random p2p messages signing privkey for each order of a privacy coin.
Generate random keypair for HTLC redeem script of privacy coins.
Refactoring.
#927

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.

Great work! Just a few questions about unclear things to me 🙂

mm2src/lp_swap/recreate_swap_data.rs Outdated Show resolved Hide resolved
mm2src/lp_swap/recreate_swap_data.rs Outdated Show resolved Hide resolved
mm2src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator

shamardy commented Feb 1, 2022

https://github.com/KomodoPlatform/atomicDEX-API/blob/c3408e9f997bea3448716115919bf78c3c83de4e/mm2src/lp_ordermatch.rs#L415-L418
It seems that pubkey banning will not work with arrr orders, I guess this is a tradeoff for privacy. Not sure if something can be done about this?
Also can from_peer from process_msg be used to link orders to each other? I can see that the max number of peers in the mesh is 12. So as long as the number of peers subscribed to the topic is more than 12 this shouldn't be a problem if I understand correctly. Just a thought I had and wanted to share 🙂

@artemii235
Copy link
Member Author

It seems that pubkey banning will not work with arrr orders, I guess this is a tradeoff for privacy. Not sure if something can be done about this?

Yes, banning won't work. For transparent addresses, we can sign p2p messages with the privkey, which address belongs to. It will allow us to check the balance to additionally validate the order in the future. It's not possible for privacy coins so yes, there will be tradeoffs for privacy. As of now, I'm unsure how we can solve it, but we will maybe have some ideas in the future 🙂

Also can from_peer from process_msg be used to link orders to each other? I can see that the max number of peers in the mesh is 12. So as long as the number of peers subscribed to the topic is more than 12 this shouldn't be a problem if I understand correctly. Just a thought I had and wanted to share

Yes, from_peer and process_msg can be used to link orders between each other. I need to check if we can modify the sender PeerId.

Gossipsub mesh size is not related to it. As per specification https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.0.md#gossipsub-the-gossiping-mesh-router, the mesh is used to transfer a message to a subset of subscribed peers to to reduce bandwidth requirements and increase decentralization and scalability. If the node is removed from another's mesh, it has to add other peers to its mesh to maintain mesh network graph completeness. It doesn't imply that some nodes will be removed from the network completely or senders' PeerIds will be hidden.

Also, in our customized gossipsub version, we form a mesh for relay nodes only, which send the messages to all subscribed "light" nodes.

@artemii235
Copy link
Member Author

Also can from_peer from process_msg be used to link orders to each other?

It should be possible to modify a sender, but I prefer to do it on the next iteration. Orderbook sync doesn't work properly now for these temporary keys anyway. This PR goal is to merge swap-related changes as fast as possible. Added to my checklist at #927 (comment)

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Great progress! First batch of comments :)

@@ -64,6 +64,7 @@ pub use ethcore_transaction::SignedTransaction as SignedEthTx;
pub use rlp;

mod web3_transport;
use crate::TradePreimageResult;

Choose a reason for hiding this comment

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

Could you please move this import to:

use super::{BalanceError, TradePreimageResult};

mm2src/coins/z_coin.rs Show resolved Hide resolved
mm2src/coins/z_coin.rs Show resolved Hide resolved
mm2src/coins/z_coin.rs Show resolved Hide resolved
mm2src/coins/z_coin.rs Show resolved Hide resolved
mm2src/lp_swap/maker_swap.rs Show resolved Hide resolved
@@ -452,12 +516,29 @@ impl MakerSwap {
},
};

let taker_pubkey = taker_data.persistent_pubkey();
let (taker_pubkey, maker_coin_htlc_pubkey, taker_coin_htlc_pubkey) = match taker_pubkey.len() {
33 => (taker_pubkey.into(), None, None),

Choose a reason for hiding this comment

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

How about adding a enum that consists of taker_pubkey only by default and consists of a maker/taker htlc pubkeys pair? It will allow us to save backward compatibility and avoid errors like:

return Ok((Some(MakerSwapCommand::Finish), vec![MakerSwapEvent::NegotiateFailed(
                    ERRL!("Unexpected persistent_pubkey field len {}", taker_pubkey.len()).into(),
                )]))

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree enum will be better, will do it tomorrow.

(None, None)
};

let (taker_coin_htlc_privkey, taker_coin_htlc_pubkey) = if self.taker_coin.is_privacy() {

Choose a reason for hiding this comment

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

How about adding a function that generates htlc keys pair if a coin is private. The code below is the same as for maker coin

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, will do.

};

Ok((Some(MakerSwapCommand::Negotiate), vec![MakerSwapEvent::Started(data)]))
}

async fn negotiate(&self) -> Result<(Option<MakerSwapCommand>, Vec<MakerSwapEvent>), String> {
let mut persistent_pubkey = Vec::with_capacity(33);
persistent_pubkey.extend_from_slice(self.r().maker_coin_htlc_keypair.public());
if self.r().maker_coin_htlc_keypair != self.r().taker_coin_htlc_keypair {

Choose a reason for hiding this comment

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

If we add a enum that is either a persistent pubkey or a pair of maker and taker htlc keypairs (see the next comment), then it can perform this checking in its AEnum::with_maker_taker_htlc_keypairs constructor.

@@ -717,7 +798,8 @@ impl MakerSwap {
.validate_taker_payment(
&self.r().taker_payment.clone().unwrap().tx_hex,
self.taker_payment_lock.load(Ordering::Relaxed) as u32,
&*self.r().other_persistent_pub,
&*self.r().other_taker_coin_htlc_pub,
&**self.r().taker_coin_htlc_keypair.public(),

Choose a reason for hiding this comment

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

I think it's a good time to add a struct that consists of the method arguments.
It's easy to make a mistake, for example, pass maker and taker pubkeys in the wrong order.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, will do it tomorrow.

@artemii235
Copy link
Member Author

@sergeyboyko0791 It seems I resolved all remarks from the first iteration. Could you review it again, please? 🙂

shamardy
shamardy previously approved these changes Feb 8, 2022
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! 🔥

Copy link

@sergeyboyko0791 sergeyboyko0791 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 changes! I left a few more comments

mm2src/docker_tests.rs Outdated Show resolved Hide resolved
mm2src/lp_swap.rs Outdated Show resolved Hide resolved
@artemii235
Copy link
Member Author

@shamardy @sergeyboyko0791 It's ready for (hopefully) final review iteration.

shamardy
shamardy previously approved these changes Feb 21, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Last questions and suggestions 😄

/// Temporary pubkey used in HTLC redeem script when applicable for taker coin
pub taker_coin_htlc_pubkey: Option<H264Json>,
/// Temporary privkey used to sign P2P messages when applicable
/// Temporary privkey used to sign P2P messages when applicable

Choose a reason for hiding this comment

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

Duplicate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix it today.

fn get_my_negotiation_data(&self) -> NegotiationDataMsg {
let r = self.r();
let secret_hash = dhash160(&r.data.secret.0).take().to_vec();
let maker_coin_swap_contract = self.maker_coin.swap_contract_address().map_or(vec![], |addr| addr.0);
Copy link

@sergeyboyko0791 sergeyboyko0791 Feb 21, 2022

Choose a reason for hiding this comment

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

I think it should be a bit more efficient as the default vector is created if there is no contract address:

self.maker_coin.swap_contract_address().map(|addr| addr.0).unwrap_or_else(Vec::new);

Could you please consider refactoring the same lines in taker_swap.rs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good note, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

According to https://doc.rust-lang.org/beta/std/option/enum.Option.html#method.map_or, we can also use map_or_else. It uses one line less after rustfmt 🙂

Choose a reason for hiding this comment

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

Great :D

let maker_coin_swap_contract = self.maker_coin.swap_contract_address().map_or(vec![], |addr| addr.0);
let taker_coin_swap_contract = self.taker_coin.swap_contract_address().map_or(vec![], |addr| addr.0);

if r.my_maker_coin_htlc_keypair != r.my_taker_coin_htlc_keypair {

Choose a reason for hiding this comment

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

I think we should use the latest version of the negotiation data.
What if we want to add or change a field in the negotiation data. Currently, I see only one way: Add NegotiationDataV2.1 and NegotiationDataV3.1 that will be used accordingly to this condition:
if r.my_maker_coin_htlc_keypair != r.my_taker_coin_htlc_keypair
I'd suggest just using NegotiationDataV3 only. On the other hand, it will break the backward compatibility with the old nodes that don't support V3 yet. Or return HtlcPubkeyData 😅
I think renaming NegotiationDataV3 to NegotiationDataPubkeysPerCoin is also ok.

Copy link
Member Author

@artemii235 artemii235 Feb 22, 2022

Choose a reason for hiding this comment

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

NegotiationDataPubkeysPerCoin will break a naming convention that we already have (V1, V2, etc). If we need to add new fields, we will add NegotiationDataV4. It won't be mandatorily tied to if r.my_maker_coin_htlc_keypair != r.my_taker_coin_htlc_keypair, we will maybe have another condition or make it default and compatible with NegotiationDataV3. Also, when the majority of nodes are updated, we can remove the condition and make the V3 default for everyone.

According to this, I don't think we need to change the existing code now (but we will change it later).

@artemii235
Copy link
Member Author

@sergeyboyko0791 Could you please review it again? 🙂

Copy link

@sergeyboyko0791 sergeyboyko0791 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 changes! LGTM

Artem Vitae added 2 commits February 22, 2022 18:18
# Conflicts:
#	mm2src/coins/eth.rs
#	mm2src/coins/utxo/qtum.rs
#	mm2src/coins/utxo/utxo_common.rs
#	mm2src/coins/utxo/utxo_standard.rs
@artemii235
Copy link
Member Author

@sergeyboyko0791 I merged the dev into my branch - there were some conflicts. Could you please take a quick look and check that I didn't break something?

@sergeyboyko0791
Copy link

If I can trust a condensed version of this 2785820 merge commit, everything looks good.
I've tested UTXO activation functionality with Trezor locally and it works as expected 👍

@artemii235 artemii235 merged commit 66cab10 into dev Feb 25, 2022
@artemii235 artemii235 deleted the arrr-integration branch February 25, 2022 07:04
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.

3 participants