Skip to main content

WebAssembly

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Mazzaroth uses a custom runtime to execute Wasm binaries of compiled smart contracts.

High Level Language Support

Many different high-level languages like C, C++, and Rust support Wasm as a target. By incorporating the Mazzaroth system interface these languages can be used to write and deploy Mazzaroth Smart Contracts.

We currently support Mazzaroth Smart Contracts written in Rust by leveraging the mazzaroth-rs library with support for more languages on the horizon.

Wasm Runtime

Contracts are compiled to WebAssembly bytecode which are loaded and executed in a custom WebAssembly (Wasm) virtual machine used by Mazzaroth. The runtime is a memory-safe sandbox execution environment which allows for a deterministic execution of functions that are defined within a Wasm binary. This runtime can be embedded with a custom system interface to provide access to memory or other outside resources.

RothVM

The Mazzaroth Virtual Machine (RothVM) is the execution environment that embeds the Wasm runtime and extends it with a system interface providing host functions that give Smart Contracts a way to interact with persistent state on the blockchain. Every Mazzaroth Node contains an instance of RothVM in which to execute transactions. Transactions may set the Wasm module by performing a contract update, or execute a function on the module with a Call transaction.

System Interface

RothVM Supports a set of functions for interacting with the system during the WebAssembly runtime. It is possible to write contracts that take advantage of this system interface by using a Mazzaroth library such as mazzaroth-rs for Rust.

The following is a list of the System Interface functions available through RothVM environment and how they can be used by the Wasm module:

NameParamsReturnsDescription
_fetch_inputi32Copies the input arguments to memory at the specified pointer location. Should call _input_length first to get the length of input to allocate enough space in WASM memory.
_input_lengthi32Returns the length of the input arguments passed to a function call.
_reti32, i32Provides the return value of a function call stored in memory. First param is the memory location of the stored return value, second is the length.
_storei32, i32, i32, i32Stores a key and value in the Contract State DB. First two params describe the key: pointer to memory and length. Second two params describe the value: pointer to memory and length.
_geti32, i32, i32Gets a value from the Contract State DB by providing a key (pointer and length) and the pointer describing where to copy the value. Should call _get_length first to get the length of the return value to allocate space in memory.
_get_lengthi32, i32i32Returns the length of the value returned for when getting a value from the State DB for a provided key. The first param is the pointer to the key in memory, followed by the key length. The return is the length of memory needed to store the value returned.
_deletei32, i32Deletes the provided key from the State DB. Params are the pointer to the key in memory, followed by the key length.
_key_existsi32, i32i32Returns whether the provided keys exists in the Contract State DB. Params are the pointer to the key in memory, followed by the key length. Returns 1 if they key exists, 0 if it does not.
_get_account_namei32, i32, i32Gets the name of an Account within the Contract State DB. The first two params are the pointer and length of memory to get the account key. The third param is the pointer in memory to copy the account name. Should call _get_account_name_length first to get the length of the name to allocate space in memory.
_get_account_name_lengthi32, i32i32Returns the length of account name for a provided account key store in the Contract State DB.
_set_account_namei32, i32, i32, i32Sets the account name for a given account key in the Contract State DB. The first two params describe the pointer and length of the key in memory. The second two params describe the pointer and length of the name to set for the account key.
_is_owneri32, i32i32Returns whether the provided account key is the owner of the contract. The params describe the pointer and length of account key in memory. The return value is a 1 if the given key is the owner or 0 otherwise.
_kq_json_inserti32, i32, i32, i32i32Inserts data into a specified table in the Contract State DB, based on provided JSON. The first two params provided the pointer and length of the table name. The second two params provide the pointer and length of the json query string. Returns 0 if the insert was successful.
_kq_query_runi32, i32, i32i32Run a query against the Contract State DB and return the hash key to lookup result. The first two params provided the pointer and length of the query. The third param provide the pointer in memory to store the hash (16 bytes should be allocated). Returns the length of the query result know how much space in memory to allocate before fetch.
_kq_query_fetchi32, i32Gets the result of query that was run against the Contract State DB by providing the hash for lookup.
_logi32, i32Writes a given message to the RothVM log location. The parameters describe the pointer and length of the message store in memory.
_log_errori32, i32Return an error message to RothVM. The parameters describe the pointer and length of the error message store in memory.