Skip to content

Commit 870edf9

Browse files
committed
chore: disk to mem kv store
1 parent 1bb1e46 commit 870edf9

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: bin/host/Cargo.toml

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ alloy-eips.workspace = true
2222
alloy-consensus.workspace = true
2323
alloy-rlp.workspace = true
2424
alloy-provider.workspace = true
25-
alloy-transport-http.workspace = true
26-
alloy-rpc-client.workspace = true
25+
alloy-transport-http.workspace = true
26+
alloy-rpc-client.workspace = true
2727
alloy-rpc-types = { workspace = true, features = ["eth"] }
2828
alloy-primitives = { workspace = true, features = ["serde"] }
29-
revm = { workspace = true, features = ["std", "c-kzg", "secp256k1", "portable", "blst"] }
29+
revm = { workspace = true, features = [
30+
"std",
31+
"c-kzg",
32+
"secp256k1",
33+
"portable",
34+
"blst",
35+
] }
3036

3137
# General
3238
anyhow.workspace = true
@@ -42,3 +48,6 @@ tracing-subscriber.workspace = true
4248
command-fds.workspace = true
4349
os_pipe.workspace = true
4450
rocksdb.workspace = true
51+
52+
[dev-dependencies]
53+
proptest.workspace = true

Diff for: bin/host/src/kv/disk.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Contains a concrete implementation of the [KeyValueStore] trait that stores data on disk
22
//! using [rocksdb].
33
4-
use super::KeyValueStore;
4+
use super::{KeyValueStore, MemoryKeyValueStore};
5+
use alloy_primitives::B256;
56
use anyhow::{anyhow, Result};
67
use rocksdb::{Options, DB};
78
use std::path::PathBuf;
@@ -46,3 +47,51 @@ impl Drop for DiskKeyValueStore {
4647
let _ = DB::destroy(&Self::get_db_options(), self.data_directory.as_path());
4748
}
4849
}
50+
51+
impl TryFrom<DiskKeyValueStore> for MemoryKeyValueStore {
52+
type Error = anyhow::Error;
53+
54+
fn try_from(disk_store: DiskKeyValueStore) -> Result<MemoryKeyValueStore> {
55+
let mut memory_store = MemoryKeyValueStore::new();
56+
let mut db_iter = disk_store.db.full_iterator(rocksdb::IteratorMode::Start);
57+
58+
while let Some(Ok((key, value))) = db_iter.next() {
59+
memory_store.set(
60+
B256::try_from(key.as_ref())
61+
.map_err(|e| anyhow!("Failed to convert slice to B256: {e}"))?,
62+
value.to_vec(),
63+
)?;
64+
}
65+
66+
Ok(memory_store)
67+
}
68+
}
69+
70+
#[cfg(test)]
71+
mod test {
72+
use super::DiskKeyValueStore;
73+
use crate::kv::{KeyValueStore, MemoryKeyValueStore};
74+
use proptest::{
75+
arbitrary::any,
76+
collection::{hash_map, vec},
77+
proptest,
78+
};
79+
use std::env::temp_dir;
80+
81+
proptest! {
82+
/// Test that converting from a [DiskKeyValueStore] to a [MemoryKeyValueStore] is lossless.
83+
#[test]
84+
fn convert_disk_kv_to_mem_kv(k_v in hash_map(any::<[u8; 32]>(), vec(any::<u8>(), 0..128), 1..128)) {
85+
let tempdir = temp_dir();
86+
let mut disk_kv = DiskKeyValueStore::new(tempdir);
87+
k_v.iter().for_each(|(k, v)| {
88+
disk_kv.set(k.into(), v.to_vec()).unwrap();
89+
});
90+
91+
let mem_kv = MemoryKeyValueStore::try_from(disk_kv).unwrap();
92+
for (k, v) in k_v {
93+
assert_eq!(mem_kv.get(k.into()).unwrap(), v.to_vec());
94+
}
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)