Skip to content

Commit

Permalink
Merge pull request #26 from stakelens/evalir/add-linting
Browse files Browse the repository at this point in the history
feat: add `clippy`/`rustfmt`
  • Loading branch information
Evalir authored Jul 12, 2024
2 parents 5365e56 + d97da72 commit 645af4e
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 135 deletions.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
too-large-for-stack = 128
10 changes: 5 additions & 5 deletions ghost-crab-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
}

Expand Down Expand Up @@ -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");
}

Expand All @@ -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;
Expand Down
27 changes: 4 additions & 23 deletions ghost-crab/src/block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -66,30 +59,18 @@ 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 => {
let templates = templates.clone();
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;
}
}
27 changes: 12 additions & 15 deletions ghost-crab/src/cache/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@ 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<Http<Client>> {
let rpc_url = self.config.networks.get(&network).unwrap();
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);
Expand All @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ghost-crab/src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod rpc_proxy;
pub mod manager;
pub mod rpc_proxy;
58 changes: 13 additions & 45 deletions ghost-crab/src/cache/rpc_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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)));
}
Expand All @@ -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)))
Expand Down
4 changes: 2 additions & 2 deletions ghost-crab/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static CONFIG_CACHE: Lazy<Config> = 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();
}
});
Expand All @@ -58,5 +58,5 @@ static CONFIG_CACHE: Lazy<Config> = Lazy::new(|| {
});

pub fn load() -> Config {
return CONFIG_CACHE.clone();
CONFIG_CACHE.clone()
}
4 changes: 2 additions & 2 deletions ghost-crab/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Context {
pub log: Log,
pub provider: RootProvider<Http<Client>>,
pub templates: TemplateManager,
pub contract_address: Address
pub contract_address: Address,
}

pub type HandleInstance = Arc<Box<(dyn Handler + Send + Sync)>>;
Expand All @@ -32,5 +32,5 @@ pub struct HandlerConfig {
pub handler: HandleInstance,
pub provider: RootProvider<Http<Client>>,
pub templates: TemplateManager,
pub execution_mode: ExecutionMode
pub execution_mode: ExecutionMode,
}
21 changes: 10 additions & 11 deletions ghost-crab/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
14 changes: 3 additions & 11 deletions ghost-crab/src/latest_block_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ pub struct LatestBlockManager {

impl LatestBlockManager {
pub fn new(cache_duration_ms: u128, provider: RootProvider<Http<Client>>) -> 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;
Expand All @@ -34,6 +26,6 @@ impl LatestBlockManager {

self.last_fetch_ms = now_ms;

return result;
result
}
}
2 changes: 1 addition & 1 deletion ghost-crab/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading

0 comments on commit 645af4e

Please sign in to comment.