What is Subscript?
In the list of recipients of the Web3 Foundation Grant program in the third quarter of 2020, this is the first time that the Subscript language has appeared. Subscript is the Substrate smart contract API and SDK in AssemblyScript.
In this article, the author will start with the Polkadot Substrate smart contract to help readers understand Grant’s definition of Subsciript achievements.
1. Substrate Smart Contract
Before Substrate appeared, it was very troublesome to build a custom Blockchain, and many factors needed to be considered. Such as security, upgradeability, extension, cross-chain, etc., and also need to do a lot of coding and testing. Developers need to have enough in-depth understanding of the technical issues on the state tree, p2p network, consensus protocol and other chains before program the Blockchain. In the process of developing Polkadot, Gavin Wood believes that, in fact many things that everyone has done in the past few years of Blockchain development are the same. According to his previous development experience, when everyone finds that everyone is doing the same thing, they will abstract the matter and create a “wheel”. Encapsulate these high-level things into a “development framework”. Encapsulate these complex infrastructures, and developers who use this “framework” can focus more on their own business logic, instead of spending a lot of time building “wheels” to complete things which are each chain will do. Gavin Wood abstracted the existing achievements of Polkadot and Ethereum. Named “Substrate” as the basic framework for Blockchain development, and spend all time in Substrate development. So “Substrate” is the first “wheel” of the Blockchain world.
The Substrate framework provides many functional modules by default, such as Staking and Consensus, which are convenient for framework users to freely combine and customize according to their needs. The contract module is one of the functional modules. The FRAME contract module (SEAL) provides Runtime with the function of deploying and executing WebAssembly smart contracts. Whether it is any independent chain based on Substrate technology, or a future parachain. As long as the contract module is integrated, it can become a smart contract platform.
Traditional smart contract platforms (such as the Ethereum system) allow users to add additional logic to some core Blockchain logic. Because anyone can publish smart contract logic, including malicious actors and inexperienced developers. Therefore, DeFi contract security vulnerabilities frequently occur in such smart contract platforms.
The Subscript smart contract sacrifices the flexibility of “adding extra logic to logic”. Avoid some vulnerabilities that may be generated by complex logic to obtain more security and interoperability.
There is no difference between API and SDK worth comparing. API (Application Programming Interface) is some pre-defined interfaces (such as functions, HTTP interfaces), or refers to the agreement between different components of the software system; SDK (Software Development Kit) refers to the collection of development tools used by some software engineers to build application software for specific software packages, software frameworks, hardware platforms, and operating systems.
When people’s computers need to call the information in the mobile phone, they will use the data cable to connect the computer and mobile phone. The interface between the computer and the mobile phone is equivalent to the “API interface”; When a company develops a certain function of a certain software, it is packaged into an SDK (for example, the data analysis SDK is an SDK that can realize the data analysis function) and sold to other companies for development. If other companies want to develop a certain function for the software, but don’t want to develop it from scratch, they can directly purchase the SDK and use it.
If you think of the SDK as a virtual package, there is a complete software function in this package. This package is almost completely closed, with only an interface that can communicate with the outside world, and this interface is the API.
WebAssembly is designed to have good support for C and C++. In the version where Go language has added experimental support in Version 1.11, Rust has also invested heavily in it.
At this point, we can interpret Subscript.
Subscript is a native contract language designed for WASM as a whole, and provides access instructions to the bottom layer of WASM. Subscript uses the TypeScript syntax familiar to Web developers, and compiles a part of TypeScript into WASM through the AssemblyScript project. Subscript can support any smart contract development platform compatible with the Substrate architecture, and supports the development of Polkadot’s native smart contracts. Compared with Parity’s official Rust-based Ink! language, Subscript provides easier-to-use features:
A. No need for Rust development foundation, Web developers can get started quickly
Easy-to-use development environment support, can run deployment and testing based on the existing Js and IDE
4.1 Subscript Contract Compilation
Subscript uses the proven Binaryen compilation framework to compile smart contracts, focusing on reducing the volume of generated code during compilation. When dealing with WASM low-layer code, Subscript can provide faster execution speed than C++ and Rust.
Subscript adds language extensions to the Binaryen compilation framework, enabling it to directly support the mapping of object structures to contract state trees. Subscript’s state decorator has built-in state serialization and deserialization functions. After the developer adds the state decorator to the structure, the language can automatically serialize the structure into a string in the state tree, and the same is true for deserialization.
4.2 Subscript Contract Debugging and Development Environment
Subscript uses SourceMap to perform a series of debugging actions and Subscript Workbench as the IDE development environment.
Subscript uses the SourceMap mechanism to generate a mapping from compiled code to source code. SourceMap is a file about source code information, through SourceMap you can know the position and identifier of the compiled code in the source code. When debugging, the debugging tool can give the corresponding source code position at the breakpoint position, which will bring great convenience to the developer.
The smart contract virtual machine that Subscript can provide debugging support is under development. The virtual machine can provide WASM’s single-step execution function and display all stack information. After adding a breakpoint to the source code, the virtual machine can pause the current execution logic and print out detailed stack information and memory data.
Subscript Workbench is a browser-based smart contract integrated development environment (IDE). Subscript Workbench is similar to the Ethereum development environment Remix, and comes with the compilation tools needed for WASM smart contract development. DAPP developers can synchronize the code in Github in Subscript Workbench, create smart contract projects, compile WASM, and publish to the test network.
After the development of the smart contract virtual machine is completed, Subscript Workbench can also provide online debugging functions for smart contracts similar to Remix. Developers can choose to add breakpoints to the browser source code to step through the compiled WASM code.
5. the value achievement of Subscript
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, platform 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.
Subscript language realizes WASM virtual machine compatibility based on Polkadot Substrate framework. Unlike Solidity’s EVM compatibility, WASM has better backward compatibility with Polkadot’s fragmented multi-chain structure. Developers use Subscript language to develop Polkadot smart contracts, which will not produce historical baggage similar to the Ethereum smart contract platform. With the enrichment of the Polkadot Parachain ecosystem in the future, this will become crucial.