Development and Implementation of Smart Contract on TrustBase

In November 2020, the TrustBase team researched and implemented Polkadot smart contract parachain based on Subscript. In view of the lack of comprehensive and easy-to-use development tools for Polkadot smart contract development. The TrustBase team independently developed a new underlying programming language which is Subscript, based on the Substrate framework, to achieve WASM virtual machine compatibility. TrustBase designed and implemented a comprehensive Polkadot integrated development environment for the research on the development process of the entire life cycle of smart contracts. Provide contract developers with a complete smart contract Web integrated development environment that includes project management, development, debugging, deployment, execution, security detection and other functions.

This article will start with the WASM virtual machine of the smart contract execution layer in the TrustBase chain, and introduce the smart contract Web integrated development environment of the TrustBase to readers.

1.WASM(Web Assembly)Bytecode

WASM (Web Assembly) is a bytecode that can run in modern browsers. It is similar to assembly language, and its binary system format is highly compact. It can greatly reduce the size of object code files and improve the efficiency of network distribution of applications. The runtime efficiency of WASM bytecode can be close to that of native machine code. At the same time, WASM can provide compilation targets for languages such as C/C++, Rust and Subscript. This enables applications written in these languages to run in the browser. WASM is the basis of distributed system development, smart contracts will be able to be developed in any language that can be compiled into WASM.

At present, many browser debugging engine solutions for WASM (Web Assembly) bytecodes do not have the ability to debug Polkadot smart contracts. WASM bytecode is a target language developed for browsers. It is independent of various hardware architectures and has high portability and efficient network distribution efficiency. WASM bytecode has been supported by major mainstream browsers, and debugging support is provided. Currently, in Chrome and Firefox browsers, debugging support for WASM bytecode has been implemented, and the principle is to use simulation to execute WASM bytecode.

The smart contract object code of the TrustBase adopts WASM (Web Assembly) bytecode form. The independent WASM virtual machine execution engine is used to execute the smart contract program code. This allows the TrustBase to deploy smart contract solutions that support the Polkadot ecology.

Take C++ as an example. If the developer writes “Hello” in C++ language, the WASM (Web Assembly) bytecode is generated after compiling and linking. Then it is deployed to the TrustBase Blockchain system network and executed by the current block production network node, then the developer will need to deploy like this:

2.Why use WASM virtual machine

Currently, the use of WASM to run smart contract environments is the mainstream trend of public chains such as Polkadot, ETH, and EOS. For example,the virtual machine (EVM) of the Ethereum infrastructure is preparing for a complete rewrite. After rewriting, the current EVM will be replaced by a new virtual machine called EWASM, which is also the Ethereum version of the WASM virtual machine.

Although EVM has high compatibility, it requires pre-compilation, pays gas as a price, and has a high programming cost. This kind of program is essentially a script program, that is, the program translates instructions and executes it. Instead of reading the instructions and executing them by the local machine CPU, it is inefficient. The Java operation is relatively repetitive and cumbersome, and it takes a long time to execute. Since EVM requires pre-compilation, and at the same time it needs to pay Gas as a price, programming on EVM is actually very expensive. At the same time, it does not help the bloat of EVM. Finally, Solidity is more difficult to learn than its language C. WASM is memory safe, independent, and can be effectively mapped to all types of CPU architectures. As the first Java native alternative since the birth of the Web, its instruction set is highly efficient. At the same time maintain sufficient portability. In addition, the WASM instruction set can be easily determined by removing floating-point instructions, which makes it suitable to replace EVM languages.

At the same time, WASM can achieve trustless programming without increasing memory consumption. Accurate calculations can be made by stack analysis and measurement on WASM.

3.smart contract deployment execution process

TrustBase smart contract developers first create or import local contract files and enter the corresponding contract code in the contract code editor. Then select the corresponding compiler version and compilation options, and compile the contract. If a contract compilation error or warning message is generated, the developer can edit and modify the contract again until there is no compilation error. Next, developers need to select or create a TrustBase account and select the appropriate permissions to deploy the compiled smart contract to the TrustBase Blockchain. If the contract deployment fails, it means the account is wrong or the permission is wrong. Users can select new accounts and permissions to deploy again. After the user deploys the contract, the server will automatically obtain the ABI interface file of the contract and display its Action interface specification in the front-end web interface. After the developer enters the corresponding parameters according to the contract interface specification, click the execute button to execute the corresponding contract Action. The process of contract development, compilation, deployment and execution by developers in this system is shown in the figure:

4.Debugging

Currently, the TrustBase contract code does not have DWARF debugging information and cannot be debugged with GBD. But developers can convert to Wat readable format to check all instructions, and check for bugs in the Wat readable format interface.

(Wat Readable Format)

The TrustBase team decided not to use the DWARF debugging standard for the following reasons: Although the DWARF debugging information format is widely used in Unix, Linux and other systems, the famous debugger GDB in the GNU project uses the DWARF debugging information format. But the DWARF debugging information format is independent of the architecture. Whether this structure will conflict with the multi-parachain cross-chain interoperability structure that Polkadot will implement is still unconfirmed. Before Polkadot is officially operating stably, the TrustBase team will continue to use Wat readable format to check instructions.

5.Subscript Language

In order to meet the performance requirements and developer requirements of Polkadot’s cross-chain smart contract execution, the TrustBase team independently developed the Subscript language. This is a contract language compatible with Assemblyscript, which is compatible with the WASM virtual machine.

Compared to Parity’s official smart contract Ink! language, Subscript can:

No need for Rust development foundation, web developers can get started quickly;

Good interoperability with JavaScript, easy to integrate with DAPP;

Easy-to-use development environment support, can be based on existing js, IDE running deployment and testing.

These features make Subscript have a lower barrier to use and stronger backward compatibility.

6.Conclusion

Through the above content, the author introduced the design and implementation principle of TrustBase core functions. It shows that the design of the smart contract by the TrustBase team takes into consideration Polkadot compatibility and developer ecology. Finally, an integrated development environment suitable for the Polkadot structure including development, compilation, debugging, deployment, and security analysis is realized.

In the long term, Blockchain technology is still an emerging technology. The development of smart contracts is still at an early stage. In this historical stage, the differentiated solutions provided by the TrustBase for Polkadot developers deserve the attention of all Blockchain technology enthusiasts to promote the development of Blockchain technology.

This allows the TrustBase platform to deploy smart contract solutions that support the Polkadot ecology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store