From e86fbe0f763f61419b5414fea1badb4dde0ac5a3 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Fri, 12 Jul 2024 16:09:09 +0200 Subject: [PATCH 1/2] add clippy/rustfmt --- clippy.toml | 2 + ghost-crab-macros/src/lib.rs | 10 ++--- ghost-crab/src/block_handler.rs | 27 ++---------- ghost-crab/src/cache/manager.rs | 27 ++++++------ ghost-crab/src/cache/mod.rs | 2 +- ghost-crab/src/cache/rpc_proxy.rs | 58 ++++++-------------------- ghost-crab/src/config.rs | 4 +- ghost-crab/src/handler.rs | 4 +- ghost-crab/src/indexer.rs | 21 +++++----- ghost-crab/src/latest_block_manager.rs | 14 ++----- ghost-crab/src/prelude.rs | 2 +- ghost-crab/src/process_logs.rs | 21 ++-------- ghost-crab/src/server.rs | 4 +- rustfmt.toml | 3 ++ 14 files changed, 64 insertions(+), 135 deletions(-) create mode 100644 clippy.toml create mode 100644 rustfmt.toml diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..7e606c3 --- /dev/null +++ b/clippy.toml @@ -0,0 +1,2 @@ +msrv = "1.76" +too-large-for-stack = 128 diff --git a/ghost-crab-macros/src/lib.rs b/ghost-crab-macros/src/lib.rs index 935f3d8..e88338a 100644 --- a/ghost-crab-macros/src/lib.rs +++ b/ghost-crab-macros/src/lib.rs @@ -41,7 +41,7 @@ struct Config { #[proc_macro_attribute] pub fn handler(metadata: TokenStream, input: TokenStream) -> TokenStream { let metadata_string = metadata.to_string(); - let mut metadata_split = metadata_string.split("."); + let mut metadata_split = metadata_string.split('.'); let name = metadata_split.next(); let event_name = metadata_split.next(); @@ -66,11 +66,11 @@ pub fn handler(metadata: TokenStream, input: TokenStream) -> TokenStream { let event_name = event_name.unwrap(); let event_name = String::from(event_name.trim()); - if name.len() == 0 { + if name.is_empty() { panic!("The source is empty"); } - if event_name.len() == 0 { + if event_name.is_empty() { panic!("The event name is empty"); } @@ -176,7 +176,7 @@ pub fn block_handler(metadata: TokenStream, input: TokenStream) -> TokenStream { let name = metadata.to_string(); let name = name.trim(); - if name.len() == 0 { + if name.is_empty() { panic!("The source is missing"); } @@ -202,7 +202,7 @@ pub fn block_handler(metadata: TokenStream, input: TokenStream) -> TokenStream { let fn_body = parsed.block; let fn_args = parsed.sig.inputs.clone(); - let data_source = Literal::string(&name); + let data_source = Literal::string(name); TokenStream::from(quote! { pub struct #fn_name; diff --git a/ghost-crab/src/block_handler.rs b/ghost-crab/src/block_handler.rs index 76b1eb0..964c0b1 100644 --- a/ghost-crab/src/block_handler.rs +++ b/ghost-crab/src/block_handler.rs @@ -33,14 +33,7 @@ pub struct BlockConfig { } pub async fn process_logs_block( - BlockConfig { - start_block, - handler, - provider, - templates, - step, - execution_mode, - }: BlockConfig, + BlockConfig { start_block, handler, provider, templates, step, execution_mode }: BlockConfig, ) { let mut current_block = start_block; let mut latest_block_manager = LatestBlockManager::new(1000, provider.clone()); @@ -66,13 +59,7 @@ pub async fn process_logs_block( let templates = templates.clone(); tokio::spawn(async move { - handler - .handle(BlockContext { - provider, - templates, - block, - }) - .await; + handler.handle(BlockContext { provider, templates, block }).await; }); } ExecutionMode::Serial => { @@ -80,16 +67,10 @@ pub async fn process_logs_block( let provider = provider.clone(); let templates = templates.clone(); - handler - .handle(BlockContext { - provider, - templates, - block, - }) - .await; + handler.handle(BlockContext { provider, templates, block }).await; } } - current_block = current_block + step; + current_block += step; } } diff --git a/ghost-crab/src/cache/manager.rs b/ghost-crab/src/cache/manager.rs index 0b1c70d..70f4965 100644 --- a/ghost-crab/src/cache/manager.rs +++ b/ghost-crab/src/cache/manager.rs @@ -17,13 +17,15 @@ pub struct RPCManager { config: config::Config, } +impl Default for RPCManager { + fn default() -> Self { + Self::new() + } +} + impl RPCManager { pub fn new() -> Self { - RPCManager { - rpcs: HashMap::new(), - current_port: 3001, - config: config::load(), - } + RPCManager { rpcs: HashMap::new(), current_port: 3001, config: config::load() } } pub async fn get(&mut self, network: String) -> RootProvider> { @@ -31,15 +33,10 @@ impl RPCManager { let provider = self.rpcs.get(rpc_url); match provider { - Some(value) => { - return value.clone(); - } + Some(value) => value.clone(), None => { - let provider = ProviderBuilder::new().on_http( - format!("http://localhost:{}", self.current_port) - .parse() - .unwrap(), - ); + let provider = ProviderBuilder::new() + .on_http(format!("http://localhost:{}", self.current_port).parse().unwrap()); self.rpcs.insert(rpc_url.clone(), provider.clone()); let rpc_with_cache = RpcWithCache::new(network, rpc_url.clone(), self.current_port); @@ -48,8 +45,8 @@ impl RPCManager { rpc_with_cache.run().await; }); - self.current_port = self.current_port + 1; - return provider; + self.current_port += 1; + provider } } } diff --git a/ghost-crab/src/cache/mod.rs b/ghost-crab/src/cache/mod.rs index 4d66375..5ff5693 100644 --- a/ghost-crab/src/cache/mod.rs +++ b/ghost-crab/src/cache/mod.rs @@ -1,2 +1,2 @@ -pub mod rpc_proxy; pub mod manager; +pub mod rpc_proxy; diff --git a/ghost-crab/src/cache/rpc_proxy.rs b/ghost-crab/src/cache/rpc_proxy.rs index 295973a..6d9b716 100644 --- a/ghost-crab/src/cache/rpc_proxy.rs +++ b/ghost-crab/src/cache/rpc_proxy.rs @@ -28,11 +28,7 @@ impl RpcWithCache { let current_dir = std::env::current_dir().unwrap(); let cache = Arc::new(DB::open_default(current_dir.join("cache").join(network)).unwrap()); - Self { - rpc_url: Arc::new(rpc_url), - cache, - port, - } + Self { rpc_url: Arc::new(rpc_url), cache, port } } pub async fn run(&self) { @@ -54,12 +50,7 @@ impl RpcWithCache { .serve_connection( io, service_fn(|request| { - handler( - request, - Arc::clone(&rpc_url), - Arc::clone(&db), - client.clone(), - ) + handler(request, Arc::clone(&rpc_url), Arc::clone(&db), client.clone()) }), ) .await @@ -78,35 +69,25 @@ fn divide_request_by_id(input: &[u8]) -> Option<(&[u8], &[u8], &[u8])> { let value_start = id_field_index + ID_FIELD.len(); let value_end = input[value_start..].iter().position(|&x| x == b',')?; - return Some(( + Some(( &input[..value_start], &input[value_start..value_start + value_end], &input[value_start + value_end..], - )); + )) } -const INVALID_WORDS: &[&[u8]] = &[ - b"eth_blockNumber", - b"earliest", - b"latest", - b"safe", - b"finalized", - b"pending", -]; +const INVALID_WORDS: &[&[u8]] = + &[b"eth_blockNumber", b"earliest", b"latest", b"safe", b"finalized", b"pending"]; #[inline] fn contains_invalid_word(input: &[u8]) -> bool { for search in INVALID_WORDS { - if input - .windows(search.len()) - .position(|x| &x == search) - .is_some() - { + if input.windows(search.len()).any(|x| &x == search) { return true; } } - return false; + false } async fn handler( @@ -125,14 +106,8 @@ async fn handler( .body(Full::new(request_received.clone())) .unwrap(); - let rpc_response = client - .request(rpc_request) - .await - .unwrap() - .collect() - .await - .unwrap() - .to_bytes(); + let rpc_response = + client.request(rpc_request).await.unwrap().collect().await.unwrap().to_bytes(); return Ok(Response::new(Full::new(rpc_response))); } @@ -154,21 +129,14 @@ async fn handler( .body(Full::new(request_received.clone())) .unwrap(); - let rpc_response = client - .request(rpc_request) - .await - .unwrap() - .collect() - .await - .unwrap() - .to_bytes(); + let rpc_response = + client.request(rpc_request).await.unwrap().collect().await.unwrap().to_bytes(); let rpc_response_string = String::from_utf8_lossy(&rpc_response); // Avoid caching errors if !rpc_response_string.contains(r#""error":{"code":-"#) { - db.put(request_hash, rpc_response_string.to_string()) - .unwrap(); + db.put(request_hash, rpc_response_string.to_string()).unwrap(); } Ok(Response::new(Full::new(rpc_response))) diff --git a/ghost-crab/src/config.rs b/ghost-crab/src/config.rs index 03112af..de3bb3f 100644 --- a/ghost-crab/src/config.rs +++ b/ghost-crab/src/config.rs @@ -49,7 +49,7 @@ static CONFIG_CACHE: Lazy = Lazy::new(|| { let mut config: Config = serde_json::from_str(&config_string).unwrap(); config.networks.iter_mut().for_each(|(_key, value)| { - if value.starts_with("$") { + if value.starts_with('$') { *value = env::var(&value[1..]).unwrap(); } }); @@ -58,5 +58,5 @@ static CONFIG_CACHE: Lazy = Lazy::new(|| { }); pub fn load() -> Config { - return CONFIG_CACHE.clone(); + CONFIG_CACHE.clone() } diff --git a/ghost-crab/src/handler.rs b/ghost-crab/src/handler.rs index e735188..4cb88da 100644 --- a/ghost-crab/src/handler.rs +++ b/ghost-crab/src/handler.rs @@ -11,7 +11,7 @@ pub struct Context { pub log: Log, pub provider: RootProvider>, pub templates: TemplateManager, - pub contract_address: Address + pub contract_address: Address, } pub type HandleInstance = Arc>; @@ -32,5 +32,5 @@ pub struct HandlerConfig { pub handler: HandleInstance, pub provider: RootProvider>, pub templates: TemplateManager, - pub execution_mode: ExecutionMode + pub execution_mode: ExecutionMode, } diff --git a/ghost-crab/src/indexer.rs b/ghost-crab/src/indexer.rs index 33739c1..1a90d85 100644 --- a/ghost-crab/src/indexer.rs +++ b/ghost-crab/src/indexer.rs @@ -21,10 +21,7 @@ impl TemplateManager { pub async fn start(&self, template: Template) { let config = config::load(); - let source = config - .templates - .get(&template.handler.get_source()) - .unwrap(); + let source = config.templates.get(&template.handler.get_source()).unwrap(); let provider = RPC_MANAGER.lock().await.get(source.network.clone()).await; let execution_mode = source.execution_mode.unwrap_or(ExecutionMode::Parallel); @@ -52,6 +49,12 @@ pub struct Indexer { templates: TemplateManager, } +impl Default for Indexer { + fn default() -> Self { + Self::new() + } +} + impl Indexer { pub fn new() -> Indexer { let config = config::load(); @@ -62,13 +65,13 @@ impl Indexer { let server = Server::new(3000); server.start(); - return Indexer { + Indexer { config: config.clone(), handlers: Vec::new(), block_handlers: Vec::new(), rx, templates, - }; + } } pub async fn load_event_handler(&mut self, handler: HandleInstance) { @@ -92,11 +95,7 @@ impl Indexer { } pub async fn load_block_handler(&mut self, handler: BlockHandlerInstance) { - let source = self - .config - .block_handlers - .get(&handler.get_source()) - .unwrap(); + let source = self.config.block_handlers.get(&handler.get_source()).unwrap(); let provider = RPC_MANAGER.lock().await.get(source.network.clone()).await; let execution_mode = source.execution_mode.unwrap_or(ExecutionMode::Parallel); diff --git a/ghost-crab/src/latest_block_manager.rs b/ghost-crab/src/latest_block_manager.rs index 994809e..e66b69c 100644 --- a/ghost-crab/src/latest_block_manager.rs +++ b/ghost-crab/src/latest_block_manager.rs @@ -11,19 +11,11 @@ pub struct LatestBlockManager { impl LatestBlockManager { pub fn new(cache_duration_ms: u128, provider: RootProvider>) -> Self { - return Self { - value: 0, - cache_duration_ms, - last_fetch_ms: 0, - provider, - }; + Self { value: 0, cache_duration_ms, last_fetch_ms: 0, provider } } pub async fn get(&mut self) -> u64 { - let now_ms = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_millis(); + let now_ms = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(); if (now_ms - self.last_fetch_ms) < self.cache_duration_ms { return self.value; @@ -34,6 +26,6 @@ impl LatestBlockManager { self.last_fetch_ms = now_ms; - return result; + result } } diff --git a/ghost-crab/src/prelude.rs b/ghost-crab/src/prelude.rs index c4535f8..4f33f90 100644 --- a/ghost-crab/src/prelude.rs +++ b/ghost-crab/src/prelude.rs @@ -17,5 +17,5 @@ pub use crate::config; pub use crate::indexer; pub use crate::indexer::Template; pub use crate::process_logs; -pub use alloy::providers::Provider; pub use alloy::primitives::Address; +pub use alloy::providers::Provider; diff --git a/ghost-crab/src/process_logs.rs b/ghost-crab/src/process_logs.rs index 618e42d..9d2e9a0 100644 --- a/ghost-crab/src/process_logs.rs +++ b/ghost-crab/src/process_logs.rs @@ -44,13 +44,10 @@ pub async fn process_logs( let source = handler.get_source(); - println!( - "[{}] Processing logs from {} to {}", - source, current_block, end_block - ); + println!("[{}] Processing logs from {} to {}", source, current_block, end_block); let filter = Filter::new() - .address(address.clone()) + .address(address) .event(&event_signature) .from_block(current_block) .to_block(end_block); @@ -66,12 +63,7 @@ pub async fn process_logs( tokio::spawn(async move { handler - .handle(Context { - log, - provider, - templates, - contract_address: address - }) + .handle(Context { log, provider, templates, contract_address: address }) .await; }); } @@ -83,12 +75,7 @@ pub async fn process_logs( let templates = templates.clone(); handler - .handle(Context { - log, - provider, - templates, - contract_address: address - }) + .handle(Context { log, provider, templates, contract_address: address }) .await; } } diff --git a/ghost-crab/src/server.rs b/ghost-crab/src/server.rs index 785c031..5bf0d8d 100644 --- a/ghost-crab/src/server.rs +++ b/ghost-crab/src/server.rs @@ -15,7 +15,7 @@ pub struct Server { impl Server { pub fn new(port: u16) -> Self { - return Self { port }; + Self { port } } pub fn start(&self) { @@ -44,5 +44,5 @@ impl Server { } async fn handler() -> Result>, Infallible> { - return Ok(Response::new(Full::new(Bytes::from("Hello world")))); + Ok(Response::new(Full::new(Bytes::from("Hello world")))) } diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..1e491f9 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +reorder_imports = true +use_field_init_shorthand = true +use_small_heuristics = "Max" From d97da7231a028ed31c5592009a72fd507251bc6b Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Fri, 12 Jul 2024 16:14:14 +0200 Subject: [PATCH 2/2] remove msrv --- clippy.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/clippy.toml b/clippy.toml index 7e606c3..fda0d7c 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1,2 +1 @@ -msrv = "1.76" too-large-for-stack = 128