Skip to content

Commit

Permalink
add caching for eth_blockNumber; fix cache update logic; set eth_max_…
Browse files Browse the repository at this point in the history
…priority_fee_per_gas to 10s
  • Loading branch information
dshiell committed Aug 9, 2024
1 parent c00d5ad commit 871f912
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Args {
#[arg(short, long, default_value = "100000")]
pub lru_max_items: usize,

#[arg(short, long, default_value = "12")]
#[arg(long, default_value = "12")]
pub reorg_ttl: u32,

#[arg(short, long = "cache", default_value = "lru", value_parser = cache_backend_parser)]
Expand Down
16 changes: 14 additions & 2 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::Local;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::cmp::min;
use tracing::info;

pub enum CacheStatus {
Cached { key: String, value: CacheValue },
Expand Down Expand Up @@ -60,16 +61,27 @@ impl CacheValue {
// if a previous entry existed then check if the response has changed
// else this is a new entry and nothing to do
if let Some(expired_value) = expired_value {
let is_new = expired_value.data == self.data;
let is_new = expired_value.data != self.data;
self.last_modified = Local::now().timestamp();

// if the value has changed then reset the reorg ttl
// else we can exponentially backoff the reorg_ttl
// but only exponential backoff if we hit the reorg_ttl
// and not the rpc ttl
self.reorg_ttl = if is_new {
reorg_ttl
} else {
self.reorg_ttl * 2
let now = Local::now().timestamp();
let age: u64 = (now - expired_value.last_modified) as u64;
if age > expired_value.reorg_ttl as u64 {
expired_value.reorg_ttl * 2
} else {
reorg_ttl
}
};
info!("self.reorg_ttl: {}", self.reorg_ttl)
} else {
self.reorg_ttl = reorg_ttl
}

self
Expand Down
19 changes: 19 additions & 0 deletions src/rpc_cache_handler/eth_block_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::rpc_cache_handler::RpcCacheHandler;
use serde_json::Value;

#[derive(Default, Clone)]
pub struct Handler {}

impl RpcCacheHandler for Handler {
fn method_name(&self) -> &'static str {
"eth_blockNumber"
}

fn extract_cache_key(&self, _: &Value) -> anyhow::Result<Option<String>> {
Ok(Some(format!("eth_blockNumber")))
}

fn get_ttl(&self) -> u32 {
2
}
}
2 changes: 1 addition & 1 deletion src/rpc_cache_handler/eth_max_priority_fee_per_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ impl RpcCacheHandler for Handler {
}

fn get_ttl(&self) -> u32 {
0
10
}
}
2 changes: 2 additions & 0 deletions src/rpc_cache_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod debug_trace_block_by_hash;
mod debug_trace_block_by_number;
mod debug_trace_call;
mod debug_trace_transaction;
mod eth_block_number;
mod eth_call;
mod eth_chainid;
mod eth_estimate_gas;
Expand Down Expand Up @@ -73,5 +74,6 @@ pub fn factories() -> Vec<RpcCacheHandlerFactory> {
get_factory::<eth_get_transaction_count::Handler>(),
get_factory::<eth_get_transaction_receipt::Handler>(),
get_factory::<eth_max_priority_fee_per_gas::Handler>(),
get_factory::<eth_block_number::Handler>(),
]
}

0 comments on commit 871f912

Please sign in to comment.