Skip to content
Mihai Schiopu edited this page Jan 13, 2023 · 5 revisions

The Virtual Machine and SmartContracts

Executing certain Transactions requires the execution of SmartContracts, which are custom programs that can perform complex tasks on the Blockchain, on behalf of the caller. TODO insert brief motivation for the existence of SCs here and some examples.

The execution any of SmartContracts is always initiated by a special kind of Transaction (TODO link to section describing SC-specific transactions) which specifies the name of the SmartContract to call and what part of it to execute, while also providing some information to pass to the SmartContract as arguments / parameters. The results of the SmartContract will be stored on the Blockchain as part of the Transaction that initiated it TODO describe other kinds of output, if applicable.

When Nodes execute the SmartContract required by a specific Transaction, it is required that all of them arrive at identical results, of course - otherwise the entire Block that was being built at that time might be rejected (see Participating in Consensus for details on how Blocks are approved or rejected).

The actual task of executing SmartContract code falls to a dedicated software component called the Virtual Machine (VM). Note that this VM has nothing to do with applications such as VirtualBox or VMWare, which are system virtual machines, specialized in virtualizing hardware and running entire operating systems. By contrast, the VM used to execute SmartContract code is a process virtual machine, specialized in providing an isolated and abstracted runtime environment, independent of the physical platform or of the underlying operating system. It is thus more similar to the Java Virtual Machine, although designed for a more specialized purpose and enriched with APIs that enable the programs it runs to perform Blockchain-specific operations.

In order to execute SmartContract code, the MultiversX Node currently provides its own custom Virtual Machine, called IELE VM. The Node is not limited to this specific VM, though: by virtue of its Virtual Machine Adapter, the Node can theoretically support any number of different Virtual Machines. There is ongoing work to integrate a WASM-based VM with the MultiversX Node. Still, the VM best supported by the MultiversX Node is currently the IELE VM.

Generalities

  • Computing resources available to SmartContracts (i.e. permanent storage)
  • Execution environment of a SmartContract
  • What is there to know when writing a SmartContract?

A SmartContract is a piece of code containing individually-callable functions named Methods, which perform some predefined tasks on the Blockchain, on behalf of its caller. These tasks are designed by the developer of the SmartContract, and are available to any user of the MultiversX Network to call.

The SmartContract developer writes the code of the SmartContract in their programming language of choice, targeting a specific VM. It is important that developers document the behavior of their SmartContracts properly, because users must understand what it does before calling any of their Methods and trust that the code is not performing any malicious actions.

After writing the code, a SmartContract must be deployed to the Blockchain before any attempt to execute it. Deploying a SmartContract involves compiling the written code to bytecode, then constructing a special Transaction which, when executed by the Nodes, will cause the SmartContract to be validated, stored and published on the Blockchain.

Once deployed, Methods of the SmartContract can be called by users of the Network by creating Transactions that contain the required information.

Writing a SmartContract

  • Structure of a SmartContract
  • Compiling a SmartContract

Deploying a SmartContract

TODO

SmartContract bytecode

TODO

Calling a SmartContract

TODO

Virtual Machine Adapter

TODO