1 of 38

Introduction to

Ewasm

Tai, Hung-Ying (hydai) @ Crosslink 2019

GitHub: @hydai

VP of Engineering, Second State

Slide: http://url.hyd.ai/LRFVT

2 of 38

Agenda

EVM bytecode recap

WebAssembly (Wasm)

Ewasm 1.0

Ewasm 2.0

www.secondstate.io

1

2

3

4

3 of 38

EVM bytecode recap

  • Stack-based virtual machine (or interpreter…)
  • 256 bit stack items
  • Too many high level instructions
    • Storage(SSTORE, SLOAD)
    • SHA3(keccak-256)
    • Call, Create Contract…
  • Too far away from actual machine architecture
  • Less language support (Vyper, Solidity, ...)

www.secondstate.io

4 of 38

How about Wasm?

  • Also, a stack-based machine but…
    • has locals ( ~= register or memory)
    • only access top 3 items from stack v.s EVM’s 16
  • Support 32/64 bit operations
  • No high level instructions
  • RISC Instruction Set, can map to a common CPU ISA
  • Large community power
    • Supported in all major browser
    • Lots of language support (C++, Rust, …)

www.secondstate.io

5 of 38

Wasm Section

www.secondstate.io

6 of 38

Wasm tooling

  • Tool
    • binaryen (a toolkit contains an interpreter in C++)
    • wabt (a toolkit contains an interpreter in C++)
    • wavm (LLVM-based JIT)
    • wagon (interpreter in Golang)
    • wasmi (interpreter in Rust)
  • Browser (hyper interpreter/JIT)
    • Chrome (V8)
    • Edge (chakra)
    • Firefox (spidermonkey)

www.secondstate.io

7 of 38

Ewasm 1.0

8 of 38

What is Ewasm 1.0

  • Ewasm ⊂ wasm
  • NOT support floating point number
  • LIMITED imports and exports (wasm section)
  • Will inject bytecode metering and has runtime metering

www.secondstate.io

9 of 38

Where did the high level instructions go? (e.g. SHA)

10 of 38

Ethereum Environment Interface (EEI)

  • EEI defines a set of Ewasm imported functions
  • With standard interface, clients can implement easier
  • Because EEI is not native instructions in bytecode
  • Quick prototyping and doing specific upgrades

www.secondstate.io

11 of 38

Definition

www.secondstate.io

12 of 38

Usage

www.secondstate.io

13 of 38

How about

Invalid instructions issues?

14 of 38

System Contracts

  • Compiled into wasm bytecode
  • Deployed on chain (like normal contract)
    • Or be part of client (like precompiles)
  • Examples:
    • Byzantium precompiles
      • sha256, ripemd160, ecrecover, modexp, …
      • Use upper-bound metering
    • Sentinel (verification and metering)

www.secondstate.io

15 of 38

Sentinel Contract

  • Before contract deployment
  • Reject non-Ewasm bytecode (e.g. floating point)
  • Insert metering statements
  • basic-block-based metering (call useGas in the beginning of block)

www.secondstate.io

16 of 38

Basic Block Metering (Injection)

www.secondstate.io

17 of 38

Ewasm Stack

www.secondstate.io

18 of 38

EVM-C

  • Connect client and VM implementation
  • Ethereum Virtual Machine includes EVM1 and Ewasm
  • With EVM-C, VM can be linked statically or loaded as plugin (.dll, .so)
  • C means C language API

www.secondstate.io

19 of 38

Performance - Sha1

parity-evm

79.3ms

geth-evm

70.7ms

life

14.9ms

evmone

6.3ms

wasmi

5.6ms

wabt

3.1ms

www.secondstate.io

20 of 38

Performance - BN128mul

life

470ms

wasmi

390ms

wabt

106ms

parity-evm

18.0ms

geth-evm

2.3ms

evmone

553.0us

www.secondstate.io

21 of 38

What happens?!!!!!

  • General wasm engine supports 32/64 bit operations
  • 256 bit operations will be simulated by 32/64 bit operations
    • ONE 256 bit operations may be replaced by 25 64 bit operations
  • As EVM provides precompiles, Wasm engine can support `bignum` library to speed up.

www.secondstate.io

22 of 38

Ewasm 2.0

23 of 38

Questions about ETH 2.0

State Rent?

Cross-shard call?

Phase 2?

DevEx???

www.secondstate.io

1

2

3

4

24 of 38

Questions about ETH 2.0

State Rent?

Cross-shard call?

Phase 2?

DevEx???

www.secondstate.io

1

2

3

4

25 of 38

Questions about ETH 2.0

State Rent?

Cross-shard call?

Phase 2?

DevEx???

www.secondstate.io

1

2

3

4

26 of 38

Questions about ETH 2.0

State Rent?

Cross-shard call?

Phase 2?

DevEx???

www.secondstate.io

1

2

3

4

27 of 38

Execution Environments (EEs)

  • “Contracts” are now called EEs
  • EEs are Wasm bytecode
  • EEs are executed on shards
  • Shards only store a short (32-byte) state root for an EE
  • On-shard execution goal is to verify the current state root and calculate the new state root based on a submitted proof and transactions

www.secondstate.io

28 of 38

Two Ewasms

  • Ewasm 1.0 - Stateful, EVM-like design, with all the legacy EVM brings
  • Ewasm 2.0 - Stateless and pure execution oriented design
  • Can launch Ewasm 1.0 on Eth 1.0 if optimising EVM in clients fails
  • Can use Ewasm 1.0 contracts within EEs on Eth 2.0

www.secondstate.io

29 of 38

Stateful V.S. Stateless

www.secondstate.io

30 of 38

Phase One and Done - Very First Step

  • Start simple - minimal execution
  • Add EEs (Now we have code fields)
  • Execute the EEs in shard blocks

  • Here comes a testing tool - Scout!!!

www.secondstate.io

31 of 38

Open Questions

  • How will cross-shard calls work?
    • Nobody knows
  • How will the Eth1 switchover work?
    • Nobody is exactly sure.
  • When Serenity?
    • January 3rd, MAYBE

www.secondstate.io

32 of 38

A MAYBE Roadmap

Phase one execution

Experiments with different EE

ETH1-esque environment with EVM/Ewasm

ETH1 -> ETH2 switchover

www.secondstate.io

1

2

3

4

33 of 38

What is Scout?

  • An Eth 2.0 execution prototyping framework
  • Based on “ETH 2.0 Phase 2 Proposal 2”

  • Defines an API for the EEs (the EEI)
  • Defines a test format (YAML format)
  • Provides a tool which can execute on that format
  • Provides example EEs

www.secondstate.io

34 of 38

EE(I)

  • eth2::loadPreStateRoot(memoryOffset: u32ptr)
  • eth2::blockDataSize() -> u32
  • eth2::blockDataCopy(memoryOffset: u32ptr, offset: u32, length: u32)
  • eth2::savePostStateRoot(memoryOffset: u32ptr)
  • eth2::pushNewDeposit(memoryOffset: u32ptr, length: u32)

www.secondstate.io

35 of 38

EE: Hello World (in Rust)

www.secondstate.io

36 of 38

Playground

  • SOLL - Compile your Solidity Smart Contract into Ewasm via LLVM
  • Scout - Ethereum 2.0 Phase 2 execution prototyping engine

www.secondstate.io

37 of 38

Reference

www.secondstate.io

38 of 38

Thanks!