Skip to content

Commit

Permalink
maintenance test (#114)
Browse files Browse the repository at this point in the history
* maintenance test
* disable maintenance slot during benchmark
* declare test feature for all targets
* fix clock related tests

---------

Co-authored-by: Britt Cyr <[email protected]>
  • Loading branch information
mschneider and brittcyr authored Sep 22, 2024
1 parent e9a7e71 commit 061647d
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-code-review-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
cargo build-sbf
- name: Run sbf tests
run: cargo test-sbf -- --test-threads 1
run: cargo test-sbf --features test -- --test-threads 1

coverage:
name: Coverage
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:


- name: Generate code coverage
run: cargo llvm-cov --lcov --output-path lcov.info --workspace -- --nocapture --test-threads 1
run: cargo llvm-cov --lcov --output-path lcov.info --workspace --features test -- --nocapture --test-threads 1
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
5 changes: 4 additions & 1 deletion client/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ authors = ["Britt Cyr <[email protected]>"]
description = "Jupiter AMM interface for Manifest exchange"
license-file = "LICENSE"

[features]
test = []

[dependencies]
anyhow = { workspace = true }
manifest = { path = "../../programs/manifest" }
manifest = { path = "../../programs/manifest", features=['no-clock'] }
hypertree = { path = "../../lib" }
jupiter-amm-interface = "0.1.1"
solana-sdk = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license-file = "LICENSE"
colored = ["dep:colored"]
fuzz = []
trace = []
test = []

[dependencies]
bytemuck = { workspace = true }
Expand Down
12 changes: 3 additions & 9 deletions programs/manifest/src/program/processor/global_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use crate::{
program::{batch_update::MarketDataTreeNodeType, get_mut_dynamic_account, ManifestError},
quantities::{GlobalAtoms, WrapperU64},
require,
state::{
utils::get_now_slot, GlobalRefMut, MarketRefMut, OrderType, RestingOrder, MARKET_BLOCK_SIZE,
},
state::{utils::get_now_slot, GlobalRefMut, MarketRefMut, RestingOrder, MARKET_BLOCK_SIZE},
validation::loaders::{GlobalCleanContext, GlobalTradeAccounts},
};

Expand Down Expand Up @@ -94,12 +92,8 @@ pub(crate) fn process_global_clean(
"Wrong global provided",
)?;

// Verify that the resting order is a global order
require!(
resting_order.get_order_type() == OrderType::Global,
ManifestError::InvalidClean,
"Tried to cancel non global",
)?;
// Do not need to require that the order is global. This ix is useful
// for cleaning up markets with all expired orders.

resting_order
};
Expand Down
6 changes: 3 additions & 3 deletions programs/manifest/src/state/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub const MARKET_FREE_LIST_BLOCK_SIZE: usize = MARKET_BLOCK_SIZE - FREE_LIST_OVE
pub const GLOBAL_FREE_LIST_BLOCK_SIZE: usize = GLOBAL_BLOCK_SIZE - FREE_LIST_OVERHEAD;

pub const NO_EXPIRATION_LAST_VALID_SLOT: u32 = 0;
pub const NEXT_PLANNED_MAINTENANCE_SLOT: u32 = 293522000; // ~Fri Oct 04 2024 midnight GMT
pub const NEXT_PLANNED_MAINTENANCE_SLOT: u32 = 293522000; // ~Fri Oct 04 2024

pub const MARKET_FIXED_DISCRIMINANT: u64 = 4859840929024028656;
pub const GLOBAL_FIXED_DISCRIMINANT: u64 = 10787423733276977665;
Expand All @@ -44,7 +44,7 @@ pub const GAS_DEPOSIT_LAMPORTS: u64 = 5_000;
/// to fit in 4 pages. This is sufficiently big such that it is not possible to
/// fully evict all seats in one flash loan transaction due to the withdraw
/// accounts limit.
#[cfg(test)]
#[cfg(feature = "test")]
pub const MAX_GLOBAL_SEATS: u16 = 4;
#[cfg(not(test))]
#[cfg(not(feature = "test"))]
pub const MAX_GLOBAL_SEATS: u16 = 999;
4 changes: 3 additions & 1 deletion programs/manifest/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,9 @@ impl<Fixed: DerefOrBorrowMut<MarketFixed>, Dynamic: DerefOrBorrowMut<[u8]>>
require!(
now_slot < NEXT_PLANNED_MAINTENANCE_SLOT,
ManifestError::AlreadyExpired,
"manifest is under planned maintenance"
"manifest is under planned maintenance now: {} maintenance_slot: {}",
now_slot,
NEXT_PLANNED_MAINTENANCE_SLOT,
)?;
assert_not_already_expired(last_valid_slot, now_slot)?;

Expand Down
2 changes: 1 addition & 1 deletion programs/manifest/src/state/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn get_now_slot() -> u32 {
// maliciously manipulated to clear all orders with expirations on the
// orderbook.
#[cfg(feature = "no-clock")]
let now_slot: u64 = u64::MAX;
let now_slot: u64 = (super::NEXT_PLANNED_MAINTENANCE_SLOT as u32 - 1_u32).into();
#[cfg(not(feature = "no-clock"))]
let now_slot: u64 = solana_program::clock::Clock::get()
.unwrap_or(solana_program::clock::Clock {
Expand Down
50 changes: 49 additions & 1 deletion programs/manifest/tests/cases/global.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use hypertree::DataIndex;
use hypertree::{DataIndex, HyperTreeValueIteratorTrait};
use manifest::{
program::{
batch_update::{CancelOrderParams, PlaceOrderParams},
Expand Down Expand Up @@ -825,3 +825,51 @@ async fn global_clean() -> anyhow::Result<()> {

Ok(())
}

#[tokio::test]
async fn maintenance_clean() -> anyhow::Result<()> {
let mut test_fixture: TestFixture = TestFixture::new().await;
test_fixture.claim_seat().await?;
test_fixture.global_add_trader().await?;
test_fixture.deposit(Token::USDC, 100).await?;

test_fixture
.batch_update_for_keypair(
None,
vec![],
vec![PlaceOrderParams::new(
100,
1,
0,
true,
OrderType::Limit,
NO_EXPIRATION_LAST_VALID_SLOT,
)],
&test_fixture.payer_keypair().insecure_clone(),
)
.await?;

test_fixture.advance_time_seconds(14 * 24 * 60 * 60).await;

// Clean should succeed.
send_tx_with_retry(
Rc::clone(&test_fixture.context),
&[global_clean_instruction(
&test_fixture.global_fixture.key,
&test_fixture.payer(),
&test_fixture.market_fixture.key,
MARKET_BLOCK_SIZE as DataIndex,
)],
Some(&test_fixture.payer()),
&[&test_fixture.payer_keypair().insecure_clone()],
)
.await?;

test_fixture.market_fixture.reload().await;

let bids = test_fixture.market_fixture.market.get_bids();
let next = bids.iter::<RestingOrder>().next();
assert_eq!(next, None);

Ok(())
}
2 changes: 2 additions & 0 deletions programs/manifest/tests/cases/place_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ async fn match_limit_orders_more_than_resting_test() -> anyhow::Result<()> {
Ok(())
}

/*
#[tokio::test]
async fn match_limit_orders_fail_expired_test() -> anyhow::Result<()> {
let mut test_fixture: TestFixture = TestFixture::new().await;
Expand Down Expand Up @@ -426,6 +427,7 @@ async fn match_limit_orders_fail_expired_test() -> anyhow::Result<()> {
);
Ok(())
}
*/

#[tokio::test]
async fn match_limit_orders_partial_match_price_test() -> anyhow::Result<()> {
Expand Down
42 changes: 0 additions & 42 deletions programs/manifest/tests/cases/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ async fn swap_full_match_test_sell_exact_in() -> anyhow::Result<()> {
)
.await?;

test_fixture
.place_order_for_keypair(
Side::Bid,
1 * SOL_UNIT_SIZE,
1,
0,
10,
OrderType::Limit,
&second_keypair,
)
.await?;

test_fixture
.place_order_for_keypair(
Side::Bid,
Expand All @@ -82,8 +70,6 @@ async fn swap_full_match_test_sell_exact_in() -> anyhow::Result<()> {
.mint_to(&test_fixture.payer_sol_fixture.key, 3 * SOL_UNIT_SIZE)
.await;

test_fixture.advance_time_seconds(20).await;

assert_eq!(
test_fixture.payer_sol_fixture.balance_atoms().await,
3 * SOL_UNIT_SIZE
Expand Down Expand Up @@ -131,18 +117,6 @@ async fn swap_full_match_test_sell_exact_out() -> anyhow::Result<()> {
)
.await?;

test_fixture
.place_order_for_keypair(
Side::Bid,
1 * SOL_UNIT_SIZE,
1,
0,
10,
OrderType::Limit,
&second_keypair,
)
.await?;

test_fixture
.place_order_for_keypair(
Side::Bid,
Expand All @@ -160,8 +134,6 @@ async fn swap_full_match_test_sell_exact_out() -> anyhow::Result<()> {
.mint_to(&test_fixture.payer_sol_fixture.key, 3 * SOL_UNIT_SIZE)
.await;

test_fixture.advance_time_seconds(20).await;

assert_eq!(
test_fixture.payer_sol_fixture.balance_atoms().await,
3 * SOL_UNIT_SIZE
Expand Down Expand Up @@ -209,18 +181,6 @@ async fn swap_full_match_test_buy_exact_in() -> anyhow::Result<()> {
)
.await?;

test_fixture
.place_order_for_keypair(
Side::Ask,
1 * SOL_UNIT_SIZE,
1,
0,
10,
OrderType::Limit,
&second_keypair,
)
.await?;

test_fixture
.place_order_for_keypair(
Side::Ask,
Expand All @@ -238,8 +198,6 @@ async fn swap_full_match_test_buy_exact_in() -> anyhow::Result<()> {
.mint_to(&test_fixture.payer_usdc_fixture.key, 3_000 * USDC_UNIT_SIZE)
.await;

test_fixture.advance_time_seconds(20).await;

assert_eq!(test_fixture.payer_sol_fixture.balance_atoms().await, 0);
assert_eq!(
test_fixture.payer_usdc_fixture.balance_atoms().await,
Expand Down
8 changes: 8 additions & 0 deletions programs/ui-wrapper/tests/cases/place_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ async fn wrapper_place_order_test() -> anyhow::Result<()> {
AccountMeta::new(global_base, false),
AccountMeta::new(global_base_vault, false),
AccountMeta::new(base_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(quote_mint, false),
AccountMeta::new(global_quote, false),
AccountMeta::new(global_quote_vault, false),
AccountMeta::new(quote_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
],
data: [
ManifestWrapperInstruction::PlaceOrder.to_vec(),
Expand Down Expand Up @@ -354,10 +356,12 @@ async fn wrapper_place_order_with_broke_owner_test() -> anyhow::Result<()> {
AccountMeta::new(global_base, false),
AccountMeta::new(global_base_vault, false),
AccountMeta::new(base_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(quote_mint, false),
AccountMeta::new(global_quote, false),
AccountMeta::new(global_quote_vault, false),
AccountMeta::new(quote_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
],
data: [
ManifestWrapperInstruction::PlaceOrder.to_vec(),
Expand Down Expand Up @@ -633,10 +637,12 @@ async fn wrapper_fill_order_test() -> anyhow::Result<()> {
AccountMeta::new(global_base, false),
AccountMeta::new(global_base_vault, false),
AccountMeta::new(base_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(quote_mint, false),
AccountMeta::new(global_quote, false),
AccountMeta::new(global_quote_vault, false),
AccountMeta::new(quote_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
],
data: [
ManifestWrapperInstruction::PlaceOrder.to_vec(),
Expand Down Expand Up @@ -729,10 +735,12 @@ async fn wrapper_fill_order_test() -> anyhow::Result<()> {
AccountMeta::new(global_base, false),
AccountMeta::new(global_base_vault, false),
AccountMeta::new(base_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(quote_mint, false),
AccountMeta::new(global_quote, false),
AccountMeta::new(global_quote_vault, false),
AccountMeta::new(quote_vault, false),
AccountMeta::new_readonly(spl_token::id(), false),
],
data: [
ManifestWrapperInstruction::PlaceOrder.to_vec(),
Expand Down

0 comments on commit 061647d

Please sign in to comment.