Dual-VM Implementation for CoinEx Smart Chain

Motivation

WebAssembly is a fast, low-level and light-weighted virtual machine, which has been a great success in web environment. It can support many languages such as C/C++, Rust, Golang and AssemblyScript and extends the programability of frontend largely from pure javascript. And its ecosystem is getting richer and richer every month.

How to re-use the legacy code developed for ETH1.0

How can we deploy some legacy code developed for ETH1.0 in a new blockchain? There are following possibilities:

  1. Compile Solidity source code to WebAssembly bytecode. SOLL is such kind of compiler. However, it is still under development.
  2. Transpile EVM bytecode to WebAssembly bytecode. Since high-level information is lost when compiling Solidity source code to EVM bytecode, such transpilers can not make enough optimizations and the resulted WebAssembly will be large and slow.
  3. Compile a EVM interpretter written in C++ or rust into WebAssembly bytecode, thus we can use WebAssembly VM to interpret EVM bytecode. This method certainly works. But since WebAssembly VM is still slower than native binary, this method has performance penalty.
  4. Implement both an EVM and a WebAssembly on the same blockchain. This is the most straight-forward method.

Build the common infrastructure

Although they follow different bytecode specification, evmone and warmer has many in common. For example, they both need host functions to work.

type ContractExecutor interface {
Execute(ctx interface{}, rev evm.Revision,
kind evm.CallKind, static bool, depth int, gas int64,
destination sdk.AccAddress, sender sdk.AccAddress, input []byte, value int64,
code []byte, create2Salt []byte) (output []byte, gasLeft int64, err error)
}

ABI and Interoperability

When a smart contract is called, by an external account or another smart contract, it takes a byte slice as input and outputs the results as another byte slice. Looking from the external world, no matter a smart contract is written in EVM or WebAssembly, it behaves the same way. You cannot distinguish a contract in EVM from a contract in WebAssembly, just from its behavior showing to the external world.

Summary

Through modularization and abstracting both VMs into one common interface, we maximized the reusable code and limit the effort of supporting an extra VMs to just writing some adapter logic. CoinEx Smart Chain can support both EVM and WebAssembly VM, and its smart contracts can interoperate with each other even they run in different VMs.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store