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

fix(orderbook): age field is set in proper way #1851

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use common::executor::{simple_map::AbortableSimpleMap, AbortSettings, AbortableS
SpawnFuture, Timer};
use common::log::{error, warn, LogOnError};
use common::time_cache::TimeCache;
use common::{bits256, log, new_uuid, now_ms, now_sec, now_sec_i64};
use common::{bits256, log, new_uuid, now_ms, now_sec};
use crypto::privkey::SerializableSecp256k1Keypair;
use crypto::{CryptoCtx, CryptoCtxError};
use derive_more::Display;
Expand Down Expand Up @@ -3956,6 +3956,16 @@ struct OrderbookP2PItem {
created_at: u64,
}

macro_rules! try_get_age_or_default {
($created_at: expr) => {{
let now = now_sec();
now.checked_sub($created_at).unwrap_or_else(|| {
warn!("now - created_at: ({} - {}) caused a u64 underflow", now, $created_at);
Default::default()
})
}};
}

impl OrderbookP2PItem {
fn as_rpc_best_orders_buy(
&self,
Expand Down Expand Up @@ -3985,7 +3995,7 @@ impl OrderbookP2PItem {
min_volume_rat: min_vol_mm.to_ratio(),
min_volume_fraction: min_vol_mm.to_fraction(),
pubkey: self.pubkey.clone(),
age: now_sec_i64(),
age: try_get_age_or_default!(self.created_at),
zcredits: 0,
uuid: self.uuid,
is_mine,
Expand Down Expand Up @@ -4051,7 +4061,7 @@ impl OrderbookP2PItem {
min_volume_rat: min_vol_mm.to_ratio(),
min_volume_fraction: min_vol_mm.to_fraction(),
pubkey: self.pubkey.clone(),
age: now_sec_i64(),
age: try_get_age_or_default!(self.created_at),
zcredits: 0,
uuid: self.uuid,
is_mine,
Expand Down Expand Up @@ -4213,7 +4223,7 @@ impl OrderbookItem {
min_volume_rat: min_vol_mm.to_ratio(),
min_volume_fraction: min_vol_mm.to_fraction(),
pubkey: self.pubkey.clone(),
age: now_sec_i64(),
age: try_get_age_or_default!(self.created_at),
zcredits: 0,
uuid: self.uuid,
is_mine,
Expand Down Expand Up @@ -4249,7 +4259,7 @@ impl OrderbookItem {
min_volume_rat: min_vol_mm.to_ratio(),
min_volume_fraction: min_vol_mm.to_fraction(),
pubkey: self.pubkey.clone(),
age: now_sec_i64(),
age: try_get_age_or_default!(self.created_at),
zcredits: 0,
uuid: self.uuid,
is_mine,
Expand Down Expand Up @@ -5622,7 +5632,7 @@ pub struct RpcOrderbookEntry {
min_volume_rat: BigRational,
min_volume_fraction: Fraction,
pubkey: String,
age: i64,
age: u64,
zcredits: u64,
uuid: Uuid,
is_mine: bool,
Expand Down