$30 off During Our Annual Pro Sale. View Details »

Run Programs in LINE Blockchain as Well - WebAssembly Useful outside Web

Run Programs in LINE Blockchain as Well - WebAssembly Useful outside Web

Shiki Takahashi
LINE / Blockchain Engineering Team / Software Engineer
Ryo Takase (loloicci)
LINE / Blockchain Engineering Team / Software Engineer

https://linedevday.linecorp.com/2021/ja/sessions/13
https://linedevday.linecorp.com/2021/en/sessions/13
https://linedevday.linecorp.com/2021/ko/sessions/13

LINE DEVDAY 2021

November 11, 2021
Tweet

More Decks by LINE DEVDAY 2021

Other Decks in Technology

Transcript

  1. View Slide

  2. Solomon Hykes on Twitter
    https://twitter.com/solomonstre/status/1111004913222324225

    View Slide

  3. Agenda
    - LINE Blockchain Developers’ Today And Future
    - Why WASM Is Suitable For Smart Contracts
    - Smart Contracts and WASM Today
    - About VM (by Takahashi)

    View Slide

  4. LINE Blockchain Developers’
    Today And Future
    Smart Contract Enables Executing Your Business Logic on Blockchain

    View Slide

  5. LINE Blockchain Developers (LBD)
    LBD supports your service with LINE Blockchain

    View Slide

  6. Tutorial Example: Mage Duel
    NFT Trading Card Games Service In LINE Blockchain
    - Each card is a NFT (Non-
    Fungible Token)
    - Ruby is FT (Fungible Token)
    - User can buy cards with ruby
    - Cards in shops is randomized
    - Users can re-randomize cards
    with ruby

    View Slide

  7. Re-Randomizing Cards in Shop
    Randomizing is proceeded off-chain, payment is proceeded on-chain
    - Payment is proceeded on-chain
    - Re-randomizing is executed in
    game server (off-chain)
    - LBD cannot support the
    randomizing, now
    - It cannot be represented with
    APIs, now
    - Better if it is on-chain
    - Service can show its fairness
    - User can see the process
    Server
    Client LINE Blockchain
    Request randomizing
    Show the shop
    Payment of ruby
    Execute randomizing
    Is it fair
    initializing?
    How to show
    the fairness?
    Cannot record & show
    initializing executions

    View Slide

  8. Smart Contract
    Proceed your business logic on-chain
    - Developers deploy own program
    - Users call the program
    - Code are executed by VM
    - The chain records executions

    View Slide

  9. Requirements: Smart Contract
    What is needed to available your program on Blockchain
    Do not threaten the chain
    and node machines
    Safe
    Every node can execute code
    and gets the same result
    Executable
    Deterministic
    Do not avoid other
    transactions
    Effective
    WebAssembly takes attentions because it meets these requirements

    View Slide

  10. Why WebAssembly Is
    Suitable For Smart Contracts
    WebAssembly meets smart contract’s technical requirements

    View Slide

  11. WebAssembly (WASM)
    WebAssembly makes WebApps more efficient
    - Compiled from C++/Rust/etc…
    - Binary formed executable file
    - Runs on VM (browser’s sidecar)
    - High performance as same as machine native operations
    - 3D games e.g.) d3wasm
    - Image processing libraries e.g.) photon

    View Slide

  12. WebAssembly
    WebASsembly corporates with JS and calculate complex parts
    Traditional Web Apps Web Apps Using WebAssembly
    HTML
    CSS
    JavaScript
    HTML
    CSS
    JavaScript WebAssembly
    Executing programs as
    efficient as host native

    View Slide

  13. Abstract
    WebAssembly’s strong points meet the requirements of smart contract
    Excutable
    Deterministic
    Effective Safe Developer Friendly
    Requirements of Smart Contract
    Strong Points of WebAssembly
    Portable Fast and Efficient Safe
    Language
    Independent
    Deterministic API Usable
    Open and
    Debugable

    View Slide

  14. Portable (Executable)
    WASM contracts run as same regardless of node hosts
    Develop Runs on
    e.g.) LINUX-Intel
    Any platform
    ARM
    Intel
    LINUX
    ARM
    Intel
    Windows
    M1
    Intel
    MacOS

    View Slide

  15. Deterministic (Deterministic)
    With a few restrictions, WASM contracts are deterministic and do not avoid consensus
    “nondeterministic execution can only occur in a small number of
    well-defined cases”
    Quoted from
    https://github.com/WebAssembly/design/blob/main/Nondeterminism.md

    View Slide

  16. Efficient and Fast (Efficient)
    WASM smart contracts are expected not to take too much time
    “Efficient: can be decoded, validated, and compiled in a fast
    single pass, equally with either just-in-time (JIT) or ahead-of-
    time (AOT) compilation.”
    Quoted from
    https://webassembly.github.io/spec/core/intro/introduction.html

    View Slide

  17. Safe (Safe)
    WASM runs in a sandbox and cannot threaten blockchain node
    Host PC
    Sard box
    virtual
    memory
    use
    process
    Cannot
    threaten
    memory
    disc
    WASM

    View Slide

  18. Open: API Usable (Safe, Friendly)
    WASM can import host/blockchain specified functions via API which the VM provides
    WASM binary
    Header
    using API α
    using API β
    ...
    ...
    ...
    ...
    ...
    ...
    ...
    call α
    call β
    Body (Logics)
    APIs via VM
    ...
    Read File A
    Write File A

    View Slide

  19. Readable and Debuggable (Safe, Friendly)
    WASM has readable text form and it helps validation and debugging
    (module
    (func (param $lhs i32) (param $rhs i32) (result i32)
    local.get $lhs
    local.get $rhs
    i32.add))

    View Slide

  20. Language Independent (Friendly)
    Developers can make smart contract with common languages
    ーーーーーー
    ーーーーーー
    ーーーーーー
    C++
    WASM binary
    Rust Other Languages
    ーーーーーー
    ーーーーーー
    ーーーーーー
    ーーーーーー
    ーーーーーー
    ーーーーーー

    View Slide

  21. Summary
    WebAssembly is suitable for smart contracts and LINE Blockchain plans to introduce it
    Excutable
    Deterministic
    Effective Safe Developer
    Friendly
    Requirements
    of Smart Contract
    Strong Points
    of WebAssembly
    Portable
    Fast and
    Efficient Safe
    Language
    Independent
    Deterministic API Usable
    Open and
    Debugable
    - WASM is suitable for smart contracts
    - Suitable for some other non-web, too
    (e.g. embedded programs)
    - LINE Blockchain plans to support
    WASM as smart contracts
    - Services can use own logics in LINE
    Blockchain with this
    - WASM enables developing smart
    contract with C++/Rust

    View Slide

  22. Smart Contracts And WASM
    Today
    VMs, WebAssembly, and Ethereum

    View Slide

  23. Runtime Executes Smart Contracts
    Smart Contracts Runs on the sidecar VM
    Blockchain
    Smart Contract
    VM
    Code
    Call
    Deploy
    Developer
    Users

    View Slide

  24. Popular Smart Contract Runtime
    Almost blockchain with smart contract uses the EVM or a WASM VM
    WASM VM
    Cloned/Forked EVM
    - Developed for Ethereum
    - Using original format binary
    - There are some DSL for binary e.g.) solidity
    Pros
    - Ethereum tools are usable for development
    - Ethereum contracts are usable
    Cons
    - Chain needs to compatible with Ethereum-
    specified functions (e.g. get block info)
    - Non-blockchain-specified VMs are usable
    - VM for WASM
    - Common languages (C++/Rust) are usable
    Pros
    - Common languages are usable
    - Non-blockchain-specified tools are usable
    - There are many VM and technical options
    Cons
    - Needs restrictions for deterministic/metering
    - Needs to provide blockchain-specified API

    View Slide

  25. Ethereum Aims WASM Contracts: eWASM
    eWASM: the subset of WASM with restrictions for smart contracts and compatibility with traditional EVM
    Required Specification
    - Restricting non-deterministic operators
    - Supporting VM’s blockchain specified functions via API
    - Able to meter execution cost (gas)
    Language Support
    - Providing API interface libraries for C++
    - Providing API interface libraries for rust
    Ethereum-Specified Restrictions
    - Compatible with traditional EVM code
    - Able to be checked with security contract – “sentinel contract”
    - Support compiling from solidity, the most popular Ethereum smart contract language

    View Slide

  26. Summary
    WebAssembly is hot in blockchain and smart contract
    - WebAssembly’s strong points suits smart contracts’ requirements
    - Chains with smart contract pays attention to WebAssembly
    LINE Blockchain aims to support WASM as smart contracts
    - Services can use own business logics in LINE Blockchain
    - Smart contracts can be developed with C++/Rust and their tools

    View Slide

  27. VM into LINE Blockchain
    The Road to Using WASM as a VM for Smart Contract

    View Slide

  28. Things to consider when implementing VM
    Executable
    Deterministic
    Effective Safe Developer
    Friendly
    Requirements
    of Smart Contract
    Strong Points
    of WebAssembly
    Portable
    Fast and
    Efficient Safe
    Language
    Independent
    Deterministic API Usable
    Open and
    Debugable

    View Slide

  29. Things to consider when implementing VM
    To make it easier to
    adapt to change
    Scalability
    Developer friendly
    Language
    Independent
    To focus on the
    global market
    Fast and
    Efficient

    View Slide

  30. Smart Contracts Execution Flow
    VM
    Runtime
    Code(C++)
    Code(Rust)
    ...
    Compiler
    (Interpreter)
    Machine code
    WASM
    Byte code
    build
    frontend backend

    View Slide

  31. Runtime
    Compiler /
    Interpreter
    Deterministic
    Support
    no_std

    View Slide

  32. Compiler
    ※ IR(Intermediate Representation): A data format used between the conversion of source code into machine language that is
    finally executed by the CPU.
    Run (from the 2nd time)
    WASM
    Byte code
    Run (first)
    Machine
    Machine
    Code
    JIT Compiler
    IR (※) CPU
    Compile
    WASM
    Byte code
    Machine
    Machine
    Code
    AOT Compiler
    IR CPU
    Compile
    JIT Compile
    AOT Compile
    Run

    View Slide

  33. Compiler Backend
    Singlepass Cranelift LLVM
    IR Not use Use Use
    Compile Speed ◎ ○ △
    Execute Speed △ ○ ◎

    View Slide

  34. Interpreter
    WASM
    Byte code
    Machine
    Interpreter CPU

    View Slide

  35. Is Compiler better?
    Developer
    Interpreter is so slow…
    Compiler is better.
    LLVM is the fastest !
    Okay, let‘s use LLVM!!

    View Slide

  36. Avoid JIT Bomb
    VM
    Runtime
    Code Compiler
    Machine
    code
    “Twemoji” ©Twitter, Inc and other contributors (Licensed under CC-BY 4.0)
    Developer
    Compile is not finish...

    View Slide

  37. Compiler Backend
    Singlepass Cranelift LLVM
    IR Not use Use Use
    Compile Speed ◎ ○ △
    Execute Speed △ ○ ◎
    Complexity and
    amount of code
    △ × ×
    Can Blockchain
    use ?

    (Need a strict
    security audit)
    ×
    (Difficult to check security audit on
    compilers)

    View Slide

  38. Which Runtime to choose?
    Wasmer
    wasmi
    wasmtime
    Lucet

    View Slide

  39. Performance Privacy Security
    What's your Priority?
    Compiler /
    Interpreter
    Support no_std Deterministic

    View Slide

  40. Performance
    What's your Priority?
    It's too slow!!
    Runtime to Compile is preferred over interpreters (e.g., Wasmer).

    View Slide

  41. Privacy
    What's your Priority?
    PRIVATE !!
    If you want to use TEE(※) for confidentiality, use a Runtime that supports no_std.
    ※ trusted execution environment

    View Slide

  42. Security
    What's your Priority?
    If security is your primary concern, aim for strict determinisitic.(e.g., wasmi).
    Is it down?

    View Slide

  43. What’s their Priority?
    Holochain Secret Network Polkadot
    Priority Performance Privacy Security
    Runtime
    Before
    wasmi
    (interpreter)
    Wasmer
    (not support no_std)
    wasmi
    (Strict deterministic)
    After
    Wasmer
    (Singlepass Compiler)
    wasmi
    (support no_std)
    (V8 or
    SpiderMonkey)
    Effect
    Increased execution
    speed
    Smart Contracts can
    be executed with TEE
    Not yet

    View Slide

  44. LINE Blockchain’s VM
    LINE Blockchain
    Priority Performance
    Runtime Wasmer (Singlepass Compiler)
    Goal Improve the performance even more.
    Continue to
    optimize Singlepass.

    View Slide

  45. Summary
    - It is necessary to prepare Runtime to use WASM as the execution module
    of Smart contract.
    - There are several points to consider when deciding on a Runtime, and what
    to adopt depends on your priorities.
    Mission : To create a Smart contract VM with WASM

    View Slide

  46. Thank you

    View Slide