preface
Substrate Contract sheet is used to introduce Substrate contract system related knowledge. This book is led by Patract (patract.io/) and written by Aten (github.com/atenjin). … Github.com/patractlabs… New Window), we welcome people with lofty ideals to contribute to this book.
This book is mainly introduced on Pallets -Contracts (namely Wasm contract) as the main body. Therefore, the contents of this book include:
- Contract platform (module) running contract
- Pallet-Contracts
- Write the language of the contract
- ink!
- Ask!
- Solang
- Tools to help contract development
- Redspot
- Europa
- Elara
Pallet-EVM (EVM/Solidity system contract) has rich pallets in Itaipan ecology, so it will not be explained in this contract. Other pallets, such as Palm-Actor, or Libra, are in their early stages of research, so they won’t be the focus of this book.
In order to make contract developers better understand how Palm-Contracts and Wasm contract work, this book will also cover some introduction of Wasm and the introduction of blockchain contract model.
Contract review
The model of blockchain operation contract can be considered as “blockchain environment (distributed consensus system)” + “platform that can run a piece of logic” in essence, for example:
Bitcoin model can be split into block model /PoW + Bitcoin script Ethereum model can be split into blocks, state model /PoW + EVM Substrate model can be split into blocks, state model /BFT Consensus + Runtime
Therefore, in general, we can disassemble the blockchain model into:
The underlying system that runs the blockchain: the business logic that provides the distributed trusted environment chain: runs in this trusted environment
However, due to the characteristics of the blockchain system, the execution results of the same code and the same state must be consistent. Therefore, in order to ensure the consistency of the execution results at the level of providing “chain business logic”, the whole operation environment must be “side-effect free”. Therefore, there is no inconsistency between running nodes (meaning different nodes) resulting in inconsistent running results.
Therefore, to ensure this feature, most blockchains use a sandbox/virtual machine model with some pruning features.
“Platform running contract” is a kind of chain business logic, and this kind of business logic, especially the code running on it, is open, and the contract content is not controlled by the chain itself. In this case, a “sandbox” environment is needed to isolate the operation of each contract.
What is a sandbox?
The concept of “sandbox” in computing is very broad, whereas in blockchain, the sandbox is typically run by a virtual machine (since it is easiest to simulate the sandbox environment using a virtual machine).
So different chains use different virtual machines to run the contract sandbox:
Ethereum
The Yellow Book written by Gavin Wood proposed the EVM model, created the CONCEPT of EVM virtual machine and drew up the OP_CODE for EVM. Then Solidity was designed to compile into OP_CODE and the entire Ethereum ecosystem was created. The contract sandbox model equivalent of Ethereum handles blockchain, building everything from zero to one. So the Solidity syntax and what EVM can do is rudimentary compared to current blockchains (Solidity syntax, EVM stack depth limits, etc.), but it was the first to pioneer the blockchain contract VIRTUAL machine model.
Fabric:
As an alliance chain, it is different from public chain in usage scenarios. Therefore Fabric proposed the concept of chain code and ran the chain code in Docker. Docker is a heavier virtual machine (relative to EVM), so the Fabric chain code can do much more than Solidity compared to EVM, and of course the running costs are much higher compared to EVM.
EOS:
EOS adopted Wasm as a contract VIRTUAL machine, which was the selling point of EOS at the time. The EOS Wasm virtual machine runs much more efficiently than EVM running Solidity, and using C++ as the language for compiling into Wasm naturally brings many traditional developers closer. As the forerunner of THE Wasm VIRTUAL machine, EOS already demonstrated the advantages of Wasm over EVM at that time. However, due to the defects in the design of its contract model (see the next section) and the constraints of the environment at that time, coupled with EOS not being a truly decentralized system, As a result, EOS does not give full play to the capability of Wasm contract system.
The Substrate Pallet – Contracts:
Palm-contracts use Wasm virtual machines to run Contracts, and at present only the Wasmi interpreter is used for Wasm for security reasons. But its contract model is similar to EVM’s contract model (see the next section). Like EVM, Wasm virtual machines have the same features of fast startup, throwaway, high performance and scalability, while not being as massive as Docker and JVM. So Wasm is slowly being used in more scenarios than just browser environments, such as edge computing, hot updates, and so on. So more and more new blockchains are using the Wasm VIRTUAL machine as their primary solution when they need a sandbox environment. On the other hand, Wasm also has the ability to compile into Wasm from different languages, such as Rust, AssemblyScript, C++, etc., which can attract developers of all kinds into the contract development process.
other
Other blockchains will have their own solutions to achieve this “sandbox” goal, with some federation chains using cropped JVMS, some public chains using RISC-V virtual machines, and some chains (Libra) designing a virtual machine from scratch. Each of these solutions has its own requirements and characteristics, but modelically, the ultimate goal of any solution is to provide a virtual machine environment to run the contract sandbox.
Run the contract sandbox
Most of the business logic of the chain described above will run in a sandbox system, and the “running contract” business logic requires sandbox isolation. On the other hand, the Substrate Runtime is a pallets environment (running in Wasm), so pallets contract modules (Pallets -EMV, Pallets -Contracts) need to run another pallets in a pallets environment, As shown below:
Among them:
On the left is the Ethereum model, which indicates that for every contract run, an EVM virtual machine needs to be started on the chain’s platform to run it.
On the right is the Substrate model, the business logic of the Substrate chain we currently know is running in the Wasm VIRTUAL machine:
Pallet-EVM: It compels Ethereum’S EVM into Runtime Wasm, so every new Pallet is actually the same as Ethereum, so a NEW EVM VM is created to run. If the chain runs in Wasm mode, an EVM vm is generated in the Wasm VM to run the chain.
Pallet – Contracts: The Wasm contract uses Wasm VMS to run the contract. Different from Palm-EVM, if the chain runs in Wasm mode, Wasm VMS jump out of the current Runtime Wasm VM and create a new Wasm VM to run.
Currently, Wasmtime is recommended for Wasm VMS of Runtime and Wasmi is recommended for Palm-contracts (0B0D124 (New Window) before Substrate submission).
Palm-contracts can also use Wasmtime, but currently Parity thinks that Wasmtime is uncontrollable, so Wasmtime is not adopted for the time being. Currently they have plans and a prototype code to run palm-contracts using Wasmtime. (note 1)
In short, most chains use a sandbox model to run contracts. For EVM, Palm-contracts, a virtual machine is created each time a contract is run.
annotations
1.Wasmtime is a JIT IMPLEMENTED Wasm VM, while Wasmi is a pure interpreter Wasm VM.
2. The execution efficiency of Wasmtime is much higher than Wasmi.
About Patract
Patract provides solutions for parallel chain and DApp development in boca’s Wasm contract ecosystem. We help the community design and develop on-chain contract module and Runtime support, and provide DApp developers with a full stack of tools and services covering the development, testing, debugging, deployment, monitoring, data provisioning, and front-end development phases.
How to join Patract
1. For contract developers, visit the official website (Patract.io) to familiarize yourself with the test chain and tool suite. Element (app.element. IO /#/room/#Pat… Discord (Discord. Gg /wJ8TnTfjcq) Search for “Patract Open Platform” and follow the Patract wechat account
2. For parallel chain projects that will integrate Wasm contract functions, or DApp projects developed using Wasm contract, please contact [email protected] for business cooperation
3. For users, welcome to join: Telegram (t.me/ Patract) Twitter (twitter.com/PatractLabs…
4. For job seekers, we are recruiting blockchain development engineer, front-end/full stack development engineer, developer operation and other positions, you can contact [email protected]