Skip to content

Commit 9649748

Browse files
committed
edit orderid
1 parent 2581617 commit 9649748

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

pallets/hybrid-orderbook/src/critbit.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use super::{Order as OrderUnit, *};
66
pub struct CritbitTree<K, V> {
77
/// Index of the root node which is part of the internal nodes.
88
root: K,
9-
/// The internal nodes of the tree.
9+
/// The internal nodes of the tree
10+
/// Here, `key` refers to `index` of `InternalNode.
1011
internal_nodes: BTreeMap<K, InternalNode<K>>,
1112
/// The leaf nodes of the tree.
1213
leaves: BTreeMap<K, LeafNode<K, V>>,
@@ -47,7 +48,7 @@ where
4748
}
4849
}
4950

50-
/// Check if the leaves are empty.
51+
/// Check whether the leaf exists
5152
pub fn is_empty(&self) -> bool {
5253
self.leaves.is_empty()
5354
}
@@ -526,6 +527,9 @@ pub enum CritbitTreeError {
526527
ValueOps,
527528
}
528529

530+
/// `InternalNode` for `critbit-tree` with `K` index. Here, `K` refer to two meaning.
531+
/// - mask
532+
/// - path of the tree
529533
#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, Eq, TypeInfo)]
530534
pub struct InternalNode<K> {
531535
/// Mask for branching the tree based on the critbit.
@@ -550,6 +554,11 @@ impl<K: OrderBookIndex> InternalNode<K> {
550554
}
551555
}
552556

557+
/// Type of `LeafNode`
558+
///
559+
/// - `parent`: Index of the path of the tree
560+
/// - `key`: Value represents the node(e.g price)
561+
/// - `value`: Actual data stored on the node(e.g orders)
553562
#[derive(Encode, Decode, Debug, Default, Clone, PartialEq, Eq, TypeInfo)]
554563
pub struct LeafNode<K, V> {
555564
/// Parent index of the node.
@@ -561,7 +570,8 @@ pub struct LeafNode<K, V> {
561570
}
562571

563572
impl<K: OrderBookIndex, V> LeafNode<K, V> {
564-
/// Create new instance of the leaf node.
573+
/// Create new instance of the leaf node with given `key` and `value`.
574+
/// `parent` is initialized to `K::PARTITION_INDEX` which refer to the start index of the leaf node
565575
pub fn new(key: K, value: V) -> Self {
566576
LeafNode {
567577
parent: K::PARTITION_INDEX,

pallets/hybrid-orderbook/src/types.rs

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub type AssetIdOf<T> =
3131
pub type AssetBalanceOf<T> =
3232
<<T as Config>::Assets as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
3333

34+
type Orders<Q, A, B> = Vec<Order<Q, A, B>>;
35+
3436
impl<T: Config, AssetId, AccountId, Balance> FrozenBalance<AssetId, AccountId, Balance>
3537
for Pallet<T>
3638
where
@@ -67,6 +69,42 @@ where
6769
)]
6870
pub struct OrderId(pub u64);
6971

72+
impl OrderId {
73+
fn start_bid_id() -> Self {
74+
Self(0)
75+
}
76+
77+
fn start_ask_id() -> Self {
78+
Self(1 << u64::BITS - 1)
79+
}
80+
81+
fn max_bid_id(&self) -> Self {
82+
Self::start_ask_id()
83+
}
84+
85+
fn max_ask_id(&self) -> Self {
86+
Self(u64::MAX)
87+
}
88+
89+
fn increase_by_one(&self) -> Option<Self> {
90+
let is_bid = self.is_bid();
91+
let max_index = if is_bid {
92+
self.max_bid_id()
93+
} else {
94+
self.max_ask_id()
95+
};
96+
if self.eq(&(max_index - 1.into())) {
97+
return None;
98+
}
99+
let new = Self(self.0 + 1);
100+
Some(new)
101+
}
102+
103+
fn is_bid(&self) -> bool {
104+
self.0 & Self::start_ask_id().0 == 0
105+
}
106+
}
107+
70108
#[derive(Debug)]
71109
pub enum OrderIdError {
72110
Overflow,
@@ -78,35 +116,18 @@ impl OrderbookOrderId for OrderId {
78116

79117
fn new(is_bid: bool) -> Self {
80118
if is_bid {
81-
Self(0)
119+
Self::start_ask_id()
82120
} else {
83-
Self(1 << (u64::BITS - 1))
121+
Self::start_bid_id()
84122
}
85123
}
86124

87125
fn checked_increase(&self, is_bid: bool) -> Option<Self> {
88-
let max_index = if is_bid {
89-
1 << (u64::BITS - 1)
90-
} else {
91-
u64::MAX
92-
};
93-
94-
let new = self.0.checked_add(1);
95-
match new {
96-
Some(new) => {
97-
// Over max index
98-
if new > max_index {
99-
return None;
100-
}
101-
Some(new.into())
102-
}
103-
// Overflow
104-
None => None,
105-
}
126+
self.increase_by_one()
106127
}
107128

108129
fn is_bid(&self) -> bool {
109-
self.0 & (1 << (u64::BITS - 1)) == 0
130+
self.is_bid()
110131
}
111132
}
112133

@@ -305,7 +326,7 @@ impl<T: Config> Pool<T> {
305326
&self,
306327
owner: &T::AccountId,
307328
is_bid: bool,
308-
) -> Vec<Order<T::Unit, T::AccountId, BlockNumberFor<T>>> {
329+
) -> Orders<T::Unit, T::AccountId, BlockNumberFor<T>> {
309330
if is_bid {
310331
self.bids.get_orders(owner)
311332
} else {
@@ -569,14 +590,16 @@ pub mod traits {
569590
/// Type of error of orderbook
570591
type Error;
571592

572-
/// Create new instance
593+
/// Create new instance of `Self`
573594
fn new() -> Self;
574595

575596
/// Check if the orderbook is empty
576597
fn is_empty(&self) -> bool;
577598

578-
fn get_orders(&self, owner: &Account) -> Vec<Order<Unit, Account, BlockNumber>>;
599+
/// Get all orders of given `owner`
600+
fn get_orders(&self, owner: &Account) -> Orders<Unit, Account, BlockNumber>;
579601

602+
/// Try get all orders for given `key`
580603
fn open_orders_at(&self, key: Unit) -> Result<Option<Self::Order>, Self::Error>;
581604

582605
/// Optionally return the minimum order of the orderbook which is (price, index).
@@ -673,7 +696,7 @@ pub mod traits {
673696
expired_at: BlockNumber,
674697
) -> Self;
675698

676-
fn find_order_of(&self, owner: &Account) -> Option<Vec<Order<Unit, Account, BlockNumber>>>;
699+
fn find_order_of(&self, owner: &Account) -> Option<Orders<Unit, Account, BlockNumber>>;
677700

678701
fn orders(&self) -> Self;
679702

@@ -742,13 +765,13 @@ pub mod traits {
742765
}
743766

744767
// for test only
745-
fn find_order_of(&self, owner: &Account) -> Option<Vec<Order<Unit, Account, BlockNumber>>> {
768+
fn find_order_of(&self, owner: &Account) -> Option<Orders<Unit, Account, BlockNumber>> {
746769
let matched = self
747770
.to_owned()
748771
.open_orders
749772
.into_values()
750773
.filter(|o| o.owner() == *owner)
751-
.collect::<Vec<Order<Unit, Account, BlockNumber>>>();
774+
.collect::<Orders<Unit, Account, BlockNumber>>();
752775
if matched.is_empty() {
753776
return None;
754777
} else {
@@ -854,7 +877,17 @@ pub mod traits {
854877

855878
#[cfg(test)]
856879
mod tests {
857-
use super::OrderBookIndex;
880+
use super::{OrderBookIndex, OrderId};
881+
882+
#[test]
883+
fn order_id_op_works() {
884+
let order_id: OrderId = OrderId::start_bid_id();
885+
assert!(order_id.is_bid());
886+
let mut order_id: OrderId = OrderId::start_ask_id();
887+
assert!(!order_id.is_bid());
888+
order_id += 1;
889+
assert!(order_id.is_bid());
890+
}
858891

859892
#[test]
860893
fn partition_index_works() {

0 commit comments

Comments
 (0)