What is a virtual machine? Why is it so important in the world of blockchain?
Hashgard Labs, a research institute of Hashgard, and BKFUND Research Institute jointly conducted an in-depth analysis of virtual machines and wrote a research report about it.
1. Smart Contract
To put it simply, smart contract is a self-validating computer trading agreement or program that does not require intermediation and can execute automatically contract terms.
Smart contracts in blockchains can facilitate transfers and describe game rules.
· Smart contract, in a narrow sense, is a computer program running on a distributed ledger with preset rules, state and conditional response, which can encapsulate, verify, and execute distributed node complex behaviors, complete information exchange, value transfer and asset management.
· In a broader sense, smart contract is a self-validating computer trading agreement that does not require intermediation and can execute automatically contract terms.
According to its design purpose, it can be divided into:
· Smart legal contract designed to be an alternative and supplement to the law
· Smart software contract designed as a functional software
· Smart alternative contracts designed to introduce new contractual relationships (such as smart contracts that stipulate machine-to-machine business practices in the Internet of Things).
2. Technical Path:
Blockchain smart contracts should meet the design requirements and implementation ideas.
· Smart contracts should be deterministic and adopt deterministic algorithms and data sources when designing.
· Smart contracts can be terminated, and can be implemented through limited commands, gas mode, resource control, and access restrictions.
At present, there are three technical ways to realize blockchain smart contracts: script mode, containerized mode, and virtual machine mode.
2.1 Script mode
Script method is a traditional way to implement programmable logic, which was firstly adopted in the BTC system. BTC’s UTXO model uses a Forth-like signature scripting system to verify the legitimacy of the transaction. Transaction generally includes an input script and an output script, which are used to unlock the output of the previous transaction and set the unlock condition of the transaction amount.
BTC script uses an reverse Polish notation in the form of a stack structure. User needs to provide matching signature, public key to the script as an execution input in order to unlock the transaction. Therefore, another feature of the script mode is that it works well with the UTXO mode.
But the shortcomings of this mode are also obvious, mainly in the scalability of the function. Therefore, in terms of functional completeness and programming extensibility, many people think that the programming tasks that Bitcoin’s script mode can achieve is very limited. Therefore, from a narrow point of view, it is considered that the script mode can only realize simple programmable features, not smart contract system in the usual sense.
The current script mode that can achieve programmable features is considered as a blockchain version 1.0 system, which can be divided into the following three categories:
1) BTC and related forks and competition token
The token that uses script mode is bitcoin and some of the early tokens of the UTXO model, including BTC’s forked Token, competitive Token, such as Litecoin, Bitcoin Cash and so on.
2) Anonymous Token
Token that boasts anonymous and privacy protection also uses the scripting system. Due to the hidden nature of privacy protection and transaction traceability, many of these tokens use an abstract and simplified scripting system, including Monero, Dash, and so on.
3) DAG Blockchain
There are other blockchain systems that use a scripted way to run contracts based on Directed Acyclic Graph structures (DAGs). Due to the inherent characteristics of the DAG system, including the uncontrollable transaction duration and the absence of a global sorting mechanism, it is difficult to implement a Turing-complete intelligent contract system on the DAG distributed ledger system. Therefore, in order to improve TPS and simplify transaction processing, some DAG-based distributed ledger systems will also use scripting mode to build their own programmable systems.
Containerization is a new type of virtualization technology that has emerged in recent years and is different from virtual machines. In contrast to the virtual machine’s added layer of intermediate environments in the user program and the underlying environment, container technology only needs to package the dependencies required by the application to run independently, without the need for an additional virtual operating system environment.
The containerized approach, compared to the virtual machine approach, is relatively more independent and flexible and can deploy more resources.
Although containerization technology is easier and more flexible from the perspective of overall system architecture, some more “heavy” system factors need to be considered if thinking about one single app, because files and systems can be accessed in container environment. This puts more demands on the operating environment of the blockchain, so blockchain that adopts this method is relatively rare.
A typical blockchain project that uses containerization is the Hyperledger Fabric. Its smart contract works by deploying a chain of code on the node, and all related nodes start a chain code process that runs independently in the Docker container. The chain code completes the interaction with the node through the external gRPC interface in the container.
At present, Fabric still manages and maintains chain code in a manual and base way. Because it is in the environment of the alliance chain, all nodes that are allowed to join the network can use the system resources, that is, the access restriction method.
This is more obvious in maintaining chain code life cycle: in the Fabric 1.3 version, only four commands, package, install, instantiate, upgrade are provided. A command to stop and start the chain code without actually deleting the chain code may be provided in the future.
Therefore, if you want to construct a smart contract environment in a containerized way in a public-chain environment, you need to adopt more methods such as resource control to adapt to the transformation.
2.3 Virtual Machine Mode
The most common way to implement smart contracting is the virtual machine approach. It provides a program with a completely transparent execution environment.
A typical application of this idea can be traced back to Java’s JVM virtual machine in traditional IT technology. The goal is to implement the “write once, run everywhere” feature, rather than having the program developer write different versions of the program for each different server. This feature is exactly what is needed for distributed deployment and operation of smart contracts: the difference between the block execution node’s own execution environment operates consistently on all nodes, to achieve the deterministic characteristics.
Currently, blockchain virtual machines mainly have six directions:
1. Neo VM (AVM, the compiler core is based on .NET CLR MSIL), currently supports the .NET series of languages (C#, VB.NET, F#), Java series languages (Java, Kotlin), Python.
2. WebAssembly (Wasm), currently supports C/C++/RUST/Go.
3. Script language virtual machine (Chrome V8 engine, Lua virtual machine, etc.), js is currently supported.
4. RISC-V (an open-source CPU instruction set architecture), currently supports Solidity.
5. Create a new virtual machine and language, similar to EVM. EVM & solidity (ethereum),Move VM & move (libra).
6. Support multiple virtual machines and multiple languages. The ontology supports neo VM & Wasm, the Thunderbolt supports EVM & Wasm, and the core chain supports neo VM & EVM.
Virtual machine VS container
2.3.1 Summary of Virtual Machine Basics
Core of the virtual machine: memory management, assembly loading, security, exception handling, and thread synchronization. The virtual machine is mainly divided into two types of virtual machines based on Stack and Register.
Pros and cons
However, when running the same code more than once, the disadvantages of the interpreter are revealed. For example, if you execute a loop, the interpreter will have to translate it again and again. This is an inefficient performance.
The source code is compiled into the target code as a whole, and the compiler is not required to execute, and it runs directly on the platform that supports the target code, so that the execution efficiency is much faster than the interpretation execution.
Pros and cons
The problem with the compiler is the opposite of the one with interpreter. The compiler needs to take some time to compile the entire source code and then generate the file to execute on the machine. For codes with loops, it executes very fast because it doesn’t need to be repeated to translate every loop.
Another difference is that the compiler can use more time to optimize the code to make the code be executed faster. The interpreter does this step at runtime, which determines that it is not possible to optimize for a lot of time during translation.
GNU (Gnu’s Not Unix) Compiler Collection (GCC), a set of programming language compilers, a free software released under the GPL and LGPL licenses, is also a key part of the GNU project and the main part of the GNU toolchain. It is the first compiler choice for cross-platform software. GCC uses the same front-end program on all platforms, producing the same mediation code, so this mediation code is compiled with GCC on various other platforms, and there is a great chance of getting the correct output program.
Supported languages: C, C++, Fortran, Pascal, Objective-C, Java, Ada, Go, etc.
Main processor structures supported: ARM, x86, x86–64, MIPS, PowerPC, etc.
It is a compiler front end for C, C++, Objective-C, and Objective-C++ programming languages. It uses the underlying virtual machine (LLVM) as its back end. Its goal is to provide a replacement for the GNU Compiler Suite (GCC).
JIT (Just-in-time compiler): combining the advantages of the interpreter and compiler. In order to solve the inefficiency of the interpreter, browsers introduced the compiler, making it a mixed mode.
In order to make execution faster, JIT adds a lot of extra overhead, including:
· expense of optimization and de-optimization overhead;
· expense of monitor recording the information;
· The memory-to-memory overhead of recovering information when deoptimization occurs;
· Memory overhead for baseline and optimized versions.
There is still a lot of room for improvement, which is eliminating overhead. One of the things that WebAssembly has to do is to further improve performance by eliminating overhead.
A JVM (Java Virtual Machine)-dependent language (Java, Scala, Groovy, Kotlin, etc.) is compiled into a bytecode, which is a class file, after a JIT (just in time) compilation. Resolved by different JVMs on different operating systems, and finally converted to machine code of different platforms, and finally executed.
(7) NET CLR
NET CLR (Common Language Runtime) An operating environment that can be used by a variety of programming languages (C++/CLI, C#, Visual Basic, F#, Iron Python, Iron Ruby, Intermediate Language (IL), etc.) The main execution engine of the .NET Framework.
LLVM (Low Level Virtual Machine) is a reusable compilation tool chain that provides an intermediate language between high-level programming languages and machine languages. LLVM itself can be used as a back end for multiple languages, providing optimization independent of the language itself and code generation for multiple CPUs. (LLVM was developed by UIUC, and the meaning of LLVM (low level virtual machine) has been exceeded.)
LLVM is for the front end of different languages, corresponding to machine code of different platforms.
The compilation process of LLVM is as follows: the source code is compiled into a file of LLVM intermediate format, and then LLVM Linker is used to link and optimize, and the obtained LLVM code is finally translated into machine code of a specific platform. In addition, LLVM supports JIT and will be in the code generation process. Insert some lightweight operation instructions to run the mobile phone information, such as identifying the hot region, and the collected information can support offline optimizer, implement profile-driven optimization strategies, and adjust native code to adapt to specific architectures.
LLVM IR(Intermediate representative)：
It can be seen from the above that the LLVM compiler first translates the source language into an “intermediate language”, and different languages have different IRs, and then translate them into a compiled language of the target platform through the backend program. LLVM IR provides three formats, namely: the IR format in memory, the binary format stored on disk, and the text format stored on disk. In addition to this, there are some file formats related to IR, which are listed below:
· End of bc, LLVM IR file and binary, can be executed by the lli command.
· Ll end, LLVM IR file and text format, can be executed by lli
· End of s, local assembly file
· out suffix and local executable file. The following image shows the conversion of the centralized file:
LLVM’s front-end compiler can use a variety of parsing compilers, usually more than clang, but in EOS is replaced by csm clang.
LLVM JIT（just-in-time) :
JIT writes the part of the original compiler to generate the machine code directly into the current memory. Through the conversion of the function pointer, the corresponding machine code is found and executed. It is often used to deal with memory management, symbol redirection, and processing external symbols.
Once the browser has downloaded WebAssembly, it can be quickly converted to machine assembly code. It is defined as “streamlined, short-loading format and execution model” and is designed as a Web multi-programming language target file format. This means that the performance of the browser side will be greatly improved, it also enables us to implement a collection of the underlying building blocks. WebAssembly-enabled browsers recognize text in binary format, and have the ability to compile binary packages that are much smaller than JS text, decoding much faster than JS.
Wasm allows users to write in their own familiar language (currently supporting C/C++/Rust) and then run on the browser in the virtual machine engine. It supports sandbox mode, which is to write the wasm module in high-level language and then load it as a library function in JS.
WebAssembly uses a stack-based virtual machine, but it doesn’t mean it works on real physical machines. When the browser translates WebAssembly to machine code, the browser uses registers, and the WebAssembly code does not specify which registers to use. This has the advantage of giving the browser maximum freedom to let it do its best.
WASM allows languages such as C/C++ to be written in WEB. WASM is a bytecode format, which is the underlying binary syntax, load time period and high-speed execution. It is the target file format designed for WEB multi-language programming.
WebAssembly provides two formats: the readable text format wast and the binary format wasm, through the tool wast2wasm to complete the format conversion from wast to wasm. Similarly, wasm2wast implements inverse conversion.
Part of the WebAssembly module
Must be part of:
Type: The function declaration of the function defined in the module and the function declaration of all imported functions.
Function: gives an index to each function in the module.
Code: The actual function body of each function in the module.
Start: A function that can be run automatically when the WebAssembly module is loaded (similar to the main function).
Global: Declares the global variables of the module. Memory: Defines the memory used by the module.
Data: Initialize imported or local memory.
Element: Initializes an imported or local table.
Wasm VS JS
· The current WebAssembly can only use numbers (integer or float) as arguments or return values
Wasm VS asm.js
Technical Advantages of ATM
· High performance: WASM uses binary encoding and has excellent performance during program execution;
· Low storage cost: binary-encoded text takes up less storage space than text format;
· Multi-language support: Users can write smart contracts in multiple languages such as C/C++/RUST/Go and compile them into WASM formatted bytecodes;
Reasons why Wasm performs faster than JS:
· In the re-optimization phase, WebAssembly does not re-optimize. The optimization hypothesis of the JS engine may result in the “abandon optimization code <-> re-optimization” phenomenon.
· In the garbage collection phase, WebAssembly garbage collection is manually controlled and is more efficient than automatic recycling.
Combination with blockchain
Web-side compiled languages are mostly interpretive in the JS era. Although they are easy to understand by users, they are not efficient, especially JS. Node.js offers a framework to write local and server-side applications, but it is too inefficient for cryptographic computing and image processing. HTML5 solves a lot of browser problems in features and performance standards, but H5 still uses JS as the main language and does not solve the essential problem. Wasm came into being when many web developments were unable to unify their own compiling languages.
In many projects on the Web platform, support for native new features requires a Web browser or Runtime to provide complex, standardized APIs, but Java APIs tend to be slow. With WebAssembly, these standard APIs are simpler and can be opearted at the bottom. For example, for a facial recognition Web project, we can implement the data flow by a simple Java API, and the face recognition native SDK does the work done by WebAssembly.
So for the blockchain DApp, its meaning is very clear:
1. Allow developers to develop in other languages and then load them on JS.
2. Improve program performance and allow the development of large blockchain DApps.
This is why ETH, EOS and other projects want to use the wasm technology.
Since the EVM needs to be pre-compiled and gas needs to be a cost, the cost to program on the EVM is actually very high. At the same time, it doesn’t solve the problem of bloated EVM. Last, Solidity is harder to learn than its language bacis C.
Wasm is memory-safe and platform-independent structure, and can be effectively mapped to all types of CPU architectures. Its instruction set is efficient and maintains sufficient portability. In addition, the Wasm instruction set can be easily deterministic by removing floating point instructions, which will make it suitable for replacing the EVM language.
At the same time, Wasm can achieve untrusted programming without consuming more memory. Accurate calculations can be performed by stack analysis and metrology on Wasm.
Currently ETH intends to use DApps for smart contracts based on eth-WASM, while EOS, Polkadot and Ontology are for virtual machines.