Title Text

Writing Ewasm in Rust

用 Rust 编写Ewasm 合约

Antonio Yang

yanganto@github

Ewasm Contract

WHAT, HOW & WHY ?

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

What

Ewasm  = Ethereum WebAssembly

WebAssembly is an open standard that defines a portable binary-code format for executable programs, and a corresponding textual assembly language.
Ethereum is a decentralized, open-source blockchain with smart contract (web3) functionality.

How

  • A deterministic subset of WebAssembly.
第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Why

Any language with HTTP protocol

Web2 Server

API

EVM

Web3 Blockchain

Restricted

Any language compile to EWasm format

WebAssembly

WHY RUST ?

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

What is Rust ?

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Why Rust ?

  • low-level control and reliable performance.
  • free from the non-deterministic garbage collection
  • no runtime, enabling small binary sizes

Web 3

BLOCKCHAIN

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Web ?

Web3 Blockchain

Talk to contract in Binary over the blockchain network

Web2 Server

API

Talk to API in Hyper Text  Markup Language over the internet

API

API

API

API

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Web 3

Web

Blockchain (Web3)

Handler(s)

Main entry

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Routing

In web2, Uniform Resource Identifier(URI) for routing

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Routing

In web3, functional signature for routing

  • 4 bytes
  • Optional attribute
  • Hash(fn_name(type,))
第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Same Backend Idea

Handlers

Main function

(entry point)

Prior Art

CONTRACT IN RUST

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Contract in Rust

ewasm-rust-api

  • Version 0.11.0

  • 2019 Nov 27

.rs

Executor

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

ewasm-rust-api

ERC20 in Rust

but ... not really

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Pain Point

  • Not really Rusty API
    • Result

    • Option

  • Operating in low-level
    • operate on Bytes32

    • Manually handle functional signature
  • Not easy to test with cargo test
第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

C vs Rust

 

pub enum CallResult {
    Successful,
    Failure,
    Revert,
    Unknown,
}
pub enum CallError {
    Failure,
    Revert,
    Unknown,
}

fn call() -> CallResult;

fn call() -> Result<(), CallError>;

Please read things behind the wordings!

Current Status

DIFFERENT FROM WEB

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Different in Functions

Constructor

Handlers

Main function

(entry point)

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Constructor

  • Another wasm executable
  • Run once
  • Return main wasm executable
  • Not storage on chain
第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Different in Storage

  • File server
  • Key value database
  • Relational database
  • Bytes 32
  • ?
  • ?

Web

Blockchain (Web3)

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Different in Test

  • Local server
  • Hot reload
  • Mock request
  • Free to request
  • Fuzz test
  • Deploy on test net
  • ?
  • ?
  • Request with fee?
  • ?

Web

Blockchain (Web3)

Writing Ewasm In Rust

SEW UP EWASM

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Sew Up

ewasm-rust-api

sewup

.rs

Web3 Blockchain

  • Rusty API
  • high-level operation (kv, rdb, ...)
  • cargo test in local
  • cargo sewup to build & deploy
  • Low-level operation
  • Byte32 operation
第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

Sew Up

Tests

Create WasmEdge runtime,

Add _compile_constructor_test,

Add _compile_runtime_test

Run each test in runtime

Use macro to add common test utilities,

then just cargo test

SewUp

HAPPY HACKING

THANK YOU

QUESTIONS?

第六届中国开源年会 COSCon'21 开心开源 Happy Hacking

扫码关注

开源社公众号

Let's meet on github or hackthon,

issue, PR are welcome