How does MeerEVM work?

Qitmeer Network
6 min readAug 26, 2023

--

EVM is the abbreviation of Ethereum Virtual Machine, a stack-based virtual machine used to execute Ethereum smart contracts. 🎯MeerEVM is a virtual machine running on the QNG network, serving as the execution engine for Qitmeer smart contracts, and is also compatible with all Ethereum smart contracts.

🐬The working principle of MeerEVM can be briefly divided into the following steps

Receive smart contracts: Users submit smart contracts to the QNG network and save them on the blockchain.

Compile smart contracts: Compile smart contracts into bytecode, which includes a series of underlying instructions.

Execute smart contracts: MeerEVM interprets bytecode as machine code for executing smart contracts. During execution, MeerEVM can also read and write Meer Coin amounts and other data.

Verify smart contract: After execution, MeerEVM verifies whether the execution result of the smart contract meets expectations. If execution fails, the transaction will be revoked.

Through these steps, MeerEVM realizes the secure and transparent execution of smart contracts, and verifies the execution results. At the same time, MeerEVM injects reliability and compatibility into the development of Qitmeer.

Figure1 MeerEVM work flow chart

Next, we will dive into the core concepts of MeerEVM to provide a basic overview of the system, although some key technical details may be overlooked along the way.

🐬Opcodes ( machine code )

MeerEVM uses a series of instructions called “ opcodes “ to perform various tasks. There are more than 140 opcodes in MeerEVM that can execute different processes.

What the opcode does is that, since MeerEVM cannot directly parse instructions written in Solidity, which is a language used to write smart contracts. As a result, smart contract code is compiled into opcodes that enable them to run in the MeerEVM environment.

🐬Gas ( fuel )

Gas is the computational resource required for executable code programs in the QNG network, similar to the “ fuel “ needed to drive a car in real life.

Each time an executable code operation is performed on the chain, a certain amount of gas is consumed, the amount of which depends on various factors such as the complexity of the operation and the congestion level of network transactions.

Gas is measured in “ Gwei “ ( QNG network unit ), 1MEER = 1 x 10 ^ 18 wei = 1 x 10 ^ 9 Gwei.

MEER A common unit of Gas measurement

Gas consists of two elements: limit ( Gas limit ) and price ( Gas Price ).

The Gas Limit is the maximum amount of Gas that the user is willing to pay ( at least 21,000 ) to perform an operation or confirm a transaction.

The Gas Price indicates how much MEER they are willing to pay for a unit of Gas.

Generally, Gwei is used as the unit; the higher the Gas Price paid by the sender, the higher the priority of its transaction, because miners will get more rewards. The sum of the gas value actually consumed in the execution is called Gas used.

If the gas used does not reach the gas limit, then the transaction service fee will only be charged according to the actual gas used, and the gas that is not used up will be returned to the original account.

Figure2 Gas costs

When making each transaction, the sender sets the Gas Limit and Gas Price, and sets the Gas Limit * Gas Price to calculate the transaction fee of Qitmeer.

The default setting of the Gas Limit for ordinary transactions is 21000. If you set Gas Price = 1000000000wei = 1Gwei, the transaction fee for this transfer is: Tx Fee = 21000 Gwei = 0.000021 MEER。

🌐 Here is an example of CandySwap’s usdt and meer exchange contract transfer transaction:

This transaction includes basic information such as transaction hash, transaction status, transaction packaging block, transfer amount, and wallet addresses of both parties to the transaction.

  • Gas situation of contract transfer transaction. If the Gas Limit is set to 321895 and Gas Price = 28800000000wei = 28.8Gwei, the maximum transaction fee for this transfer is: Tx Fee =321895 * 28.8Gwei = 0.009270576 MEER。
  • The actual gas value gas used during execution. Gas used is 131341 (only 40% of the value set by Gas Limit), then the actual transaction fee for this transfer is: Tx Fee =131341 * 28.8Gwei = 0.0037826208 MEER。

All unused gas is returned to the sender’s account.

【 ⚠️ Note:If the limit value set by Gas is too low, then the transaction may be considered invalid and will be cancelled due to the “ insufficient Gas “ error, and the Gas used for calculation will not be returned to the account.

Regardless of whether the command you execute succeeds or fails, a calculation fee is required. Even if it fails, the node verifies and executes your transaction ( calculation ), so it must be the same fee as successfully executing the payment.】

🐬Smart contracts

Smart contracts are computer programs that automatically execute contracts based on blockchain technology. Its essence is a piece of code that can automatically execute the program and conduct transactions and cooperation according to agreed rules.

The characteristics of smart contracts include immutability, autonomy, and transparency. Immutability means that the code of a smart contract cannot be changed after being deployed on the blockchain. Autonomy means that smart contracts can be executed autonomously without external control. Transparency is reflected in the fact that their performance rules are written in public code.

Smart contracts are widely used in various fields such as digital finance, supply chain management, and government contracts. In government contract services, smart contracts automate the execution of transactions, eliminating the possibility of human intervention. In the field of digital finance, smart contracts can automate the purchase and sale of financial products, thus standardizing the entire transaction process.

The biggest advantage of smart contracts lies in their automatic execution characteristics, which can avoid human operations and the intervention of intermediaries, thereby significantly improving transaction efficiency and reducing transaction costs. At the same time, the decentralized properties of smart contracts also enhance the transparency and credibility of transactions.

🐬Transaction

There are two types of transactions, message invocation and contract creation. Message invocation includes amount transfer and information transfer.

Amount transfer: This involves the transfer of MEER transactions, through instructions from the wallet account, instructing MeerEVM to send MEER from one address to another.

Information transmission: Not every transaction involves value transfer, and some transactions may simply transmit arbitrary data, enabling interaction and data sharing between different contracts.

Contract creation: An ordinary external ownership account ( EOA ) can create contracts. Contract creation is an important transaction type that allows accounts to deploy a brand new smart contract to the blockchain network. These contracts will be saved on blockchain and can be automatically executed according to pre-set rules.

Figure3 Message invocation and contract creation

🗣 Official website publication

https://qitmeer.io/#/library/article/107

--

--

Qitmeer Network
Qitmeer Network

Written by Qitmeer Network

Qitmeer Network is the next generation payment network infrastructure based on BlockDAG technology.