Skip to content

Commit

Permalink
remove test
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaiton committed Nov 25, 2024
1 parent e787025 commit e88ec71
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 94 deletions.
94 changes: 2 additions & 92 deletions crates/hyperdrive-math/src/short/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl State {
//
// The guess that we make is very important in determining how quickly
// we converge to the solution.
let mut max_bond_guess = self.absolute_max_short_guess(spot_price, checkpoint_exposure)?;
let mut max_bond_guess = self.absolute_max_short_guess(checkpoint_exposure)?;
// If the initial guess is insolvent, we need to throw an error.
let mut solvency = self.solvency_after_short(max_bond_guess, checkpoint_exposure)?;
for _ in 0..maybe_max_iterations.unwrap_or(7) {
Expand Down Expand Up @@ -612,7 +612,7 @@ impl State {
if self.effective_share_reserves()? < min_share_reserves {
return Err(eyre!(
"Current effective pool share reserves = {:#?} are below the minimum = {:#?}.",
self.effective_share_reserves(),
self.effective_share_reserves()?,
min_share_reserves
));
}
Expand Down Expand Up @@ -931,96 +931,6 @@ mod tests {
Ok(())
}

/// This test differentially fuzzes the `calculate_max_short` function against
/// the Solidity analogue `calculateMaxShort`. `calculateMaxShort` doesn't take
/// a trader's budget into account, so it only provides a subset of
/// `calculate_max_short`'s functionality. With this in mind, we provide
/// `calculate_max_short` with a budget of `U256::MAX` to ensure that the two
/// functions are equivalent.
#[tokio::test]
async fn fuzz_calculate_absolute_max_short() -> Result<()> {
// TODO: We should be able to pass these tests with a much lower (if not zero) tolerance.
let sol_correctness_tolerance = fixed!(1e17);

// Fuzz the rust and solidity implementations against each other.
let chain = TestChain::new().await?;
let mut rng = thread_rng();
for _ in 0..*FAST_FUZZ_RUNS {
let state = rng.gen::<State>();
let checkpoint_exposure = {
let value = rng.gen_range(fixed!(0)..=FixedPoint::from(U256::from(U128::MAX)));
if rng.gen() {
-I256::try_from(value)?
} else {
I256::try_from(value)?
}
};
let max_iterations = 7;
// We need to catch panics because of overflows.
let rust_max_bond_amount = panic::catch_unwind(|| {
state.calculate_absolute_max_short(
state.calculate_spot_price_down()?,
checkpoint_exposure,
Some(max_iterations),
)
});
// Run the solidity function & compare outputs.
match chain
.mock_hyperdrive_math()
.calculate_max_short(
MaxTradeParams {
share_reserves: state.info.share_reserves,
bond_reserves: state.info.bond_reserves,
longs_outstanding: state.info.longs_outstanding,
long_exposure: state.info.long_exposure,
share_adjustment: state.info.share_adjustment,
time_stretch: state.config.time_stretch,
vault_share_price: state.info.vault_share_price,
initial_vault_share_price: state.config.initial_vault_share_price,
minimum_share_reserves: state.config.minimum_share_reserves,
curve_fee: state.config.fees.curve,
flat_fee: state.config.fees.flat,
governance_lp_fee: state.config.fees.governance_lp,
},
checkpoint_exposure,
max_iterations.into(),
)
.call()
.await
{
Ok(sol_max_bond_amount) => {
// Make sure the solidity & rust runctions gave the same value.
let rust_max_bonds_unwrapped = rust_max_bond_amount.unwrap().unwrap();
let sol_max_bonds_fp = FixedPoint::from(sol_max_bond_amount);
let error = if sol_max_bonds_fp > rust_max_bonds_unwrapped {
sol_max_bonds_fp - rust_max_bonds_unwrapped
} else {
rust_max_bonds_unwrapped - sol_max_bonds_fp
};
assert!(
error < sol_correctness_tolerance,
"expected abs(solidity_amount={} - rust_amount={})={} < tolerance={}",
sol_max_bonds_fp,
rust_max_bonds_unwrapped,
error,
sol_correctness_tolerance,
);
}
// Hyperdrive Solidity calculate_max_short threw an error
Err(sol_err) => {
assert!(
rust_max_bond_amount.is_err()
|| rust_max_bond_amount.as_ref().unwrap().is_err(),
"expected rust_max_short={:#?} to have an error.\nsolidity error={:#?}",
rust_max_bond_amount,
sol_err
);
}
};
}
Ok(())
}

#[tokio::test]
async fn fuzz_calculate_max_short_budget_consumed() -> Result<()> {
// TODO: This should be fixed!(0.0001e18) == 0.01%
Expand Down
12 changes: 10 additions & 2 deletions crates/hyperdrive-math/src/test_utils/preamble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ pub async fn initialize_pool_with_random_state(

// Add some liquidity again to make sure future bots can make trades.
let liquidity_amount = rng.gen_range(fixed!(1_000e18)..=fixed!(100_000_000e18));
alice.fund(liquidity_amount).await?;
alice.add_liquidity(liquidity_amount, None).await?;
let exposure = FixedPoint::try_from(
alice
.get_checkpoint_exposure(alice.latest_checkpoint().await?)
.await?
.max(I256::zero()),
)? + alice.get_state().await?.long_exposure();
alice.fund(liquidity_amount + exposure).await?;
alice
.add_liquidity(liquidity_amount + exposure, None)
.await?;

Ok(())
}
Expand Down

0 comments on commit e88ec71

Please sign in to comment.