From 94ca3b4a1b6b2015e5016e31c24a2e681839a5fd Mon Sep 17 00:00:00 2001 From: martoon Date: Wed, 6 Sep 2023 22:47:14 +0300 Subject: [PATCH 1/3] MIR: Add main processing loop --- contrib/mir/src/lib.rs | 8 ++---- contrib/mir/src/rollup.rs | 8 ++++++ contrib/mir/src/rollup/kernel.rs | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 contrib/mir/src/rollup.rs create mode 100644 contrib/mir/src/rollup/kernel.rs diff --git a/contrib/mir/src/lib.rs b/contrib/mir/src/lib.rs index 5034c683ce18..54051fa1e4d5 100644 --- a/contrib/mir/src/lib.rs +++ b/contrib/mir/src/lib.rs @@ -5,11 +5,7 @@ /* */ /******************************************************************************/ +mod rollup; use tezos_smart_rollup::kernel_entry; -use tezos_smart_rollup::prelude::{debug_msg, Runtime}; -kernel_entry!(kernel_entry); - -pub fn kernel_entry(host: &mut Host) { - debug_msg!(host, "Hello Kernel"); -} +kernel_entry!(rollup::kernel::kernel_entry); diff --git a/contrib/mir/src/rollup.rs b/contrib/mir/src/rollup.rs new file mode 100644 index 000000000000..296177b158b7 --- /dev/null +++ b/contrib/mir/src/rollup.rs @@ -0,0 +1,8 @@ +/******************************************************************************/ +/* */ +/* SPDX-License-Identifier: MIT */ +/* Copyright (c) [2023] Serokell */ +/* */ +/******************************************************************************/ + +pub mod kernel; diff --git a/contrib/mir/src/rollup/kernel.rs b/contrib/mir/src/rollup/kernel.rs new file mode 100644 index 000000000000..5014f6b7a907 --- /dev/null +++ b/contrib/mir/src/rollup/kernel.rs @@ -0,0 +1,46 @@ +/******************************************************************************/ +/* */ +/* SPDX-License-Identifier: MIT */ +/* Copyright (c) [2023] Serokell */ +/* */ +/******************************************************************************/ + +use tezos_smart_rollup::{ + prelude::{debug_msg, Runtime}, + types::Message, +}; + +// This module follows the reference examples: https://gitlab.com/tezos/kernel-gallery + +/// The root method of the kernel. +/// +/// Executed every level. Can be asked from inside to be run again at the same +/// level to handle some of the messages in the inbox, mind the limits on +/// execution time of one such call. +pub fn kernel_entry(host: &mut impl Runtime) { + debug_msg!(host, "Kernel invoked"); + + let mut first_message_for_invocation = true; + // Handle all the messages we got at this level + loop { + match host.read_input() { + Ok(Some(msg)) => { + if first_message_for_invocation { + debug_msg!(host, "Handling messages at level {}", msg.level); + first_message_for_invocation = false; + } + + // TODO [#6411]: wrap into catch_unwind + process_message(host, &msg); + } + // The kernel gallery and some experienced devs advise to keep + // reading, errors in messages reading are really unlikely here. + Err(_) => continue, + Ok(None) => break, + } + } +} + +pub fn process_message(host: &mut impl Runtime, msg: &Message) { + let _ = (host, msg); +} -- GitLab From 036f7672b88cba1e27bdfa5207c24c20f9c51fd1 Mon Sep 17 00:00:00 2001 From: martoon Date: Wed, 6 Sep 2023 23:35:52 +0300 Subject: [PATCH 2/3] MIR: Add messages parsing --- contrib/mir/src/rollup.rs | 1 + contrib/mir/src/rollup/kernel.rs | 43 +++++++++++++++++++++++++++++--- contrib/mir/src/rollup/types.rs | 15 +++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 contrib/mir/src/rollup/types.rs diff --git a/contrib/mir/src/rollup.rs b/contrib/mir/src/rollup.rs index 296177b158b7..bbb1dc551f66 100644 --- a/contrib/mir/src/rollup.rs +++ b/contrib/mir/src/rollup.rs @@ -6,3 +6,4 @@ /******************************************************************************/ pub mod kernel; +pub mod types; diff --git a/contrib/mir/src/rollup/kernel.rs b/contrib/mir/src/rollup/kernel.rs index 5014f6b7a907..b56c6360a634 100644 --- a/contrib/mir/src/rollup/kernel.rs +++ b/contrib/mir/src/rollup/kernel.rs @@ -6,12 +6,17 @@ /******************************************************************************/ use tezos_smart_rollup::{ + inbox::{InboxMessage, InternalInboxMessage}, prelude::{debug_msg, Runtime}, types::Message, }; +use super::types::*; + // This module follows the reference examples: https://gitlab.com/tezos/kernel-gallery +pub type Error = String; + /// The root method of the kernel. /// /// Executed every level. Can be asked from inside to be run again at the same @@ -31,7 +36,10 @@ pub fn kernel_entry(host: &mut impl Runtime) { } // TODO [#6411]: wrap into catch_unwind - process_message(host, &msg); + let res = process_message(host, &msg); + res.unwrap_or_else(|err| { + debug_msg!(host, "Processing message #{} failed: {}", msg.id, err) + }) } // The kernel gallery and some experienced devs advise to keep // reading, errors in messages reading are really unlikely here. @@ -41,6 +49,35 @@ pub fn kernel_entry(host: &mut impl Runtime) { } } -pub fn process_message(host: &mut impl Runtime, msg: &Message) { - let _ = (host, msg); +pub fn process_message(host: &mut impl Runtime, msg: &Message) -> Result<(), Error> { + let msg_id = msg.id; + let (rest, msg) = + InboxMessage::::parse(msg.as_ref()).map_err(|x| x.to_string())?; + // Likely we don't want to restrict the unparsed input for the sake of + // forward compatibility. And the reference kernels do the same thing. + debug_assert!(rest.is_empty()); + match msg { + InboxMessage::External(payload) => { + debug_msg!(host, "Message #{msg_id} - external: {payload:#x?}") + } + // [optimization] If payload is bytes, it should not be hard + // to avoid copying payload when parsing if we use our own structures. + // If it is not necessarily bytes, Nom lib still supports returning borrowed + // data and for concrete small type we won't need to write much of a + // decoding logic. + InboxMessage::Internal(in_msg) => match in_msg { + InternalInboxMessage::Transfer(transfer) => { + debug_msg!( + host, + "Message #{msg_id} - internal transfer to {} with payload: {:#x?}", + transfer.destination, + &transfer.payload.0 + ) + } + InternalInboxMessage::StartOfLevel => {} + InternalInboxMessage::InfoPerLevel(_) => {} + InternalInboxMessage::EndOfLevel => {} + }, + } + Ok(()) } diff --git a/contrib/mir/src/rollup/types.rs b/contrib/mir/src/rollup/types.rs new file mode 100644 index 000000000000..d8193a779154 --- /dev/null +++ b/contrib/mir/src/rollup/types.rs @@ -0,0 +1,15 @@ +/******************************************************************************/ +/* */ +/* SPDX-License-Identifier: MIT */ +/* Copyright (c) [2023] Serokell */ +/* */ +/******************************************************************************/ + +use tezos_smart_rollup::michelson::MichelsonBytes; + +/// What we accept in internal messages as payload (i.e. what is our parameter +/// type). +/// +/// If you change this, then in the rollup origination command you should also +/// change the `type` CLI argument respectively. +pub type IncomingTransferParam = MichelsonBytes; -- GitLab From 197cfce6b051d302a6c63fbd5a1b7bfb11316d60 Mon Sep 17 00:00:00 2001 From: martoon Date: Wed, 13 Sep 2023 13:19:12 +0300 Subject: [PATCH 3/3] MIR: Deal with unused warnings Problem: for all targets except `wasm32` I get warnings about all kernel code being unused. This happens because `kernel_entry!` macro exposes our kernel only when compiled for `wasm32`. Solution: add a dummy use of `kernel_entry` for non-wasm32 targets. --- contrib/mir/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/contrib/mir/src/lib.rs b/contrib/mir/src/lib.rs index 54051fa1e4d5..a85f197c992e 100644 --- a/contrib/mir/src/lib.rs +++ b/contrib/mir/src/lib.rs @@ -6,6 +6,16 @@ /******************************************************************************/ mod rollup; -use tezos_smart_rollup::kernel_entry; +use rollup::kernel::kernel_entry; +use tezos_smart_rollup::{kernel_entry, prelude::Runtime}; kernel_entry!(rollup::kernel::kernel_entry); + +// kernel_entry! does something only on wasm32 target, on others we should deal +// with `kernel_entry` being unused. +#[cfg(not(feature = "wasm32-unknown-unknown"))] +#[allow(dead_code)] +fn consider_kernel_used(host: &mut impl Runtime) -> ! { + let _ = kernel_entry(host); + panic!("Should not be called"); +} -- GitLab