Blockchain, hardware and service-oriented architecture, is wasm about to explode?

Time:2020-1-13

Blockchain, hardware and service-oriented architecture, is wasm about to explode?

Blockchain, hardware and service-oriented architecture, is wasm about to explode?

In the last article, we discussed the huge potential of wasm on the server side. In this paper, from a technical point of view, we will interpret web assembly (wasm) in depth by compiling the rust program and C program into an example of wasm, and discuss the implementation of wasm in blockchain, hardware and service-oriented architecture (SOA).

Author: Tim McCallum, researcher of second state and open source core development.

The text is as follows:

This paper not only discusses the technology of wasm, but also discusses the future potential of wasm in a broader scope.

Technical example 1: compile a simple rust program into wasm and deploy it to a stand-alone wasm virtual machine (called wavm).

Technical example 2: write a C program, and then compile it into wasm and deploy it on X86? 64 hardware (MAC OS Catalina). In this example, we will use fastly’s local web assembly compiler and a runtime called lucet to execute.

This article will also discuss:

  • Application and implementation of wasm in blockchain (Global decentralized computing)
  • Hardware implementation (portable binary)
  • Service Oriented Architecture (SOA) implementation

What is wasm?

Wasm is a near machine, platform independent, low-level, assembly like language (Reiser and BL ä ser, 2017). Wasm gives the web low-level code that is secure, fast and portable (rossberg et al., 2018).

The wasm computing model is based on the stack machine. The instruction controls the value through the implicit operand stack, uses the (out of stack) parameter value and generates or returns (in stack) result value (webassembly.github.io, 2019).

Wasm has been greatly developed

The figure below shows the number of “web assembly” academic papers in the past few years.
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
It can be seen that the number of academic papers related to “webassembly” has increased dramatically, and the number of papers containing the keywords “webassembly” and “blockchain” has also increased.

This paper will discuss the implementation of wasm in browser and in blockchain.

Wasm implementation in browser

Wasm is designed to implement progressive web development (webassembly. Org, 2019). Wasm has many eye-catching implementations in browsers.

Case 1: online wasm maze game.

After compiling, the size of this web game does not exceed 2048 bytes!
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
The second case of wasm implementation in browser: the compression / decompression software of wasm flat, which is also eye-catching.

Wasm flat is the fastest compression and decompression software in the current browser. This kind of wasm execution in browser gives web developers the opportunity to integrate powerful new functions into their web applications seamlessly. Such wasm development means that the end-user does not need to install the third-party system level applications or switch between the third-party system level applications.

Can wasm applications like wasm flat in browsers eventually replace traditional system level competitive applications, such as winzip?

Blockchain, hardware and service-oriented architecture, is wasm about to explode?

Implementation of wasm in blockchain

Bitcoin and Ethereum use a stack based architecture similar to the stack based architecture of web assembly.

Of course, each unique stack based virtual machine has some differences. For example, in wasm, we can’t find the function similar to the well-known stack item repeated operation, such as the op_dup operation code of bitcoin and the dup1-dup16 operation code of Ethereum.
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
Replication operations in Ethereum yellow book.

Fortunately, wasm provides a fixed number of local variables for each wasm function. These variables store information in a single index space local to that particular function. More importantly, there are other ways to simulate specific stack behavior.

Another important difference is the number of items that can be pushed per operation. Looking closely at the Ethereum yellow book (above), you can notice two columns labeled δ and α.

The column labeled δ represents the number of items to remove from the stack. The next column labeled α represents the number of other items to place on the stack. Each operation on the Ethereum virtual machine (EVM) can stack many projects. In the above example, dup16 can stack 17 items.

However, in the current version of wasm, an instruction can only stack one result value (webassembly.github.io, 2019).

There are many nuances like this.

There is no doubt that building a compiler that can convert any high-level blockchain smart contract source code into executable wasm type code is very complex and arduous.

But the developers of second state recently built a compiler named sol (click here to have the video demo), which is the first compiler that allows the compilation, deployment and interaction of Ethereum’s solid smart contract on the ewasm test network.

Such pioneering work marks the beginning of the exchange of digital value and data in decentralized networks, as well as the rule-based interaction between devices. Weaving browser based devices into the decentralized blockchain architecture can make transactions without permission, anti censorship, borderless, secure and web based become the mainstream.

At this year’s devcon5 (Ethereum Developers Conference), second state is also considering building compilers from Ethereum’s intermediate language Yul to llvm to ewasm after communicating with Ethereum developers.

This new work may lead to the deployment of smart contracts written in C + +, rust, Vyper and other languages into the implementation of Ethereum’s wasm blockchain.

Soon, you will realize that the introduction of a new language (across different parts of the compiler toolchain) will bring endless possibilities for multilingual collaboration.

This is a huge potential benefit for wasm.

Wasm – closer to hardware

Wasm is not just the bytecode of a web browser or blockchain virtual machine. Do you know this?
Blockchain, hardware and service-oriented architecture, is wasm about to explode?Photo source: Raimond spekking / cc by-sa 4.0 (Wikimedia Commons)

The web runs on different browsers, different types of devices, machine architectures, and operating systems. The code for the web must be independent of the hardware and platform, so that applications run on different types of hardware and can perform the same results (rossberg et al., 2018).

Even small shells, mobile devices, desktops and IOT devices can host wasm’s operating environment. Wasm can drive microchips and even the entire data center. (Webassembly.org,2019)。
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
Solomon hykes, co-founder of docker, said earlier this year, “if we had wasm + Wasi in 2008, we would not need to create docker at all. Wasm is that important. Web assembly on the server side is the future of computing. “
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
It can be predicted that wasm is attractive to most developers in the global software development community. Because of the incredible speed, flexibility and portability of wasm’s design, it is clear that we can infer that wasm will play an important role in every computing solution in the world, whether it is on the web or not.

Technical example 1: writing wasm with rust

Let’s delve into some code. Next, we will write a rule program, compile it, and deploy it on a stand-alone wasm virtual machine.

Install Rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

add component

rustup component add rls rust-analysis rust-src

Create a new rule project

cd ~
cargo new --lib add_numbers_via_wavm
cd add_numbers_via_wavm

Edit the cargo.toml file; add the Lib section to the end of the file as follows

[lib]
name = "adding_lib"
path = "src/adding.rs"
crate-type =["cdylib"]

A brief description of “cdylib”

“If you plan to create a library to use in C language (or another language of C FFI), then rust doesn’t need to include anything specific to it in the final target code. For such a library, you need to use the cdylib craft type in cargo.toml “(doc.rust-lang.org, 2019)

Next we add the necessary wasm software and configuration

rustup target add wasm32-wasi
rustup override set nightly

Now, create a new file named ~ /. Cargo / config, and put the following build text into the newly created configuration file

[build]
target = "wasm32-wasi"

Note: wasm32 unknown Wasi of rust Lang was recently renamed wasm32 Wasi.

Rust source code

Create a file named adding.rs (adding. RS /) in the directory / home / Ubuntu / add’numbers’via’wavm / SRC, and fill in the following rule code

#[no_mangle]
pub extern fn main(a: i32, b: i32) {
    let z = a + b;
    println!("The value of x is: {}", z);
}

Compile source code

This will be

/home/ubuntu/add_numbers_via_wavm/target/wasm32-wasi/release

Create an adding? Lib.wasm file in the directory.

cargo build –release

We are about to execute the wasm file, but we must install the webassembly virtual machine first.

Create a stand-alone wasm virtual machine

In general, we use software like wasm pack to integrate the web assembly generated by rust with JavaScript, and software like wasm binden can promote the high-level interaction between wasm module and JavaScript.

But what we have to do here is totally different.

We didn’t use JavaScript, node. JS, or run any programs in the browser. We execute the rule program in a stand-alone Web assembly virtual machine designed for non web applications.

Install the virtual machine named wavm

sudo apt-get install gcc
sudo apt-get install clang 
wget https://github.com/WAVM/WAVM/releases/download/nightly%2F2019-11-04/wavm-0.0.0-prerelease-linux.deb
sudo apt install ./wavm-0.0.0-prerelease-linux.deb

Execute wasm on virtual machine

Let’s try to execute the first to wasm code we just compiled using the following wavm command

wavm run --abi=wasi --function=main ~/add_numbers_via_wavm/target/wasm32-wasi/release/adding_lib.wasm 2 2
The value of x is: 4

As you can see above, we can pass in two values (2 and 2), and the web assembly virtual machine (wavm) can calculate the sum and return the answer to us through the console. It’s exciting!!!

Technical example 2: deploying C program on X86 μ 64 hardware

We just executed rust / wasm in a stand-alone virtual machine. Now, let’s use lucet to deploy C program on X86? 64 hardware (MAC OS Catalina). Lucet is not a virtual machine, but a web assembly compiler and runtime that supports wasm on the server side.

Like wavm, lucet also supports the web assembly system interface (Wasi). Wasi is a new proposed standard for securely exposing low-level interfaces to file systems, networks, and other system facilities.

Let’s start this demonstration by compiling lucet from source.

Then, we create the Hello World C source code file (as shown below)

#include <stdio.h>
int main(int argc, char* argv[])
{
    if (argc > 1) {
            printf("Hello from Lucet, %s!\n", argv[1]);
    } else {
            puts("Hello, world!");
    }
    return 0;
}

… Location changed to where lucet is installed

cd /opt/lucet/bin

… Compile c code to wasm

./wasm32-wasi-clang ~/lucet/tpmccallum/hello.c -o ~/lucet/tpmccallum/hello.wasm

… Then you can see the following command line
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
Then, we pass the hello.wasm file to the next command, which generates hello.so

lucetc-wasi ~/lucet/tpmccallum/hello.wasm -o ~/lucet/tpmccallum/hello.so

The output of the previous command is as follows.
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
To complete this demonstration, we finally run the following command, which executes the program in a mach-o 64 bit dynamically linked shared library x86 ʄ 64 file (on Mac OS Catalina).
Blockchain, hardware and service-oriented architecture, is wasm about to explode?

What we are doing here is basically executing wasm on the server side.

Other wasm compilers and runtimes

As mentioned earlier, we have more and more wasm compilers and runtimes. These include Intel’s wasm micro runtime and wasm virtual machines. In addition to these projects, there is the wasmer project, which enables developers to compile everything into a web assembly and then run it on any operating system or embed it in other languages (wasmer.io, 2019). For example, you can first write code using go, rust, python, ruby, PHP, C, C + +, and C. The code is then compiled to wasm, which is then embedded in any of the above languages. Wasmer is also committed to creating binaries that can be executed on any platform.

Wasm’s internal working mechanism

Let’s briefly describe the internal workings of wasm, and then list the resources that will help you create a handwritten wasm application.

Digital instruction

Digital instructions are divided by value type. For each type, several subcategories can be distinguished:

  • Unary operation: consumes an operand and produces a result of the corresponding type
  • Binary operation: consumes two operands and produces a corresponding type of result
  • compare: consumes two operands of each type and produces a Boolean integer result
  • test: consumes an operand of the corresponding type and produces a Boolean integer result
  • Transformation: consumes one type of value and produces another type of result

Use load and store instructions to access memory. All values are read and written in small endian order.

Variable instructions that provide access to local and global variables.

Control instructions include if, loop and other instructions that affect code execution control.

Global state

The store represents all global states that can be manipulated by the webassembly program.

Each function instance, table instance, memory instance, and global instance allocated for the life cycle of the abstract machine are stored in a separate index location. These individual index locations can be referenced / accessed by using an address.

Address is a dynamic, globally unique reference to runtime objects (webassembly.github.io, 2019).

The value type classifies the individual values that webassembly code can use for calculations and the values that variables accept. The I32 and i64 types classify 32-bit and 64 bit integers, respectively. Integers are not inherently signed or unsigned, and their interpretation is determined by a single operation (webassembly.github.io, 2019). Types F32 and f64 represent floating-point values.

As shown below, each file is explicitly encoded by a byte.
Blockchain, hardware and service-oriented architecture, is wasm about to explode?

Wasm code online editor

Now that you have a basic understanding of wasm, it’s time to write and deploy handwritten wasm code. We can use the web-based online web assembly studio application to perform this task.
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
Wasm has a text file format of “. Wat” and a binary file format of “. Wasm”. The web assembly studio application (shown above) enables us to create wasm applications in various source formats. Include the above text format. Wat for wasm.

This is a simple function, written in wasm text within the editor.
Blockchain, hardware and service-oriented architecture, is wasm about to explode?
So what can this function do?

  • As shown above, line 1 defines the module. Effective modules can be as few as text “(module)”
  • Line 2 defines a function called “add.”. This function takes two I32 parameters, firstvalue and secondvalue. It returns an I32 variable
  • Line 3 of the code pushes the value of firstvalue into the stack
  • Line 4 stacks the value of secondvalue
  • Line 5 pops two current items out of the stack, then calculates the sum of the two values and puts the sum on the stack
  • When a function explicitly declares a return value, in this case (the result is I32), the last remaining item in the stack is always specified as the value the function promises to return.

You may want to know how to consider the parameter requirements of each individual operation and the return commitment of the entire function based on the perfect number of items in the stack.

With the “function type” to classify the function signature, the relationship between the operation and the project can be calculated in advance. More specifically, function types define the number of items “out of stack” for each individual operation and the number of items that the operation then “pushes” into the stack. This information allows explicit code validation to be performed.

The following is a description of the add operation (pop up two I32 values and merge them into one I32 value of the stack).

i32.add
------------------------
[pops off]      [pushes]
[i32  i32]   ->  [i32]

Manually convert wat to wasm

Although online products like web assembly studio can handle all compilation and translation for us. We can also perform tasks on the command line, such as creating wasm output.

tpmccallum$ ./wat2wasm adding_numbers.wat -o adding_numbers.wasm

In the command line C to wasm example mentioned above, the output in wasm binary format is illegible to the human eye. These executable binaries are not designed to be viewed locally by the operating system (i.e., VI or Emacs). Fortunately, we can rely on the pre built wasm software library to transform wasm code.

Wabt, pronounced “Wabbit”, is a very useful wasm tool library. Wabt can perform the following tasks, including but not limited to converting wasm text to wasm binary (wat2wasm), converting binary to text (wasm2wat), calculating the operation code usage of instructions (wasm_opcodecnt), converting wasm to C (wasm2c), etc.

Take the following command for example.

tpmccallum $ ./wat2wasm adding_numbers.wat -v

For the complete output of this assembly based code, see Appendix a.1.

Result

Back to our handwritten wasm demo. If you click the build and run button in web assembly studio, we will see that the function adds “firstvalue” and “secondvalue”, and now it returns the sum of these values “2”.

Blockchain, hardware and service-oriented architecture, is wasm about to explode?

conclusion

Wasm is still in a very early stage of development. However, many popular programming languages, such as C, C + +, rust, go, and C, can be compiled into wasm code for production.

This unprecedented portability is significant for developers to adopt wasm and collaborate.

We know that there are already many very impressive in browser wasm applications. , more and more wasm compilers and runtimes allow wasm to execute outside the web browser, closer to the hardware.

Wasm style compilers will inevitably get closer to hardware. This enables us to develop many efficient, portable, accessible and independent decentralization functions.

Wasm has all the capabilities of the next generation of service-oriented architecture (SOA). It can be specific to specific results, or exist independently, support abstraction, and easily share and use other underlying functional units.

This is an exciting area of cooperation. The arduous development work of many projects is in progress, all of which will show you great achievements.

Reference

  1. Doc.trust-lang.org. (2019). Cdylib Crites for C interoperability – the edition guide. [online resources] link: doc.trust-lang.org/edition-gui [visit 6 November 2019]
  2. GitHub / appcypher. (2019). Awesome webassembly languages. [online resources] link: github.com/appcypher/a [visit 27 October 2019]
  3. Reiser, M. and Bläser, L., 2017, October. Accelerate JavaScript applications by cross-compiling to WebAssembly. In Proceedings of the 9th ACM SIGPLAN International Workshop on Virtual Machines and Intermediate Languages (pp. 10–17). ACM.
  4. Rossberg, A., Titzer, B., Haas, A., Schuff, D., Gohman, D., Wagner, L., Zakai, A., Bastien, J. and Holman, M. (2018). Bringing the web up to speed with WebAssembly. Communications of the ACM, 61(12), pp.107–115.
  5. Wasmer. Io. (2019). Wasmer – the universal webassembly runtime. [online resources] link: wasmer. IO / [accessed November 4, 2019]
  6. Webassembly.github.io. (2019). Webassembly specification. [online resources] link: webassembly.github.io/spec/core/ [accessed October 23, 2019]
  7. Webassembly.org. (2019). Non web embeddings – webassembly. [online resources] link: webassembly.org/docs/non-we [visit October 28, 2019]

appendix

20191128 update

Any sequence of instructions can now use and generate any number of stack values. Instead of pushing only one generated stack value as before. The current implementation of the multi value proposal, as explained here, is in the third stage of the wasm standardization process. Appendix A.1

tpmccallum$ ./wat2wasm adding_numbers.wat -v
0000000: 0061 736d                                 ; WASM_BINARY_MAGIC
0000004: 0100 0000                                 ; WASM_BINARY_VERSION
; section "Type" (1)
0000008: 01                                        ; section code
0000009: 00                                        ; section size (guess)
000000a: 01                                        ; num types
; type 0
000000b: 60                                        ; func
000000c: 03                                        ; num params
000000d: 7f                                        ; i32
000000e: 7f                                        ; i32
000000f: 7f                                        ; i32
0000010: 01                                        ; num results
0000011: 7f                                        ; i32
0000009: 08                                        ; FIXUP section size
; section "Function" (3)
0000012: 03                                        ; section code
0000013: 00                                        ; section size (guess)
0000014: 01                                        ; num functions
0000015: 00                                        ; function 0 signature index
0000013: 02                                        ; FIXUP section size
; section "Export" (7)
0000016: 07                                        ; section code
0000017: 00                                        ; section size (guess)
0000018: 01                                        ; num exports
0000019: 03                                        ; string length
000001a: 6164 64                                  add  ; export name
000001d: 00                                        ; export kind
000001e: 00                                        ; export func index
0000017: 07                                        ; FIXUP section size
; section "Code" (10)
000001f: 0a                                        ; section code
0000020: 00                                        ; section size (guess)
0000021: 01                                        ; num functions
; function body 0
0000022: 00                                        ; func body size (guess)
0000023: 00                                        ; local decl count
0000024: 20                                        ; local.get
0000025: 00                                        ; local index
0000026: 20                                        ; local.get
0000027: 01                                        ; local index
0000028: 6a                                        ; i32.add
0000029: 0b                                        ; end
0000022: 07                                        ; FIXUP func body size
0000020: 09                                        ; FIXUP section size