Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ usc = { version = "2.6.0-rc.0", package = "universal-sierra-compiler" }
num-bigint = { version = "0.4" }
bigdecimal = { version = "0.4.5" }
enum-helper-macros = "0.0.1"
lru = "0.16.2"
once_cell = "1.21.3"

# Starknet dependencies
starknet-types-core = "=0.2.3"
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet-devnet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ tokio = { workspace = true }
openssl = { workspace = true }

cargo-platform = { workspace = true }
lru = { workspace = true }
once_cell = { workspace = true }

[dev-dependencies]
hex = { workspace = true }
Expand Down
20 changes: 20 additions & 0 deletions crates/starknet-devnet-core/src/starknet/defaulter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::num::NonZero;
use std::sync::Mutex;

use blockifier::execution::contract_class::RunnableCompiledClass;
use blockifier::state::errors::StateError;
use blockifier::state::state_api::StateResult;
use lru::LruCache;
use once_cell::sync::Lazy;
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::state::StorageKey;
use starknet_rs_core::types::Felt;
Expand All @@ -10,6 +15,10 @@ use tracing::debug;

use super::starknet_config::ForkConfig;

#[allow(clippy::unwrap_used)] // safe: 128 is non-zero
static CACHE: Lazy<Mutex<LruCache<String, serde_json::Value>>> =
Lazy::new(|| Mutex::new(LruCache::new(NonZero::new(128).unwrap())));

#[derive(thiserror::Error, Debug)]
enum OriginError {
#[error("Error in communication with forking origin: {0}")]
Expand Down Expand Up @@ -121,6 +130,14 @@ impl BlockingOriginReader {
"id": 0,
});

// Forking is done at specified block, so responses should not change
if let Ok(mut cache) = CACHE.lock() {
if let Some(cached) = cache.get(&body.to_string()) {
debug!("Forking origin cache hit for {body:?}");
return Ok(cached.clone());
}
}

match self.blocking_post(body.clone()) {
Ok(resp_json_value) => {
let result = &resp_json_value["result"];
Expand All @@ -131,6 +148,9 @@ impl BlockingOriginReader {
Err(OriginError::NoResult)
} else {
debug!("Forking origin received {body:?} and successfully returned: {result}");
if let Ok(mut cache) = CACHE.lock() {
cache.put(body.to_string(), result.clone());
}
Ok(result.clone())
}
}
Expand Down
Loading