diff --git a/src/kernel_sequencer/src/lib.rs b/src/kernel_sequencer/src/lib.rs index 1ed998f3d2c3471d688cc36b4685e64516b956ee..078dcca82610255dce778e8fca0ec31e34d0d58a 100644 --- a/src/kernel_sequencer/src/lib.rs +++ b/src/kernel_sequencer/src/lib.rs @@ -7,5 +7,6 @@ mod message; pub mod routing; mod sequencer_macro; pub mod sequencer_runtime; +mod storage; pub use routing::FilterBehavior; diff --git a/src/kernel_sequencer/src/message.rs b/src/kernel_sequencer/src/message.rs index 7b57a4f09b73d0637904ef460784db88a2ce6609..8ecc5d5e80e18a772c65ab3265f070721afc05d0 100644 --- a/src/kernel_sequencer/src/message.rs +++ b/src/kernel_sequencer/src/message.rs @@ -224,8 +224,6 @@ mod tests { let mut bin: Vec = Vec::new(); sequence.bin_write(&mut bin).unwrap(); - println!("{:?}", bin); - // Deserializing let (_, msg_read) = KernelMessage::nom_read(&bin).expect("deserialization should work"); @@ -268,8 +266,6 @@ mod tests { sequence.bin_write(&mut bin).unwrap(); enc::put_bytes(&[0x01, 0x02, 0x03, 0x04], &mut bin); - println!("{:?}", bin); - // Deserializing let (remaining, msg_read) = KernelMessage::nom_read(&bin).expect("deserialization should work"); diff --git a/src/kernel_sequencer/src/sequencer_runtime.rs b/src/kernel_sequencer/src/sequencer_runtime.rs index d9651ea007a34d4238b392ba9577e05790421872..8b1ea976430cfdd6416aeb25cd34f8f2243e44b8 100644 --- a/src/kernel_sequencer/src/sequencer_runtime.rs +++ b/src/kernel_sequencer/src/sequencer_runtime.rs @@ -11,7 +11,7 @@ use tezos_smart_rollup_host::{ runtime::{Runtime, RuntimeError, ValueType}, }; -use crate::{delayed_inbox::read_input, routing::FilterBehavior}; +use crate::{delayed_inbox::read_input, routing::FilterBehavior, storage::map_user_path}; pub struct SequencerRuntime where @@ -55,7 +55,8 @@ where } fn store_has(&self, path: &T) -> Result, RuntimeError> { - self.host.store_has(path) + let path = map_user_path(path)?; + self.host.store_has(&path) } fn store_read( @@ -64,7 +65,8 @@ where from_offset: usize, max_bytes: usize, ) -> Result, RuntimeError> { - self.host.store_read(path, from_offset, max_bytes) + let path = map_user_path(path)?; + self.host.store_read(&path, from_offset, max_bytes) } fn store_read_slice( @@ -73,11 +75,13 @@ where from_offset: usize, buffer: &mut [u8], ) -> Result { - self.host.store_read_slice(path, from_offset, buffer) + let path = map_user_path(path)?; + self.host.store_read_slice(&path, from_offset, buffer) } fn store_read_all(&self, path: &impl Path) -> Result, RuntimeError> { - self.host.store_read_all(path) + let path = map_user_path(path)?; + self.host.store_read_all(&path) } fn store_write( @@ -86,15 +90,18 @@ where src: &[u8], at_offset: usize, ) -> Result<(), RuntimeError> { - self.host.store_write(path, src, at_offset) + let path = map_user_path(path)?; + self.host.store_write(&path, src, at_offset) } fn store_delete(&mut self, path: &T) -> Result<(), RuntimeError> { - self.host.store_delete(path) + let path = map_user_path(path)?; + self.host.store_delete(&path) } fn store_count_subkeys(&self, prefix: &T) -> Result { - self.host.store_count_subkeys(prefix) + let prefix = map_user_path(prefix)?; + self.host.store_count_subkeys(&prefix) } fn store_move( @@ -102,7 +109,9 @@ where from_path: &impl Path, to_path: &impl Path, ) -> Result<(), RuntimeError> { - self.host.store_move(from_path, to_path) + let from_path = map_user_path(from_path)?; + let to_path = map_user_path(to_path)?; + self.host.store_move(&from_path, &to_path) } fn store_copy( @@ -110,7 +119,9 @@ where from_path: &impl Path, to_path: &impl Path, ) -> Result<(), RuntimeError> { - self.host.store_copy(from_path, to_path) + let from_path = map_user_path(from_path)?; + let to_path = map_user_path(to_path)?; + self.host.store_copy(&from_path, &to_path) } fn reveal_preimage( @@ -122,7 +133,8 @@ where } fn store_value_size(&self, path: &impl Path) -> Result { - self.host.store_value_size(path) + let path = map_user_path(path)?; + self.host.store_value_size(&path) } fn mark_for_reboot(&mut self) -> Result<(), RuntimeError> { diff --git a/src/kernel_sequencer/src/storage.rs b/src/kernel_sequencer/src/storage.rs new file mode 100644 index 0000000000000000000000000000000000000000..d81838cc6abde616cbe596bee4b24e442501edc5 --- /dev/null +++ b/src/kernel_sequencer/src/storage.rs @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2023 Marigold +// +// SPDX-License-Identifier: MIT + +use tezos_smart_rollup_host::{ + path::{concat, OwnedPath, Path, RefPath, PATH_SEPARATOR}, + runtime::RuntimeError, + Error, +}; + +const SEQUENCER_PREFIX_PATH: RefPath = RefPath::assert_from(b"/__sequencer"); +const USER_PREFIX_PATH: RefPath = RefPath::assert_from(b"/u"); + +/// Prefix the given path by `/__sequencer`. +/// +/// This function has to be used when writing/reading storage related to the sequencer kernel. +/// Then with this function, all the sequencer kernel storage should be under the path `__sequencer`. +#[allow(dead_code)] +pub fn sequencer_prefix(path: &T) -> Result { + concat(&SEQUENCER_PREFIX_PATH, path).map_err(|_| RuntimeError::HostErr(Error::StoreInvalidKey)) +} + +/// Prefix the given path by `/u`. +/// +/// If the given path starts by `/kernel`, then the path is unchanged. +/// If the given path starts by `/__sdk`, then the path is unchanged. +pub fn map_user_path(path: &T) -> Result { + match path.as_bytes() { + [PATH_SEPARATOR, b'k', b'e', b'r', b'n', b'e', b'l', PATH_SEPARATOR, ..] + | [PATH_SEPARATOR, b'_', b'_', b's', b'd', b'k', PATH_SEPARATOR, ..] => Ok(path.into()), + _ => concat(&USER_PREFIX_PATH, path) + .map_err(|_| RuntimeError::HostErr(Error::StoreInvalidKey)), + } +} + +#[cfg(test)] +mod tests { + use tezos_smart_rollup_host::path::{concat, OwnedPath, RefPath}; + + use super::{map_user_path, sequencer_prefix, USER_PREFIX_PATH}; + + #[test] + fn test_sequencer_prefixed() { + let path = RefPath::assert_from(b"/a/b/c"); + let prefixed = sequencer_prefix(&path).expect("prefixing the path should work"); + + assert_eq!(prefixed, RefPath::assert_from(b"/__sequencer/a/b/c").into()); + } + + #[test] + fn test_map_kernel_boot() { + let kernel_boot_path: OwnedPath = RefPath::assert_from(b"/kernel/boot.wasm").into(); + let mapped_kernel_boot_path = map_user_path(&kernel_boot_path).unwrap(); + + assert_eq!(kernel_boot_path, mapped_kernel_boot_path) + } + + #[test] + fn test_map_user_path() { + let user_path = RefPath::assert_from(b"/state/defined/by/user"); + let prefixed_user_path = map_user_path(&user_path).unwrap(); + let expected = concat(&USER_PREFIX_PATH, &user_path).unwrap(); + assert_eq!(expected, prefixed_user_path); + } +}