Skip to content

Commit

Permalink
Follow naming conventions for Storage/Cachekey
Browse files Browse the repository at this point in the history
  • Loading branch information
neysofu committed Aug 23, 2023
1 parent 84777da commit ab3ce2d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
28 changes: 14 additions & 14 deletions module-system/sov-state/src/internal_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,44 @@ impl StorageInternalCache {
/// Gets a value from the cache or reads it from the provided `ValueReader`.
pub(crate) fn get_or_fetch<S: Storage>(
&mut self,
key: StorageKey,
key: &StorageKey,
value_reader: &S,
witness: &S::Witness,
) -> Option<StorageValue> {
let cache_key = key.clone().as_cache_key();
let cache_value = self.get_value_from_cache(cache_key.clone());
let cache_key = key.to_cache_key();
let cache_value = self.get_value_from_cache(&cache_key);

match cache_value {
cache::ValueExists::Yes(cache_value_exists) => cache_value_exists.map(Into::into),
// If the value does not exist in the cache, then fetch it from an external source.
cache::ValueExists::No => {
let storage_value = value_reader.get(key, witness);
let cache_value = storage_value.as_ref().map(|v| v.clone().as_cache_value());
let cache_value = storage_value.as_ref().map(|v| v.clone().into_cache_value());

self.add_read(cache_key, cache_value);
storage_value
}
}
}

pub fn try_get(&self, key: StorageKey) -> ValueExists {
let cache_key = key.as_cache_key();
self.get_value_from_cache(cache_key)
pub fn try_get(&self, key: &StorageKey) -> ValueExists {
let cache_key = key.to_cache_key();
self.get_value_from_cache(&cache_key)
}

pub(crate) fn set(&mut self, key: StorageKey, value: StorageValue) {
let cache_key = key.as_cache_key();
let cache_value = value.as_cache_value();
pub(crate) fn set(&mut self, key: &StorageKey, value: StorageValue) {
let cache_key = key.to_cache_key();
let cache_value = value.into_cache_value();
self.tx_cache.add_write(cache_key, Some(cache_value));
}

pub(crate) fn delete(&mut self, key: StorageKey) {
let cache_key = key.as_cache_key();
pub(crate) fn delete(&mut self, key: &StorageKey) {
let cache_key = key.to_cache_key();
self.tx_cache.add_write(cache_key, None);
}

fn get_value_from_cache(&self, cache_key: CacheKey) -> cache::ValueExists {
self.tx_cache.get_value(&cache_key)
fn get_value_from_cache(&self, cache_key: &CacheKey) -> cache::ValueExists {
self.tx_cache.get_value(cache_key)
}

pub fn merge_left(
Expand Down
13 changes: 8 additions & 5 deletions module-system/sov-state/src/prover_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<S: MerkleProofSpec> ProverStorage<S> {
})
}

fn read_value(&self, key: StorageKey) -> Option<StorageValue> {
fn read_value(&self, key: &StorageKey) -> Option<StorageValue> {
match self
.db
.get_value_option_by_key(self.db.get_next_version(), key.as_ref())
Expand All @@ -68,7 +68,7 @@ impl<S: MerkleProofSpec> Storage for ProverStorage<S> {
Self::with_path(config.path.as_path())
}

fn get(&self, key: StorageKey, witness: &Self::Witness) -> Option<StorageValue> {
fn get(&self, key: &StorageKey, witness: &Self::Witness) -> Option<StorageValue> {
let val = self.read_value(key);
witness.add_hint(val.clone());
val
Expand Down Expand Up @@ -246,7 +246,7 @@ mod test {
.validate_and_commit(cache, &witness)
.expect("storage is valid");

assert_eq!(test.value, prover_storage.get(test.key, &witness).unwrap());
assert_eq!(test.value, prover_storage.get(&test.key, &witness).unwrap());
assert_eq!(prover_storage.db.get_next_version(), test.version + 1)
}
}
Expand All @@ -257,7 +257,7 @@ mod test {
for test in tests {
assert_eq!(
test.value,
storage.get(test.key, &Default::default()).unwrap()
storage.get(&test.key, &Default::default()).unwrap()
);
}
}
Expand Down Expand Up @@ -290,7 +290,10 @@ mod test {
{
let prover_storage = ProverStorage::<DefaultStorageSpec>::with_path(path).unwrap();
assert!(!prover_storage.is_empty());
assert_eq!(value, prover_storage.get(key, &Default::default()).unwrap());
assert_eq!(
value,
prover_storage.get(&key, &Default::default()).unwrap()
);
}
}
}
20 changes: 10 additions & 10 deletions module-system/sov-state/src/scratchpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<S: Storage> StateCheckpoint<S> {
}
}

pub fn get(&mut self, key: StorageKey) -> Option<StorageValue> {
pub fn get(&mut self, key: &StorageKey) -> Option<StorageValue> {
self.delta.get(key)
}

Expand Down Expand Up @@ -201,20 +201,20 @@ impl<S: Storage> WorkingSet<S> {

impl<S: Storage> RevertableDelta<S> {
fn get(&mut self, key: StorageKey) -> Option<StorageValue> {
let key = key.as_cache_key();
let key = key.to_cache_key();
if let Some(value) = self.writes.get(&key) {
return value.clone().map(Into::into);
}
self.inner.get(key.into())
self.inner.get(&key.into())
}

fn set(&mut self, key: StorageKey, value: StorageValue) {
self.writes
.insert(key.as_cache_key(), Some(value.as_cache_value()));
.insert(key.to_cache_key(), Some(value.into_cache_value()));
}

fn delete(&mut self, key: StorageKey) {
self.writes.insert(key.as_cache_key(), None);
self.writes.insert(key.to_cache_key(), None);
}
}

Expand All @@ -224,9 +224,9 @@ impl<S: Storage> RevertableDelta<S> {

for (k, v) in self.writes.into_iter() {
if let Some(v) = v {
inner.set(k.into(), v.into());
inner.set(&k.into(), v.into());
} else {
inner.delete(k.into());
inner.delete(&k.into());
}
}

Expand Down Expand Up @@ -270,15 +270,15 @@ impl<S: Storage> Debug for Delta<S> {
}

impl<S: Storage> Delta<S> {
fn get(&mut self, key: StorageKey) -> Option<StorageValue> {
fn get(&mut self, key: &StorageKey) -> Option<StorageValue> {
self.cache.get_or_fetch(key, &self.inner, &self.witness)
}

fn set(&mut self, key: StorageKey, value: StorageValue) {
fn set(&mut self, key: &StorageKey, value: StorageValue) {
self.cache.set(key, value)
}

fn delete(&mut self, key: StorageKey) {
fn delete(&mut self, key: &StorageKey) {
self.cache.delete(key)
}
}
Expand Down
14 changes: 10 additions & 4 deletions module-system/sov-state/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ impl StorageKey {
self.key.clone()
}

pub fn as_cache_key(self) -> CacheKey {
pub fn to_cache_key(&self) -> CacheKey {
CacheKey {
key: self.key.clone(),
}
}

pub fn into_cache_key(self) -> CacheKey {
CacheKey { key: self.key }
}
}
Expand Down Expand Up @@ -130,8 +136,8 @@ impl StorageValue {
&self.value
}

/// Convert this value into a `CacheValue`.
pub fn as_cache_value(self) -> CacheValue {
/// Convert this value into a [`CacheValue`].
pub fn into_cache_value(self) -> CacheValue {
CacheValue { value: self.value }
}
}
Expand Down Expand Up @@ -166,7 +172,7 @@ pub trait Storage: Clone {
fn with_config(config: Self::RuntimeConfig) -> Result<Self, anyhow::Error>;

/// Returns the value corresponding to the key or None if key is absent.
fn get(&self, key: StorageKey, witness: &Self::Witness) -> Option<StorageValue>;
fn get(&self, key: &StorageKey, witness: &Self::Witness) -> Option<StorageValue>;

/// Returns the latest state root hash from the storage.
fn get_state_root(&self, witness: &Self::Witness) -> anyhow::Result<[u8; 32]>;
Expand Down
2 changes: 1 addition & 1 deletion module-system/sov-state/src/zk_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<S: MerkleProofSpec> Storage for ZkStorage<S> {
Ok(Self::new(config))
}

fn get(&self, _key: StorageKey, witness: &Self::Witness) -> Option<StorageValue> {
fn get(&self, _key: &StorageKey, witness: &Self::Witness) -> Option<StorageValue> {
witness.get_hint()
}

Expand Down

0 comments on commit ab3ce2d

Please sign in to comment.