From a4adeb3ee0b9591082ce5507b80b60f36e5ad490 Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Wed, 6 Nov 2024 15:16:12 -0500 Subject: [PATCH] chore(katana): remove unused codes (#2647) --- Cargo.lock | 3 - crates/katana/runner/Cargo.toml | 3 - crates/katana/runner/src/lib.rs | 1 - crates/katana/runner/src/logs.rs | 116 ------------------------------- 4 files changed, 123 deletions(-) delete mode 100644 crates/katana/runner/src/logs.rs diff --git a/Cargo.lock b/Cargo.lock index fcfedda3cc..2c5537c005 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8436,11 +8436,8 @@ version = "1.0.0-rc.1" dependencies = [ "anyhow", "assert_fs", - "chrono", "katana-node-bindings", "katana-runner-macro", - "serde", - "serde_json", "starknet 0.12.0", "tokio", "url", diff --git a/crates/katana/runner/Cargo.toml b/crates/katana/runner/Cargo.toml index 9bd5323b50..293ccbed0c 100644 --- a/crates/katana/runner/Cargo.toml +++ b/crates/katana/runner/Cargo.toml @@ -11,9 +11,6 @@ katana-runner-macro = { path = "macro" } anyhow.workspace = true assert_fs.workspace = true -chrono.workspace = true -serde.workspace = true -serde_json.workspace = true starknet.workspace = true tokio.workspace = true url.workspace = true diff --git a/crates/katana/runner/src/lib.rs b/crates/katana/runner/src/lib.rs index c3633b0e5d..816c3b490e 100644 --- a/crates/katana/runner/src/lib.rs +++ b/crates/katana/runner/src/lib.rs @@ -1,6 +1,5 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] -mod logs; mod utils; use std::path::PathBuf; diff --git a/crates/katana/runner/src/logs.rs b/crates/katana/runner/src/logs.rs deleted file mode 100644 index cdaa7a7ebe..0000000000 --- a/crates/katana/runner/src/logs.rs +++ /dev/null @@ -1,116 +0,0 @@ -use std::fs::File; -use std::io::{BufRead, BufReader}; -use std::time::Duration; - -use chrono::prelude::*; -use serde::{Deserialize, Serialize}; -use tokio::time::sleep; - -use crate::KatanaRunner; - -#[derive(Serialize, Deserialize, Debug)] -pub struct TimedLog { - timestamp: String, - level: String, - fields: T, - target: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Message { - message: String, -} - -pub type Log = TimedLog; - -impl KatanaRunner { - pub fn blocks(&self) -> Vec { - BufReader::new(File::open(&self.log_file_path).unwrap()) - .lines() - .map_while(Result::ok) - .filter_map(|line| match serde_json::from_str(&line) { - Ok(log) => Some(log), - Err(_) => None, - }) - .filter_map(|log: Log| match log.fields.message.contains("⛏️ Block") { - true => Some(log), - false => None, - }) - .collect() - } - - pub async fn blocks_until_empty(&self) -> Vec { - let mut blocks = self.blocks(); - loop { - if let Some(block) = blocks.last() { - if block.fields.message.contains("mined with 0 transactions") { - break; - } - } - - let len_at_call = blocks.len(); - while len_at_call == blocks.len() { - sleep(Duration::from_secs(1)).await; - blocks = self.blocks(); - } - } - blocks - } - - pub async fn block_sizes(&self) -> Vec { - self.blocks_until_empty() - .await - .into_iter() - .map(|block| { - let limit = block - .fields - .message - .find(" transactions") - .expect("Failed to find transactions in block"); - let number = block.fields.message[..limit].split(' ').last().unwrap(); - number.parse::().expect("Failed to parse number of transactions") - }) - .collect() - } - - pub async fn block_times(&self) -> Vec { - let mut v = self - .blocks_until_empty() - .await - .into_iter() - .map(|block| block.timestamp.parse().expect("Failed to parse time")) - .collect::>>() - .windows(2) - .map(|w| (w[1] - w[0]).num_milliseconds()) - .collect::>(); - - // First block has no previous one, so always has a time of 0 - v.insert(0, 0); - v - } - - pub async fn steps(&self) -> Vec { - let matching = "Transaction resource usage: Steps: "; - BufReader::new(File::open(&self.log_file_path).unwrap()) - .lines() - .filter_map(|line| { - let line = line.unwrap(); - if let Some(start) = line.find(matching) { - let end = line.find(" | "); - let steps = line[start + matching.len()..end.unwrap()].to_string(); - - Some(steps.parse::().unwrap()) - } else { - None - } - }) - .collect() - } -} - -#[test] -fn test_parse_katana_logs() { - let log = r#"{"timestamp":"2024-01-24T15:59:50.793948Z","level":"INFO","fields":{"message":"⛏️ Block 45 mined with 0 transactions"},"target":"backend"}"#; - let log: Log = serde_json::from_str(log).unwrap(); - assert_eq!(log.fields.message, "⛏️ Block 45 mined with 0 transactions"); -}