-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(host): Add local key value store #189
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need standard definitions of the rollup / chain configuration per-chain. Might actually be time to make those
superchain-registry
bindings 🫠There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's open up a ticket for this so we track and just merge this as is for now to not block