Writing Ethereum

Rust Engineer

Antonio

yanganto@Github

2021/10/26 Hackthon @OnAir

Wasmassembly with SewUp

Hackathon

What is SewUp

  • Rust
  • Open source at Github
  • Contract Dev as Backend
  • Utilize Ethereum WebAssembly

Benefit from Rust

  • Safe
  • Performance
  • Small size

What is Ethereum Webassembly?

  • Aka, eWasm
  • redesign of the Ethereum smart contract execution layer

Relation between Wasm

  • A deterministic subset of WebAssembly.

What is contract in Web?

Application layer of web3

EVM

API

API

API

Web2 Server

Web3 Blockchain

  • ewasm-rust-api
  • Version 0.11.0 (2019 Nov 27)

 Contract in Rust

Executor

.rs

ERC20 with ewasm Rust api

There is a gap !

No operation on bytes in Web 2

Pain point eWasm with Rust

  • Not really Rusty API
    • Result

    • Option

  • Operating in low-level
    • operate on Bytes32

    • Manually handle functional signature
  • Not easy to test with cargo test

Executor

.rs

This is C

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

This is Rust

pub enum CallError {
    Failure,
    Revert,
    Unknown,
}

fn call() -> CallResult;

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

Please read things behind the wordings!

Web 2 Web 3
Business level Web application Blockchain application
Handler level Rocket SewUp
Byte level
hyper ewasm-rust-api

Let's Sew up

How to Sew Up

  • Security
  • High level development
  • Same develop experience with web2

Rust

Contract Dev as Backend

The detail goal of Sewup

ewasm-rust-api

sewup

  • Rusty API
    • Result

    • Option

    • Error

  • high level operation
    • like handle KV db, RDB

    • functional signature macro
  • Easy to test
  • Easy to deploy

.rs

Web3 Blockchain

Error

  • Customize error
  • Good error message
  • Easy to use

match your function

Like develop traditional web

Rust style result

Rust Style

structure

bytes

Easy for

Developing

string

store without touching Bytes32

Let's SewUp

Rusty Development

  • Initialized project - cargo sewup init

  • Run test - cargo test
  • Deploy contract - cargo sewup
  • (Optional) Interact with contract - cargo run

Initialized project

  • Install cargo sewup
    cargo install cargo-sewup

Initialized project

  • Initialize project to a specific folder
    • cargo sewup init -p /path/to/your project
  • Initialize project in current folder
    • cargo sewup init -m <default|rusty|auto>

.
├── .cargo
│         └── config.rs
├── Cargo.toml
├── sewup.toml
└── src
          └── lib.rs

Set up Rust flags fro wasm

Deploy information

(git ignored)

Write the contract as library

Develop & Test

Constructor

Handlers

Main function

(entry point)

Tests

Constructor

  • Only run once when the contract deploy
  • There is no return value
  • For example, table schema can be created here

Handler & Main

  • Web 2 main fn route URI for handlers
  • Web 3 main fn route Signature for handlers

Auto generate HELLO_SIG

will be  HELLO_SIG

Learn more from Rust doc

Run Test in test runtime

Learn more from Rust doc

  • Add #[wasm_test] attribute
  • cargo test
  • Test case will be run in WasmEdge

Quick

View

Inputs

Inputs for handler

  • from contract developer
    • compile time defined inputs
  • from contract caller
    • runtime inputs

Compile time defined inputs

  • from contract developer
    • compile time defined inputs
    • Never changed

Runtime Inputs

  • from contract caller
    • runtime inputs
    • binary passing from client
    • Should be Deserialize
    • One input
  • ewasm_input_from!(contract move handler)

Outputs

Outputs

  • Default
    • thiserror message
  • Rusty
    • return a Result
    • customized error
  • Auto
    • return T for Result<T>
    • none-rust client

Outputs

  • Default
    • thiserror message
  • Rusty
    • return a Result
    • customized Error
  • Auto
    • return T for Result<T>
    • none-rust client

Outputs

  • Default
    • thiserror message
  • Rusty
    • return a Result
    • customized Error
  • Auto
    • return T for Result<T>
    • none-rust client

Output with different type

  • use sewup::primitives::EwasmAny

  • use auto mode for ewasm_main

EwasmAny

  • A wrapper type for output binary
  • It is easy to covert from any type with Serialize Trait
  • check out the Rust doc

Tests

Test Macro

Learn more from Rust doc

Create WasmEdge runtime,

Add _compile_runtime_test,

Add _compile_contract_test

Run test in WasmEdge runtim

Assert Macros

  • Macros help you run test case in runtime

Learn more from Rust doc

  • `by` run test case with specific account

Debug in test runtime

  • sewup::ewasm_dbg!
  • set log #[ewasm_test(log=/tmp/default.log)] on test mod
  • run test with --nocapture option
    • cargo test -- --nocapture

The message you debug on

The message you debug on

Caller Address

Flag section preserved not used currently

Deploy

Ewasm build & devploy flow

  • Contractor wasm execute once and return runtime wasm and storage it on chain
  • Provide keys, url in sewup.toml
  • `cargo sewup`

Interact

Generate abi.json

  • ERC tokens can use web3.js client with abi.json
  • `cargo sewup --generate-abi`
  • Rust client is also possible, please checkout client.rs in examples

Examples

Examples are listed here and will be covered in future events.

 

 

 

 

 

 

 

It is always welcome if you move on these examples and hack much deep!

  • Voting example with KV feature
  • only chairman can give the ballots to voters
  • the voter can vote the proposal once
  • everyone can check out the voting result after everyone voted

Hand in Hand

Hand in Hand Examples

  1. KV feature - Hangman
  2. RDB feature
  3. ERC feature

Last but not least

Blockchain bring TRUST to the world

Rust is the stem of TRUST

Thanks for listening

Star the project if you like it

There are also help want issues and good first issues

Interested in VM?

WasmEdge is welcom to contribute

Q&A

Made with Slides.com