Skip to content

Commit

Permalink
chore: Add typed errors for cache loading
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed Jul 21, 2024
1 parent 60cb736 commit 3109020
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
29 changes: 13 additions & 16 deletions ghost-crab/src/cache/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ impl RPCManager {
network: String,
rpc_url: String,
) -> RootProvider<Http<Client>> {
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
}
}
30 changes: 20 additions & 10 deletions ghost-crab/src/cache/rpc_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,28 @@ pub struct RpcWithCache {
port: u16,
}

#[derive(Debug)]
pub enum Error {
DB(rocksdb::Error),
CacheFileNotFound(std::io::Error),
}

type Result<T> = core::result::Result<T, Error>;

fn load_cache(network: &str) -> Result<DB> {
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<Self, Box<dyn std::error::Error>> {
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<Self> {
Ok(Self { rpc_url: Arc::new(rpc_url), cache: Arc::new(load_cache(network)?), port })
}

pub async fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
pub async fn run(&self) -> core::result::Result<(), Box<dyn std::error::Error>> {
let addr: SocketAddr = ([127, 0, 0, 1], self.port).into();
let listener = TcpListener::bind(addr).await?;
let https = HttpsConnector::new();
Expand Down Expand Up @@ -96,7 +106,7 @@ async fn handler(
rpc_url: Arc<String>,
db: Arc<DB>,
client: Client<HttpsConnector<HttpConnector>, Full<Bytes>>,
) -> Result<Response<Full<Bytes>>, hyper::Error> {
) -> core::result::Result<Response<Full<Bytes>>, hyper::Error> {
let request_received = request.collect().await?.to_bytes();

if contains_invalid_word(&request_received) {
Expand Down

0 comments on commit 3109020

Please sign in to comment.