Skip to content

Commit

Permalink
Merge pull request blockworks-foundation#24 from blockworks-foundatio…
Browse files Browse the repository at this point in the history
…n/ci

ci and clippy fixes
  • Loading branch information
godmodegalactus authored Sep 18, 2023
2 parents 660a5f5 + 74b80cf commit 44ef92f
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 116 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Rust Build and Clippy Check

on:
pull_request:
branches:
- main
push:
branches:
- main

env:
SCCACHE_GHA_ENABLED: true
RUSTC_WRAPPER: sccache
SCCACHE_CACHE_SIZE: "1G"

jobs:
build_all:
name: Rust project
runs-on: ubuntu-22.04
steps:
- name: Install Linux Packages
run: |
sudo apt-get update -y
sudo apt-get install libssl-dev openssl -y
- uses: actions/checkout@v4

# The toolchain action should definitely be run before the cache action
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
# use toolchain version from rust-toolchain.toml
components: rustfmt, clippy
cache: true
# avoid the default "-D warnings" which thrashes cache
rustflags: ""


- name: Run sccache-cache
uses: mozilla-actions/[email protected]


# https://github.com/actions/cache/blob/main/examples.md#rust---cargo
# https://blog.arriven.wtf/posts/rust-ci-cache/
- uses: Swatinem/rust-cache@v2
with:
# will be covered by sscache
cache-targets: false
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}


- name: Early Build
run: |
cargo build --locked --workspace --tests
- name: Run fmt+clippy
run: |
cargo fmt --all --check
cargo clippy --locked --workspace --all-targets
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Default for Config {
}

/// Defines and builds the CLI args for a run of the benchmark
pub fn build_args<'a, 'b>(version: &'b str) -> App<'a, 'b> {
pub fn build_args(version: &str) -> App<'_, '_> {
App::new(crate_name!())
.about(crate_description!())
.version(version)
Expand Down Expand Up @@ -302,7 +302,7 @@ pub fn extract_args(matches: &ArgMatches) -> Config {
&config.keypair_path,
);

args.keeper_authority = read_keypair_file(kp_auth_path.clone()).ok();
args.keeper_authority = read_keypair_file(kp_auth_path).ok();

args.number_of_markers_per_mm = match matches.value_of("markets-per-mm") {
Some(x) => x
Expand Down
30 changes: 13 additions & 17 deletions src/confirmation_strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,15 @@ pub async fn process_blocks(
};
for signature in &transaction.signatures {
let transaction_record_op = {
let rec = transaction_map.get(&signature);
match rec {
Some(x) => Some(x.clone()),
None => None,
}
let rec = transaction_map.get(signature);
rec.map(|x| x.clone())
};
// add CU in counter
if let Some(meta) = &meta {
match meta.compute_units_consumed {
solana_transaction_status::option_serializer::OptionSerializer::Some(x) => {
cu_consumed = cu_consumed.saturating_add(x);
}
_ => {}
if let solana_transaction_status::option_serializer::OptionSerializer::Some(x) =
meta.compute_units_consumed
{
cu_consumed = cu_consumed.saturating_add(x);
}
}
if let Some(transaction_record) = transaction_record_op {
Expand Down Expand Up @@ -110,7 +106,7 @@ pub async fn process_blocks(
}
}

transaction_map.remove(&signature);
transaction_map.remove(signature);
}
}
// push block data
Expand Down Expand Up @@ -210,7 +206,7 @@ pub fn confirmation_by_lite_rpc_notification_stream(
if tx_notification.commitment != CommitmentLevel::Finalized {
continue;
}

if let Some(value) = transaction_map.get(&tx_notification.signature) {
let (tx_sent_record, _) = value.clone();
let error = match &tx_notification.transaction_status {
Expand Down Expand Up @@ -258,9 +254,9 @@ pub fn confirmation_by_lite_rpc_notification_stream(
};

let cleaner_jh = {
let transaction_map = transaction_map.clone();
let exit_signal = exit_signal.clone();
let tx_confirm_records = tx_confirm_records.clone();
let transaction_map = transaction_map;
let exit_signal = exit_signal;
let tx_confirm_records = tx_confirm_records;
tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(60)).await;
Expand Down Expand Up @@ -391,7 +387,7 @@ pub fn confirmations_by_blocks(
timed_out: true,
priority_fees: sent_record.priority_fees,
});
to_remove.push(signature.clone());
to_remove.push(*signature);
}
}

Expand All @@ -409,7 +405,7 @@ pub fn confirmations_by_blocks(
};

let block_confirmation_jh = {
let exit_signal = exit_signal.clone();
let exit_signal = exit_signal;
tokio::spawn(async move {
let mut start_block = from_slot;
let mut start_instant = tokio::time::Instant::now();
Expand Down
1 change: 1 addition & 0 deletions src/crank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct KeeperConfig {
pub websocket_url: String,
}

#[allow(clippy::too_many_arguments)]
pub fn start(
config: KeeperConfig,
exit_signal: Arc<AtomicBool>,
Expand Down
10 changes: 4 additions & 6 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn to_sdk_instruction(

pub async fn load_from_rpc<T: Loadable>(rpc_client: &RpcClient, pk: &Pubkey) -> T {
let acc = rpc_client.get_account(&to_sdk_pk(pk)).await.unwrap();
return T::load_from_bytes(acc.data.as_slice()).unwrap().clone();
*T::load_from_bytes(acc.data.as_slice()).unwrap()
}

pub async fn get_latest_blockhash(rpc_client: &RpcClient) -> Hash {
Expand Down Expand Up @@ -116,10 +116,8 @@ pub async fn poll_blockhash_and_slot(
*blockhash.write().await = new_blockhash;
}
blockhash_last_updated = Instant::now();
} else {
if blockhash_last_updated.elapsed().as_secs() > 120 {
break;
}
} else if blockhash_last_updated.elapsed().as_secs() > 120 {
break;
}

tokio::time::sleep(Duration::from_millis(100)).await;
Expand Down Expand Up @@ -198,7 +196,7 @@ pub async fn get_mango_market_perps_cache(
order_base_lots,
price,
price_quote_lots,
mango_program_pk: mango_program_pk.clone(),
mango_program_pk: *mango_program_pk,
mango_group_pk,
mango_cache_pk,
perp_market_pk,
Expand Down
3 changes: 2 additions & 1 deletion src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn prepare_transaction(
priority_fees: prioritization_fee,
keeper_instruction: Some(keeper_instruction),
};
return (tx, tx_send_record);
(tx, tx_send_record)
}

pub fn create_update_and_cache_quote_banks(
Expand Down Expand Up @@ -161,6 +161,7 @@ pub fn create_update_and_cache_quote_banks(
vec![to_sdk_instruction(ix_update), to_sdk_instruction(ix_cache)]
}

#[allow(clippy::too_many_arguments)]
pub fn start_keepers(
exit_signal: Arc<AtomicBool>,
tpu_manager: TpuManager,
Expand Down
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use {
stats::MangoSimulationStats,
tpu_manager::TpuManager,
},
serde_json,
solana_client::nonblocking::rpc_client::RpcClient as NbRpcClient,
solana_lite_rpc_core::{
block_store::BlockStore,
Expand Down Expand Up @@ -105,7 +104,8 @@ pub async fn main() -> anyhow::Result<()> {
solana_logger::setup_with_default("info");
solana_metrics::set_panic_hook("bench-mango", /*version:*/ None);

let matches = cli::build_args(solana_version::version!()).get_matches();
let version = solana_version::version!();
let matches = cli::build_args(version).get_matches();
let cli_config = cli::extract_args(&matches);

let cli::Config {
Expand Down Expand Up @@ -206,9 +206,7 @@ pub async fn main() -> anyhow::Result<()> {
account_keys_parsed.len(),
number_of_markers_per_mm,
quotes_per_second,
account_keys_parsed.len()
* number_of_markers_per_mm as usize
* quotes_per_second.clone() as usize,
account_keys_parsed.len() * number_of_markers_per_mm as usize * *quotes_per_second as usize,
duration
);

Expand All @@ -221,7 +219,6 @@ pub async fn main() -> anyhow::Result<()> {
let quote_root_bank =
Pubkey::from_str(mango_group_config.tokens.last().unwrap().root_key.as_str())
.expect("Quote root bank should be able to convert into pubkey");
();
let quote_node_banks = mango_group_config
.tokens
.last()
Expand Down Expand Up @@ -287,7 +284,7 @@ pub async fn main() -> anyhow::Result<()> {
blockhash.clone(),
current_slot.clone(),
tpu_manager.clone(),
&duration,
duration,
*quotes_per_second,
*priority_fees_proba,
number_of_markers_per_mm,
Expand Down
16 changes: 6 additions & 10 deletions src/mango_v3_perp_crank_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl MangoV3PerpCrankSink {
Self {
mkt_pks_by_evq_pks: pks
.iter()
.map(|(mkt_pk, evq_pk)| (evq_pk.clone(), mkt_pk.clone()))
.map(|(mkt_pk, evq_pk)| (*evq_pk, *mkt_pk))
.collect(),
group_pk,
cache_pk,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl AccountWriteSink for MangoV3PerpCrankSink {
const HEADER_SIZE: usize = size_of::<EventQueueHeader>();
let header_data = array_ref![account.data(), 0, HEADER_SIZE];
let header = RefCell::<EventQueueHeader>::new(*bytemuck::from_bytes(header_data));
let seq_num = header.clone().into_inner().seq_num.clone();
let seq_num = header.clone().into_inner().seq_num;
// trace!("evq {} seq_num {}", mkt.name, header.seq_num);

const QUEUE_SIZE: usize = EVENT_SIZE * QUEUE_LEN;
Expand All @@ -87,8 +87,7 @@ impl AccountWriteSink for MangoV3PerpCrankSink {
// only crank if at least 1 fill or a sufficient events of other categories are buffered
let contains_fill_events = event_queue
.iter()
.find(|e| e.event_type == EventType::Fill as u8)
.is_some();
.any(|e| e.event_type == EventType::Fill as u8);
let len = event_queue.iter().count();
let has_backlog = len > MAX_BACKLOG;
debug!("evq {pk:?} seq_num={seq_num} len={len} contains_fill_events={contains_fill_events} has_backlog={has_backlog}");
Expand Down Expand Up @@ -121,7 +120,7 @@ impl AccountWriteSink for MangoV3PerpCrankSink {
let mkt_pk = self
.mkt_pks_by_evq_pks
.get(&pk)
.expect(&format!("{pk:?} is a known public key"));
.unwrap_or_else(|| panic!("{pk:?} is a known public key"));

let ix = to_sdk_instruction(
consume_events(
Expand All @@ -130,16 +129,13 @@ impl AccountWriteSink for MangoV3PerpCrankSink {
&to_sp_pk(&self.cache_pk),
&to_sp_pk(mkt_pk),
&to_sp_pk(&pk),
&mut mango_accounts
.iter()
.map(|pk| pk.clone())
.collect::<Vec<_>>(),
&mut mango_accounts.iter().copied().collect::<Vec<_>>(),
MAX_EVENTS_PER_TX,
)
.unwrap(),
);

(Ok(ix), mkt_pk.clone())
(Ok(ix), *mkt_pk)
};

// info!(
Expand Down
22 changes: 11 additions & 11 deletions src/market_markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,19 @@ fn generate_random_fees(
.map(|_| {
if prioritization_fee_proba == 0 {
0
} else if range_probability.sample(&mut rng) <= prioritization_fee_proba {
range.sample(&mut rng)
} else {
if range_probability.sample(&mut rng) <= prioritization_fee_proba {
range.sample(&mut rng) as u64
} else {
0
}
0
}
})
.collect()
}

#[allow(clippy::too_many_arguments)]
pub async fn send_mm_transactions(
quotes_per_second: u64,
perp_market_caches: &Vec<PerpMarketCache>,
perp_market_caches: &[PerpMarketCache],
tpu_manager: TpuManager,
mango_account_pk: Pubkey,
mango_account_signer: &Keypair,
Expand All @@ -179,7 +178,7 @@ pub async fn send_mm_transactions(
let mut tx = create_ask_bid_transaction(
c,
mango_account_pk,
&mango_account_signer,
mango_account_signer,
prioritization_fee,
);

Expand Down Expand Up @@ -207,6 +206,7 @@ pub async fn send_mm_transactions(
}
}

#[allow(clippy::too_many_arguments)]
pub fn start_market_making_threads(
account_keys_parsed: Vec<AccountKeys>,
perp_market_caches: Vec<PerpMarketCache>,
Expand All @@ -226,7 +226,7 @@ pub fn start_market_making_threads(
let exit_signal = exit_signal.clone();
let blockhash = blockhash.clone();
let current_slot = current_slot.clone();
let duration = duration.clone();
let duration = *duration;
let perp_market_caches = perp_market_caches.clone();
let mango_account_pk =
Pubkey::from_str(account_keys.mango_account_pks[0].as_str()).unwrap();
Expand All @@ -241,7 +241,7 @@ pub fn start_market_making_threads(
);
let perp_market_caches = perp_market_caches
.choose_multiple(&mut rng, number_of_markers_per_mm as usize)
.map(|x| x.clone())
.cloned()
.collect_vec();

tokio::spawn(async move {
Expand Down Expand Up @@ -312,7 +312,7 @@ fn create_cancel_all_orders(

pub async fn clean_market_makers(
rpc_client: Arc<RpcClient>,
account_keys_parsed: &Vec<AccountKeys>,
account_keys_parsed: &[AccountKeys],
perp_market_caches: &Vec<PerpMarketCache>,
blockhash: Arc<RwLock<Hash>>,
) {
Expand All @@ -323,11 +323,11 @@ pub async fn clean_market_makers(
for market_maker in account_keys_parsed {
let mango_account_pk =
Pubkey::from_str(market_maker.mango_account_pks[0].as_str()).unwrap();

for perp_market in perp_market_caches {
let market_maker = market_maker.clone();
let perp_market = perp_market.clone();
let rpc_client = rpc_client.clone();
let mango_account_pk = mango_account_pk.clone();
let blockhash = blockhash.clone();

let task = tokio::spawn(async move {
Expand Down
Loading

0 comments on commit 44ef92f

Please sign in to comment.