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

[Sumtree]: E2E and Fuzz Testing #191

Merged
merged 109 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
b35f421
feat: e2e test scaffolding
crnbarr93 Jun 14, 2024
713ec25
feat: fuzz testing first pass
crnbarr93 Jun 14, 2024
cc9c2f8
test: added claiming to fuzz tests
crnbarr93 Jun 14, 2024
24edf77
test: improved fuzz testing algorithm
crnbarr93 Jun 14, 2024
f756b45
fix: fixed an issue with tick syncing logic
crnbarr93 Jun 15, 2024
5e5ea36
fix: added exit condition to tick sync to account for moving etas value
crnbarr93 Jun 17, 2024
ee3d61a
test: fixed test_get_prefix_sum_valid
crnbarr93 Jun 17, 2024
225d219
test: fixed spot price query test
crnbarr93 Jun 17, 2024
84ad5d5
test: fixed test_run_market_order
crnbarr93 Jun 17, 2024
55e061c
chore: removed unused imports
crnbarr93 Jun 17, 2024
dcef0f3
test: fixed failing e2e fuzz test
crnbarr93 Jun 17, 2024
453b4d2
test: added etas invariant check to ticks for e2e
crnbarr93 Jun 17, 2024
49684e0
chore: commented e2e linear fuzz test
crnbarr93 Jun 17, 2024
d0dc7db
test: added more e2e tests and improved cancelled orders test
crnbarr93 Jun 17, 2024
3a4c16a
Merge branch 'connor/tick-query' into connor/e2e-testing
crnbarr93 Jun 17, 2024
6915386
chore: fixed post merge issues
crnbarr93 Jun 17, 2024
a185746
test: fixed failing tests
crnbarr93 Jun 17, 2024
27c5a73
test: fixed claim success to use new cancels query
crnbarr93 Jun 17, 2024
8b8baba
tests: fixed failing tests
crnbarr93 Jun 17, 2024
7f844f7
tests: fixed expected claim amount expected output
crnbarr93 Jun 18, 2024
421c2f5
feat: added count to orders by owner query
crnbarr93 Jun 18, 2024
545f4d9
feat: added orders by tick id query
crnbarr93 Jun 18, 2024
756c320
refactor: commented unused test env methods
crnbarr93 Jun 19, 2024
97365e3
test: added mixed fuzz tests
crnbarr93 Jun 19, 2024
0884304
feat: added mixed fuzz tests
crnbarr93 Jun 21, 2024
50761af
feat: added syncing to cancelled limits and removed unused quantity r…
crnbarr93 Jun 21, 2024
721b82f
refactor: improved error handling for sync_tick
crnbarr93 Jun 21, 2024
4518498
fix: potential fix for prefix sum algorithm [WIP]
crnbarr93 Jun 24, 2024
536c42f
feat: added min order check when placing a limit
crnbarr93 Jun 24, 2024
5f4e85a
test: fixed failing tests
crnbarr93 Jun 24, 2024
dbbb298
chore: removed unused imports
crnbarr93 Jun 24, 2024
c812877
test: added cases for min quantity
crnbarr93 Jun 24, 2024
cad9c4a
feat: improved sync tick error handling
crnbarr93 Jun 24, 2024
5610016
fix: added case for when syncing the left node would cause the right …
crnbarr93 Jun 24, 2024
700043d
test: fixed all failing tests
crnbarr93 Jun 24, 2024
64bfa57
chore: commented new prefix sum condition
crnbarr93 Jun 24, 2024
5533a9c
chore: small comment addition
crnbarr93 Jun 24, 2024
7685ed2
feat: added time restriction for testing
crnbarr93 Jun 25, 2024
5cdd161
refactor: altered prefix sum algorithm
crnbarr93 Jun 25, 2024
26ae6ec
fix: removed max range - value check for prefix sum
crnbarr93 Jun 26, 2024
bccdac2
feat: silently fail on zero amount claims
crnbarr93 Jun 26, 2024
1b3ac8f
Merge branch 'connor/min-order-quantity' into connor/e2e-testing
crnbarr93 Jun 26, 2024
1228cdb
Merge branch 'connor/prefix-sum' into connor/e2e-testing
crnbarr93 Jun 26, 2024
ae2245c
refactor: stopped removing claimed orders in fuzz tests unless they a…
crnbarr93 Jun 26, 2024
9231aa9
Merge branch 'connor/tick-query' into connor/e2e-testing
crnbarr93 Jun 26, 2024
f35734a
tests: fixed any failing tests
crnbarr93 Jun 26, 2024
e7eafb0
Update contracts/sumtree-orderbook/src/sumtree/tree.rs
crnbarr93 Jun 27, 2024
f43f41f
Update contracts/sumtree-orderbook/src/sumtree/tree.rs
crnbarr93 Jun 27, 2024
dd196a0
Update contracts/sumtree-orderbook/src/sumtree/tree.rs
crnbarr93 Jun 27, 2024
cfb29ab
Update contracts/sumtree-orderbook/src/sumtree/tree.rs
crnbarr93 Jun 27, 2024
801d28e
Update contracts/sumtree-orderbook/src/sumtree/tree.rs
crnbarr93 Jun 27, 2024
03a137c
Update contracts/sumtree-orderbook/src/sumtree/tree.rs
crnbarr93 Jun 27, 2024
a85387c
Merge branch 'main' into connor/prefix-sum
crnbarr93 Jun 27, 2024
c390f7e
fix: add required input to prefix sum in get_unrealized_cancels
crnbarr93 Jun 27, 2024
6637ec7
fix: fixed spot price calculation to account for direction
crnbarr93 Jun 27, 2024
9d6f82a
fix: inverted base/quote denoms for spot price to be correct
crnbarr93 Jun 27, 2024
6c3636d
test: added more cases
crnbarr93 Jun 27, 2024
06ea434
test: added more prefix sum test cases for batch realization
crnbarr93 Jun 27, 2024
6f9e331
test: added batch realization test cases
crnbarr93 Jun 27, 2024
c896369
chore: removed redundant check
crnbarr93 Jun 27, 2024
e07de7b
test: added cancelled orders test case
crnbarr93 Jun 27, 2024
32d867a
test: fixed failing test, added last_tick_sync_etas and added cancell…
crnbarr93 Jun 28, 2024
f4bd03e
test: added additional checks for mutating state in test_sync_tick
crnbarr93 Jun 28, 2024
ba5392e
test: extended cancelled orders test case
crnbarr93 Jun 28, 2024
dbbe47f
chore: fixed tree comment in cancelled orders
crnbarr93 Jun 28, 2024
8660908
Merge branch 'connor/prefix-sum' into connor/e2e-testing
crnbarr93 Jun 28, 2024
05e8fa1
fix: fixed compiler errors and failing tests
crnbarr93 Jun 28, 2024
d02e390
Merge branch 'connor/spot-price-adjustment' into connor/e2e-testing
crnbarr93 Jun 28, 2024
008a24b
test: added more fuzz test assertions
crnbarr93 Jun 30, 2024
83fcb72
Merge branch 'alpo/swap-step-check' into connor/e2e-testing
crnbarr93 Jun 30, 2024
ecea399
chore: commented more of test_fuzz
crnbarr93 Jun 30, 2024
67850a1
refactor: cleaned up utils for e2e
crnbarr93 Jun 30, 2024
9327086
refactor: added run_for_duration to fuzz testing
crnbarr93 Jun 30, 2024
4c7b759
test: added more comments and adjusted assertions for linear fuzz
crnbarr93 Jun 30, 2024
225def7
test: fixed failing tests
crnbarr93 Jun 30, 2024
1c575e4
fix: skip ticks with no liquidity
crnbarr93 Jun 30, 2024
cfa55aa
test: fix market order generation
crnbarr93 Jun 30, 2024
b5197c2
fix price computation and recompute base market order test cases
AlpinYukseloglu Jun 30, 2024
1a4c2e0
fix: corrected price calculation and related e2e tests
crnbarr93 Jul 1, 2024
7628b9b
fix: fixed spot price query and test
crnbarr93 Jul 1, 2024
a2d6b6f
test: fixed calc_out_amount_given_in test
crnbarr93 Jul 1, 2024
fd05e99
test: fixed run_market_order_moving_tick
crnbarr93 Jul 1, 2024
9924ca1
test: fixed test_claim_order
crnbarr93 Jul 1, 2024
d7eb87f
test: fixed test_direcitonal_liquidity
crnbarr93 Jul 1, 2024
44dc901
test: fixed test_batch_claim
crnbarr93 Jul 1, 2024
100006d
chore: removed unused imports
crnbarr93 Jul 1, 2024
2a1178f
chore: commented test_claim_order
crnbarr93 Jul 1, 2024
552f3cc
Merge branch 'alpo/fix-price' into connor/e2e-testing
crnbarr93 Jul 1, 2024
37d5aac
fix: failing test
crnbarr93 Jul 1, 2024
f609198
test: adjusted tick bounds for e2e
crnbarr93 Jul 1, 2024
c81cf3b
test: added run time for all e2e tests and decrement market value ass…
crnbarr93 Jul 2, 2024
2dd35e4
test: added test to ensure market price deteriorates as expected
crnbarr93 Jul 2, 2024
bd2491b
test: removed unused test
crnbarr93 Jul 3, 2024
ff9fe52
chore: removed print lines
crnbarr93 Jul 3, 2024
2c7e372
chore: removed unused code/empty lines
crnbarr93 Jul 3, 2024
da338b1
chore: further cleanup
crnbarr93 Jul 3, 2024
16d6cef
refactor: rewrote tick invariants assertion
crnbarr93 Jul 3, 2024
25d255e
chore: util function scoping and additional comments
crnbarr93 Jul 3, 2024
64c2f4d
chore: remove unused variable
crnbarr93 Jul 3, 2024
b3f60d3
chore: refactored out given in querys for e2e
crnbarr93 Jul 3, 2024
523f283
chore: cleanup test_env
crnbarr93 Jul 3, 2024
a5ca8b2
test: rearranged e2e files and refactored order cleanup for fuzz tests
crnbarr93 Jul 3, 2024
74337a7
Merge branch 'main' into alpo/fix-price
crnbarr93 Jul 3, 2024
d49afab
test: fixed failing test
crnbarr93 Jul 3, 2024
030176e
chore: claim fuzz operation cleanup
crnbarr93 Jul 3, 2024
619a8fa
Merge branch 'alpo/fix-price' into connor/e2e-testing
crnbarr93 Jul 3, 2024
ad963ea
chore: final cleanup
crnbarr93 Jul 3, 2024
50343e6
test: reduced fuzz test duration
crnbarr93 Jul 3, 2024
5c1911a
test: reduced fuzz duration oper count
crnbarr93 Jul 3, 2024
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
2,395 changes: 2,137 additions & 258 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion contracts/sumtree-orderbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rpath = false
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []
skip-integration-tests = []

[package.metadata.scripts]
optimize = """docker run --rm -v "$(pwd)":/code \
Expand All @@ -52,12 +53,13 @@ schemars = "0.8.15"
serde = { version = "1.0.189", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.49" }
osmosis-std-derive = "0.15.3"
osmosis-std = "0.16.0"
osmosis-std = "0.25.0"
prost = { version = "0.11.2", default-features = false, features = [
"prost-derive",
] }


[dev-dependencies]
cw-multi-test = "0.18.0"
osmosis-test-tube = { version = "25.0.0", features = ["wasm-sudo"] }
rand = "0.8.4"
1 change: 1 addition & 0 deletions contracts/sumtree-orderbook/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
} => Ok(to_json_binary(&query::orders_by_owner(
deps, owner, start_from, end_at, limit,
)?)?),
QueryMsg::OrderbookState {} => Ok(to_json_binary(&query::orderbook_state(deps)?)?),
QueryMsg::TicksById { tick_ids } => {
Ok(to_json_binary(&query::ticks_by_id(deps, tick_ids)?)?)
}
Expand Down
5 changes: 4 additions & 1 deletion contracts/sumtree-orderbook/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum QueryMsg {
#[returns(bool)]
IsActive {},

#[returns(Vec<crate::types::LimitOrder>)]
#[returns(OrdersResponse)]
OrdersByOwner {
// The address of the order maker
owner: Addr,
Expand All @@ -129,6 +129,9 @@ pub enum QueryMsg {
limit: Option<u64>,
},

#[returns(crate::types::Orderbook)]
OrderbookState {},

#[returns(DenomsResponse)]
Denoms {},

Expand Down
10 changes: 7 additions & 3 deletions contracts/sumtree-orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ pub fn place_limit(
.with_placed_at(env.block.time);

let quant_dec256 = Decimal256::from_ratio(limit_order.quantity.u128(), Uint256::one());

// Save the order to the orderbook
orders().save(deps.storage, &(tick_id, order_id), &limit_order)?;

Expand Down Expand Up @@ -166,7 +165,6 @@ pub fn cancel_limit(
.effective_total_amount_swapped,
)?;

// Ensure the order has not been filled.
let tick_state = TICK_STATE.load(deps.storage, tick_id).unwrap_or_default();
let tick_values = tick_state.get_values(order.order_direction);
ensure!(
Expand Down Expand Up @@ -213,10 +211,10 @@ pub fn cancel_limit(
);

orders().remove(deps.storage, &(order.tick_id, order.order_id))?;

curr_tick_values.total_amount_of_liquidity = curr_tick_values
.total_amount_of_liquidity
.checked_sub(Decimal256::from_ratio(order.quantity, Uint256::one()))?;

curr_tick_state.set_values(order.order_direction, curr_tick_values);
TICK_STATE.save(deps.storage, order.tick_id, &curr_tick_state)?;
subtract_directional_liquidity(deps.storage, order.order_direction, quant_dec256)?;
Expand Down Expand Up @@ -503,6 +501,11 @@ pub(crate) fn run_market_order_internal(
let current_tick_id = maybe_current_tick?;
let mut current_tick = TICK_STATE.load(storage, current_tick_id)?;
let mut current_tick_values = current_tick.get_values(order.order_direction.opposite());

if current_tick_values.total_amount_of_liquidity.is_zero() {
continue;
}

let tick_price = tick_to_price(current_tick_id)?;
last_tick_price = tick_price;

Expand Down Expand Up @@ -634,6 +637,7 @@ pub(crate) fn claim_order(
// Sync the tick the order is on to ensure correct ETAS
let bid_tick_values = tick_state.get_values(OrderDirection::Bid);
let ask_tick_values = tick_state.get_values(OrderDirection::Ask);

sync_tick(
storage,
tick_id,
Expand Down
25 changes: 20 additions & 5 deletions contracts/sumtree-orderbook/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
sudo::ensure_swap_fee,
sumtree::tree::{get_prefix_sum, get_root_node},
tick_math::tick_to_price,
types::{FilterOwnerOrders, LimitOrder, MarketOrder, OrderDirection, TickState},
types::{FilterOwnerOrders, MarketOrder, OrderDirection, Orderbook, TickState},
ContractError,
};

Expand Down Expand Up @@ -60,8 +60,8 @@ pub(crate) fn spot_price(
let price = tick_to_price(next_tick)?;

let spot_price = match direction {
OrderDirection::Ask => price.inv().unwrap(),
OrderDirection::Bid => price,
OrderDirection::Ask => price,
OrderDirection::Bid => price.inv().unwrap(),
};

Ok(SpotPriceResponse {
Expand Down Expand Up @@ -205,15 +205,25 @@ pub(crate) fn orders_by_owner(
start_from: Option<(i64, u64)>,
end_at: Option<(i64, u64)>,
limit: Option<u64>,
) -> ContractResult<Vec<LimitOrder>> {
) -> ContractResult<OrdersResponse> {
let count = orders()
.idx
.owner
.prefix(owner.clone())
.keys(deps.storage, None, None, Order::Ascending)
.count();
let orders = get_orders_by_owner(
deps.storage,
FilterOwnerOrders::all(owner),
start_from,
end_at,
limit,
)?;
Ok(orders)

Ok(OrdersResponse {
count: count as u64,
orders,
})
}

pub(crate) fn denoms(deps: Deps) -> ContractResult<DenomsResponse> {
Expand All @@ -224,6 +234,11 @@ pub(crate) fn denoms(deps: Deps) -> ContractResult<DenomsResponse> {
})
}

pub(crate) fn orderbook_state(deps: Deps) -> ContractResult<Orderbook> {
let orderbook = ORDERBOOK.load(deps.storage)?;
Ok(orderbook)
}

pub(crate) fn ticks_by_id(deps: Deps, tick_ids: Vec<i64>) -> ContractResult<TicksResponse> {
let mut ticks: Vec<TickIdAndState> = vec![];
for tick_id in tick_ids {
Expand Down
2 changes: 1 addition & 1 deletion contracts/sumtree-orderbook/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn get_orders_by_owner(
page_size: Option<u64>,
) -> StdResult<Vec<LimitOrder>> {
let page_size = page_size.unwrap_or(DEFAULT_PAGE_SIZE) as usize;
let min = min.map(Bound::exclusive);
let min = min.map(Bound::inclusive);
let max = max.map(Bound::inclusive);

// Define the prefix iterator based on the filter
Expand Down
1 change: 1 addition & 0 deletions contracts/sumtree-orderbook/src/sumtree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn get_prefix_sum(
// Since the longest path this function can walk is from the root to a leaf, it runs in O(log(N)) time. Given
// how it is able to terminate early using our sumtree's range properties, in many cases it will likely run
// in much less.

fn prefix_sum_walk(
storage: &dyn Storage,
node: &TreeNode,
Expand Down
2 changes: 2 additions & 0 deletions contracts/sumtree-orderbook/src/tests/e2e/cases/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod test_fuzz;
mod test_orders;
Loading
Loading