-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(host): Add local key value store
Adds the `LocalKeyValueStore` and the `SplitKeyValueStore`, which routes request for local data to the in-memory configuration.
- Loading branch information
Showing
6 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Contains a concrete implementation of the [KeyValueStore] trait that stores data on disk. | ||
use super::KeyValueStore; | ||
use crate::cli::HostCli; | ||
use alloy_primitives::{B256, U256}; | ||
use kona_preimage::PreimageKey; | ||
|
||
pub(crate) const L1_HEAD_KEY: U256 = U256::from_be_slice(&[1]); | ||
pub(crate) const L2_OUTPUT_ROOT_KEY: U256 = U256::from_be_slice(&[2]); | ||
pub(crate) const L2_CLAIM_KEY: U256 = U256::from_be_slice(&[3]); | ||
pub(crate) const L2_CLAIM_BLOCK_NUMBER_KEY: U256 = U256::from_be_slice(&[4]); | ||
pub(crate) const L2_CHAIN_ID_KEY: U256 = U256::from_be_slice(&[5]); | ||
pub(crate) const L2_CHAIN_CONFIG_KEY: U256 = U256::from_be_slice(&[6]); | ||
pub(crate) const L2_ROLLUP_CONFIG_KEY: U256 = U256::from_be_slice(&[7]); | ||
|
||
/// A simple, synchronous key-value store that returns data from a [HostCli] config. | ||
pub struct LocalKeyValueStore { | ||
cfg: HostCli, | ||
} | ||
|
||
impl LocalKeyValueStore { | ||
/// Create a new [LocalKeyValueStore] with the given [HostCli] config. | ||
pub fn new(cfg: HostCli) -> Self { | ||
Self { cfg } | ||
} | ||
} | ||
|
||
impl KeyValueStore for LocalKeyValueStore { | ||
fn get(&self, key: B256) -> Option<Vec<u8>> { | ||
let preimage_key = PreimageKey::try_from(*key).ok()?; | ||
match preimage_key.key_value() { | ||
L1_HEAD_KEY => Some(self.cfg.l1_head.to_vec()), | ||
L2_OUTPUT_ROOT_KEY => Some(self.cfg.l2_output_root.to_vec()), | ||
L2_CLAIM_KEY => Some(self.cfg.l2_claim.to_vec()), | ||
L2_CLAIM_BLOCK_NUMBER_KEY => Some(self.cfg.l2_block_number.to_be_bytes().to_vec()), | ||
L2_CHAIN_ID_KEY => todo!(), | ||
L2_CHAIN_CONFIG_KEY => todo!(), | ||
L2_ROLLUP_CONFIG_KEY => todo!(), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn set(&mut self, _: B256, _: Vec<u8>) { | ||
unreachable!("LocalKeyValueStore is read-only") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! Contains a concrete implementation of the [KeyValueStore] trait that splits between two separate | ||
//! [KeyValueStore]s depending on [PreimageKeyType]. | ||
use alloy_primitives::B256; | ||
use kona_preimage::PreimageKeyType; | ||
|
||
use super::KeyValueStore; | ||
|
||
/// A split implementation of the [KeyValueStore] trait that splits between two separate | ||
/// [KeyValueStore]s. | ||
#[derive(Clone)] | ||
pub struct SplitKeyValueStore<L, R> | ||
where | ||
L: KeyValueStore, | ||
R: KeyValueStore, | ||
{ | ||
local_store: L, | ||
remote_store: R, | ||
} | ||
|
||
impl<L, R> SplitKeyValueStore<L, R> | ||
where | ||
L: KeyValueStore, | ||
R: KeyValueStore, | ||
{ | ||
/// Create a new [SplitKeyValueStore] with the given left and right [KeyValueStore]s. | ||
pub fn new(local_store: L, remote_store: R) -> Self { | ||
Self { local_store, remote_store } | ||
} | ||
} | ||
|
||
impl<L, R> KeyValueStore for SplitKeyValueStore<L, R> | ||
where | ||
L: KeyValueStore, | ||
R: KeyValueStore, | ||
{ | ||
fn get(&self, key: B256) -> Option<Vec<u8>> { | ||
match PreimageKeyType::try_from(key[0]).ok()? { | ||
PreimageKeyType::Local => self.local_store.get(key), | ||
_ => self.remote_store.get(key), | ||
} | ||
} | ||
|
||
fn set(&mut self, key: B256, value: Vec<u8>) { | ||
self.remote_store.set(key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters