Deep into Cartesi Machine
Before introducing Cartesi Machine, let’s briefly introduce what a virtual machine is. Like the JAVA virtual machine, the blockchain virtual machine is the running environment of the blockchain program. Virtual machines are divided into process-level virtual machines and system-level virtual machines. Process-level is designed to execute computer programs in a platform-independent environment. A system-level virtual machine can run an unmodified operating system on it. At present, blockchain virtual machines are divided into EVM virtual machines, WASM virtual machines and RISC-V virtual machines.
EVM
EVM is a lightweight virtual machine used to run various smart contracts on the Ethereum network. EVM is a high-level abstraction built on a native operating system to simulate a physical machine, and EVM is a process-level virtual machine. Using EVM, the same platform can run on many different operating systems and hardware architectures, and these networks can be accessed by different clients distributed on different machines around the world. The EVM simulates the functions executed on the physical CPU and is responsible for most of the functions on the Ethereum network.
EVM features
- Deterministic: Deterministic means that the virtual machine can provide the same output for a certain set of inputs.
- Isolable: Smart contracts need to run in a completely isolated environment.
- Terminable: Ethereum smart contracts are Turing complete, which uses a “gas” limit to terminate smart contracts that exceed the execution limit.
Therefore, EVM is a decentralized processing unit of the Ethereum network. Regardless of transfer operations or contract interactions, it processes the instructions and operands sent to the virtual machine, creates a runtime environment for different smart contracts on the Ethereum network, and updates storage, state, block information and world state.
WASM
WASM is a binary instruction format based on a stack-based virtual machine. It is designed to be directly compiled into WASM intermediate bytecode using high-level languages (eg C/C++/Rust). WASM is a process-level virtual machine. WASM virtual machines are already supported by some of the top internet companies and also run in all popular browsers. The WASM virtual machine was originally designed to solve the increasingly severe performance problems of Web programs, and is currently used by more and more non-Web projects.
WASM Features
- Fast, efficient, and portable: By leveraging common hardware capabilities, WASM code can run at near-native speeds on different platforms.
- Readable and debuggable: WASM is a low-level language, but it has a human-readable text format, which allows coding and debugging by humans.
- Stay safe: WASM is restricted to run in a secure sandbox execution environment. Like other web code, it follows the browser’s same-origin policy and authorization policy.
- Do not break the network: WASM is designed to live in harmony with other network technologies and maintain backward compatibility.
Cartesi Machine
RISC-V is an open instruction set architecture (ISA) based on the principle of reduced instruction set computing (RISC). It is an open source CPU architecture. Cartesi Machine is a system-level virtual machine that mimics RISC-V CPU hardware, so you can use any high-level language you are good at for smart contract development. Cartesi Machine implements a minimal 32-bit integer instruction set based on the RISC-V ISA and can be extended to 64-bit. Cartesi designs the physical memory distribution corresponding to the virtual machine, including Processor shadow, Board shadow, RAM, ROM, interrupts and hosts target interface, and also includes a complete Linux operating system, GNU compilation set and GNU C library. In this way, Cartesi Machine executes the complex smart contracts deployed inside it, records the machine’s internal state, inputs and outputs three parts of information for blockchain state transfer, and broadcasts blocks. Cartesi Machine uniquely records machine status information on the chain, so after the transaction is executed, the entire transaction can be easily verified.
Cartesi Machine Physical Address Mapping
Because the Cartesi Machine implements the RV64IMASU ISA, the entire processor state section contains 4KB and is divided into 64 registers, each of which holds 64 bits, which represents a 64-bit machine as well as multiplication and division integer operations, atomic operations, and optional user-level processor that executes instructions. There are a total of 99 execution instructions, including 59 integer, 13 multiplication and division instructions, 22 atomic instructions, and 5 priority instructions. The processor part also implements 59 read-only states, including two Cartesi-specific states, mapped in the lower 512 bytes of the physical address. Two non-memory devices, the kernel local interrupter (or CLINT), are mapped into address spaces 0x0200bff8 and 0x02004000, respectively, into registers mtime and mtimecmp. The Host Target Interface (HTIF) mediates communication with the outside world and its active addresses are 0x40000000 (tohost) and 0x40000008 (fromhost). ROM includes 64KB of space starting at address 0x1000, and RAM physical address starting at 0x80000000. The following is the defined physical address mapping relationship.
State Transition
Blockchain is the process of state machine making state transitions. The status of the Cartesi Machine only needs to be represented by the addresses clearly defined in the above table, and the rest of the addresses only need to be filled with zeros. The RISC-V ISA manual defines the corresponding state transition for each instruction execution, so the time complexity of all instructions executed by the Cartesi Machine is O(1). The smart contract cannot store the actual state of the Cartesi Machine on the chain, so the Cartesi Machine uses the hash value to represent the machine state, and defines a variety of additional primitives to allow the smart contract to operate conveniently. The figure below is the content relationship between the Cartesi Machine on-chain and off-chain representations. The final off-chain status information is aggregated into a hash value and recorded on the chain.
Cartesi Machine Features
- Supports a wide range of programming languages.
- Allows execution of complex computational logic without worrying about gas costs.
- The state transfer on the chain contains the virtual machine state information, which ensures that the execution of the program in the virtual machine can be perfectly reproduced.
Summary
By comparing the three virtual machines of EVM, WASM and Cartesi Machine, we found that EVM can only process 256-bit bytecode, which means that bytecode whose length is less than 256-bit needs to be converted to 256-bit before it can be sent to EVM for execution. And although the EVM can be precompiled without consuming gas to judge whether the transaction is correct, the new precompiler needs to be updated through a system hard fork, and the scalability problem is obvious. Although WASM supports a variety of traditional languages, the stability of WASM needs to be guaranteed by the browser vendor. RISC-V ensures stability by simulating real hardware, and can work at a lower level, with more development toolsets, and can naturally be combined with the field of industrial intelligence. But at the same time, the application of RISC-V on the blockchain is still in the early stage, and it is hoped that it can operate more efficiently and can be used under limited resources, including computing resources and storage resources.
Reference
https://medium.com/momentum6/the-war-on-virtual-machines-wasm-vs-evm-8e68f9d53ef4