Skip to content

Commit 0950a33

Browse files
authored
feat: add RocksDB backend (#58)
* feat: add RocksDB backend * refactor: integrate RocksDBBackend with Store * test: add general test battery for StorageBackends * feat: use RocksDB backend by default * chore: remove "rocksdb" compilation feature
1 parent 228a95c commit 0950a33

13 files changed

Lines changed: 531 additions & 190 deletions

File tree

Cargo.lock

Lines changed: 106 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ tree_hash_derive = "0.9.1"
6868
vergen-git2 = { version = "9", features = ["rustc"] }
6969

7070
rand = "0.9"
71+
rocksdb = "0.24"

bin/ethlambda/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
collections::{BTreeMap, HashMap},
55
net::{IpAddr, SocketAddr},
66
path::{Path, PathBuf},
7+
sync::Arc,
78
};
89

910
use clap::Parser;
@@ -19,7 +20,7 @@ use tracing::{error, info};
1920
use tracing_subscriber::{EnvFilter, Layer, Registry, layer::SubscriberExt};
2021

2122
use ethlambda_blockchain::BlockChain;
22-
use ethlambda_storage::Store;
23+
use ethlambda_storage::{Store, backend::RocksDBBackend};
2324

2425
const ASCII_ART: &str = r#"
2526
_ _ _ _ _
@@ -91,7 +92,8 @@ async fn main() {
9192
read_validator_keys(&validators_path, &validator_keys_dir, &options.node_id);
9293

9394
let genesis_state = State::from_genesis(&genesis, validators);
94-
let store = Store::from_genesis(genesis_state);
95+
let backend = Arc::new(RocksDBBackend::open("./data").expect("Failed to open RocksDB"));
96+
let store = Store::from_genesis(backend, genesis_state);
9597

9698
let (p2p_tx, p2p_rx) = tokio::sync::mpsc::unbounded_channel();
9799
let blockchain = BlockChain::spawn(store.clone(), p2p_tx, validator_keys);

crates/blockchain/tests/forkchoice_spectests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::{
22
collections::{HashMap, HashSet},
33
path::Path,
4+
sync::Arc,
45
};
56

67
use ethlambda_blockchain::{SECONDS_PER_SLOT, store};
7-
use ethlambda_storage::Store;
8+
use ethlambda_storage::{Store, backend::InMemoryBackend};
89
use ethlambda_types::{
910
attestation::Attestation,
1011
block::{Block, BlockSignatures, BlockWithAttestation, SignedBlockWithAttestation},
@@ -35,7 +36,8 @@ fn run(path: &Path) -> datatest_stable::Result<()> {
3536
let anchor_state: State = test.anchor_state.into();
3637
let anchor_block: Block = test.anchor_block.into();
3738
let genesis_time = anchor_state.config.genesis_time;
38-
let mut store = Store::get_forkchoice_store(anchor_state, anchor_block);
39+
let backend = Arc::new(InMemoryBackend::new());
40+
let mut store = Store::get_forkchoice_store(backend, anchor_state, anchor_block);
3941

4042
// Block registry: maps block labels to their roots
4143
let mut block_registry: HashMap<String, H256> = HashMap::new();

crates/blockchain/tests/signature_spectests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::path::Path;
2+
use std::sync::Arc;
23

34
use ethlambda_blockchain::{SECONDS_PER_SLOT, store};
4-
use ethlambda_storage::Store;
5+
use ethlambda_storage::{Store, backend::InMemoryBackend};
56
use ethlambda_types::{
67
block::{Block, SignedBlockWithAttestation},
78
primitives::TreeHash,
@@ -41,7 +42,8 @@ fn run(path: &Path) -> datatest_stable::Result<()> {
4142

4243
// Initialize the store with the anchor state and block
4344
let genesis_time = anchor_state.config.genesis_time;
44-
let mut st = Store::get_forkchoice_store(anchor_state, anchor_block);
45+
let backend = Arc::new(InMemoryBackend::new());
46+
let mut st = Store::get_forkchoice_store(backend, anchor_state, anchor_block);
4547

4648
// Step 2: Run the state transition function with the block fixture
4749
let signed_block: SignedBlockWithAttestation = test.signed_block_with_attestation.into();

crates/net/rpc/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ mod tests {
7474
body::Body,
7575
http::{Request, StatusCode},
7676
};
77-
use ethlambda_storage::Store;
77+
use ethlambda_storage::{Store, backend::InMemoryBackend};
7878
use ethlambda_types::{
7979
block::{BlockBody, BlockHeader},
8080
primitives::TreeHash,
8181
state::{ChainConfig, Checkpoint, JustificationValidators, JustifiedSlots, State},
8282
};
8383
use http_body_util::BodyExt;
8484
use serde_json::json;
85+
use std::sync::Arc;
8586
use tower::ServiceExt;
8687

8788
/// Create a minimal test state for testing.
@@ -116,7 +117,8 @@ mod tests {
116117
#[tokio::test]
117118
async fn test_get_latest_justified_checkpoint() {
118119
let state = create_test_state();
119-
let store = Store::from_genesis(state);
120+
let backend = Arc::new(InMemoryBackend::new());
121+
let store = Store::from_genesis(backend, state);
120122

121123
let app = build_api_router(store.clone());
122124

@@ -151,7 +153,8 @@ mod tests {
151153
use ethlambda_types::primitives::Encode;
152154

153155
let state = create_test_state();
154-
let store = Store::from_genesis(state);
156+
let backend = Arc::new(InMemoryBackend::new());
157+
let store = Store::from_genesis(backend, state);
155158

156159
// Get the expected state from the store
157160
let finalized = store.latest_finalized();

crates/storage/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ version.workspace = true
1313
ethlambda-types.workspace = true
1414

1515
tracing.workspace = true
16+
rocksdb.workspace = true
17+
18+
[dev-dependencies]
19+
tempfile = "3"

0 commit comments

Comments
 (0)