From 74af01da1e1d8c610f476d940046ddbdd8c7032a Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Thu, 3 Aug 2023 15:20:20 +0200 Subject: [PATCH] WASM: Add `reveal_dal_page` to the Rust SDK --- src/kernel_evm/kernel/src/safe_storage.rs | 12 ++++++ src/kernel_sdk/CHANGES.md | 5 +++ src/kernel_sdk/core/Cargo.toml | 1 + src/kernel_sdk/core/src/rollup_host.rs | 11 +++++ src/kernel_sdk/core/src/smart_rollup_core.rs | 18 ++++++++ src/kernel_sdk/host/Cargo.toml | 1 + src/kernel_sdk/host/src/runtime.rs | 43 ++++++++++++++++++++ src/kernel_sdk/mock/Cargo.toml | 1 + src/kernel_sdk/mock/src/host.rs | 12 ++++++ src/kernel_sdk/sdk/Cargo.toml | 1 + 10 files changed, 105 insertions(+) diff --git a/src/kernel_evm/kernel/src/safe_storage.rs b/src/kernel_evm/kernel/src/safe_storage.rs index 23ab28df8c72..18f8d0321885 100644 --- a/src/kernel_evm/kernel/src/safe_storage.rs +++ b/src/kernel_evm/kernel/src/safe_storage.rs @@ -143,6 +143,18 @@ impl Runtime for SafeStorage<&mut Host> { self.0.reveal_metadata() } + #[cfg(all(feature = "alloc", feature = "proto-alpha"))] + fn reveal_dal_page( + &self, + published_level: i32, + slot_index: u8, + page_index: i16, + destination: &mut [u8], + ) -> Result { + self.0 + .reveal_dal_page(published_level, slot_index, page_index, destination) + } + fn last_run_aborted(&self) -> Result { self.0.last_run_aborted() } diff --git a/src/kernel_sdk/CHANGES.md b/src/kernel_sdk/CHANGES.md index 3f288b7c14dd..5b8f22439d2a 100644 --- a/src/kernel_sdk/CHANGES.md +++ b/src/kernel_sdk/CHANGES.md @@ -4,6 +4,11 @@ ### SDK +- Add a feature flag `proto-alpha` to enable host functions introduced in + unreleased protocols. +- Add `Runtime::reveal_dal_page` to let a kernel request pages from Tezos’ Data + Availability Layer (DAL). + ### Installer client/kernel ## Version 0.2.1 diff --git a/src/kernel_sdk/core/Cargo.toml b/src/kernel_sdk/core/Cargo.toml index b8e45e953d3d..b30183e17aca 100644 --- a/src/kernel_sdk/core/Cargo.toml +++ b/src/kernel_sdk/core/Cargo.toml @@ -26,3 +26,4 @@ version = "0.11.0" [features] "testing" = ["mockall"] "proto-nairobi" = [] +"proto-alpha" = [] diff --git a/src/kernel_sdk/core/src/rollup_host.rs b/src/kernel_sdk/core/src/rollup_host.rs index 72b7f10e4443..7af71709fcc7 100644 --- a/src/kernel_sdk/core/src/rollup_host.rs +++ b/src/kernel_sdk/core/src/rollup_host.rs @@ -120,6 +120,17 @@ unsafe impl SmartRollupCore for RollupHost { core::reveal_preimage(hash_addr, hash_len, destination_addr, max_bytes) } + #[cfg(feature = "proto-alpha")] + unsafe fn reveal( + &self, + payload_addr: *const u8, + payload_len: usize, + destination_addr: *mut u8, + max_bytes: usize, + ) -> i32 { + core::reveal(payload_addr, payload_len, destination_addr, max_bytes) + } + unsafe fn store_value_size(&self, path: *const u8, path_len: usize) -> i32 { core::store_value_size(path, path_len) } diff --git a/src/kernel_sdk/core/src/smart_rollup_core.rs b/src/kernel_sdk/core/src/smart_rollup_core.rs index 53831cea31dc..961de2d0c1f9 100644 --- a/src/kernel_sdk/core/src/smart_rollup_core.rs +++ b/src/kernel_sdk/core/src/smart_rollup_core.rs @@ -134,6 +134,7 @@ extern "C" { /// `payload_len` is the same as the one used by the Tezos protocol. /// /// Returns the size of the data loaded in memory. + #[cfg(feature = "proto-alpha")] pub fn reveal( payload_addr: *const u8, payload_len: usize, @@ -313,6 +314,23 @@ pub unsafe trait SmartRollupCore { /// - `destination_addr` must point to a mutable slice of bytes with /// `capacity >= max_bytes` unsafe fn reveal_metadata(&self, destination_addr: *mut u8, max_bytes: usize) -> i32; + + /// Loads the result of a raw reveal request to memory. + /// If the preimage is larger than `max_bytes`, its contents is trimmed. + /// + /// # Safety + /// - `payload_addr` must be a ptr to a slice containing a hash. + /// - `payload_len` must be the length of the slice. + /// - `destination_addr `must point to a mutable slice of bytes with + /// `capacity >= max_bytes`. + #[cfg(feature = "proto-alpha")] + unsafe fn reveal( + &self, + payload_addr: *const u8, + payload_len: usize, + destination_addr: *mut u8, + max_bytes: usize, + ) -> i32; } /// Information about message level & id. diff --git a/src/kernel_sdk/host/Cargo.toml b/src/kernel_sdk/host/Cargo.toml index 58f23081896d..08829bd0992f 100644 --- a/src/kernel_sdk/host/Cargo.toml +++ b/src/kernel_sdk/host/Cargo.toml @@ -44,5 +44,6 @@ crypto = ["tezos_crypto_rs"] alloc = ["thiserror", "tezos_data_encoding"] testing = ["crypto"] proto-nairobi = ["tezos-smart-rollup-core/proto-nairobi"] +proto-alpha = ["tezos-smart-rollup-core/proto-alpha"] # Required for 'impl Error for RuntimeError' std = [] diff --git a/src/kernel_sdk/host/src/runtime.rs b/src/kernel_sdk/host/src/runtime.rs index ab9cccdfdc81..51ebbb72964a 100644 --- a/src/kernel_sdk/host/src/runtime.rs +++ b/src/kernel_sdk/host/src/runtime.rs @@ -186,6 +186,16 @@ pub trait Runtime { destination: &mut [u8], ) -> Result; + /// Reveal a DAL page. + #[cfg(all(feature = "alloc", feature = "proto-alpha"))] + fn reveal_dal_page( + &self, + published_level: i32, + slot_index: u8, + page_index: i16, + destination: &mut [u8], + ) -> Result; + /// Return the size of value stored at `path` fn store_value_size(&self, path: &impl Path) -> Result; @@ -580,6 +590,39 @@ where RollupMetadata::from(destination) } + #[cfg(all(feature = "alloc", feature = "proto-alpha"))] + fn reveal_dal_page( + &self, + published_level: i32, + slot_index: u8, + page_index: i16, + destination: &mut [u8], + ) -> Result { + // This will match the encoding declared for a DAL page in the Tezos protocol. + let payload: &[u8] = &[ + &[2u8], // tag + published_level.to_be_bytes().as_ref(), + &[slot_index], + page_index.to_be_bytes().as_ref(), + ] + .concat(); + + let res = unsafe { + SmartRollupCore::reveal( + self, + payload.as_ptr(), + payload.len(), + destination.as_mut_ptr(), + destination.len(), + ) + }; + + match Error::wrap(res) { + Ok(size) => Ok(size), + Err(e) => Err(RuntimeError::HostErr(e)), + } + } + fn store_value_size(&self, path: &impl Path) -> Result { check_path_exists(self, path)?; let res = unsafe { diff --git a/src/kernel_sdk/mock/Cargo.toml b/src/kernel_sdk/mock/Cargo.toml index 9cd41589ec0e..71bcd1b005a7 100644 --- a/src/kernel_sdk/mock/Cargo.toml +++ b/src/kernel_sdk/mock/Cargo.toml @@ -37,3 +37,4 @@ features = ["default"] [features] proto-nairobi = ["tezos-smart-rollup-core/proto-nairobi", "tezos-smart-rollup-host/proto-nairobi"] +proto-alpha = ["tezos-smart-rollup-core/proto-alpha", "tezos-smart-rollup-host/proto-alpha"] diff --git a/src/kernel_sdk/mock/src/host.rs b/src/kernel_sdk/mock/src/host.rs index 06cc55d98cf0..3660a3d38331 100644 --- a/src/kernel_sdk/mock/src/host.rs +++ b/src/kernel_sdk/mock/src/host.rs @@ -238,6 +238,18 @@ unsafe impl SmartRollupCore for MockHost { slice.copy_from_slice(metadata.as_slice()); metadata.len().try_into().unwrap() } + + #[cfg(feature = "proto-alpha")] + unsafe fn reveal( + &self, + _payload_addr: *const u8, + _payload_len: usize, + _destination_addr: *mut u8, + _max_bytes: usize, + ) -> i32 { + // TODO: https://gitlab.com/tezos/tezos/-/issues/6171 + unimplemented!("The `reveal` host function is not yet mocked.") + } } #[cfg(test)] diff --git a/src/kernel_sdk/sdk/Cargo.toml b/src/kernel_sdk/sdk/Cargo.toml index 5113b0a33de5..9b34f55a3beb 100644 --- a/src/kernel_sdk/sdk/Cargo.toml +++ b/src/kernel_sdk/sdk/Cargo.toml @@ -70,3 +70,4 @@ storage = ["tezos-smart-rollup-storage"] std = ["alloc", "tezos-smart-rollup-entrypoint/std"] testing = ["crypto", "tezos-smart-rollup-mock"] proto-nairobi = ["tezos-smart-rollup-core/proto-nairobi", "tezos-smart-rollup-host/proto-nairobi", "tezos-smart-rollup-mock/proto-nairobi"] +proto-alpha = ["tezos-smart-rollup-core/proto-alpha", "tezos-smart-rollup-host/proto-alpha", "tezos-smart-rollup-mock/proto-alpha"] -- GitLab