项目作者: ontio

项目描述 :
Expired , please use https://github.com/ontio/wagon
高级语言: Go
项目地址: git://github.com/ontio/ontology-wasm.git
创建时间: 2018-03-28T08:56:39Z
项目社区:https://github.com/ontio/ontology-wasm

开源协议:GNU Lesser General Public License v3.0

下载


Ontology wasm

Introduction

Ontology wasm is a VM for ontology block chain, it can also be used for other stand-alone environment not only for block chains.

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust.

Structure

Ontology-wasm disassemble and execute wasm binary codes based on Wagon project with extra memory management.

Currently, we support int, int64, float, double, string(byte array), int array and int64 array data types,since wasm only has 4 types (i32,i64,f32 and f64), that means only the 4 types data could be pushed into the stack, other complex data types must be stored in memory.

In wasm MVP version,every module can only has one linear memory.

memory

Useage

  1. create a Engine to contain the VM, example like below:
  1. type ExecutionEngine struct {
  2. service *InteropService
  3. vm *VM
  4. version string //for test different contracts
  5. backupVM *vmstack
  6. }
  7. func NewExecutionEngine(iservice IInteropService, ver string) *ExecutionEngine {
  8. engine := &ExecutionEngine{
  9. service: NewInteropService(),
  10. version: ver,
  11. }
  12. if iservice != nil {
  13. engine.service.MergeMap(iservice.GetServiceMap())
  14. }
  15. engine.backupVM = newStack(VM_STACK_DEPTH)
  16. return engine
  17. }

service contains the system apis which exists in the “import ‘env’ “ section, that means you can create any api calls implemented by golang code.

We already put some apis in env_service.go

ver represents the version of engine, you can use this field to decide how to deserialize your parameters.

Then load the wasm module(from a file or other stream)

  1. code, err := ioutil.ReadFile("./test_data2/contract.wasm")
  2. if err != nil {
  3. fmt.Println("error in read file", err.Error())
  4. return
  5. }

Pass the Parameters (Json format for example)

  1. par := make([]Param, 2)
  2. par[0] = Param{Ptype: "int", Pval: "20"}
  3. par[1] = Param{Ptype: "int", Pval: "30"}
  4. p := Args{Params: par}
  5. jbytes, err := json.Marshal(p)
  6. if err != nil {
  7. fmt.Println(err)
  8. t.Fatal(err.Error())
  9. }
  10. bf := bytes.NewBufferString("add")
  11. bf.WriteString("|")
  12. bf.Write(jbytes)

Execute the wasm vm

  1. res, err := engine.Call(nil, code, bf.Bytes())
  2. if err != nil {
  3. fmt.Println("call error!", err.Error())
  4. }

If you know the result is not a basic type(int,int64,float or double),you should get the data from memory

  1. retbytes, err := engine.vm.GetPointerMemory(uint64(binary.LittleEndian.Uint32(res)))
  2. if err != nil {
  3. fmt.Println(err)
  4. t.Fatal("errors:" + err.Error())
  5. }
  6. fmt.Println("retbytes is " + string(retbytes))
  7. result := &Result{}
  8. json.Unmarshal(retbytes, result)

You can try the tests in exec/engine_test.go and smart contract tests in exec/contract_test.go

Ontology Smart contract

Please refer to smart-contract-tutorial