Skip to content

Commit

Permalink
add kv and trie crate
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsing committed Dec 6, 2024
1 parent 7621ce1 commit 7c31650
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 11 deletions.
120 changes: 113 additions & 7 deletions Cargo.lock

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

16 changes: 12 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
members = [
"crates/bin",
"crates/core",
"crates/kv",
"crates/primitives",
"crates/sbv",
"crates/trie",
"crates/utils",
]
resolver = "2"
Expand All @@ -18,20 +20,24 @@ homepage = "https://github.com/scroll-tech/stateless-block-verifier"
repository = "https://github.com/scroll-tech/stateless-block-verifier"

[workspace.dependencies]
# https://github.com/alloy-rs/alloy
alloy = "0.5"

alloy-eips = "0.5"
alloy-consensus = "0.5"
alloy-rpc-types-eth = "0.5"
alloy-rpc-types-debug = "0.5"
alloy-serde = "0.5"

# https://github.com/alloy-rs/eips
alloy-eip2930 = "0.1"

# https://github.com/alloy-rs/rlp
alloy-rlp = "0.3"

# https://github.com/alloy-rs/trie
alloy-trie = "0.7"
# https://github.com/alloy-rs/core
alloy-primitives = { version = "0.8", features = ["rkyv"] }

reth-trie-sparse = { git = "https://github.com/paradigmxyz/reth", tag = "v1.1.2" }

hex = "0.4"
once_cell = "1.19"
rkyv = "0.8"
Expand Down Expand Up @@ -70,7 +76,9 @@ reqwest = "0.12"
# workspace
sbv = { path = "crates/sbv" }
sbv-core = { path = "crates/core" }
sbv-kv = { path = "crates/kv" }
sbv-primitives = { path = "crates/primitives" }
sbv-trie = { path = "crates/trie" }
sbv-utils = { path = "crates/utils" }

[workspace.dependencies.revm]
Expand Down
20 changes: 20 additions & 0 deletions crates/kv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "sbv-kv"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[lints]
workspace = true

[dependencies]
alloy-trie = { workspace = true, optional = true}
sled = { workspace = true, optional = true }

[features]
sled = ["dep:sled"]
alloy-trie = ["dep:alloy-trie"]
13 changes: 13 additions & 0 deletions crates/kv/src/imps/alloy_trie.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use alloy_trie::nodes::TrieNode;

impl crate::Value for TrieNode {
#[cfg(feature = "sled")]
fn serialize(&self) -> Vec<u8> {
todo!()
}

#[cfg(feature = "sled")]
fn deserialize(buf: &[u8]) -> Self {
todo!()
}
}
46 changes: 46 additions & 0 deletions crates/kv/src/imps/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{KeyValueStore, KeyValueStoreGet, KeyValueStoreInsert, Value};
use std::borrow::{Borrow, Cow};
use std::hash::Hash;

#[cfg(feature = "alloy-trie")]
mod alloy_trie;
#[cfg(feature = "sled")]
mod sled;
mod std_collections;

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value, S: KeyValueStore<K, V>> KeyValueStoreGet<K, V>
for &S
{
fn get<Q: ?Sized>(&self, k: &Q) -> Option<Cow<V>>
where
K: Borrow<Q>,
Q: Ord + Hash + Eq + AsRef<[u8]>,
{
S::get(*self, k)
}
}

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value, S: KeyValueStore<K, V>> KeyValueStoreGet<K, V>
for &mut S
{
fn get<Q: ?Sized>(&self, k: &Q) -> Option<Cow<V>>
where
K: Borrow<Q>,
Q: Ord + Hash + Eq + AsRef<[u8]>,
{
S::get(*self, k)
}
}

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value, S: KeyValueStore<K, V>> KeyValueStoreInsert<K, V>
for &mut S
{
fn insert(&mut self, k: K, v: V) {
S::insert(*self, k, v)
}
}

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value, S: KeyValueStore<K, V>> KeyValueStore<K, V>
for &mut S
{
}
24 changes: 24 additions & 0 deletions crates/kv/src/imps/sled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{KeyValueStore, KeyValueStoreGet, KeyValueStoreInsert, Value};
use std::borrow::{Borrow, Cow};
use std::hash::Hash;

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value> KeyValueStoreInsert<K, V> for sled::Tree {
fn insert(&mut self, k: K, v: V) {
sled::Tree::insert(self, k, v.serialize()).expect("sled io error");
}
}

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value> KeyValueStoreGet<K, V> for sled::Tree {
fn get<Q: ?Sized>(&self, k: &Q) -> Option<Cow<V>>
where
K: Borrow<Q>,
Q: Ord + Hash + Eq + AsRef<[u8]>,
{
sled::Tree::get(self, k)
.expect("sled io error")
.map(|vec| Value::deserialize(vec.as_ref()))
.map(Cow::Owned)
}
}

impl<K: Ord + Hash + Eq + AsRef<[u8]>, V: Value> KeyValueStore<K, V> for sled::Tree {}
Loading

0 comments on commit 7c31650

Please sign in to comment.