Skip to content

Commit

Permalink
Use the overflow-checks=true equivalent with the fixed crate (#476)
Browse files Browse the repository at this point in the history
* Vendor `fixed` crate to have checked math in release mode
* remove all cm!()
* drop superfluous parens
* drop use of checked_math crate
* manual removal of redundant checked_* functions
  • Loading branch information
ckamm authored Feb 24, 2023
1 parent 3307393 commit 5c7a2e3
Show file tree
Hide file tree
Showing 44 changed files with 390 additions and 508 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
[submodule "switchboard-v2"]
path = 3rdparty/switchboard-v2
url = https://github.com/blockworks-foundation/sbv2-solana.git
[submodule "3rdparty/fixed"]
path = 3rdparty/fixed
url = https://gitlab.com/ckamm/fixed.git
1 change: 1 addition & 0 deletions 3rdparty/fixed
Submodule fixed added at d0f6e6
67 changes: 14 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions bin/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ anyhow = "1.0"
clap = { version = "3.1.8", features = ["derive", "env"] }
dotenv = "0.15.0"
env_logger = "0.8.4"
fixed = { version = "=1.11.0", features = ["serde", "borsh"] }
fixed-macro = "^1.1.1"
fixed = { path = "../../3rdparty/fixed", version = "=1.23.0", features = ["serde", "borsh"] }
futures = "0.3.21"
log = "0.4.0"
mango-v4 = { path = "../../programs/mango-v4", features = ["client"] }
Expand Down
3 changes: 1 addition & 2 deletions bin/keeper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ anyhow = "1.0"
clap = { version = "3.1.8", features = ["derive", "env"] }
dotenv = "0.15.0"
env_logger = "0.8.4"
fixed = { version = "=1.11.0", features = ["serde", "borsh"] }
fixed-macro = "^1.1.1"
fixed = { path = "../../3rdparty/fixed", version = "=1.23.0", features = ["serde", "borsh"] }
futures = "0.3.21"
itertools = "0.10.3"
log = "0.4.0"
Expand Down
3 changes: 1 addition & 2 deletions bin/liquidator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ async-trait = "0.1"
bs58 = "0.3.1"
bytemuck = "^1.7.2"
bytes = "1.0"
checked_math = { path = "../../lib/checked_math" }
clap = { version = "3.1.8", features = ["derive", "env"] }
dotenv = "0.15.0"
fixed = { version = "=1.11.0", features = ["serde"] }
fixed = { path = "../../3rdparty/fixed", version = "=1.23.0", features = ["serde"] }
futures = "0.3.17"
futures-core = "0.3"
futures-util = "0.3"
Expand Down
3 changes: 1 addition & 2 deletions bin/settle-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ bincode = "1.3.3"
bs58 = "0.3.1"
bytemuck = "^1.7.2"
bytes = "1.0"
checked_math = { path = "../../lib/checked_math" }
clap = { version = "3.1.8", features = ["derive", "env"] }
dotenv = "0.15.0"
fixed = { version = "=1.11.0", features = ["serde"] }
fixed = { path = "../../3rdparty/fixed", version = "=1.23.0", features = ["serde"] }
futures = "0.3.17"
futures-core = "0.3"
futures-util = "0.3"
Expand Down
3 changes: 1 addition & 2 deletions lib/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ anyhow = "1.0"
async-channel = "1.6"
async-once-cell = { version = "0.4.2", features = ["unpin"] }
async-trait = "0.1.52"
fixed = { version = "=1.11.0", features = ["serde", "borsh"] }
fixed-macro = "^1.1.1"
fixed = { path = "../../3rdparty/fixed", version = "=1.23.0", features = ["serde", "borsh"] }
futures = "0.3.25"
itertools = "0.10.3"
jsonrpc-core = "18.0.0"
Expand Down
4 changes: 1 addition & 3 deletions programs/mango-v4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ arrayref = "0.3.6"
bincode = "1.3.3"
borsh = { version = "0.9.3", features = ["const-generics"] }
bytemuck = { version = "^1.7.2", features = ["min_const_generics"] }
checked_math = { path = "../../lib/checked_math" }
default-env = "0.1.1"
derivative = "2.2.0"
fixed = { version = "=1.11.0", features = ["serde", "borsh"] } # todo: higher versions don't work
fixed-macro = "^1.1.1"
fixed = { path = "../../3rdparty/fixed", version = "=1.23.0", features = ["serde", "borsh", "disable-cache", "debug-assert-in-release"] }
num_enum = "0.5.1"
pyth-sdk-solana = "0.1.0"
serde = "^1.0"
Expand Down
3 changes: 1 addition & 2 deletions programs/mango-v4/src/accounts_ix/token_register.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, Token, TokenAccount};
use fixed::types::I80F48;
use fixed_macro::types::I80F48;

use crate::error::*;
use crate::state::*;

pub const INDEX_START: I80F48 = I80F48!(1_000_000);
pub const INDEX_START: I80F48 = I80F48::lit("1_000_000");

const FIRST_BANK_NUM: u32 = 0;

Expand Down
17 changes: 8 additions & 9 deletions programs/mango-v4/src/health/account_retriever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::accounts_zerocopy::*;
use crate::error::*;
use crate::serum3_cpi;
use crate::state::{Bank, MangoAccountRef, PerpMarket, PerpMarketIndex, TokenIndex};
use crate::util::checked_math as cm;

/// This trait abstracts how to find accounts needed for the health computation.
///
Expand Down Expand Up @@ -64,17 +63,17 @@ pub fn new_fixed_order_account_retriever<'a, 'info>(
let active_token_len = account.active_token_positions().count();
let active_serum3_len = account.active_serum3_orders().count();
let active_perp_len = account.active_perp_positions().count();
let expected_ais = cm!(active_token_len * 2 // banks + oracles
let expected_ais = active_token_len * 2 // banks + oracles
+ active_perp_len * 2 // PerpMarkets + Oracles
+ active_serum3_len); // open_orders
+ active_serum3_len; // open_orders
require_eq!(ais.len(), expected_ais);

Ok(FixedOrderAccountRetriever {
ais: AccountInfoRef::borrow_slice(ais)?,
n_banks: active_token_len,
n_perps: active_perp_len,
begin_perp: cm!(active_token_len * 2),
begin_serum3: cm!(active_token_len * 2 + active_perp_len * 2),
begin_perp: active_token_len * 2,
begin_serum3: active_token_len * 2 + active_perp_len * 2,
staleness_slot: Some(Clock::get()?.slot),
})
}
Expand Down Expand Up @@ -130,7 +129,7 @@ impl<T: KeyedAccountReader> AccountRetriever for FixedOrderAccountRetriever<T> {
)
})?;

let oracle_index = cm!(self.n_banks + active_token_position_index);
let oracle_index = self.n_banks + active_token_position_index;
let oracle_price = self.oracle_price_bank(oracle_index, bank).with_context(|| {
format!(
"getting oracle for bank with health account index {} and token index {}, passed account {}",
Expand All @@ -149,7 +148,7 @@ impl<T: KeyedAccountReader> AccountRetriever for FixedOrderAccountRetriever<T> {
active_perp_position_index: usize,
perp_market_index: PerpMarketIndex,
) -> Result<(&PerpMarket, I80F48)> {
let perp_index = cm!(self.begin_perp + active_perp_position_index);
let perp_index = self.begin_perp + active_perp_position_index;
let perp_market = self
.perp_market(group, perp_index, perp_market_index)
.with_context(|| {
Expand All @@ -161,7 +160,7 @@ impl<T: KeyedAccountReader> AccountRetriever for FixedOrderAccountRetriever<T> {
)
})?;

let oracle_index = cm!(perp_index + self.n_perps);
let oracle_index = perp_index + self.n_perps;
let oracle_price = self.oracle_price_perp(oracle_index, perp_market).with_context(|| {
format!(
"getting oracle for perp market with health account index {} and perp market index {}, passed account {}",
Expand All @@ -174,7 +173,7 @@ impl<T: KeyedAccountReader> AccountRetriever for FixedOrderAccountRetriever<T> {
}

fn serum_oo(&self, active_serum_oo_index: usize, key: &Pubkey) -> Result<&OpenOrders> {
let serum_oo_index = cm!(self.begin_serum3 + active_serum_oo_index);
let serum_oo_index = self.begin_serum3 + active_serum_oo_index;
let ai = &self.ais[serum_oo_index];
(|| {
require_keys_eq!(*key, *ai.key());
Expand Down
Loading

0 comments on commit 5c7a2e3

Please sign in to comment.