Skip to content

Commit

Permalink
feat(host): Use RocksDB as the disk K/V store (#471)
Browse files Browse the repository at this point in the history
* feat(host): Use `RocksDB` as the disk K/V store

* chore(client): Update `testdata` with `RocksDB` data

* switch to snappy compression

* rebase
  • Loading branch information
clabby authored Sep 5, 2024
1 parent b1ebc3c commit 54eb6bf
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 47 deletions.
125 changes: 125 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ revm = { version = "14.0", default-features = false }
superchain = "0.3"
superchain-primitives = { version = "0.3", default-features = false }

# K/V database
rocksdb = { version = "0.22", default-features = false, features = ["snappy"] }

# Alloy
alloy-rlp = { version = "0.3", default-features = false }
alloy-trie = { version = "0.5", default-features = false }
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions bin/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ serde_json.workspace = true
tracing-subscriber.workspace = true
command-fds.workspace = true
os_pipe.workspace = true
rocksdb.workspace = true
54 changes: 29 additions & 25 deletions bin/host/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ where
kv_lock.set(
PreimageKey::new(*hash, PreimageKeyType::Keccak256).into(),
raw_header.into(),
);
)?;
}
HintType::L1Transactions => {
// Validate the hint data length.
Expand Down Expand Up @@ -207,7 +207,7 @@ where
kv_write_lock.set(
PreimageKey::new(*hash, PreimageKeyType::Sha256).into(),
sidecar.kzg_commitment.to_vec(),
);
)?;

// Write all the field elements to the key-value store. There should be 4096.
// The preimage oracle key for each field element is the keccak256 hash of
Expand All @@ -221,11 +221,11 @@ where
kv_write_lock.set(
PreimageKey::new(*blob_key_hash, PreimageKeyType::Keccak256).into(),
blob_key.into(),
);
)?;
kv_write_lock.set(
PreimageKey::new(*blob_key_hash, PreimageKeyType::Blob).into(),
sidecar.blob[(i as usize) << 5..(i as usize + 1) << 5].to_vec(),
);
)?;
}

// Write the KZG Proof as the 4096th element.
Expand All @@ -235,11 +235,11 @@ where
kv_write_lock.set(
PreimageKey::new(*blob_key_hash, PreimageKeyType::Keccak256).into(),
blob_key.into(),
);
)?;
kv_write_lock.set(
PreimageKey::new(*blob_key_hash, PreimageKeyType::Blob).into(),
sidecar.kzg_proof.to_vec(),
);
)?;
}
HintType::L1Precompile => {
// Validate the hint data length.
Expand Down Expand Up @@ -270,9 +270,11 @@ where
kv_lock.set(
PreimageKey::new(*input_hash, PreimageKeyType::Keccak256).into(),
hint_data.into(),
);
kv_lock
.set(PreimageKey::new(*input_hash, PreimageKeyType::Precompile).into(), result);
)?;
kv_lock.set(
PreimageKey::new(*input_hash, PreimageKeyType::Precompile).into(),
result,
)?;
}
HintType::L2BlockHeader => {
// Validate the hint data length.
Expand All @@ -297,7 +299,7 @@ where
kv_lock.set(
PreimageKey::new(*hash, PreimageKeyType::Keccak256).into(),
raw_header.into(),
);
)?;
}
HintType::L2Transactions => {
// Validate the hint data length.
Expand Down Expand Up @@ -371,7 +373,7 @@ where

let mut kv_write_lock = self.kv_store.write().await;
kv_write_lock
.set(PreimageKey::new(*hash, PreimageKeyType::Keccak256).into(), code.into());
.set(PreimageKey::new(*hash, PreimageKeyType::Keccak256).into(), code.into())?;
}
HintType::StartingL2Output => {
const OUTPUT_ROOT_VERSION: u8 = 0;
Expand Down Expand Up @@ -415,7 +417,7 @@ where
kv_write_lock.set(
PreimageKey::new(*output_root, PreimageKeyType::Keccak256).into(),
raw_output.into(),
);
)?;
}
HintType::L2StateNode => {
if hint_data.len() != 32 {
Expand All @@ -439,7 +441,7 @@ where
kv_write_lock.set(
PreimageKey::new(*hash, PreimageKeyType::Keccak256).into(),
preimage.into(),
);
)?;
}
HintType::L2AccountProof => {
if hint_data.len() != 8 + 20 {
Expand All @@ -463,11 +465,12 @@ where
let mut kv_write_lock = self.kv_store.write().await;

// Write the account proof nodes to the key-value store.
proof_response.account_proof.into_iter().for_each(|node| {
proof_response.account_proof.into_iter().try_for_each(|node| {
let node_hash = keccak256(node.as_ref());
let key = PreimageKey::new(*node_hash, PreimageKeyType::Keccak256);
kv_write_lock.set(key.into(), node.into());
});
kv_write_lock.set(key.into(), node.into())?;
Ok(())
})?;
}
HintType::L2AccountStorageProof => {
if hint_data.len() != 8 + 20 + 32 {
Expand All @@ -492,19 +495,21 @@ where
let mut kv_write_lock = self.kv_store.write().await;

// Write the account proof nodes to the key-value store.
proof_response.account_proof.into_iter().for_each(|node| {
proof_response.account_proof.into_iter().try_for_each(|node| {
let node_hash = keccak256(node.as_ref());
let key = PreimageKey::new(*node_hash, PreimageKeyType::Keccak256);
kv_write_lock.set(key.into(), node.into());
});
kv_write_lock.set(key.into(), node.into())?;
Ok(())
})?;

// Write the storage proof nodes to the key-value store.
let storage_proof = proof_response.storage_proof.remove(0);
storage_proof.proof.into_iter().for_each(|node| {
storage_proof.proof.into_iter().try_for_each(|node| {
let node_hash = keccak256(node.as_ref());
let key = PreimageKey::new(*node_hash, PreimageKeyType::Keccak256);
kv_write_lock.set(key.into(), node.into());
});
kv_write_lock.set(key.into(), node.into())?;
Ok(())
})?;
}
}

Expand Down Expand Up @@ -544,8 +549,7 @@ where
// `ProofRetainer` in the event that there are no leaves inserted.
if nodes.is_empty() {
let empty_key = PreimageKey::new(*EMPTY_ROOT_HASH, PreimageKeyType::Keccak256);
kv_write_lock.set(empty_key.into(), [EMPTY_STRING_CODE].into());
return Ok(())
return kv_write_lock.set(empty_key.into(), [EMPTY_STRING_CODE].into())
}

let mut hb = kona_mpt::ordered_trie_with_encoder(nodes, |node, buf| {
Expand All @@ -558,7 +562,7 @@ where
let value_hash = keccak256(value.as_ref());
let key = PreimageKey::new(*value_hash, PreimageKeyType::Keccak256);

kv_write_lock.set(key.into(), value.into());
kv_write_lock.set(key.into(), value.into())?;
}

Ok(())
Expand Down
Loading

0 comments on commit 54eb6bf

Please sign in to comment.