Skip to content

Commit

Permalink
refactor(best_orders): use cloned my_zhtlc_orders_pubkeys to not hold…
Browse files Browse the repository at this point in the history
… orderbook context
  • Loading branch information
rozhkovdmitrii committed Jun 9, 2023
1 parent 4f1f5c2 commit 317f997
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
18 changes: 11 additions & 7 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ fn process_maker_order_updated(
// So, each ZHTLC order has unique «pubkey» field that doesn’t match node persistent pubkey derived from passphrase.
// We can compare pubkeys from maker_orders and from asks or bids, to find our order.
#[inline(always)]
fn is_my_order(my_orders_pubkeys: &HashSet<String>, my_pub: &Option<String>, order_pubkey: &str) -> bool {
my_pub.as_deref() == Some(order_pubkey) || my_orders_pubkeys.contains(order_pubkey)
fn is_my_order(order_pubkey: &str, my_pub: &Option<String>, my_zhtlc_orders_pubkeys: &HashSet<String>) -> bool {
my_pub.as_deref() == Some(order_pubkey) || my_zhtlc_orders_pubkeys.contains(order_pubkey)
}

/// Request best asks and bids for the given `base` and `rel` coins from relays.
Expand Down Expand Up @@ -394,7 +394,7 @@ async fn request_and_fill_orderbook(ctx: &MmArc, base: &str, rel: &str) -> Resul

let ordermatch_ctx = OrdermatchContext::from_ctx(ctx).unwrap();
let mut orderbook = ordermatch_ctx.orderbook.lock();

let my_zhtlc_orders_pubkeys = orderbook.my_zhtlc_orders_pubkeys.clone();
let my_pubsecp = match CryptoCtx::from_ctx(ctx).discard_mm_trace() {
Ok(crypto_ctx) => Some(crypto_ctx.mm2_internal_pubkey_hex()),
Err(CryptoCtxError::NotInitialized) => None,
Expand All @@ -411,7 +411,7 @@ async fn request_and_fill_orderbook(ctx: &MmArc, base: &str, rel: &str) -> Resul
},
};

if is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &pubkey) {
if is_my_order(&pubkey, &my_pubsecp, &my_zhtlc_orders_pubkeys) {
continue;
}

Expand Down Expand Up @@ -450,7 +450,9 @@ fn insert_or_update_my_order(ctx: &MmArc, item: OrderbookItem, my_order: &MakerO
let mut orderbook = ordermatch_ctx.orderbook.lock();
orderbook.insert_or_update_order_update_trie(item);
if let Some(key) = my_order.p2p_privkey {
orderbook.my_p2p_pubkeys.insert(hex::encode(key.public_slice()));
orderbook
.my_zhtlc_orders_pubkeys
.insert(hex::encode(key.public_slice()));
}
}

Expand All @@ -469,7 +471,9 @@ fn delete_my_order(ctx: &MmArc, uuid: Uuid, p2p_privkey: Option<SerializableSecp
let mut orderbook = ordermatch_ctx.orderbook.lock();
orderbook.remove_order_trie_update(uuid);
if let Some(key) = p2p_privkey {
orderbook.my_p2p_pubkeys.remove(&hex::encode(key.public_slice()));
orderbook
.my_zhtlc_orders_pubkeys
.remove(&hex::encode(key.public_slice()));
}
}

Expand Down Expand Up @@ -2525,7 +2529,7 @@ struct Orderbook {
topics_subscribed_to: HashMap<String, OrderbookRequestingState>,
/// MemoryDB instance to store Patricia Tries data
memory_db: MemoryDB<Blake2Hasher64>,
my_p2p_pubkeys: HashSet<String>,
my_zhtlc_orders_pubkeys: HashSet<String>,
}

fn hashed_null_node<T: TrieConfiguration>() -> TrieHash<T> { <T::Codec as NodeCodecT>::hashed_null_node() }
Expand Down
11 changes: 6 additions & 5 deletions mm2src/mm2_main/src/lp_ordermatch/best_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub async fn best_orders_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>,
if let Some((p2p_response, peer_id)) = best_orders_res {
log::debug!("Got best orders {:?} from peer {}", p2p_response, peer_id);
let my_pubsecp = mm2_internal_pubkey_hex(&ctx).map_err(|mm2_err| mm2_err.to_string())?;
let orderbook = ordermatch_ctx.orderbook.lock();
let my_zhtlc_orders_pubkeys = ordermatch_ctx.orderbook.lock().my_zhtlc_orders_pubkeys.clone();
for (coin, orders_w_proofs) in p2p_response.orders {
let coin_conf = coin_conf(&ctx, &coin);
if coin_conf.is_null() {
Expand Down Expand Up @@ -261,7 +261,7 @@ pub async fn best_orders_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>,
},
};
let conf_settings = p2p_response.conf_infos.get(&order.uuid);
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &order.pubkey);
let is_mine = is_my_order(&order.pubkey, &my_pubsecp, &my_zhtlc_orders_pubkeys);
let entry = match req.action {
BestOrdersAction::Buy => order.as_rpc_best_orders_buy(address, conf_settings, is_mine),
BestOrdersAction::Sell => order.as_rpc_best_orders_sell(address, conf_settings, is_mine),
Expand Down Expand Up @@ -338,7 +338,8 @@ pub async fn best_orders_rpc_v2(
if let Some((p2p_response, peer_id)) = best_orders_res {
log::debug!("Got best orders {:?} from peer {}", p2p_response, peer_id);
let my_pubsecp = mm2_internal_pubkey_hex(&ctx)?;
let orderbook = ordermatch_ctx.orderbook.lock();
let my_zhtlc_orders_pubkeys = ordermatch_ctx.orderbook.lock().my_zhtlc_orders_pubkeys.clone();

for (coin, orders_w_proofs) in p2p_response.orders {
let coin_conf = coin_conf(&ctx, &coin);
if coin_conf.is_null() {
Expand All @@ -354,7 +355,7 @@ pub async fn best_orders_rpc_v2(
}
for order_w_proof in orders_w_proofs {
let order = order_w_proof.order;
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &order.pubkey);
let is_mine = is_my_order(&order.pubkey, &my_pubsecp, &my_zhtlc_orders_pubkeys);
if req.exclude_mine && is_mine {
continue;
}
Expand Down Expand Up @@ -404,7 +405,7 @@ fn mm2_internal_pubkey_hex(ctx: &MmArc) -> Result<Option<String>, MmError<BestOr
match CryptoCtx::from_ctx(ctx).discard_mm_trace() {
Ok(crypto_ctx) => Ok(Some(crypto_ctx.mm2_internal_pubkey_hex())),
Err(CryptoCtxError::NotInitialized) => Ok(None),
Err(other) => MmError::err(BestOrdersRpcError::CtxError(other.to_string())),
Err(error) => MmError::err(BestOrdersRpcError::CtxError(error.to_string())),
}
}

Expand Down
14 changes: 8 additions & 6 deletions mm2src/mm2_main/src/lp_ordermatch/orderbook_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ pub async fn orderbook_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, S
};

let orderbook = ordermatch_ctx.orderbook.lock();
let my_zhtlc_orders_pubkeys = &orderbook.my_zhtlc_orders_pubkeys;

let mut asks = match orderbook.unordered.get(&(base_ticker.clone(), rel_ticker.clone())) {
Some(uuids) => {
let mut orderbook_entries = Vec::new();
Expand All @@ -159,7 +161,7 @@ pub async fn orderbook_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, S
&ask.pubkey,
address_format,
));
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &ask.pubkey);
let is_mine = is_my_order(&ask.pubkey, &my_pubsecp, my_zhtlc_orders_pubkeys);
orderbook_entries.push(ask.as_rpc_entry_ask(address, is_mine));
}
orderbook_entries
Expand All @@ -186,7 +188,7 @@ pub async fn orderbook_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, S
&bid.pubkey,
address_format,
));
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &bid.pubkey);
let is_mine = is_my_order(&bid.pubkey, &my_pubsecp, my_zhtlc_orders_pubkeys);
orderbook_entries.push(bid.as_rpc_entry_bid(address, is_mine));
}
orderbook_entries
Expand Down Expand Up @@ -305,15 +307,15 @@ pub async fn orderbook_rpc_v2(
.await
.map_to_mm(OrderbookRpcError::P2PSubscribeError)?;

let orderbook = ordermatch_ctx.orderbook.lock();

let my_pubsecp = match CryptoCtx::from_ctx(&ctx).split_mm() {
Ok(crypto_ctx) => Some(crypto_ctx.mm2_internal_pubkey_hex()),
Err((CryptoCtxError::NotInitialized, _trace)) => None,
Err((CryptoCtxError::Internal(e), trace)) => {
return MmError::err_with_trace(OrderbookRpcError::Internal(e), trace)
},
};
let orderbook = ordermatch_ctx.orderbook.lock();
let my_zhtlc_orders_pubkeys = &orderbook.my_zhtlc_orders_pubkeys;

let mut asks = match orderbook.unordered.get(&(base_ticker.clone(), rel_ticker.clone())) {
Some(uuids) => {
Expand All @@ -334,7 +336,7 @@ pub async fn orderbook_rpc_v2(
continue;
},
};
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &ask.pubkey);
let is_mine = is_my_order(&ask.pubkey, &my_pubsecp, my_zhtlc_orders_pubkeys);
orderbook_entries.push(ask.as_rpc_v2_entry_ask(address, is_mine));
}
orderbook_entries
Expand Down Expand Up @@ -364,7 +366,7 @@ pub async fn orderbook_rpc_v2(
continue;
},
};
let is_mine = is_my_order(&orderbook.my_p2p_pubkeys, &my_pubsecp, &bid.pubkey);
let is_mine = is_my_order(&bid.pubkey, &my_pubsecp, my_zhtlc_orders_pubkeys);
orderbook_entries.push(bid.as_rpc_v2_entry_bid(address, is_mine));
}
orderbook_entries
Expand Down

0 comments on commit 317f997

Please sign in to comment.