Skip to content

Commit c07ad8e

Browse files
committed
add converter
1 parent 1bb1e46 commit c07ad8e

File tree

7 files changed

+51
-10
lines changed

7 files changed

+51
-10
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

+10-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,4 @@ tracing-subscriber.workspace = true
4248
command-fds.workspace = true
4349
os_pipe.workspace = true
4450
rocksdb.workspace = true
51+
rkyv = "0.7.42"

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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};
55
use anyhow::{anyhow, Result};
66
use rocksdb::{Options, DB};
77
use std::path::PathBuf;
@@ -39,8 +39,24 @@ impl KeyValueStore for DiskKeyValueStore {
3939
fn set(&mut self, key: alloy_primitives::B256, value: Vec<u8>) -> Result<()> {
4040
self.db.put(*key, value).map_err(|e| anyhow!("Failed to set key-value pair: {}", e))
4141
}
42+
43+
/// Converts the [DiskKeyValueStore] to a [MemoryKeyValueStore].
44+
fn to_memory_store(&self) -> MemoryKeyValueStore {
45+
let mut memory_store = MemoryKeyValueStore::new();
46+
let iter = self.db.iterator(rocksdb::IteratorMode::Start);
47+
for item in iter {
48+
if let Ok((key, value)) = item {
49+
if let Ok(b256_key) = alloy_primitives::B256::try_from(key.as_ref()) {
50+
let _ = memory_store.set(b256_key, value.to_vec());
51+
}
52+
}
53+
}
54+
memory_store
55+
}
4256
}
4357

58+
impl DiskKeyValueStore {}
59+
4460
impl Drop for DiskKeyValueStore {
4561
fn drop(&mut self) {
4662
let _ = DB::destroy(&Self::get_db_options(), self.data_directory.as_path());

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Contains a concrete implementation of the [KeyValueStore] trait that stores data on disk.
22
3-
use super::KeyValueStore;
3+
use super::{KeyValueStore, MemoryKeyValueStore};
44
use crate::cli::HostCli;
55
use alloy_primitives::B256;
66
use anyhow::Result;
@@ -44,4 +44,8 @@ impl KeyValueStore for LocalKeyValueStore {
4444
fn set(&mut self, _: B256, _: Vec<u8>) -> Result<()> {
4545
unreachable!("LocalKeyValueStore is read-only")
4646
}
47+
48+
fn to_memory_store(&self) -> MemoryKeyValueStore {
49+
unimplemented!("LocalKeyValueStore is read-only")
50+
}
4751
}

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ use std::collections::HashMap;
77

88
/// A simple, synchronous key-value store that stores data in memory. This is useful for testing and
99
/// development purposes.
10-
#[derive(Default, Clone, Debug, Eq, PartialEq)]
10+
#[derive(
11+
Default, Clone, Debug, Eq, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize,
12+
)]
1113
pub struct MemoryKeyValueStore {
12-
store: HashMap<B256, Vec<u8>>,
14+
pub store: HashMap<[u8; 32], Vec<u8>>,
1315
}
1416

1517
impl MemoryKeyValueStore {
@@ -21,11 +23,15 @@ impl MemoryKeyValueStore {
2123

2224
impl KeyValueStore for MemoryKeyValueStore {
2325
fn get(&self, key: B256) -> Option<Vec<u8>> {
24-
self.store.get(&key).cloned()
26+
self.store.get(&key.0).cloned()
2527
}
2628

2729
fn set(&mut self, key: B256, value: Vec<u8>) -> Result<()> {
28-
self.store.insert(key, value);
30+
self.store.insert(key.0, value);
2931
Ok(())
3032
}
33+
34+
fn to_memory_store(&self) -> MemoryKeyValueStore {
35+
self.clone()
36+
}
3137
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ pub trait KeyValueStore {
2727

2828
/// Set the value associated with the given key.
2929
fn set(&mut self, key: B256, value: Vec<u8>) -> Result<()>;
30+
31+
/// Converts the [KeyValueStore] to a [MemoryKeyValueStore].
32+
fn to_memory_store(&self) -> MemoryKeyValueStore;
3033
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains a concrete implementation of the [KeyValueStore] trait that splits between two separate
22
//! [KeyValueStore]s depending on [PreimageKeyType].
33
4-
use super::KeyValueStore;
4+
use super::{KeyValueStore, MemoryKeyValueStore};
55
use alloy_primitives::B256;
66
use anyhow::Result;
77
use kona_preimage::PreimageKeyType;
@@ -44,4 +44,8 @@ where
4444
fn set(&mut self, key: B256, value: Vec<u8>) -> Result<()> {
4545
self.remote_store.set(key, value)
4646
}
47+
48+
fn to_memory_store(&self) -> MemoryKeyValueStore {
49+
self.remote_store.to_memory_store()
50+
}
4751
}

0 commit comments

Comments
 (0)