Skip to content

Commit

Permalink
[deepbook] - make critbit tree and order getters public (#14927)
Browse files Browse the repository at this point in the history
## Description 

Make critbit tree and order getters public

## Test Plan 

There are no logic changes, rely on existing unit tests.
---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
patrickkuo authored Nov 28, 2023
1 parent ef5b712 commit 9e5ba1c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/sui-framework-snapshot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
]
},
"32": {
"git_revision": "dd79f120e0",
"git_revision": "bdbc732269",
"package_ids": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
Expand Down
28 changes: 22 additions & 6 deletions crates/sui-framework/packages/deepbook/sources/clob_v2.move
Original file line number Diff line number Diff line change
Expand Up @@ -1882,27 +1882,43 @@ module deepbook::clob_v2 {
}

// Methods for accessing pool data, used by the order_query package
public(friend) fun asks<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): &CritbitTree<TickLevel> {
public fun asks<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): &CritbitTree<TickLevel> {
&pool.asks
}

public(friend) fun bids<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): &CritbitTree<TickLevel> {
public fun bids<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): &CritbitTree<TickLevel> {
&pool.bids
}

public(friend) fun open_orders(tick_level: &TickLevel): &LinkedTable<u64, Order> {
public fun tick_size<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): u64 {
pool.tick_size
}

public fun maker_rebate_rate<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): u64 {
pool.maker_rebate_rate
}

public fun taker_fee_rate<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): u64 {
pool.taker_fee_rate
}

public fun pool_size<BaseAsset, QuoteAsset>(pool: &Pool<BaseAsset, QuoteAsset>): u64 {
critbit::size(&pool.asks) + critbit::size(&pool.bids)
}

public fun open_orders(tick_level: &TickLevel): &LinkedTable<u64, Order> {
&tick_level.open_orders
}

public(friend) fun order_id(order: &Order): u64 {
public fun order_id(order: &Order): u64 {
order.order_id
}

public(friend) fun tick_level(order: &Order): u64 {
public fun tick_level(order: &Order): u64 {
order.price
}

public(friend) fun expire_timestamp(order: &Order): u64 {
public fun expire_timestamp(order: &Order): u64 {
order.expire_timestamp
}

Expand Down
13 changes: 6 additions & 7 deletions crates/sui-framework/packages/deepbook/sources/critbit.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module deepbook::critbit {

friend deepbook::clob;
friend deepbook::clob_v2;
friend deepbook::order_query;

// <<<<<<<<<<<<<<<<<<<<<<<< Error codes <<<<<<<<<<<<<<<<<<<<<<<<
const EExceedCapacity: u64 = 2;
Expand Down Expand Up @@ -74,15 +73,15 @@ module deepbook::critbit {

// Return (key, index) the leaf with minimum value.
// A market buy order will start consuming liquidty from the min leaf.
public(friend) fun min_leaf<V: store>(tree: &CritbitTree<V>): (u64, u64) {
public fun min_leaf<V: store>(tree: &CritbitTree<V>): (u64, u64) {
assert!(!is_empty(tree), ELeafNotExist);
let min_leaf = table::borrow(&tree.leaves, tree.min_leaf);
return (min_leaf.key, tree.min_leaf)
}

// Return (key, index) the leaf with maximum value.
// A market sell order will start consuming liquidity from the max leaf.
public(friend) fun max_leaf<V: store>(tree: &CritbitTree<V>): (u64, u64) {
public fun max_leaf<V: store>(tree: &CritbitTree<V>): (u64, u64) {
assert!(!is_empty(tree), ELeafNotExist);
let max_leaf = table::borrow(&tree.leaves, tree.max_leaf);
return (max_leaf.key, tree.max_leaf)
Expand Down Expand Up @@ -111,7 +110,7 @@ module deepbook::critbit {
// Return the next leaf (key, index) of the input leaf.
// Market buy orders consume liquidities by iterating through the leaves in ascending order starting from the min leaf of the asks Critbit Tree.
// This function provides the iterator for this procedure.
public(friend) fun next_leaf<V: store>(tree: &CritbitTree<V>, key: u64): (u64, u64) {
public fun next_leaf<V: store>(tree: &CritbitTree<V>, key: u64): (u64, u64) {
let (_, index) = find_leaf(tree, key);
assert!(index != PARTITION_INDEX, ELeafNotExist);
let ptr = MAX_U64 - index;
Expand Down Expand Up @@ -229,7 +228,7 @@ module deepbook::critbit {

// Find the leaf from the tree.
// Returns true and the index of the leaf if exists.
public(friend) fun find_leaf<V: store>(tree: & CritbitTree<V>, key: u64): (bool, u64) {
public fun find_leaf<V: store>(tree: & CritbitTree<V>, key: u64): (bool, u64) {
if (is_empty(tree)) {
return (false, PARTITION_INDEX)
};
Expand Down Expand Up @@ -308,12 +307,12 @@ module deepbook::critbit {
&mut entry.value
}

public(friend) fun borrow_leaf_by_index<V: store>(tree: & CritbitTree<V>, index: u64): &V {
public fun borrow_leaf_by_index<V: store>(tree: & CritbitTree<V>, index: u64): &V {
let entry = table::borrow(&tree.leaves, index);
&entry.value
}

public(friend) fun borrow_leaf_by_key<V: store>(tree: & CritbitTree<V>, key: u64): &V {
public fun borrow_leaf_by_key<V: store>(tree: & CritbitTree<V>, key: u64): &V {
let (is_exist, index) = find_leaf(tree, key);
assert!(is_exist, ELeafNotExist);
borrow_leaf_by_index(tree, index)
Expand Down
1 change: 1 addition & 0 deletions crates/sui-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const MAX_PROTOCOL_VERSION: u64 = 32;
// Add sui::token module to sui framework.
// Enable transfer to object in testnet.
// Enable Narwhal CertificateV2 on mainnet
// Make critbit tree and order getters public in deepbook.

#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProtocolVersion(u64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,56 +240,56 @@ validators:
next_epoch_worker_address: ~
extra_fields:
id:
id: "0x5dbb10b34d4c23a5458fcf573e386e21187b7b60c23358ed0113f24c37b17e51"
id: "0x0af01ebba3529e6850838ef823f53c370849b36fec6e6d7081425441fe281110"
size: 0
voting_power: 10000
operation_cap_id: "0xe8625a1fa906c684596b16e6a0ef3291636d3a88a46443b46daab23cc49e71a6"
operation_cap_id: "0x623a34bdb3a0e7ffe9e305aae693be3fb7f6943b441b759e1206b3685c4c0a3b"
gas_price: 1000
staking_pool:
id: "0xc94c882e5e5bbc25a192e7159e53562d443af82c5e7d03e3cc9f7316018d9171"
id: "0x85c2335401868e29a079c26ef6889b8930434e99cdbf8c748a561870283e1876"
activation_epoch: 0
deactivation_epoch: ~
sui_balance: 20000000000000000
rewards_pool:
value: 0
pool_token_balance: 20000000000000000
exchange_rates:
id: "0xfacbb65dd18bc0bbf6892491a587d745c96b1898956df9d3ca35b77c298bbdf4"
id: "0x15008428b167998b23901a4e63e76cdf5461e9c7e153c7e256c9fa7892eb3729"
size: 1
pending_stake: 0
pending_total_sui_withdraw: 0
pending_pool_token_withdraw: 0
extra_fields:
id:
id: "0xd29644c9bd3ecb0140f1f57670e17e97b6440637a6c3ceb301c9c24b2e4df0f7"
id: "0xaf897b62563479de0db23ae5ba41bad9cf55d2f12b5611412932560db5a6fa96"
size: 0
commission_rate: 200
next_epoch_stake: 20000000000000000
next_epoch_gas_price: 1000
next_epoch_commission_rate: 200
extra_fields:
id:
id: "0x1591cd2cfd886d24c8b5229b50c7aaffda5bbd3249672b4083d14ed92a8d70af"
id: "0xd2bc886a2dcbc02edc9cb87f17ceec2beebc5b8563ab8a8743f80022c5d4a09a"
size: 0
pending_active_validators:
contents:
id: "0xf6d48eed3ab9593de4910cacf53464becd8cf1d3aded193e73c001ac8964a831"
id: "0x1336e7ecdac6fa9fb6f4614f73e38650285e279896195201ebc019fc9c9659de"
size: 0
pending_removals: []
staking_pool_mappings:
id: "0x6c105cc19043b9ad1acb6ada678abf2e9266e725b13980406b5187e1882e5ae2"
id: "0x49925e08476815f2e07310fc27786daf8d5e88a54256bb23c11f9505c6559a46"
size: 1
inactive_validators:
id: "0x45c26aa437deeba1bdf15bb20614eb3cde9446b2c6f2617abfdf0929808a4c31"
id: "0x3de4cea00a989554a671623c48cbff976978e3084ef244ed1bcc9cc2730481ea"
size: 0
validator_candidates:
id: "0x95c589a53c7987f02de29459f63c3410b8f7bace76dcf428201fab5299591fea"
id: "0xe859f322af4f115d6cce4fe5cbe6a9199402fc30d92ce555db0b6c250f1cd081"
size: 0
at_risk_validators:
contents: []
extra_fields:
id:
id: "0xe6ebd935fb13f133b53bdcc148e3ec201fa75f21c93207940750dc13df421fab"
id: "0x245a7cb32c8bc70bdc826aef155b795f10f8e0b2df58b816e1d8c42571731690"
size: 0
storage_fund:
total_object_storage_rebates:
Expand All @@ -306,7 +306,7 @@ parameters:
validator_low_stake_grace_period: 7
extra_fields:
id:
id: "0x48421b50830f851a7091b5fa1fde298556742a08488dc7c7f6960886d42fe826"
id: "0xe58bdc95b81969cf8f949141fa83c15ff48dfe5744c39b458216381c1c662c54"
size: 0
reference_gas_price: 1000
validator_report_records:
Expand All @@ -320,7 +320,7 @@ stake_subsidy:
stake_subsidy_decrease_rate: 1000
extra_fields:
id:
id: "0xcd675f544674a9df883ac223f314122b5bcb55d2ade427657cc12d6d7446e13c"
id: "0x277de611da9753c887f415691e88258f98ecfab61c0f52f1fb08d4c29afb3bf6"
size: 0
safe_mode: false
safe_mode_storage_rewards:
Expand All @@ -332,6 +332,6 @@ safe_mode_non_refundable_storage_fee: 0
epoch_start_timestamp_ms: 10
extra_fields:
id:
id: "0xd8335338871c81cbd13592efd5cb154b4c4d260c5be7caa180069bf9475da152"
id: "0xd43a7991c87c5a951063adddb889dd90eb4612bd9880a2c34395238c7d20d5cf"
size: 0

0 comments on commit 9e5ba1c

Please sign in to comment.