Marigold
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: Kernel in SCORU tags: Kernel --- # Knowledge - Kernel in SCORU ## Gather knowledge - Big picture about SCORU and kernel :::spoiler - A big picture of SCORU A talk by Yann and Hans about scaling Tezos with rollups: https://www.youtube.com/watch?v=SRDlaAhhKBY ![](https://hackmd.io/_uploads/ryfs2lWBj.png) - The relationship of the two VMs: - Use Fast VM by default [^1] - Take snapshots - every 20s - In case of a refutation, replay from a snapshot on the PVM - Key elements to build on top of Optimistic rollups ![](https://hackmd.io/_uploads/H1lwHHAxrs.png) - A big picture of environments will be available on Optimistic Rollups ![](https://hackmd.io/_uploads/B1DW50eSs.png) ::: [^1]: Design document: https://hackmd.io/dkiwun7JQnCgHjuVbmtX_Q - What is "WASM" - WebAssembly PVM? - It is a low-level assembly-like language. - A Wasm rollup runs a Wasm program named a "kernel". - This wasm desiged as a compilation target for other languages. - A WebAssembly engine is responsible for parsing and instantiating modules, maintaining the state of module instances, and executing instructions, either interpretation or compilation. - Why choose WASM as the programming language for smart rollups? - One of the reason is because it has gradually become an ubiquitous compilation target over the year. - Go or Rust now natively compile to WASM. - `cargo` - the official Rust package manager, provides an official target to compile `.wasm` binary file that are valid WASM kernel. - What is "PVM"? - A PVM stands for *Proof-generating Virtual Machine*. This PVM is implemented in the Tezos protocol. It's a slightly modified virtual machine that can **output a proof that operations have been processed corrected**. The rollup node can use this implementation to produce a proof and post it to Layer 1, where it will be checked by the Layer 1 node. - What is "kernel" in SCORU?[^2] - A kernel can be seen as an operation system, it can be anything, for instance: a kernel EVM engine - Ethereum Virtual machine, enabling Solidity smart contracts, or a kernel transaction engine - focus on transactions of assets ![](https://hackmd.io/_uploads/BJtLyT1Hj.png) - From a Layer 1 point of view, the kernel acts like a smart contract, interacting with its host through a Rollup Management Protocol, which lets it accept deposits, effect withdrawal, self-upgrade and so on. [^2]: Reference from NL post: https://research-development.nomadic-labs.com/next-generation-rollups.html - Recap: the relationship of kernel, wasm, pvm - What is the role of a "kernel" in SCORU? - The role of the kernel is to process input messages, to update a state, and to output messages targetting the Layer 1 following a user-defined logic. - What are the "rules" for kernel that can be complied with the WASM?[^3] - Some instructions and types of the WASM language are forbidden, specially the one related to floating-point arithmetic. - The call stack of the WASM kernel is restricted to 300. A valid kernel statisfies the following constraints: - It exports a function `kernel_next` that takes no arguments and returns nothing. - It declares and exports exactly one memory. - It only imports the host functions, exported by the (virtual) module `rollup_safe_core`. [^3]: Reference from this MR: https://gitlab.com/tezos/tezos/-/merge_requests/6629 - What is the Rust bindings/host to the Wasm PVM - Rust SDK? - The Host capabilities is provided by the VM. Used by kernels. - An example of a minial kernel using the host capabilities :::spoiler ![](https://hackmd.io/_uploads/rJRPtCxBo.png) ::: - This host bindings is like an API, for instance, if you want to send an asset from the rollup to a Layer 1 address, there is a ready-made function for that. - With these bindings, developers can write a kernel, compile it to WebAssembly, and know that its interaction with the Tezos blockchain will be reliable and secure. - It is written in [Rust](https://docs.substrate.io/fundamentals/rust-basics/) because it is a popular language with a mature toolchain providing robust compilation to Wasm. Diagram of the kernel repo (maybe out of date), but good to add here to have a general picture :::success :::spoiler ![](https://hackmd.io/_uploads/B1oXBbLHi.png) ::: Host bindings implementation: :::success :::spoiler Details Source code of this bindings is at: https://gitlab.com/tezos/kernel/-/tree/main/host and its doc is at https://tezos.gitlab.io/kernel/doc/host/index.html This define the SCORU wasm host function. The host exposes "safe capabilites" as a set of C-style APIs. The `host` crate defines these as `extern` functions (see [`rollup_core`](https://tezos.gitlab.io/kernel/doc/host/rollup_core/index.html)) and is reponsible for providing safe wrappers which can be called from **safe rust**. - [input](https://tezos.gitlab.io/kernel/doc/host/input/index.html): the possible types that may be returned by (Runtime) `crate::runtime::Runtime` when reading an input - [path](https://tezos.gitlab.io/kernel/doc/host/path/index.html): enforcing correct encoding of [storage paths](https://tezos.gitlab.io/kernel/doc/host/path/trait.Path.html). A storage path can be written to, or read from the kernel - and may correspond to a sequence of bytes in the runtime storage. - [rollup_core](https://tezos.gitlab.io/kernel/doc/host/rollup_core/index.html): defines the *raw* bindings to the **rollup_safe_core** host module. These can be accessed by a kernel running in safe mode - which prevents the kernel messing up the state tree w.r.t hardware gas limits, and inputs. - [wasm_host](https://tezos.gitlab.io/kernel/doc/host/wasm_host/index.html): implementation of `RawRollupCore` that used when compiling to wasm. ::: Example of some use cases of kernel in SCORU at the moment: :::success :::spoiler Tx-kernel A simple kernel with transaction functionality similar to TORUs: https://tezos.gitlab.io/kernel/doc/kernel_core/index.html. e2e tests covering the top-level behaviour of the transactions kernel: https://gitlab.com/tezos/kernel/-/blob/main/e2e_kernel_tests/tests/tx_kernel.rs. The milestone of warm kernel transactions: https://gitlab.com/tezos/tezos/-/milestones/86#tab-issues - [bls](https://tezos.gitlab.io/kernel/doc/kernel_core/bls/index.html): BLS support for the kernel. Provides signature verification and de-serialization for BLS signatures and public keys. - [deposit](https://tezos.gitlab.io/kernel/doc/kernel_core/deposit/index.html): deposit tickets into the kernel state - [encoding](https://tezos.gitlab.io/kernel/doc/kernel_core/encoding/index.html): defines tezos-encoding compabible structures. - [inbox](https://tezos.gitlab.io/kernel/doc/kernel_core/inbox/index.html): types and encoding for the *inbox-half* of the L1/L2 communication protocol. In general, this module is a re-implementation of the tezos protocol [inbox message repr](https://gitlab.com/tezos/tezos/-/blob/9028b797894a5d9db38bc61a20abb793c3778316/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.mli). - memory: defines operations over kernel memory - persisted in RAM between yields - [outbox](https://tezos.gitlab.io/kernel/doc/kernel_core/outbox/index.html): types and encodings for the *outbox-half* of the L1/L2 communcation protocol. Similar as inbox, this module is a re-implementation of the tezos-protocol [outbox message repr](https://gitlab.com/tezos/tezos/-/blob/80b2cccb9c663dde2d86a6c94806fc149b7d1ef3/src/proto_alpha/lib_protocol/sc_rollup_outbox_message_repr.ml) - transactions: transactions kernel core logic for handling messages - tx_kernel: define the `kernel_next` for the transaction kernel :::info :::spoiler Examples of using Host in Tx-kernel - deposit ticket function: deposits ticket into account. Returns error if amount is negative, or if the amount is greater than it can fit in the account. Where [`Host: RawRollupCore`](https://tezos.gitlab.io/kernel/doc/host/rollup_core/trait.RawRollupCore.html) is defined in `wasm_host.rs` ``` pub fn deposit_ticket<Host: RawRollupCore>( memory: &mut Memory, account_address: Layer2Tz4Hash, ticket: StringTicket ) -> Result<(), DepositError> ``` - Transactions: - Function [`transactions_run`](https://tezos.gitlab.io/kernel/doc/src/kernel_core/lib.rs.html#48-70) the entrypoint of the *transactions* kernel (like main function for the tx-kernel). It is where it calls the host modules for the implementation, such as: `input`, `rollup_core`, `runtime` ``` pub fn transactions_run<Host: RawRollupCore>(host: &mut Host) { ... if let Some(input) = host.read_input (MAX_READ_INPUT_SIZE){ match input { Input::Message(message) => { ... } Input::Slot(_message) => todo! } } } ``` - external inbox: prepare an external message for processing, in a stepwise manner. ``` pub fn prepare_for_processing<'a, Host>( message: ExternalInboxMessage<'a>, accounts: &mut Accounts ) -> Option<impl FnMut(&Accounts) -> ProcessedOutcome + 'a> where Host: RawRollupCore, ``` - handle withdrawals: send given withdrawals to Layer 1, in an OutboxMessage ``` pub fn handle_withdrawals<Host: RawRollupCore>( host: &mut Host, memory: &mut Memory, withdrawals: Vec<Withdrawal> ) ``` - Using the [`kernel`](https://tezos.gitlab.io/kernel/doc/kernel/index.html): `kernel_entry` macros derive `kernel_next` and `mock_kernel_next` entrypoints. - [`tx_kernel`](https://tezos.gitlab.io/kernel/doc/kernel_core/tx_kernel/index.html): define the `kernel_next` for the transactions kernel. It defines inside the function `transactions_run` above. - `kernel_next`: function is called by the wasm host at regular intervals. ``` pub fn kernel_next() #[cfg(feature = "tx-kernel")] pub mod tx_kernel { use crate::transactions_run; use kernel::kernel_entry; kernel_entry!(transactions_run); } ``` - `mock_kernel_next`: is called by the [mock_host](https://tezos.gitlab.io/kernel/doc/mock_host/index.html) at regular intervals. Mock runtime provides a host that can used in integration and unit tests. Used when not compiling to wasm. ``` pub fn mock_kernel_next(host: &mut MockHost) ``` ::: :::success :::spoiler EVM-kernel https://tezos.gitlab.io/kernel/doc/evm_kernel/index.html. This contains the EVM kernel, this kernel runs EVM contract code emulating Ethereum, but on a rollup. e2e tests for the EVM-kernel: https://gitlab.com/tezos/kernel/-/blob/main/e2e_kernel_tests/tests/evm_kernel.rs. The milestone of evm on wasm: https://gitlab.com/tezos/tezos/-/milestones/108#tab-issues - ethereum: types and functions for Ethereum compapility - deposit: deposit tickets into the kernel state - inbox: types and encodings for the *inbox* for the EVM kernel - memory: defines operations over kernel memory - persisted in RAM between yields - outbox: types and encodings for the *outbox-half* of the L1/L2 communication protocols - transactions: handle transactions - evm_kernel: define the `kernel_next` for the transactions kernel ::: Details design of kernel in SCORU: :::info Execution environment :::spoiler - State: the smart rollups have two states - A transient state: it is reset after each call to the `kernel_next` function and is similar to RAM. - A persistent state: it is preserved across `kernel_next` calls. It consists: inbox, and outbox, and a durable storage which is similar to a file system. - A WASM kernel can write/read raw bytes stored under a given path (files). It can also delete/copy/move/etc. with subtrees (directories). - The value and subtress at key `read_only` are not writable by a kernel, but it can be used by the PVM to give information to the kernel. - Control Flow: - WASM kernel has to filter the inputs (the inbox exposed to the smart rollup is populated with all the inputs published on Tezos in this block). - After the inbox has been populated with the inputs of the Tezos block, the `kernel_next` is called from a "transient state". - The WASM kernel is parsed, linked, initialized, then `kernel_next` is called. - By default, `kernel_next` is called only once. - Host functions: - The host functions provides an API to the WASM program to interact with an "outer world". - The host functions exposed to a WASM kernel allow it to interact with the various components of "persistent state": - read_input - write_output - write_debug - store_has - store_delete - store_copy - store_move - store_read - store_write - store_value_size - store_list_size - store_get_nth_key - reveal_preimage - reveal_metadata - These host functions use a "C-like" API, most of them return a signed 32bit integer, where negative values are reserved for conveying errors. ::: :::info Implementing a WASM kernel in Rust :::spoiler - Setting up Rust - Host functions in Rust ::: :::info Testing the kernel :::spoiler ::: :::info :::spoiler Reference from this MR: https://gitlab.com/tezos/tezos/-/merge_requests/6629 ::: Some on-going milestones: :::info :::spoiler - EVM on Wasm: run EVM interpreter from kernel: https://gitlab.com/tezos/tezos/-/milestones/108#tab-issues - Wasm kernel IV: upgrade path https://gitlab.com/tezos/tezos/-/milestones/139#tab-issues - Wasm kernel V: Beta kernel framework https://gitlab.com/tezos/tezos/-/milestones/140#tab-issues - SCORU WebAssembly PVM I: https://gitlab.com/tezos/tezos/-/milestones/83#tab-issues - SCORU wasm kernel (core): https://gitlab.com/tezos/tezos/-/milestones/85#tab-issues: the goal is to create a minimal infrastructure/library for building a wasm kernel in rust. (The bindings APIs rust above). - Wasm kernel (transactions): https://gitlab.com/tezos/tezos/-/milestones/86#tab-issues: this is a build on SCORU wasm kernel (core) to create a proof-of-concept WASM kernel, able to carry out TORU style exchanges with signature checking. - Wasm kernel III (DAL integration): https://gitlab.com/tezos/tezos/-/milestones/116#tab-issues: this is a build on the wasm kernel (transactions) to provide integration with the DAL layer for both the `TX_kernel` and future `EVM kernel` :::spoiler Glossary in SCORU - message: is a mere sequence of bytes following no particular underlying format. The interpretation of this sequence of bytes is the responsibility of the kernel. - inbox: is a sequence of messages from the Layer 1 to smart rollups. The contents of the inbox is determined by the consensus of the Tezos protocol. - outbox: is a sequence of messages from a smart rollup to the Layer 1. Messages are smart contract calls, potentially containing tickets. These calls can be triggered only when the related commitment is cemented (hence, at least two weeks after the actual execution of the operation) :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully