Skip to content

Commit

Permalink
chore: Remove network from cache layer
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed Jul 23, 2024
1 parent 1076b56 commit f40033a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions ghost-crab/src/cache/cache_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloy::rpc::json_rpc::{
Id, RequestPacket, Response, ResponsePacket, ResponsePayload, SerializedRequest,
};
use alloy::transports::{RpcError, TransportError, TransportErrorKind};
use core::str;
use rocksdb::DB;
use serde_json::value::RawValue;
use std::{
Expand All @@ -15,42 +14,30 @@ use std::{
use tower::{Layer, Service};

pub struct CacheLayer {
network: String,
db: Arc<DB>,
}

impl CacheLayer {
pub fn new(network: &str, db: DB) -> Self {
Self { network: network.into(), db: Arc::new(db) }
pub fn new(db: DB) -> Self {
Self { db: Arc::new(db) }
}
}

impl<S> Layer<S> for CacheLayer {
type Service = CacheService<S>;

fn layer(&self, inner: S) -> Self::Service {
CacheService { inner, network: self.network.clone(), db: Arc::clone(&self.db) }
CacheService { inner, db: Arc::clone(&self.db) }
}
}

#[derive(Debug, Clone)]
pub struct CacheService<S> {
inner: S,
network: String,
db: Arc<DB>,
}

impl<S> CacheService<S> {
fn cache_get(&self, key: &str) -> Option<Vec<u8>> {
let key = self.network.clone() + key;

if let Ok(result) = self.db.get(key) {
return result;
}

None
}

fn convert_to_response(
&self,
raw_response: Vec<u8>,
Expand Down Expand Up @@ -128,7 +115,7 @@ where
let id_new = "\"id\":0";
let raw_request = raw_request.replace(&id_old, id_new);

if let Some(raw_data) = self.cache_get(&raw_request) {
if let Ok(Some(raw_data)) = self.db.get(&raw_request) {
return self.convert_to_response(raw_data);
}

Expand Down
2 changes: 1 addition & 1 deletion ghost-crab/src/cache/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl RPCManager {
}

let cache = load_cache(&network).unwrap();
let cache_layer = CacheLayer::new(&network, cache);
let cache_layer = CacheLayer::new(cache);

let client = ClientBuilder::default().layer(cache_layer).http(rpc_url.parse().unwrap());
let provider = ProviderBuilder::new().on_client(client);
Expand Down

0 comments on commit f40033a

Please sign in to comment.