Skip to content

Latest commit

History

History
111 lines (70 loc) 路 4.68 KB

File metadata and controls

111 lines (70 loc) 路 4.68 KB
description cover coverY
WASM-based amazing virtual machine by KLY
../../../.gitbook/assets/photo_2020-05-10_21-01-55.jpg
97

馃挘 KLY-WVM

Intro

KLYNTAR WVM is based on WebAssembly which allows you to write in any language, then compile it into .wasm bytecode and run it in a secure environment.

WebAssembly is secure thanks to

  • Lack of network access
  • Lack of access to the file system
  • Lack of access to the random number generator
  • Other stuff

Thus, it is possible to locally (in your development environment, if you are a developer) create compressed and compiled .wasm files, and then publish them to the KLYNTAR network for execution.

You will be able to write in any language that you know well and compile into WASM modules, as well as use advanced features such as requests to the network or external storage of big data and much more.

More about WebAssembly

Today, WebAssembly is becoming popular all over the world and in various areas - from small modules for working in the browser to cloud FaaS services and blockchains (Cosmos, Near, etc.).

Below is a list of languages that can already be used to compile to WASM and work safely on KLYNTAR symbiotes

{% embed url="https://github.com/appcypher/awesome-wasm-langs" %}

{% hint style="info" %} Not all langs will be supported by KLY {% endhint %}

WebAssembly is not exactly a language, it's more of a low-level bytecode that executes quickly and safely. For an illustrative example, we can consider the .wat file format - these are the files of the so-called WebAssemblyText. It can also be compiled to wasm and vice versa. So for example you can repeat this procedure using the tools wat2wasm (obviously .wat => .wasm) and wasm2wat (vice versa). Just the same, .wat files allow us to see WASM opcodes, understand that there is work with the stack like low-level languages, and so on. For example, here is the implementation of a simple function that takes 2 parameters and returns their sum

File add.wat

(module
 (func (export "calculate")
 (param $value_1 i32) (param $value_2 i32)
 (result i32)
    local.get $value_1
    local.get $value_2
    i32.add
    )
 )

Next, we use the wat2wasm tool that comes with the wat-wasm package.

We compile (with the help of flags -O3z we compress as much as possible and carry out optimization)

wat2wasm add.wat -o add.wasm -O3z

And use in Node.js

import fs from 'fs';
const bytes = fs.readFileSync (__dirname+'/add.wasm');

let value_1 = parseInt (process.argv[2]);
let value_2 = parseInt (process.argv[3]);

let obj = await WebAssembly.instantiate(new Uint8Array(bytes));

let add_value = obj.instance.exports.calculate(value_1,value_2);

console.log(`${value_1}+${value_2}=${add_value}`);
node test.js 11 33
11+33=44

This was a simple use case and you learned a little more about its format by looking at the code of the .wat file.

Of course, it is difficult to write in pure WAT and therefore you will most likely use high-level languages such as AssemblyScript (an add-on on TypeScript), Rust, C ++, Go and others.

Since there are several tools for turning source code into WASM modules, we will try to use the most popular, documented and convenient ones in order to create the best working conditions for users and developers on KLY\

Build WASM modules with Rust

Ofiicial WASM builder for Go

Links

{% embed url="https://wasmbook.com/" %}

{% embed url="https://www.rust-lang.org/fr/what/wasm" %}

{% embed url="https://docs.klyntar.org/smart-contracts/kly-wvm" %}