Skip to content

Commit

Permalink
review: simplify "swap_version" field type, make it non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Nov 14, 2024
1 parent 8d5ed46 commit 707946a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 78 deletions.
38 changes: 21 additions & 17 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,9 +1171,9 @@ pub struct TakerRequest {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub rel_protocol_info: Option<Vec<u8>>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_version: Option<u32>,
#[serde(default = "legacy_swap_version")]
#[serde(skip_serializing_if = "is_legacy_swap_version")]
pub swap_version: u32,
}

impl TakerRequest {
Expand Down Expand Up @@ -1251,6 +1251,10 @@ impl TakerRequest {
fn get_rel_amount(&self) -> &MmNumber { &self.rel_amount }
}

fn legacy_swap_version() -> u32 { 1 }

fn is_legacy_swap_version(swap_version: &u32) -> bool { *swap_version == 1 }

pub struct TakerOrderBuilder<'a> {
base_coin: &'a MmCoinEnum,
rel_coin: &'a MmCoinEnum,
Expand Down Expand Up @@ -1509,7 +1513,7 @@ impl<'a> TakerOrderBuilder<'a> {
conf_settings: self.conf_settings,
base_protocol_info: Some(base_protocol_info),
rel_protocol_info: Some(rel_protocol_info),
swap_version: Some(self.swap_version),
swap_version: self.swap_version,
},
matches: Default::default(),
min_volume,
Expand Down Expand Up @@ -1550,7 +1554,7 @@ impl<'a> TakerOrderBuilder<'a> {
conf_settings: self.conf_settings,
base_protocol_info: Some(base_protocol_info),
rel_protocol_info: Some(rel_protocol_info),
swap_version: Some(self.swap_version),
swap_version: self.swap_version,
},
matches: HashMap::new(),
min_volume: Default::default(),
Expand Down Expand Up @@ -1712,9 +1716,9 @@ pub struct MakerOrder {
/// A custom priv key for more privacy to prevent linking orders of the same node between each other
/// Commonly used with privacy coins (ARRR, ZCash, etc.)
p2p_privkey: Option<SerializableSecp256k1Keypair>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_version: Option<u32>,
#[serde(default = "legacy_swap_version")]
#[serde(skip_serializing_if = "is_legacy_swap_version")]
pub swap_version: u32,
}

pub struct MakerOrderBuilder<'a> {
Expand Down Expand Up @@ -1983,7 +1987,7 @@ impl<'a> MakerOrderBuilder<'a> {
base_orderbook_ticker: self.base_orderbook_ticker,
rel_orderbook_ticker: self.rel_orderbook_ticker,
p2p_privkey,
swap_version: Some(self.swap_version),
swap_version: self.swap_version,
})
}

Expand All @@ -2008,7 +2012,7 @@ impl<'a> MakerOrderBuilder<'a> {
base_orderbook_ticker: None,
rel_orderbook_ticker: None,
p2p_privkey: None,
swap_version: Some(self.swap_version),
swap_version: self.swap_version,
}
}
}
Expand Down Expand Up @@ -2215,9 +2219,9 @@ pub struct MakerReserved {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub rel_protocol_info: Option<Vec<u8>>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_version: Option<u32>,
#[serde(default = "legacy_swap_version")]
#[serde(skip_serializing_if = "is_legacy_swap_version")]
pub swap_version: u32,
}

impl MakerReserved {
Expand Down Expand Up @@ -3026,7 +3030,7 @@ fn lp_connect_start_bob(ctx: MmArc, maker_match: MakerMatch, maker_order: MakerO

let alice_swap_v = maker_match.request.swap_version;
// if taker order doesn't have swap version or taker uses legacy swap protocol, start legacy swap
if alice_swap_v.is_none() || alice_swap_v == Some(1) {
if alice_swap_v == 1u32 {
let params = LegacySwapParams {
maker_coin: &maker_coin,
taker_coin: &taker_coin,
Expand All @@ -3041,7 +3045,7 @@ fn lp_connect_start_bob(ctx: MmArc, maker_match: MakerMatch, maker_order: MakerO
return;
}

if ctx.use_trading_proto_v2() && alice_swap_v == Some(2) {
if ctx.use_trading_proto_v2() && alice_swap_v == 2u32 {
let params = StateMachineParams {
secret_hash_algo: &detect_secret_hash_algo(&maker_coin, &taker_coin),
uuid: &uuid,
Expand Down Expand Up @@ -3271,7 +3275,7 @@ fn lp_connected_alice(ctx: MmArc, taker_order: TakerOrder, taker_match: TakerMat

let bob_swap_v = taker_match.reserved.swap_version;
// if maker order doesn't have swap version or maker uses legacy swap protocol, start legacy swap
if bob_swap_v.is_none() || bob_swap_v == Some(1) {
if bob_swap_v == 1u32 {
let params = LegacySwapParams {
maker_coin: &maker_coin,
taker_coin: &taker_coin,
Expand All @@ -3286,7 +3290,7 @@ fn lp_connected_alice(ctx: MmArc, taker_order: TakerOrder, taker_match: TakerMat
return;
}

if ctx.use_trading_proto_v2() && bob_swap_v == Some(2) {
if ctx.use_trading_proto_v2() && bob_swap_v == 2u32 {
let taker_secret = match generate_secret() {
Ok(s) => s.into(),
Err(e) => {
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_ordermatch/my_orders_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ mod tests {
base_orderbook_ticker: None,
rel_orderbook_ticker: None,
p2p_privkey: None,
swap_version: None,
swap_version: 1,
}
}

Expand All @@ -743,7 +743,7 @@ mod tests {
conf_settings: None,
base_protocol_info: None,
rel_protocol_info: None,
swap_version: None,
swap_version: 1,
},
matches: HashMap::new(),
created_at: now_ms(),
Expand Down
14 changes: 7 additions & 7 deletions mm2src/mm2_main/src/lp_ordermatch/new_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mm2_rpc::data::legacy::{MatchBy as SuperMatchBy, OrderConfirmationsSettings,
use std::collections::{HashMap, HashSet};
use uuid::Uuid;

use crate::lp_ordermatch::{AlbOrderedOrderbookPair, H64};
use crate::lp_ordermatch::{is_legacy_swap_version, legacy_swap_version, AlbOrderedOrderbookPair, H64};

#[derive(Debug, Deserialize, Serialize)]
#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -265,9 +265,9 @@ pub struct TakerRequest {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub rel_protocol_info: Option<Vec<u8>>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_version: Option<u32>,
#[serde(default = "legacy_swap_version")]
#[serde(skip_serializing_if = "is_legacy_swap_version")]
pub swap_version: u32,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand All @@ -285,9 +285,9 @@ pub struct MakerReserved {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub rel_protocol_info: Option<Vec<u8>>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub swap_version: Option<u32>,
#[serde(default = "legacy_swap_version")]
#[serde(skip_serializing_if = "is_legacy_swap_version")]
pub swap_version: u32,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down
Loading

0 comments on commit 707946a

Please sign in to comment.