WebAssembly on the Internet Computer

DFINITY
The Internet Computer Review
7 min readSep 29, 2023

DFINITY joins the newly launched WebAssembly Research Center at Carnegie Mellon University to further explore WebAssembly applications and optimize various features of the Internet Computer. This complements existing partnerships with ByteCode Alliance and W3C and reinforces the importance of WebAssembly in future development.

Written by Ulan Degenbaev

WebAssembly, a.k.a Wasm, is a virtual machine for executing general purpose code. It was first announced in 2015 as a collaboration between major Web browser vendors, including Apple, Google, Microsoft, Mozilla. The original designers envisioned it as a safe and secure execution environment for running untrusted code on the Web with near-native performance. WebAssembly was officially released in 2017 accompanied by a paper “Bringing the web up to speed with WebAssembly”. Since then, WebAssembly has worked seamlessly on all major browsers.

When designing the architecture of the Internet Computer, the DFINITY Foundation recognized the potential of WebAssembly as a virtual machine for blockchain and invited a co-designer of WebAssembly, Andreas Rossberg, to work on its development. Long story short, WebAssembly has been a core part of the Internet Computer from the very beginning, and continues to play a key role in enabling large-scale, high performant, decentralized applications.

Today, the WebAssembly ecosystem is flourishing with more than 30 runtimes and more than 20 programming languages. And to stay abreast of the latest developments, DFINITY actively collaborates with experts in the field. The Foundation is a member of the World Wide Web Consortium (W3C) as well as Bytecode Alliance — an organization that develops a high-performance implementation of WebAssembly called Wasmtime runtime. And this week, DFINITY proudly joins the WebAssembly Research Center at Carnegie Mellon University (CMU).

WebAssembly as a blockchain virtual machine

The Internet Computer is a blockchain-based platform that hosts general-purpose applications. It aims to bring the benefits of the blockchain such as decentralization and statefulness to cloud computing. Conversely, it can be also viewed as bringing cloud features such as fast, scalable, general-purpose computation into the domain of blockchain.

WebAssembly plays a crucial role in this mission as it is a general-purpose virtual machine designed to be secure, safe, fast, and deterministic. These attributes align perfectly with the blockchain use case, which involves executing untrusted code across multiple untrusted nodes. An additional advantage of WebAssembly is its rich ecosystem with the support of a wide range of programming languages. This means that developers don’t have to learn a custom blockchain-specific language in order to write smart contracts and decentralized applications.

How Internet Computer uses WebAssembly

Speaking of developers, this section walks through the application development journey from writing code to its deployment and executing, highlighting the role of WebAssembly at each stage.

Application development begins with choosing one of the supported programming languages. While WebAssembly supports more than 20 languages, only four currently have Canister Development Kits (CDK) — a suite of libraries and scripts for building WebAssembly binaries that are compatible with the Internet Computer. These languages are Motoko, Python, TypeScript, and Rust. Thanks to a community project that implemented support for the WebAssembly System Interface (WASI), development of new language CDKs is much easier than before. This will hopefully pave the way for the addition of more languages by the community in the future.

The next step after choosing the programming language is to write the actual code of the application. The programming model of the Internet Computer is based on the Actor Model. A deployed WebAssembly binary can be thought of as an actor that receives messages from users and other actors and sends messages to other actors. Each actor has its own state and memory. On the Internet Computer, these actors are referred to as canisters, which are enhanced smart contracts. An application may include multiple canisters. Figure 1 shows the process of building and deploying a canister. The source code of the canister, written in a high-level language, is linked with the CDK library to generate a WebAssembly binary. After that the WebAssembly binary can be deployed as a canister on the Internet Computer.

Figure 1. Building and deploying WebAssembly binary

Once deployed and running, the canister receives messages from users or other canisters, and handles these messages by executing WebAssembly code. Figure 2 shows the high-level steps involved in the execution of a message.

Prior to execution, the WebAssembly binary is instrumented and compiled into the machine code. Instrumentation inserts system code to the WebAssembly binary to count the number of executed instructions. This is necessary: 1) for charging cycles (also known as gas on other blockchains) for execution, and 2) for properly handling long-running execution to ensure they don’t halt the blockchain’s progress.

The Internet Computer employs the scheme known as ahead-of-time compilation, which means that the WebAssembly binary is compiled into the machine code ahead of its execution.

Figure 2. WebAssembly instrumentation, compilation, and execution

When a message arrives at the canister, it starts the execution of the handler function specified in the message header. The execution uses the current state of the canister and the incoming message as input and produces the new state of the canister and a list of outgoing messages as output. One of the challenges here is to efficiently keep track of the state changes as explained in this blog post: IC Internals: Orthogonal Persistence.

The Internet Computer replicates the execution process described above across multiple nodes and runs Byzantine fault tolerance consensus between the nodes. To guarantee consensus among honest nodes it is vital to ensure deterministic execution. This is where WebAssembly shines because it was designed with deterministic execution in mind. Note that WebAssembly does have non-deterministic features such as multi-threading, but they are well-known and easy to disable.

WebAssembly and EVM

How does WebAssembly on the Internet Computer compare with EVM? This is a natural question to ask for developers building on the Ethereum network, since both WebAssembly and EVM are virtual machines powering blockchains.

The most notable difference is that WebAssembly is a general-purpose virtual machine meaning that it has a wide range of applications from browsers to edge computing to embedded systems to blockchains. It also serves as a compilation target for mainstream languages. On the other hand, EVM is specifically designed for Ethereum and comes with its own language, Solidity. For example, EVM uses 256-bit values and includes some domain-specific instructions such as querying the balance, computing cryptographic hashes. Note that the Internet Computer provides this functionality as library functions instead of instructions.

In terms of compute and storage, the virtual machines show orders of magnitude of differences, as listed in the following table.

Since WebAssembly is more expressive and performant, it can in theory emulate EVM. This approach is currently being explored by the community in Bitfinity EVM.

Ethereum developers transitioning to the Internet Computer often highlight one notable difference in the programming model: The designers of the Internet Computer prioritized scalability and leaned heavily on the Actor Model. Given that each canister smart contract acts as an individual actor, it can only communicate with other smart contracts asynchronously via message passing.

WebAssembly R&D on the Internet Computer

DFINITY recognises WebAssembly as a critical part of the Internet Computer and has been investing in research and development to improve the WebAssembly integration. Here are some highlights of the projects:

Completed:

On-going:

Potential future explorations:

Building a decentralized “everything stack”

The Internet Computer was designed to function as a decentralized “everything stack” that hosts general-purpose applications as WebAssembly modules, a critical component that could eventually lead to fast, scalable smart contract technology replacing traditional IT. With WebAssembly at its core, building decentralized applications on the Internet Computer at large-scale, and with high performance is all the more possible. And the fact that WebAssembly supports multiple programming languages will facilitate developer adoption and open up opportunities for new applications.

In general, WebAssembly is becoming a choice for both the server side of systems as well as the client browser side. And the Internet Computer is a secure platform to run server WebAssembly components and code in a more distributed and secure execution environment. Being a member of the CMU WebAssembly Research Center gives R&D teams at DFINITY the ability to explore new use cases for clients and enterprises alike.

--

--

DFINITY
The Internet Computer Review

The Internet Computer is a revolutionary blockchain that hosts unlimited data and computation on-chain. Build scalable Web3 dapps, DeFi, games, and more.