Virtual Machine Improvements

NervosLearning Resources

Introduced with the Major Protocol Upgrade is the CKB-VM v1 virtual machine, which is now available on the Nervos Layer 1 Mainnet. This new version of CKB-VM improves the performance of smart contract execution and adds several new features for developers to utilize when developing smart contracts.

These upgrades may increase the efficiency of both existing smart contracts and new smart contracts, and the new features being added widen the scope of what is possible on Nervos by improving the foundations of Layer 1. The CKB-VM v0 virtual machine is still available for backward compatibility, and developers can now choose which version should be used for the execution of their smart contracts.

Key Features

RISC-V B Extension: An update to the RISC-V instruction set that improves the ability to do certain bit manipulations, which can result in increased performance, decreased code size, and decreased power consumption. This will also allow for better performance of CKB-VM, particularly when running on x86_64 processors.

Macro-Op Fusion: An update to the RISC-V instruction set that allows for two or more simple instructions to be fused into a single more complex instruction that can be executed more efficiently. Combined with the RISC-V B extension, this results in substantial improvements to the number of cycles used for certain operations. One example is dramatically improved support for BLS digital signatures, which are seeing increased adoption in the cryptocurrency industry.

Syscalls: Three new system calls have been added: VM Version, Current Cycles, and Exec. VM Version allows the smart contract to detect which VM environment it is being executed in. Current Cycles allows the smart contract to view how many cycles have been consumed thus far during execution. Exec allows the currently executing smart contract to execute another smart contract, allowing for more modular code and the possibility of chained execution of smart contracts.

Address Format: Commonly known as “CKB2021 Addresses”, this new address format allows the VM version to be specified through the `hash type` value. Specifying the new `hash type` values for CKB-VM v1 is not possible in the previous address versions, meaning only CKB-VM v0 can be targeted unless you are using the new address format.

Virtual Machine Version Targeting

Nervos Layer 1 provides both CKB-VM v0 and CKB-VM v1 for smart contract execution. This allows older smart contracts to remain fully backward compatible using CKB-VM v0, while new smart contracts can use CKB-VM v1.

It is recommended that all developers working with Layer 1 learn the basic concepts of how to target specific virtual machines so they can fully take advantage of the new features in CKB-VM v1, or revert to CKB-VM v0 for backward compatibility if needed.

The hash type value of a script is now used to specify matching criteria and the virtual machine which is targeted. Before the Major Protocol Upgrade, only hash type values “0” and “1” were available. Now a new value, “2”, is available.

Hash TypeJSON ValueMatch ByCKB-VM Version
0“data”Data Hash0
1“type”Type Script Hash1
2“data1”Data Hash1

Hash Type 0: Smart contracts which rely on hash type “0” will continue to use CKB-VM v0. This is because a hash type of “0” implies that code matching is being done by the script code data hash, meaning a specific binary is being targetted for execution. Existing smart contracts which were using hash type “0” will continue to utilize CKB-VM v0 and run exactly as they did before the Major Protocol Upgrade.

Hash Type 1: Smart contracts which rely on hash type “1” will use CKB-VM v1. This includes all smart contracts deployed before and after the Major Protocol Upgrade. This is because a hash type of “1” implies that code matching is being done by the type script hash, which generally means the script code is upgradeable by the developer. Because a specific binary is not being targeted and an upgrade path exists, developers are expected to update their code if needed since CKB-VM v1 is now being used for execution.

Hash Type 2: Smart contracts which rely on hash type “2” will use CKB-VM v1. This is because a hash type of “2” matches a specific binary similar to hash type “0”. Hash type “2” was not available before the Major Protocol Upgrade, so only new smart contracts being deployed will be able to use hash type “2” to target CKB-VM v1.

Closing

Nervos is committed to continual improvement and keeping Nervos’ smart contract platform at the leading edge of technology. Not only do these updates improve our platform, but by keeping pace with the advances in RISC-V and Rust, our ecosystem will continue to benefit from their improvements while remaining as secure as possible.

These virtual machine changes affect the lowest levels of Nervos’ platform where they are largely invisible except to Layer 1 developers working on the foundations of the ecosystem. Even though the average user may not perceive a difference right now, these updates will continue to give Nervos the edge in providing the best possible user experience in the future.