Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. Agenda - LINE Blockchain Developers’ Today And Future - Why

    WASM Is Suitable For Smart Contracts - Smart Contracts and WASM Today - About VM (by Takahashi)
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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))
  15. Language Independent (Friendly) Developers can make smart contract with common

    languages ーーーーーー ーーーーーー ーーーーーー C++ WASM binary Rust Other Languages ーーーーーー ーーーーーー ーーーーーー ーーーーーー ーーーーーー ーーーーーー
  16. 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
  17. Runtime Executes Smart Contracts Smart Contracts Runs on the sidecar

    VM Blockchain Smart Contract VM Code Call Deploy Developer Users
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. Smart Contracts Execution Flow VM Runtime Code(C++) Code(Rust) ... Compiler

    (Interpreter) Machine code WASM Byte code build frontend backend
  24. 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
  25. Compiler Backend Singlepass Cranelift LLVM IR Not use Use Use

    Compile Speed ◎ ◦ △ Execute Speed △ ◦ ◎
  26. Is Compiler better? Developer Interpreter is so slow… Compiler is

    better. LLVM is the fastest ! Okay, let‘s use LLVM!!
  27. 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...
  28. 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)
  29. Performance What's your Priority? It's too slow!! Runtime to Compile

    is preferred over interpreters (e.g., Wasmer).
  30. Privacy What's your Priority? PRIVATE !! If you want to

    use TEE(※) for confidentiality, use a Runtime that supports no_std. ※ trusted execution environment
  31. Security What's your Priority? If security is your primary concern,

    aim for strict determinisitic.(e.g., wasmi). Is it down?
  32. 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
  33. LINE Blockchain’s VM LINE Blockchain Priority Performance Runtime Wasmer (Singlepass

    Compiler) Goal Improve the performance even more. Continue to optimize Singlepass.
  34. 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