Skip to content

Commit

Permalink
style: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-melnychuk committed Oct 23, 2024
1 parent ce55a10 commit ad10967
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
48 changes: 38 additions & 10 deletions src/exe/cache.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use std::{collections::HashMap, sync::{LazyLock, RwLock}};
use ethers::types::U256;
use starknet_api::{core::ContractAddress, state::StorageKey};
use starknet_types_core::felt::Felt as StarkFelt;
use ethers::types::U256;
use std::{
collections::HashMap,
sync::{LazyLock, RwLock},
};

use crate::gen;

type Key = (U256, U256, U256); // block hash + contract address + storage key
type Value = U256;

static CACHE: LazyLock<RwLock<HashMap<Key, Value>>> = LazyLock::new(|| {
RwLock::new(HashMap::new())
});
static CACHE: LazyLock<RwLock<HashMap<Key, Value>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));

fn get(key: &Key) -> Option<Value> {
let guard = CACHE.read().expect("cache-rlock");
Expand All @@ -22,7 +24,11 @@ fn set(key: Key, value: Value) -> Option<Value> {
guard.insert(key, value)
}

fn key(block_hash: &gen::Felt, contract_address: &ContractAddress, storage_key: &StorageKey) -> Key {
fn key(
block_hash: &gen::Felt,
contract_address: &ContractAddress,
storage_key: &StorageKey,
) -> Key {
(
block_hash.as_ref().parse().unwrap(),
contract_address.0.key().to_bytes_be().into(),
Expand All @@ -31,19 +37,41 @@ fn key(block_hash: &gen::Felt, contract_address: &ContractAddress, storage_key:
}

pub trait StorageCache {
fn lookup(&self, block_hash: &gen::Felt, contract_address: &ContractAddress, storage_key: &StorageKey) -> Option<StarkFelt>;
fn insert(&self, block_hash: &gen::Felt, contract_address: &ContractAddress, storage_key: &StorageKey, val: &gen::Felt);
fn lookup(
&self,
block_hash: &gen::Felt,
contract_address: &ContractAddress,
storage_key: &StorageKey,
) -> Option<StarkFelt>;
fn insert(
&self,
block_hash: &gen::Felt,
contract_address: &ContractAddress,
storage_key: &StorageKey,
val: &gen::Felt,
);
}

pub struct NaiveUnboundedCache;

impl StorageCache for NaiveUnboundedCache {
fn lookup(&self, block_hash: &gen::Felt, contract_address: &ContractAddress, storage_key: &StorageKey) -> Option<StarkFelt> {
fn lookup(
&self,
block_hash: &gen::Felt,
contract_address: &ContractAddress,
storage_key: &StorageKey,
) -> Option<StarkFelt> {
get(&key(block_hash, contract_address, storage_key))
.map(|value| StarkFelt::from_raw(value.0))
}

fn insert(&self, block_hash: &gen::Felt, contract_address: &ContractAddress, storage_key: &StorageKey, val: &gen::Felt) {
fn insert(
&self,
block_hash: &gen::Felt,
contract_address: &ContractAddress,
storage_key: &StorageKey,
val: &gen::Felt,
) {
let val = val.as_ref().parse().unwrap();
set(key(block_hash, contract_address, storage_key), val);
}
Expand Down
51 changes: 32 additions & 19 deletions src/exe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ use crate::{
gen::{self, blocking::Rpc},
};

pub mod cache;
pub mod err;
pub mod map;
pub mod cache;

use err::Error;

Expand Down Expand Up @@ -143,21 +143,28 @@ pub fn call<T: gen::client::blocking::HttpClient>(
Ok(call_info)
}

struct StateProxy<T: gen::client::blocking::HttpClient, C: cache::StorageCache> {
struct StateProxy<T: gen::client::blocking::HttpClient, C: cache::StorageCache>
{
client: gen::client::blocking::Client<T>,
state: State,
cache: C,
}

impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache> StateReader for StateProxy<T, C> {
impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache> StateReader
for StateProxy<T, C>
{
fn get_storage_at(
&self,
contract_address: ContractAddress,
storage_key: StarknetStorageKey,
) -> StateResult<StarkFelt> {
tracing::info!(?contract_address, ?storage_key, "get_storage_at");

if let Some(ret) = self.cache.lookup(&self.state.block_hash, &contract_address, &storage_key) {
if let Some(ret) = self.cache.lookup(
&self.state.block_hash,
&contract_address,
&storage_key,
) {
return Ok(ret);
}

Expand All @@ -173,16 +180,17 @@ impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache> StateReader f

let ret = self
.client
.getStorageAt(
address.clone(),
key.clone(),
block_id.clone(),
)
.getStorageAt(address.clone(), key.clone(), block_id.clone())
.map_err(Into::<Error>::into)?;
tracing::info!(?address, ?key, value=?ret, "get_storage_at");

if ret.as_ref() == "0x0" {
self.cache.insert(&self.state.block_hash, &contract_address, &storage_key, &gen::Felt::try_new("0x0").unwrap());
self.cache.insert(
&self.state.block_hash,
&contract_address,
&storage_key,
&gen::Felt::try_new("0x0").unwrap(),
);
tracing::info!("get_storage_at: skipping proof for zero value");
return Ok(ret.try_into()?);
}
Expand All @@ -195,16 +203,19 @@ impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache> StateReader f

let global_root = self.state.root.clone();
let value = ret.clone();
proof.verify(global_root, address, key, value).map_err(
|e| {
StateError::StateReadError(format!(
"Failed to verify merkle proof: {e:?}"
))
},
)?;
proof.verify(global_root, address, key, value).map_err(|e| {
StateError::StateReadError(format!(
"Failed to verify merkle proof: {e:?}"
))
})?;
tracing::info!("get_storage_at: proof verified");

self.cache.insert(&self.state.block_hash, &contract_address, &storage_key, &ret);
self.cache.insert(
&self.state.block_hash,
&contract_address,
&storage_key,
&ret,
);
Ok(ret.try_into()?)
}

Expand Down Expand Up @@ -273,7 +284,9 @@ impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache> StateReader f
}
}

impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache> BlockifierState for StateProxy<T, C> {
impl<T: gen::client::blocking::HttpClient, C: cache::StorageCache>
BlockifierState for StateProxy<T, C>
{
fn set_storage_at(
&mut self,
contract_address: ContractAddress,
Expand Down

0 comments on commit ad10967

Please sign in to comment.