6

The next generation Ethereum Virtual Machine — Ewasm VM

 3 years ago
source link: https://medium.com/taipei-ethereum-meetup/the-next-generation-ethereum-virtual-machine-ewasm-vm-2fe3fd9b94a4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The next generation Ethereum Virtual Machine — Ewasm VM

Image for post
Image for post
The next generation Ethereum Virtual Machine — Crosslink 2019 Taiwan

這篇文章是 Crosslink 2019 Taiwan 的一個議程紀錄:The next generation Ethereum Virtual Machine,由來自 Second State 的工程部 VP Hung-Ying Tai 分享 Ewasm VM 目前研究內容及未來的方向,內容非常精彩,包含了 EVM bytecode 、 Webassembly、Ewasm1.0 以及 Ewasm2.0 。

EVM bytecode 及 Webassembly(WASM)

以太坊的智能合約交易在執行時,例如 :轉 Token 到別的地址,我們是將 EVM bytecode 讀進以太坊的虛擬機執行,而 EVM bytecode 有以下幾點特色:

  1. 256 位元且堆疊式(staked-based)的虛擬機
  2. 很多高階的指令,例如:SSTORE, SLOAD, SHA3, EC, Call/Create contract
  3. 與實體系統架構(通常是 32/64 位元)有差異,而 256 位元則需要靠模擬來完成
  4. 較少程式語言(Vyper, Solidity, …)

Webassembly(WASM)是為了讓不同程式語言開發的套件都能在瀏覽器使用的一種二進位程式語言,WASM 有以下幾點特色:

  1. 堆疊式(staked-based)的虛擬機:有獨立的區域空間(暫存器或是記憶體),存取堆疊前 3 個物件(EVM 存取 16 個)
  2. 支持 32 / 64 位元的操作
  3. 沒有高階的指令
  4. RISC 指令集也可以對應到 CPU ISA
  5. 較大的社群:主流的瀏覽器都支援,也有較多的程式語言(C++, Rust, GO, …)

Ewasm 1.0

接下來看看以太坊 Ewasm 的特性:

  1. Ewasm 是 wasm 的子集合
  2. 因為不能有誤差,所以不支援浮點數運算
  3. 只能 import 以太坊的函式庫,避免 importㄒ系統函式庫
  4. 在每段指令之前插入 useGAS 來計算 GAS 的花費

Ethereum Environment Interface

EVM 裡有很多像是 SSLOAD, SHA3 的高階指令,這些指令在 Ewasm 1.0 裡,因為 WASM 可以動態讀取函式庫(模組),以太坊定義了 Ethereum Environment Interface 讓客戶端可以用不同的語言實作相對應的函示庫,而且也更容易完成 prototype 跟升級。

下圖是 Ethereum Environment Interface 定義的函數列表。

Image for post
Image for post
Ethereum Environment Interface Definition.

如何移除非法的指令?

Ewasm 使用 system contract 移除非法指令以及加入 useGas 的 bytecode,像是浮點數或是非法的 import,有以下兩種做法:

  1. 使用 system contract 檢查合約的 bytecode
  2. 像目前的 precompiles 運行在客戶端上,在部署前先檢查合約

下圖是 Ewasm 1.0 的 stack,在部署合約前 Ewasm bytecode 會先經過 Sentinal 的檢查,成功部署後客戶端如果要執行合約會透過 EVM-C 跟 Heru(Wasm Engine)溝通。

Image for post
Image for post
Ewasm Stack

效能問題

究竟使用 Ewasm 效能真的會比較快嗎?講者分享各 EVM 執行 Sha1 以及 BN128mul 的結果,可以發現 EVM 在運行 BN128mul 時會是最快,主要是因為 WASM 支持 32 / 64 位元的操作,256 位元則需要另外模擬(1 個 256 位元的運算可以換成 25 個 64 位元的運算),所以 WASM 在跑 BN128mul 時才會比較慢。

Ewasm 2.0

Ewasm 2.0 的智能合約改叫 Execution Environments(EE),與 Ewasm 1.0 不一樣的有下列幾點

  1. EE 全部都是 WASM 寫的
  2. 因為支援 cross shard,每個 EE 都是在一個 shard 上執行
  3. EE 只能拿到 state root,而在合約的執行寫法也跟原來不一樣
  4. EE 是 stateless

下圖可以看到 ERC20 Token 在 Ewasm 2.0 跟 Ewasm 1.0 storage 的比較,Ewasm 1.0 每個 data 都會有相對應的 key,而 Ewasm 2.0 只有存 state root,所以只能跟 state root 互動。

Image for post
Image for post
Ewasm 2.0 vs Ewasm 1.0

Phase One and Done

目前 Ewasm 2.0 到 phase one and done 的階段,也有測試的網路可以在 shard block 執行 EE,以太坊也有開源 Ewasm 2.0 的測試工具 Scout

Image for post
Image for post
Hello World for Ewasm 2.0

上圖是 Eth 2 的 Hello World EE,可以看到 main 函數裡第一行讀取 pre state root,接下來驗證 block data size 是不是為 0,最後再將 state root 存回去,Eth 2 的智能合約寫起來都會像這樣。

Ewasm 1.0 目前已經支援 EVM 1 大部分的功能也有測試鏈了,second state 開發一個編譯器 soll,能將 solidity 編譯成 Ewasm,想研究的人可以參考看看。

Ewasm 2.0 目前還在研究中,下圖是講者給大家分享的研究及貢獻的方向。

Image for post
Image for post
A MAYBE Roadmap

Crosslink 簡報

webassembly.org

scout

soll

Ewasm overview and the precompile problem: Alex Beregszaszi and Casey Detrio @ Ethereum \\ Part 1 — YouTube

Ewasm overview and the precompile problem: Alex Beregszaszi and Casey Detrio @ Ethereum \\ Part 2 — YouTube

Wasm for blockchain&Eth2 execution: Paul Dworzanski,Alex Beregszaszi,Casey Detrio@Ethereum \\ Part 2 — YouTube

Ewasm for sharding

Ewasm updates

Ewasm design

wasm-intro


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK