From 310902049d838454d624fdb0d060c9f459f94533 Mon Sep 17 00:00:00 2001 From: Luis Herasme Date: Sun, 21 Jul 2024 17:21:16 -0400 Subject: [PATCH] chore: Add typed errors for cache loading --- ghost-crab/src/cache/manager.rs | 29 +++++++++++++---------------- ghost-crab/src/cache/rpc_proxy.rs | 30 ++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/ghost-crab/src/cache/manager.rs b/ghost-crab/src/cache/manager.rs index b529400..cc07772 100644 --- a/ghost-crab/src/cache/manager.rs +++ b/ghost-crab/src/cache/manager.rs @@ -19,25 +19,22 @@ impl RPCManager { network: String, rpc_url: String, ) -> RootProvider> { - let provider = self.rpcs.get(&rpc_url); + if let Some(provider) = self.rpcs.get(&rpc_url) { + return provider.clone(); + } - match provider { - 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).unwrap(); + self.rpcs.insert(rpc_url.clone(), provider.clone()); + let rpc_with_cache = + RpcWithCache::new(&network, rpc_url.clone(), self.current_port).unwrap(); - tokio::spawn(async move { - rpc_with_cache.run().await.unwrap(); - }); + tokio::spawn(async move { + rpc_with_cache.run().await.unwrap(); + }); - self.current_port += 1; - provider - } - } + self.current_port += 1; + provider } } diff --git a/ghost-crab/src/cache/rpc_proxy.rs b/ghost-crab/src/cache/rpc_proxy.rs index a86371d..3af23ac 100644 --- a/ghost-crab/src/cache/rpc_proxy.rs +++ b/ghost-crab/src/cache/rpc_proxy.rs @@ -21,18 +21,28 @@ pub struct RpcWithCache { port: u16, } +#[derive(Debug)] +pub enum Error { + DB(rocksdb::Error), + CacheFileNotFound(std::io::Error), +} + +type Result = core::result::Result; + +fn load_cache(network: &str) -> Result { + let current_dir = std::env::current_dir().map_err(|e| Error::CacheFileNotFound(e))?; + let cache_path = current_dir.join("cache").join(network); + let db = DB::open_default(cache_path).map_err(|e| Error::DB(e))?; + + Ok(db) +} + impl RpcWithCache { - pub fn new( - network: String, - rpc_url: String, - port: u16, - ) -> Result> { - let current_dir = std::env::current_dir()?; - let cache = Arc::new(DB::open_default(current_dir.join("cache").join(network))?); - Ok(Self { rpc_url: Arc::new(rpc_url), cache, port }) + pub fn new(network: &str, rpc_url: String, port: u16) -> Result { + Ok(Self { rpc_url: Arc::new(rpc_url), cache: Arc::new(load_cache(network)?), port }) } - pub async fn run(&self) -> Result<(), Box> { + pub async fn run(&self) -> core::result::Result<(), Box> { let addr: SocketAddr = ([127, 0, 0, 1], self.port).into(); let listener = TcpListener::bind(addr).await?; let https = HttpsConnector::new(); @@ -96,7 +106,7 @@ async fn handler( rpc_url: Arc, db: Arc, client: Client, Full>, -) -> Result>, hyper::Error> { +) -> core::result::Result>, hyper::Error> { let request_received = request.collect().await?.to_bytes(); if contains_invalid_word(&request_received) {