Skip to content

Commit

Permalink
Fix P2P messages backwards compatibilty in update_maker_order PR #958 (
Browse files Browse the repository at this point in the history
…#960)

* P2P msg backward compatibilty in MakerOrderUpdated

* fix WASM test build

* remove V1 error in HistoricalOrder build function
  • Loading branch information
shamardy authored Jun 1, 2021
1 parent 4e48b22 commit bd6bed0
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 38 deletions.
36 changes: 20 additions & 16 deletions mm2src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ impl BalanceTradeFeeUpdatedHandler for BalanceUpdateOrdermatchHandler {
spawn(async move { maker_order_cancelled_p2p_notify(ctx, &order).await });
None
} else if new_volume < order.available_amount() {
let update_msg =
new_protocol::MakerOrderUpdated::new(order.uuid).with_new_max_volume(new_volume.to_ratio());
let mut update_msg = new_protocol::MakerOrderUpdated::new(order.uuid);
update_msg.with_new_max_volume(new_volume.to_ratio());
let base = order.base.to_owned();
let rel = order.rel.to_owned();
let ctx = self.ctx.clone();
Expand Down Expand Up @@ -2714,8 +2714,8 @@ async fn process_taker_connect(ctx: MmArc, sender_pubkey: H256Json, connect_msg:

// If volume is less order will be cancelled a bit later
if my_order.available_amount() >= my_order.min_base_vol {
let updated_msg = new_protocol::MakerOrderUpdated::new(my_order.uuid)
.with_new_max_volume(my_order.available_amount().into());
let mut updated_msg = new_protocol::MakerOrderUpdated::new(my_order.uuid);
updated_msg.with_new_max_volume(my_order.available_amount().into());
maker_order_updated_p2p_notify(ctx.clone(), &my_order.base, &my_order.rel, updated_msg).await;
}
save_my_maker_order(&ctx, &my_order);
Expand Down Expand Up @@ -3428,13 +3428,13 @@ pub async fn update_maker_order(ctx: MmArc, req: Json) -> Result<Response<Vec<u8
drop(my_maker_orders);

let mut update_msg = new_protocol::MakerOrderUpdated::new(req.uuid);
update_msg = update_msg.with_new_conf_settings(updated_conf_settings);
update_msg.with_new_conf_settings(updated_conf_settings);

// Validate and Add new_price to update_msg if new_price is found in the request
let new_price = match req.new_price {
Some(new_price) => {
try_s!(validate_price(new_price.clone()));
update_msg = update_msg.with_new_price(new_price.clone().into());
update_msg.with_new_price(new_price.clone().into());
new_price
},
None => original_price,
Expand All @@ -3452,13 +3452,13 @@ pub async fn update_maker_order(ctx: MmArc, req: Json) -> Result<Response<Vec<u8
Some(min_volume),
new_price.clone()
));
update_msg = update_msg.with_new_min_volume(actual_min_vol.into());
update_msg.with_new_min_volume(actual_min_vol.into());
}

// Calculate order volume and add to update_msg if new_volume is found in the request
let new_volume = if req.max.unwrap_or(false) {
let max_volume = try_s!(get_max_volume(&ctx, &base_coin, &rel_coin).await) + reserved_amount.clone();
update_msg = update_msg.with_new_max_volume(max_volume.clone().into());
update_msg.with_new_max_volume(max_volume.clone().into());
max_volume
} else if Option::is_some(&req.volume_delta) {
let volume = original_volume + req.volume_delta.unwrap();
Expand All @@ -3477,7 +3477,7 @@ pub async fn update_maker_order(ctx: MmArc, req: Json) -> Result<Response<Vec<u8
)
.await
);
update_msg = update_msg.with_new_max_volume(volume.clone().into());
update_msg.with_new_max_volume(volume.clone().into());
volume
} else {
original_volume
Expand Down Expand Up @@ -3512,7 +3512,7 @@ pub async fn update_maker_order(ctx: MmArc, req: Json) -> Result<Response<Vec<u8
order.apply_updated(&update_msg);
order.changes_history.get_or_insert(Vec::new()).push(new_change);
save_maker_order_on_update(&ctx, &order);
update_msg = update_msg.with_new_max_volume((new_volume - reserved_amount).into());
update_msg.with_new_max_volume((new_volume - reserved_amount).into());
(MakerOrderForRpc::from(&*order), order.base.as_str(), order.rel.as_str())
},
};
Expand Down Expand Up @@ -3937,26 +3937,30 @@ struct HistoricalOrder {
impl HistoricalOrder {
fn build(new_order: &new_protocol::MakerOrderUpdated, old_order: &MakerOrder) -> HistoricalOrder {
HistoricalOrder {
max_base_vol: if new_order.new_max_volume.is_some() {
max_base_vol: if new_order.new_max_volume().is_some() {
Some(old_order.max_base_vol.clone())
} else {
None
},
min_base_vol: if new_order.new_min_volume.is_some() {
min_base_vol: if new_order.new_min_volume().is_some() {
Some(old_order.min_base_vol.clone())
} else {
None
},
price: if new_order.new_price.is_some() {
price: if new_order.new_price().is_some() {
Some(old_order.price.clone())
} else {
None
},
updated_at: old_order.updated_at,
conf_settings: if new_order.conf_settings == old_order.conf_settings {
None
conf_settings: if let Some(settings) = new_order.new_conf_settings() {
if Some(settings) == old_order.conf_settings {
None
} else {
old_order.conf_settings
}
} else {
old_order.conf_settings
None
},
}
}
Expand Down
182 changes: 160 additions & 22 deletions mm2src/lp_ordermatch/new_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,59 +137,111 @@ pub struct MakerOrderCancelled {
pub pair_trie_root: H64,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MakerOrderUpdated {
#[derive(Clone, Debug, Eq, Deserialize, PartialEq, Serialize)]
pub struct MakerOrderUpdatedV1 {
uuid: CompactUuid,
pub new_price: Option<BigRational>,
pub new_max_volume: Option<BigRational>,
pub new_min_volume: Option<BigRational>,
pub conf_settings: Option<OrderConfirmationsSettings>,
timestamp: u64,
pair_trie_root: H64,
}

#[derive(Clone, Debug, Eq, Deserialize, PartialEq, Serialize)]
pub struct MakerOrderUpdatedV2 {
uuid: CompactUuid,
pub new_price: Option<BigRational>,
pub new_max_volume: Option<BigRational>,
pub new_min_volume: Option<BigRational>,
timestamp: u64,
pair_trie_root: H64,
pub conf_settings: Option<OrderConfirmationsSettings>,
}

#[derive(Clone, Debug, Eq, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum MakerOrderUpdated {
V1(MakerOrderUpdatedV1),
V2(MakerOrderUpdatedV2),
}

impl MakerOrderUpdated {
pub fn new(uuid: Uuid) -> Self {
MakerOrderUpdated {
MakerOrderUpdated::V2(MakerOrderUpdatedV2 {
uuid: uuid.into(),
new_price: None,
new_max_volume: None,
new_min_volume: None,
conf_settings: None,
timestamp: now_ms() / 1000,
pair_trie_root: H64::default(),
}
})
}

pub fn with_new_price(mut self, new_price: BigRational) -> Self {
self.new_price = Some(new_price);
self
pub fn with_new_price(&mut self, new_price: BigRational) {
match self {
MakerOrderUpdated::V1(v1) => v1.new_price = Some(new_price),
MakerOrderUpdated::V2(v2) => v2.new_price = Some(new_price),
}
}

pub fn with_new_max_volume(mut self, new_max_volume: BigRational) -> Self {
self.new_max_volume = Some(new_max_volume);
self
pub fn with_new_max_volume(&mut self, new_max_volume: BigRational) {
match self {
MakerOrderUpdated::V1(v1) => v1.new_max_volume = Some(new_max_volume),
MakerOrderUpdated::V2(v2) => v2.new_max_volume = Some(new_max_volume),
}
}

pub fn with_new_min_volume(mut self, new_min_volume: BigRational) -> Self {
self.new_min_volume = Some(new_min_volume);
self
pub fn with_new_min_volume(&mut self, new_min_volume: BigRational) {
match self {
MakerOrderUpdated::V1(v1) => v1.new_min_volume = Some(new_min_volume),
MakerOrderUpdated::V2(v2) => v2.new_min_volume = Some(new_min_volume),
}
}

pub fn with_new_conf_settings(mut self, conf_settings: OrderConfirmationsSettings) -> Self {
self.conf_settings = Some(conf_settings);
self
pub fn with_new_conf_settings(&mut self, conf_settings: OrderConfirmationsSettings) {
match self {
MakerOrderUpdated::V1(_) => {},
MakerOrderUpdated::V2(v2) => {
v2.conf_settings = Some(conf_settings);
},
}
}

pub fn new_price(&self) -> Option<MmNumber> { self.new_price.as_ref().map(|num| num.clone().into()) }
pub fn new_price(&self) -> Option<MmNumber> {
match self {
MakerOrderUpdated::V1(v1) => v1.new_price.as_ref().map(|num| num.clone().into()),
MakerOrderUpdated::V2(v2) => v2.new_price.as_ref().map(|num| num.clone().into()),
}
}

pub fn new_max_volume(&self) -> Option<MmNumber> { self.new_max_volume.as_ref().map(|num| num.clone().into()) }
pub fn new_max_volume(&self) -> Option<MmNumber> {
match self {
MakerOrderUpdated::V1(v1) => v1.new_max_volume.as_ref().map(|num| num.clone().into()),
MakerOrderUpdated::V2(v2) => v2.new_max_volume.as_ref().map(|num| num.clone().into()),
}
}

pub fn new_min_volume(&self) -> Option<MmNumber> { self.new_min_volume.as_ref().map(|num| num.clone().into()) }
pub fn new_min_volume(&self) -> Option<MmNumber> {
match self {
MakerOrderUpdated::V1(v1) => v1.new_min_volume.as_ref().map(|num| num.clone().into()),
MakerOrderUpdated::V2(v2) => v2.new_min_volume.as_ref().map(|num| num.clone().into()),
}
}

pub fn new_conf_settings(&self) -> Option<OrderConfirmationsSettings> { self.conf_settings }
pub fn new_conf_settings(&self) -> Option<OrderConfirmationsSettings> {
match self {
MakerOrderUpdated::V1(_) => None,
MakerOrderUpdated::V2(v2) => v2.conf_settings,
}
}

pub fn uuid(&self) -> Uuid { self.uuid.into() }
pub fn uuid(&self) -> Uuid {
match self {
MakerOrderUpdated::V1(v1) => v1.uuid.into(),
MakerOrderUpdated::V2(v2) => v2.uuid.into(),
}
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -226,3 +278,89 @@ pub struct MakerConnected {
pub taker_order_uuid: CompactUuid,
pub maker_order_uuid: CompactUuid,
}

#[cfg(test)]
mod new_protocol_tests {
use common::new_uuid;

use super::*;

#[test]
fn check_maker_order_updated_serde() {
let uuid = CompactUuid::from(new_uuid());
let timestamp = now_ms() / 1000;
let conf_settings = Some(OrderConfirmationsSettings {
base_confs: 5,
base_nota: true,
rel_confs: 5,
rel_nota: true,
});
// old format should be deserialized to MakerOrderUpdated::V1
let v1 = MakerOrderUpdatedV1 {
uuid,
new_price: Some(BigRational::from_integer(2.into())),
new_max_volume: Some(BigRational::from_integer(3.into())),
new_min_volume: Some(BigRational::from_integer(1.into())),
timestamp,
pair_trie_root: H64::default(),
};

let expected = MakerOrderUpdated::V1(MakerOrderUpdatedV1 {
uuid,
new_price: Some(BigRational::from_integer(2.into())),
new_max_volume: Some(BigRational::from_integer(3.into())),
new_min_volume: Some(BigRational::from_integer(1.into())),
timestamp,
pair_trie_root: H64::default(),
});

let serialized = rmp_serde::to_vec(&v1).unwrap();

let deserialized: MakerOrderUpdated = rmp_serde::from_read_ref(serialized.as_slice()).unwrap();

assert_eq!(deserialized, expected);

// new format should be deserialized to old
let v2 = MakerOrderUpdated::V2(MakerOrderUpdatedV2 {
uuid,
new_price: Some(BigRational::from_integer(2.into())),
new_max_volume: Some(BigRational::from_integer(3.into())),
new_min_volume: Some(BigRational::from_integer(1.into())),
timestamp,
pair_trie_root: H64::default(),
conf_settings,
});

let expected = MakerOrderUpdatedV1 {
uuid,
new_price: Some(BigRational::from_integer(2.into())),
new_max_volume: Some(BigRational::from_integer(3.into())),
new_min_volume: Some(BigRational::from_integer(1.into())),
timestamp,
pair_trie_root: H64::default(),
};

let serialized = rmp_serde::to_vec(&v2).unwrap();

let deserialized: MakerOrderUpdatedV1 = rmp_serde::from_read_ref(serialized.as_slice()).unwrap();

assert_eq!(deserialized, expected);

// new format should be deserialized to new
let v2 = MakerOrderUpdated::V2(MakerOrderUpdatedV2 {
uuid,
new_price: Some(BigRational::from_integer(2.into())),
new_max_volume: Some(BigRational::from_integer(3.into())),
new_min_volume: Some(BigRational::from_integer(1.into())),
timestamp,
pair_trie_root: H64::default(),
conf_settings,
});

let serialized = rmp_serde::to_vec(&v2).unwrap();

let deserialized: MakerOrderUpdated = rmp_serde::from_read_ref(serialized.as_slice()).unwrap();

assert_eq!(deserialized, v2);
}
}

0 comments on commit bd6bed0

Please sign in to comment.