From 2ecd0cc68dbc83f153ecb33e72556c68c1540fe0 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Date: Thu, 8 Jun 2023 11:16:05 +0200 Subject: [PATCH 1/2] kernel/sequencer: add sequencer space --- src/kernel_sequencer/src/lib.rs | 1 + src/kernel_sequencer/src/message.rs | 4 ---- src/kernel_sequencer/src/storage.rs | 35 +++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/kernel_sequencer/src/storage.rs diff --git a/src/kernel_sequencer/src/lib.rs b/src/kernel_sequencer/src/lib.rs index 1ed998f3d2c3..078dcca82610 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 7b57a4f09b73..8ecc5d5e80e1 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/storage.rs b/src/kernel_sequencer/src/storage.rs new file mode 100644 index 000000000000..19e36f041f2f --- /dev/null +++ b/src/kernel_sequencer/src/storage.rs @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: 2023 Marigold +// +// SPDX-License-Identifier: MIT + +use tezos_smart_rollup_host::{ + path::{concat, OwnedPath, Path, RefPath}, + runtime::RuntimeError, + Error, +}; + +const SEQUENCER_PREFIX_PATH: RefPath = RefPath::assert_from(b"/__sequencer"); + +/// 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)) +} + +#[cfg(test)] +mod tests { + use tezos_smart_rollup_host::path::RefPath; + + use super::sequencer_prefix; + + #[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()); + } +} -- GitLab From 4d69282b0630cb85091ba6c9d27f8ece9413f14e Mon Sep 17 00:00:00 2001 From: Pierre-Louis Date: Fri, 23 Jun 2023 10:02:44 +0200 Subject: [PATCH 2/2] kernel/sequencer: add prefix for user space --- src/kernel_sequencer/src/sequencer_runtime.rs | 34 ++++++++++++------ src/kernel_sequencer/src/storage.rs | 36 +++++++++++++++++-- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/kernel_sequencer/src/sequencer_runtime.rs b/src/kernel_sequencer/src/sequencer_runtime.rs index d9651ea007a3..8b1ea976430c 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 index 19e36f041f2f..d81838cc6abd 100644 --- a/src/kernel_sequencer/src/storage.rs +++ b/src/kernel_sequencer/src/storage.rs @@ -3,12 +3,13 @@ // SPDX-License-Identifier: MIT use tezos_smart_rollup_host::{ - path::{concat, OwnedPath, Path, RefPath}, + 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`. /// @@ -19,11 +20,24 @@ 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::RefPath; + use tezos_smart_rollup_host::path::{concat, OwnedPath, RefPath}; - use super::sequencer_prefix; + use super::{map_user_path, sequencer_prefix, USER_PREFIX_PATH}; #[test] fn test_sequencer_prefixed() { @@ -32,4 +46,20 @@ mod tests { 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); + } } -- GitLab