Ewasm实施说明

概念介绍

Heraewasm(revision 4)虚拟机的 C++版本,对接了 EVMC ABIv6。Hera 目的是利用各种 wasm 的后端,为 ethereum 提供服务。Hera 已经在 alethgeth 上被测试过了。同时,Hera 支持各种匹配于 EVMC 的客户端。

ewasm 是以太坊系的 WebAssembly (Ethereum flavored WebAssembly),目前的版本是 Revision 4。ewasm是使用WebAssembly确定性子集重新设计的以太坊智能合约执行层。

使用WebAssembly作为智能合约的格式可以获得以下好处:

  • 近乎原生的智能合约的执行速度
  • 使用许多传统编程语言(如C,C ++和Rust)开发智能合约成为可能
  • WebAssembly庞大的开发人员社区和WebAssembly工具链

EVMC 是以太坊虚拟机(EVMs)和以太坊客户端(如Geth)之间的低级别ABI。在EVM侧,它支持经典的EVM1和ewasm。 在客户端(如Geth)侧,它定义了EVM实现访问以太坊环境和状态(state)的接口。

ewasm实施设计1:Hera + geth

  1. 创世块配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"config": {
"chainId": 1337,
"homesteadBlock": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"ewasmBlock": 0, //**odyssey划重点**
"ethash": {}
},
"nonce": "0xdeadbeefdeadbeef",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x40",
"gasLimit": "0xfffffffffffffff",
"alloc": {
"0x7eff122b94897ea5b0e2a9abf47b86337fafebdc": { "balance": "10000000000000000000000000000000000" }
}
}
  1. Build geth with EVMC,编译带有 EVMC 的 geth

从目前最新的版本 geth 1.9.2 with EVMC 6 support 下载代码。其内已经有 Linux-amd64 的可执行文件,版本基于 geth1.9.2,在 ubuntu 等 linux 系统中可以直接运行。

也可以进入到代码中自行 build:

1
2
> cd go-ethereum
> make geth
  1. 将 Hera 编译成为一个共享库文件
1
2
3
4
5
> git clone https://github.com/ewasm/hera -b v0.2.4
> cd hera
> mkdir build && cd build
> cmake .. -DBUILD_SHARED_LIBS=ON //该步骤前可能会需要执行 git submodule update --init
> cmake --build .
  1. 使用步骤 1 中的创世块配置文档 geth-config.json
1
> ./build/bin/geth --datadir /tmp/ewasm-node/4201/ init geth-config.json
  1. 运行 geth 与 Hera
1
2
3
4
5
6
7
8
9
10
> ./build/bin/geth \
--vm.ewasm="/path/to/libhera.so,metering=true,evm1mode=fallback" \
--datadir /tmp/ewasm-node/4201/ \
--rpc --rpcapi "web3,net,eth,debug" \
--rpcvhosts="*" --rpcaddr "0.0.0.0" \
--rpccorsdomain "*" \
--vmodule "miner=12,rpc=12" \ //在 odyssey 中不必使用
--mine --miner.threads 1 \ //在 odyssey 中不必使用
--nodiscover \
--networkid 1337

另外 Wagon 作为WebAssembly-based Go interpreter也是可用的。但是https://github.com/gballet/go-ethereum项目年久失修。即使是下面的方式也不建议使用的。

1
2
3
4
5
> go get github.com/ethereum/go-ethereum
> cd $GOROOT/src/github.com/ethereum/go-ethereum
> git remote add gballet git@github.com:gballet/go-ethereum.git
> git fetch gballet add-ewasm
> git checkout add-ewasm

架构图如下,请参考:

参考链接==>