Commit 0544e252 authored by WuEcho's avatar WuEcho

add code

parents
Pipeline #635 failed with stages
# hello-wasm-abi
>本文在 `wasm` 合约中使用 `ABI` 我们假设您已经阅读了 [README.md](https://github.com/WuEcho/ewasm-rust-demo/tree/master/README.md) 并掌握了 `hello-wasm` 例子工程<br>
>接下来我们会在此基础上加以修改,具体代码放在 `hello-wasm-abi` 目录中<br>
## 定义 ABI
>在 `hello-wasm-abi/src/abi.rs` 中定义了 Contract 对象,包含了 `hello-wasm` 样例中的 <br>
>`put/get/getcounter` 三个方法的 `ABI` 描述,注意,我们还不能直接用 `JSON` 来描述 `ABI`<br>
>必须使用 `metavmabi::Contract` 来定义声明;
建议通过以下三步来生成 ABI :
1. 使用 `solidity` 编写 `contract interface`;
1. 使用 `remix` 编译 `contract interface` 得到对应的 `ABI` 描述;
1. 参照 `ABI` 描述文件编写 `metavmabi::Contract`
部署 wasm 合约后可以使用合约地址和 contract interface 在 remix 里对合约进行实例化,方便测试
### Solidity Contract Interface
[Remix IDE](http://remix.ethereum.org/#optimize=false&version=soljson-v0.5.3+commit.10d17f24.js&evmVersion=null&appVersion=0.7.7) 中编写合约接口,并编译
```solidity
pragma solidity ^0.5.3;
contract hello_wasm_abi {
function getcounter() public view returns(uint256);
function get(string memory key) public view returns(string memory);
function put(string memory key,string memory val) public payable;
}
```
### JSON ABI
编译合约接口可以得到对应的 `ABI JSON` 描述,提供合约地址和此 `JSON ABI` 文档,
`DAPP` 开发者即可实例化 `hello_wasm_abi` 合约,并使用其中的三个函数
```json
[
{
"constant": false,
"inputs": [
{
"name": "key",
"type": "string"
},
{
"name": "val",
"type": "string"
}
],
"name": "put",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "key",
"type": "string"
}
],
"name": "get",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getcounter",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
```
### metavmabi::Contract
根据 `JSON ABI` 描述实例化 `metavmabi::Contract` 对象,用来对合约的 `input/output` 进行序列化和反序列化
```rust
pub fn get_contract_abi() -> metavmabi::Contract {
let mut functions: HashMap<String, metavmabi::Function> = HashMap::new();
let fn_put = metavmabi::Function {
constant: false,
name: String::from("put"),
inputs: Vec::from(vec![
metavmabi::Param { name: String::from("key"), kind: metavmabi::param_type::ParamType::String },
metavmabi::Param { name: String::from("val"), kind: metavmabi::param_type::ParamType::String },
]),
outputs: Vec::default(),
};
let fn_get = metavmabi::Function {
constant: true,
name: String::from("get"),
inputs: Vec::from(vec![
metavmabi::Param { name: String::from("key"), kind: metavmabi::param_type::ParamType::String },
]),
outputs: Vec::from(vec![
metavmabi::Param { name: String::default(), kind: metavmabi::param_type::ParamType::String },
]),
};
let fn_getcounter = metavmabi::Function {
constant: true,
name: String::from("getcounter"),
inputs: Vec::default(),
outputs: Vec::from(vec![
metavmabi::Param { name: String::default(), kind: metavmabi::param_type::ParamType::Uint(256) },
]),
};
functions.insert(fn_put.clone().name, fn_put.clone());
functions.insert(fn_get.clone().name, fn_get.clone());
functions.insert(fn_getcounter.clone().name, fn_getcounter.clone());
metavmabi::Contract {
constructor: None,
functions: functions,
events: HashMap::default(),
fallback: false,
signers: HashMap::default(),
}
}
```
## 使用 `ABI`
>在 hello-wasm-abi 合约中
```rust
extern crate wasm_bindgen;
extern crate ewasm_api;
use wasm_bindgen::prelude::*;
use ewasm_api::types::*;
use ewasm_api::metavm::utils::*;
// 倒入处理 abi 的开发库
use ewasm_api::metavmabi;
// metavmabi::Contract 定义的对象放在 abi 模块中
pub mod abi;
const COUNTER_KEY: Bytes32 = Bytes32 { bytes: [255; 32] };
fn inc_counter() {
let old_v = ewasm_api::storage_load(&COUNTER_KEY);
let old_i = bytes_to_uint(&old_v.bytes[..]);
let new_i = old_i + 1;
let val = u32_to_bytes32(new_i as u32);
let value = Bytes32 { bytes: val };
ewasm_api::storage_store(&COUNTER_KEY, &value);
}
fn get_counter() -> Vec<u8> {
let v = ewasm_api::storage_load(&COUNTER_KEY);
Vec::from(&v.bytes[..])
}
fn put_data(k: String, v: String) {
ewasm_api::metavm::storage_store(k.as_bytes(), v.as_bytes());
}
fn get_data(k: String) -> Vec<u8> {
ewasm_api::metavm::storage_load(k.as_bytes())
}
#[wasm_bindgen]
pub fn main() {
inc_counter();
let input = ewasm_api::calldata_acquire();
if !input.is_empty() {
// 获取 metavmabi::Contract 对象,这个函数写在 abi 模块中
let mut contract = abi::get_contract_abi();
// 从 input 获取方法签名,按照 ABI 规范,input 的前 4 个 byte 为方法签名
let fn_sig = &Vec::from(&input[..4]);
// 根据方法签名获取 function 对象
let function = contract.function_by_sig(fn_sig).expect("error_fn_sig");
// 通过 function.name 来匹配相应的 handler
match function.name.as_str() {
"getcounter" => { // function getcounter() public view returns(uint256);
// 调用 get_counter 得到返回值,转换成 uint
let rtn = ewasm_api::metavm::utils::bytes_to_uint(get_counter().as_slice());
// 此方法没有输入值,只有输出,通过 function.encode_output 序列化输出
let data = function.encode_output(&[metavmabi::Token::Uint(rtn.into())]).unwrap();
// 将结果返回给合约调用者
ewasm_api::finish_data(data.as_slice());
}
"get" => { // function get(string memory key) public view returns(string memory);
// 此方法有定义输入 string key , 先用 function.decode_input 解析 input, 得到输入列表
let tokens = function.decode_input(input.as_slice()).expect("error_put_input");
// 接口中 input 只定义了一个参数,所以 key = tokens[0]
let key = tokens.get(0).expect("error_put_key");
// 调用 get_data(key) 函数,得到 val 的字节数组
let val = get_data(key.clone().to_string().unwrap());
// 接口描述输出值为 string,所以要将 val 转换为 string
let rtn = String::from_utf8(val).expect("error_get_val");
// 使用 function.encode_output 对返回值进行序列化
let data = function.encode_output(&[metavmabi::Token::String(rtn)]).expect("error_get_output");
// 将结果返回给合约调用者
ewasm_api::finish_data(data.as_slice());
}
"put" => { // function put(string memory key,string memory val) public payable;
// 此方法有定义输入 [string key,string val] , 先用 function.decode_input 解析 input, 得到输入列表
let tokens = function.decode_input(input.as_slice()).expect("error_put_input");
// 接口中定义了两个参数,分别对应 key = tokens[0] , val = tokens[1]
let key = tokens.get(0).expect("error_put_key");
let val = tokens.get(1).expect("error_put_val");
// 调用 put_data(key,val)
put_data(key.clone().to_string().unwrap(), val.clone().to_string().unwrap());
// 结束调用,此方法没有返回值
ewasm_api::finish()
}
_ => ewasm_api::finish() // 如果方法匹配失败,则直接返回不做任何处理
}
}
}
```
## 部署与使用
* 部署合约方式与 `hello-wasm` 样例相同,可以参照 [README.md](https://github.com/WuEcho/ewasm-rust-demo/tree/master/README.md) 中关于`部署`的描述;
* 调用合约:部署成功后会得到 `Contract Address` ,如果使用 `web3` 系列 `SDK` 可以使用 `JSON ABI` + `Contract Address` 来实例化合约,并进行调用,如果使用 `remix IDE` 进行测试调用,可以使用 `Solidity Contract Interface` + `Contract Address` 来实例化合约并调用
关于 web3 提供的 SDK 和 remix IDE 的详细资料请参阅 web3 基金会的相关资料
## Solidity 调用 Wasm 合约
`sol` 合约来调用 `wasm` 合约,与 `sol` 调用 `sol` 方式相同,
假设已经部署过 `hello-wasm-abi` 这个合约,并得到合约地址 `0xda3df11d916ffba3a1289cef66a7f142ec5d0f76`,
通过 `hello-wasm-abi` 合约接口和地址,即可实例化这个合约,之后用法与 `sol` 调用 `sol` 一致,
例如:
```solidity
pragma solidity ^0.5.3;
// hello-wasm-abi 合约接口
contract hello_wasm_abi {
function getcounter() public view returns(uint256);
function get(string memory key) public view returns(string memory);
function put(string memory key,string memory val) public payable;
}
// 使用 hello-wasm-abi 合约的 solidity 合约
contract foobar {
function fetch(address addr,string memory key) public view returns(string memory) {
// 第一个参数 addr 为 wasm 合约地址,通过接口和地址实例化合约对象
hello_wasm_abi hello = hello_wasm_abi(addr);
// 调用 wasm 合约方法
return hello.get(key);
}
function set(address addr,string memory key,string memory val) public payable {
hello_wasm_abi hello = hello_wasm_abi(addr);
hello.put(key,val);
}
}
```
部署 `foobar` 合约后,使用 `hello-wasm-abi` 的合约地址 `0xda3df11d916ffba3a1289cef66a7f142ec5d0f76` 作为第一个参数分别
调用 `fetch``set` 方法,完成对 `hello-wasm-abi` 合约的 `get``put` 的调用。
## 合约开发规范
ewasm 合约接口规范由以太坊定制,指定模块结构等信息,MetaVM 严格遵循此规范,具体如下
### 数据类型
禁止使用浮点数,兼容 evm 中规定的数据类型,例如:
* bytes : 不定长字节数组
* address : 160 bit 数字,在内存中以 20字节 小字节无符号整型表示
* u128 : 128 bit 数字,在内存中以 16字节 小字节无符号整型表示
* u256 : 256 bit 数字,在内存中以 32字节 小字节无符号整型表示
### 格式
每个合约必须存储为 wasm 字节码
### 导入模块
规定合约 `import` 的范围仅限于 [EEI](https://github.com/ewasm/design/blob/master/eth_interface.md) 提供的模块,`ethereum` 名称空间以外的包只允许使用 `debug` ,在生产环境中 `debug` 也应被禁止使用
### 导出函数
每个合约必须导出两个函数(只能导出两个函数)
* memory : 可供 [EEI](https://github.com/ewasm/design/blob/master/eth_interface.md) 写入的共享内存
* main : 一个入口函数,没有参数也没有返回值,将被 VM 执行
要关闭 `wasm``start function` 功能,开启它会影响 `ewasm` 在启动前获取合约内存地址指针的功能
### 关于 ABI
>我们看到有关导出函数的规定与 `solidity` 合约中定义的 `ABI` 有些不一样,<br>
>`solidity` 合约根据方法签名来生成相应的 `ABI` 以便对合约中的函数进行调度,<br>
>这在 `ewasm` 看来似乎行不通,因为只有一个 `main` 函数被导出了, <br>
>如何使用 `main` 函数之外的函数呢?我们很自然就想到了使用合约的 `input` 来 <br>
>定义目标方法和输入参数,事实上 `solidity` 也是这么做的,只是我们把这个灵活性 <br>
>交还给开发者实现,以统一的 `main` 函数作为入口,然后自行封装 `input` 序列化方案,<br>
>在后面的例子中我们可以看到更加灵活的方式。<br>
## 开发环境安装
MetaVM 使用 rust 作为 ewasm 合约开发语言,并通过 rust 工具链对合约进行编译,具体安装与使用流程如下
1. 安装 rustup
```
curl https://sh.rustup.rs -sSf | sh
```
>注意,在安装脚本执行时要选择 `nightly` 频道,否则无法完成后续工具安装
>安装时如果 path 处理失败,需要手动加载一下环境变量 : `source $HOME/.cargo/env`
2. 安装 rust 标准库
```
rustup component add rust-src
```
3. 安装 wasm-pack 编译工具
```
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
```
4. 安装 wasm 后期处理工具
```bash
$> git clone https://github.com/WuEcho/wasm-chisel.git
$> cd wasm-chisel
$> cargo build --release
```
编译成功后会在 `target/release` 目录下找到 `chisel` 程序,确保将其复制到 `$PATH` 目录
__有关 Rust 更多内容请参考:__
https://www.rust-lang.org/zh-CN/learn/get-started
## WASM 样例合约
此步骤之前请确保合约开发环境已经安装完成,我们接下来会用到 `cargo` 创建合约,
使用 `wasm-pack` 来编译合约,使用 `chisel` 对合约进行后期处理
### 创建 hello-wasm 合约
假设工作目录为 `/Users/wuxinyang/Desktop/MyGo/src/rust` ,在终端下进入目录
```bash
$> cargo new --lib hello-wasm
$> cd hello-wasm
$> touch chisel.yml
```
编辑 `chisel.yml` 文件,填入下文中的内容,其中 `file` 属性为 `hello-wasm` 合约编译后产生的二进制文件:
```yml
hello:
file: "/Users/wuxinyang/Desktop/MyGo/src/rust/hello_wasm_bg.wasm"
remapimports:
preset: "ewasm"
trimexports:
preset: "ewasm"
verifyimports:
preset: "ewasm"
verifyexports:
preset: "ewasm"
repack:
preset: "ewasm"
```
### 添加依赖
一个 `wasm` 合约至少要依赖两个开发包,`ewasm-rust-api``wasm-bindgen`
前者提供 `api` 与 MetaVm 交互,后者负责编译 rust 为 wasm ;
`MetaVm``eei` 进行了扩展,需要使用 `MetaVm` 提供的 `ewasm-rust-api`
编辑 `hello-wasm/Cargo.toml` 文件,添加依赖到 `dependencies` 下,并且配置 `profile.release` 以优化编译结果
```toml
[package]
name = "hello-wasm"
version = "0.1.0"
authors = ["WuEcho <emailforecho@163.com>"]
edition = "2022"
publish = false
[dependencies]
wasm-bindgen = "0.2"
ewasm_api = { git = "https://github.com/WuEcho/ewasm-rust-api", tag = "0.9" }
[lib]
crate-type = ["cdylib"]
[profile.release]
opt-level = 'z'
debug = false
rpath = false
debug-assertions = false
codegen-units = 1
lto = true
```
使用 `cargo check` 检查并下载依赖
```bash
$> cargo check
Updating crates.io index
Compiling proc-macro2 v0.4.30
Compiling unicode-xid v0.1.0
Compiling syn v0.15.42
Compiling wasm-bindgen-shared v0.2.48
Compiling log v0.4.8
Compiling cfg-if v0.1.9
Compiling lazy_static v1.3.0
Compiling libc v0.2.60
Compiling bumpalo v2.5.0
Checking void v1.0.2
Compiling wee_alloc v0.4.4
Compiling wasm-bindgen v0.2.48
Checking memory_units v0.4.0
Checking unreachable v1.0.0
Checking ewasm_api v0.9.1
Compiling quote v0.6.13
Compiling wasm-bindgen-backend v0.2.48
Compiling wasm-bindgen-macro-support v0.2.48
Compiling wasm-bindgen-macro v0.2.48
Checking hello-wasm v0.1.0 (/Users/wuxinyang/Desktop/MyGo/src/rust/hello-wasm)
Finished dev [unoptimized + debuginfo] target(s) in 16.90s
```
### 编写合约代码
至此合约的开发工作已经准备完毕,接下来我们将在合约中实现 `put / get` 函数,以及一个简单的计数器
用来演示通过合约存储 `k/v` 值并根据 `k` 获取值,以及如何通过 `contract.input` 来进行不同函数的调度
编辑 `hello-wasm/src/lib.rs` 添加合约代码
```rust
use wasm_bindgen::prelude::*;
use ewasm_api::types::*;
use ewasm_api::metavm::utils::*;
// 为 counter 定义一个 32位的 key
const COUNTER_KEY: Bytes32 = Bytes32 { bytes: [255; 32] };
// 每次方法被调用时都会执行一个 counter++ 操作,在链上记录执行次数,以测试状态写入操作
// EEI 函数回调 storageLoad / storageStore
fn inc_counter() {
// storage_load 为 eei 中提供的函数,约束 k/v 均为 32byte
// 此处将获取 key 对应的 Bytes32 类型的 value
let old_v = ewasm_api::storage_load(&COUNTER_KEY);
// 此方法由 ewasm_api::metavm::utils 名称空间所提供
// 用来将 32 byte 字节数组转换为对应的整型
let old_i = bytes_to_uint(&old_v.bytes[..]);
let new_i = old_i + 1;
// 此方法由 ewasm_api::metavm::utils 名称空间所提供
// 用来将 uint32 转换为 32 byte 数组
let val = u32_to_bytes32(new_i as u32);
let value = Bytes32 { bytes: val };
// storage_store 为 eei 中提供的函数,约束 k/v 均为 32byte
// 用来保存 k/v 到当前合约的状态库
ewasm_api::storage_store(&COUNTER_KEY, &value);
}
// EEI 函数回调 storageLoad
fn get_counter() {
let v = ewasm_api::storage_load(&COUNTER_KEY);
// 如果想将合约的执行结果返回给调用方,不需要使用 return 也无需在方法签名中指明
// 必须使用 eei 中规定的 finish_data 函数
ewasm_api::finish_data(&v.bytes[..]);
}
fn put_data() {
// input 格式为 "put:key,value"
let input = ewasm_api::calldata_acquire();
let data = String::from_utf8(input).expect("error_params");
// 将 input 分割为 ["put","key,value"]
let sd: Vec<&str> = data.split(":").collect();
if sd.len() > 1 {
// 将 "key,value" 分割为 ["key","value"]
let sp: Vec<&str> = sd[1].split(",").collect();
if sp.len() > 1 {
let k = sp[0].trim();
let v = sp[1].trim();
// storage_store 为 metavm 提供的扩展函数
// 用来将不限制大小的 key / value 保存在合约状态中
// 值得注意的是此方法的 gas 是以数据大小来计算的
// 每 32byte 数据所使用的 gas 与 storage_store 相同
ewasm_api::metavm::storage_store(k.as_bytes(), v.as_bytes());
}
}
}
fn get_data() {
// input 格式为 "get:key"
let input = ewasm_api::calldata_acquire();
let data = String::from_utf8(input).expect("error_params");
// 将 input 分割为 ["get","key"]
let sd: Vec<&str> = data.split(":").collect();
if sd.len() > 1 {
let k = sd[1].trim();
// storage_load 为 metavm 提供的扩展函数
// 用来获取 key 对应的不限制大小的 value
// 值得注意的是此方法的 gas 是以数据大小来计算的
// 每 32byte 数据所使用的 gas 与 storage_store 相同
let v: Vec<u8> = ewasm_api::metavm::storage_load(k.as_bytes());
// 将合约执行结果返回给调用端
ewasm_api::finish_data(&v[..]);
}
}
//fn constructor() {}
// 同 solidity 中的匿名函数,每次给这个合约转账时都会回调这个函数
// 如果需要使用匿名函数在收到转账时做特殊处理,则可实现这个函数
fn anonymous() {
// TODO 不需要返回任何值
}
// 合约入口 : 必须使用 #[wasm_bindgen] 注解来声明导出 main 函数
#[wasm_bindgen]
pub fn main() {
// 当合约通过 tx 调用时表示需要改变状态,此时计数器会加一,否则无效
inc_counter();
// 获取本次合约调用的 contract.input
let input = ewasm_api::calldata_acquire();
// 当 create 合约时 input 始终为空
// 当给合约发送普通转账交易时,input 也应为空
if !input.is_empty() {
// 本 demo 使用了文本协议来序列化 input
// 格式为: "目标函数:参数1,参数2,参数n"
// 解析
let data = match String::from_utf8(input) {
Ok(s) => s,
Err(e) => e.to_string(),// 也可以在此处终止合约
};
// 将 input 分割为 ["目标函数","参数1,参数2,参数n"]
let sd: Vec<&str> = data.split(":").collect();
// 通过这个匹配可以看出我们这个合约对外暴漏 3 个函数,函数名称不区分大小写:
// GETCOUNTER : 通过 eth_call 调用,用来获取计数器结果
// PUT : 通过 tx 向合约中添加一个 k/v 对,具体参数格式为 "put:k,v"
// GET : 通过 eth_call 调用,获取 k 对应的 v,具体参数格式为 "get:k"
// 当方法名得不到匹配时,会返回 "METHOD_NOT_FOUND" 标识
match sd[0].trim().to_uppercase().as_str() {
"GETCOUNTER" => get_counter(),
"PUT" => put_data(),
"GET" => get_data(),
_ => ewasm_api::finish_data(String::from("METHOD_NOT_FOUND").as_bytes()),
}
} else {
// 当 input 为空时,调度匿名函数
anonymous();
}
}
```
### 编译合约
在控制台进入 `hello-wasm` 工程目录,编译并完成后期处理
```bash
$> wasm-pack build
$> chisel run
```
以上步骤将在 `/Users/wuxinyang/Desktop/MyGo/src/rust` 目录中得到 `hello_wasm_bg.wasm` 文件,接下来我们去链上部署这份合约
### 哨兵合约示例
rust代码 https://github.com/ewasm/sentinel-rs
solodity代码 https://github.com/ewasm/sentinel-governance
### 部署以及调用
以构造交易体并发送交易给链上的方式进行部署,构造交易的代码如下:
```go
import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"testing"
"io/ioutil"
"encoding/hex"
)
func TestDeploy(t *testing.T) {
fp := "/Users/wuxinyang/Desktop/MyGo/src/rust/hello-wasm/pkg/hello_wasm_bg.wasm"
ctx := context.Background()
code, err := ioutil.ReadFile(fp)
if err != nil {
t.Error(err)
return
}
t.Log(code)
t.Log(hex.EncodeToString(code))
client, err := ethclient.Dial(ipc)
if err != nil {
t.Error(err)
return
}
nonce, err := client.PendingNonceAt(ctx, addr)
if err != nil {
t.Error(err)
return
}
tx, _ := types.SignTx(
types.NewContractCreation(
nonce,
new(big.Int),
11826015, //gasLimit
new(big.Int).Mul(big.NewInt(1e9), big.NewInt(18)), // gasPrice
code),
signer,
prvKey)
t.Log(nonce, tx)
err = client.SendTransaction(ctx, tx)
t.Log(err, len(code), tx.Hash().Hex())
}
//调用方法
func TestCall(t *testing.T) {
to := common.HexToAddress("0xda3df11d916ffba3a1289cef66a7f142ec5d0f76")
ctx := context.Background()
client, err := ethclient.Dial(ipc)
if err != nil {
t.Error(err)
return
}
nonce, err := client.PendingNonceAt(ctx, addr)
if err != nil {
t.Error(err)
return
}
_tx := types.NewTransaction(
nonce,
to,
big.NewInt(1),
params.GenesisGasLimit, // gasLimit
new(big.Int).Mul(big.NewInt(1e9), big.NewInt(18)), // gasPrice
[]byte("put:helloworld,wuecho@163.com"))
//get:helloworld
tx, _ := types.SignTx(_tx, signer, prvKey)
t.Log(nonce, tx)
err = client.SendTransaction(ctx, tx)
t.Log(err, tx.Hash().Hex())
}
```
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "arrayvec"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
dependencies = [
"nodrop",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "error-chain"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc"
dependencies = [
"version_check",
]
[[package]]
name = "ethabi"
version = "8.0.1"
source = "git+https://github.com/WuEcho/ewasm-rust-api?tag=0.9#8b694f0cba1c6a935e78e4d219637fbca4dbd774"
dependencies = [
"error-chain",
"ethereum-types",
"rustc-hex",
"serde",
"serde_derive",
"serde_json",
"tiny-keccak",
]
[[package]]
name = "ethbloom"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3932e82d64d347a045208924002930dc105a138995ccdc1479d0f05f0359f17c"
dependencies = [
"crunchy",
"fixed-hash",
"impl-rlp",
"impl-serde",
"tiny-keccak",
]
[[package]]
name = "ethereum-types"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62d1bc682337e2c5ec98930853674dd2b4bd5d0d246933a9e98e5280f7c76c5f"
dependencies = [
"ethbloom",
"fixed-hash",
"impl-rlp",
"impl-serde",
"primitive-types",
"uint",
]
[[package]]
name = "ewasm_api"
version = "0.9.1"
source = "git+https://github.com/WuEcho/ewasm-rust-api?tag=0.9#8b694f0cba1c6a935e78e4d219637fbca4dbd774"
dependencies = [
"cfg-if 0.1.10",
"ethabi",
"wee_alloc",
]
[[package]]
name = "fixed-hash"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1a683d1234507e4f3bf2736eeddf0de1dc65996dc0164d57eba0a74bcf29489"
dependencies = [
"byteorder",
"heapsize",
"rand",
"rustc-hex",
"static_assertions",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "heapsize"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461"
dependencies = [
"winapi",
]
[[package]]
name = "hello-wasm"
version = "0.1.0"
dependencies = [
"ewasm_api",
"wasm-bindgen",
]
[[package]]
name = "impl-codec"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2050d823639fbeae26b2b5ba09aca8907793117324858070ade0673c49f793b"
dependencies = [
"parity-codec",
]
[[package]]
name = "impl-rlp"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f7a72f11830b52333f36e3b09a288333888bf54380fd0ac0790a3c31ab0f3c5"
dependencies = [
"rlp",
]
[[package]]
name = "impl-serde"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58e3cae7e99c7ff5a995da2cf78dd0a5383740eda71d98cf7b1910c301ac69b8"
dependencies = [
"serde",
]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "libc"
version = "0.2.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "parity-codec"
version = "3.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b9df1283109f542d8852cd6b30e9341acc2137481eb6157d2e62af68b0afec9"
dependencies = [
"arrayvec",
"serde",
]
[[package]]
name = "primitive-types"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2288eb2a39386c4bc817974cc413afe173010dc80e470fcb1e9a35580869f024"
dependencies = [
"fixed-hash",
"impl-codec",
"impl-rlp",
"impl-serde",
"uint",
]
[[package]]
name = "proc-macro2"
version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b1106fec09662ec6dd98ccac0f81cef56984d0b49f75c92d8cbad76e20c005c"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"winapi",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rlp"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1190dcc8c3a512f1eef5d09bb8c84c7f39e1054e174d1795482e18f5272f2e73"
dependencies = [
"rustc-hex",
]
[[package]]
name = "rustc-hex"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6"
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "static_assertions"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5"
[[package]]
name = "syn"
version = "2.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tiny-keccak"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d8a021c69bb74a44ccedb824a046447e2c84a01df9e5c20779750acb38e11b2"
dependencies = [
"crunchy",
]
[[package]]
name = "uint"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2143cded94692b156c356508d92888acc824db5bffc0b4089732264c6fcf86d4"
dependencies = [
"byteorder",
"crunchy",
"heapsize",
"rustc-hex",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasm-bindgen"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "hello-wasm"
version = "0.1.0"
edition = "2021"
authors = ["WuEcho <emailforecho@163.com>"]
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
wasm-bindgen = "0.2"
ewasm_api = { git = "https://github.com/WuEcho/ewasm-rust-api", tag = "0.9" }
[lib]
crate-type = ["cdylib"]
[profile.release]
opt-level = 'z'
debug = false
rpath = false
debug-assertions = false
codegen-units = 1
lto = true
hello:
file: "/Users/wuxinyang/Desktop/MyGo/src/rust/hello_wasm_bg.wasm"
remapimports:
preset: "ewasm"
trimexports:
preset: "ewasm"
verifyimports:
preset: "ewasm"
verifyexports:
preset: "ewasm"
repack:
preset: "ewasm"
extern crate wasm_bindgen;
extern crate ewasm_api;
use wasm_bindgen::prelude::*;
use ewasm_api::types::*;
use ewasm_api::metavm::utils::*;
use ewasm_api::metavmabi;
pub mod abi;
const COUNTER_KEY: Bytes32 = Bytes32 { bytes: [255; 32] };
fn inc_counter() {
let old_v = ewasm_api::storage_load(&COUNTER_KEY);
let old_i = bytes_to_uint(&old_v.bytes[..]);
let new_i = old_i + 1;
let val = u32_to_bytes32(new_i as u32);
let value = Bytes32 { bytes: val };
ewasm_api::storage_store(&COUNTER_KEY, &value);
}
fn get_counter() -> Vec<u8> {
let v = ewasm_api::storage_load(&COUNTER_KEY);
Vec::from(&v.bytes[..])
}
fn put_data(k: String, v: String) {
ewasm_api::metavm::storage_store(k.as_bytes(), v.as_bytes());
}
fn get_data(k: String) -> Vec<u8> {
ewasm_api::metavm::storage_load(k.as_bytes())
}
#[wasm_bindgen]
pub fn main() {
inc_counter();
let input = ewasm_api::calldata_acquire();
if !input.is_empty() {
let mut contract = abi::get_contract_abi();
let fn_sig = &Vec::from(&input[..4]);
let function = contract.function_by_sig(fn_sig).expect("error_fn_sig");
match function.name.as_str() {
"getcounter" => { // function getcounter() public view returns(uint256);
let rtn = ewasm_api::metavm::utils::bytes_to_uint(get_counter().as_slice());
let data = function.encode_output(&[metavmabi::Token::Uint(rtn.into())]).unwrap();
ewasm_api::finish_data(data.as_slice());
}
"get" => { // function get(string memory key) public view returns(string memory);
let tokens = function.decode_input(input.as_slice()).expect("error_put_input");
let key = tokens.get(0).expect("error_put_key");
let val = get_data(key.clone().to_string().unwrap());
let rtn = String::from_utf8(val).expect("error_get_val");
let data = function.encode_output(&[metavmabi::Token::String(rtn)]).expect("error_get_output");
ewasm_api::finish_data(data.as_slice());
}
"put" => { // function put(string memory key,string memory val) public payable;
let tokens = function.decode_input(input.as_slice()).expect("error_put_input");
let key = tokens.get(0).expect("error_put_key");
let val = tokens.get(1).expect("error_put_val");
put_data(key.clone().to_string().unwrap(), val.clone().to_string().unwrap());
ewasm_api::finish()
}
_ => ewasm_api::finish()
}
}
}
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "arrayvec"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
dependencies = [
"nodrop",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "error-chain"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc"
dependencies = [
"version_check",
]
[[package]]
name = "ethabi"
version = "8.0.1"
source = "git+https://github.com/WuEcho/ewasm-rust-api?tag=0.9#8b694f0cba1c6a935e78e4d219637fbca4dbd774"
dependencies = [
"error-chain",
"ethereum-types",
"rustc-hex",
"serde",
"serde_derive",
"serde_json",
"tiny-keccak",
]
[[package]]
name = "ethbloom"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3932e82d64d347a045208924002930dc105a138995ccdc1479d0f05f0359f17c"
dependencies = [
"crunchy",
"fixed-hash",
"impl-rlp",
"impl-serde",
"tiny-keccak",
]
[[package]]
name = "ethereum-types"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62d1bc682337e2c5ec98930853674dd2b4bd5d0d246933a9e98e5280f7c76c5f"
dependencies = [
"ethbloom",
"fixed-hash",
"impl-rlp",
"impl-serde",
"primitive-types",
"uint",
]
[[package]]
name = "ewasm_api"
version = "0.9.1"
source = "git+https://github.com/WuEcho/ewasm-rust-api?tag=0.9#8b694f0cba1c6a935e78e4d219637fbca4dbd774"
dependencies = [
"cfg-if 0.1.10",
"ethabi",
"wee_alloc",
]
[[package]]
name = "fixed-hash"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1a683d1234507e4f3bf2736eeddf0de1dc65996dc0164d57eba0a74bcf29489"
dependencies = [
"byteorder",
"heapsize",
"rand",
"rustc-hex",
"static_assertions",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "heapsize"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461"
dependencies = [
"winapi",
]
[[package]]
name = "hello-wasm"
version = "0.1.0"
dependencies = [
"ewasm_api",
"wasm-bindgen",
]
[[package]]
name = "impl-codec"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2050d823639fbeae26b2b5ba09aca8907793117324858070ade0673c49f793b"
dependencies = [
"parity-codec",
]
[[package]]
name = "impl-rlp"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f7a72f11830b52333f36e3b09a288333888bf54380fd0ac0790a3c31ab0f3c5"
dependencies = [
"rlp",
]
[[package]]
name = "impl-serde"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58e3cae7e99c7ff5a995da2cf78dd0a5383740eda71d98cf7b1910c301ac69b8"
dependencies = [
"serde",
]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "libc"
version = "0.2.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "parity-codec"
version = "3.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b9df1283109f542d8852cd6b30e9341acc2137481eb6157d2e62af68b0afec9"
dependencies = [
"arrayvec",
"serde",
]
[[package]]
name = "primitive-types"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2288eb2a39386c4bc817974cc413afe173010dc80e470fcb1e9a35580869f024"
dependencies = [
"fixed-hash",
"impl-codec",
"impl-rlp",
"impl-serde",
"uint",
]
[[package]]
name = "proc-macro2"
version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b1106fec09662ec6dd98ccac0f81cef56984d0b49f75c92d8cbad76e20c005c"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"winapi",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rlp"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1190dcc8c3a512f1eef5d09bb8c84c7f39e1054e174d1795482e18f5272f2e73"
dependencies = [
"rustc-hex",
]
[[package]]
name = "rustc-hex"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6"
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.188"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "static_assertions"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5"
[[package]]
name = "syn"
version = "2.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tiny-keccak"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d8a021c69bb74a44ccedb824a046447e2c84a01df9e5c20779750acb38e11b2"
dependencies = [
"crunchy",
]
[[package]]
name = "uint"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2143cded94692b156c356508d92888acc824db5bffc0b4089732264c6fcf86d4"
dependencies = [
"byteorder",
"crunchy",
"heapsize",
"rustc-hex",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasm-bindgen"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "hello-wasm"
version = "0.1.0"
edition = "2021"
authors = ["WuEcho <emailforecho@163.com>"]
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
wasm-bindgen = "0.2"
ewasm_api = { git = "https://github.com/WuEcho/ewasm-rust-api", tag = "0.9" }
[lib]
crate-type = ["cdylib"]
[profile.release]
opt-level = 'z'
debug = false
rpath = false
debug-assertions = false
codegen-units = 1
lto = true
hello:
file: "/Users/wuxinyang/Desktop/MyGo/src/rust/hello_wasm_bg.wasm"
remapimports:
preset: "ewasm"
trimexports:
preset: "ewasm"
verifyimports:
preset: "ewasm"
verifyexports:
preset: "ewasm"
repack:
preset: "ewasm"
use wasm_bindgen::prelude::*;
use ewasm_api::types::*;
use ewasm_api::metavm::utils::*;
const COUNTER_KEY: Bytes32 = Bytes32 { bytes: [255; 32] };
fn inc_counter() {
let old_v = ewasm_api::storage_load(&COUNTER_KEY);
let old_i = bytes_to_uint(&old_v.bytes[..]);
let new_i = old_i + 1;
let val = u32_to_bytes32(new_i as u32);
let value = Bytes32 { bytes: val };
ewasm_api::storage_store(&COUNTER_KEY, &value);
}
fn get_counter() {
let v = ewasm_api::storage_load(&COUNTER_KEY);
ewasm_api::finish_data(&v.bytes[..]);
}
fn put_data() {
let input = ewasm_api::calldata_acquire();
let data = String::from_utf8(input).expect("error_params");
let sd: Vec<&str> = data.split(":").collect();
if sd.len() > 1 {
let sp: Vec<&str> = sd[1].split(",").collect();
if sp.len() > 1 {
let k = sp[0].trim();
let v = sp[1].trim();
ewasm_api::metavm::storage_store(k.as_bytes(), v.as_bytes());
}
}
}
fn get_data() {
let input = ewasm_api::calldata_acquire();
let data = String::from_utf8(input).expect("error_params");
let sd: Vec<&str> = data.split(":").collect();
if sd.len() > 1 {
let k = sd[1].trim();
let v: Vec<u8> = ewasm_api::metavm::storage_load(k.as_bytes());
ewasm_api::finish_data(&v[..]);
}
}
fn anonymous() {
}
#[wasm_bindgen]
pub fn main() {
inc_counter();
let input = ewasm_api::calldata_acquire();
if !input.is_empty() {
let data = match String::from_utf8(input) {
Ok(s) => s,
Err(e) => e.to_string(),
};
let sd: Vec<&str> = data.split(":").collect();
match sd[0].trim().to_uppercase().as_str() {
"GETCOUNTER" => get_counter(),
"PUT" => put_data(),
"GET" => get_data(),
_ => ewasm_api::finish_data(String::from("METHOD_NOT_FOUND").as_bytes()),
}
} else {
anonymous();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment