Source of this article: PolkaWorld , original title "Substrate Minimalist Summary"
Translation: PolkaWorld Community
Today we will discuss Substrate, a library that can help you build your own custom blockchain. Substrate was developed by ParityTech and also provides the foundation for Polkadot.
Explanation from the Oxford Dictionary: A substance or layer that underlies something, or on which some process occurs. As the name suggests, Substrate can be used to grow Or build something on it. In our case, we can build a blockchain, or in Polkadot's case, we can build a complete blockchain family.
Software is ultimately about abstraction. The history of computer science shows that we are constantly getting more and more abstractions: from discrete logic to integrated circuits and CPUs; from machine code to assembly, from assembly to C, to C ++, Rust, Haskell, and so on. The same goes for programming APIs. For example, few people now write web in pure HTML. "Every problem can be solved by introducing another layer of abstraction. Except for many problems of the abstraction layer … so we invented the framework."-Rewritten in Andrew Koenig's Financial Times Each abstraction layer tries to solve a specific problem. So, what is the purpose of making Substrate? It turns out that there are many things you need to consider before you start implementing a brand new blockchain solution:
- Why do we need another blockchain? !!
- Various cryptographic primitives: encryption, signature, RNG¹, etc.
- Consensus algorithms and fault-tolerant voting
- Proof of Waste, Proof of Stake, Proof of Authority? Or something else entirely?
- Block structure and efficient storage, message serialization
- P2P network, peer discovery, block and transaction interworking
- State machine, execution logic, smart contract
- Light client support
Although Substrate doesn't solve the first problem, Substrate may help you solve the remaining problems because it provides existing implementation tools. These are carefully designed, written and tested based on our many years of experience in developing blockchains.
Of course, you can do all these tasks yourself, but I can say that you might end up with a solution that is untested and not well documented. Not to mention, usually an encryption algorithm you design or implement yourself is considered a very bad idea, unless you are a cryptographer and you really know what you are doing.
So, by providing a generic implementation of typical algorithms, Substrate lets you focus on the project itself: the business logic of the chain, its state machine.
Let's take a look at the most important part of the blockchain and see what Substrate can provide.
Blockchain as permanent storage
The sole purpose of any blockchain is to provide a verifiable, global, and permanent method of storing and transforming data, which means that all parties need to have a zero-trust approach at any point in time To check and agree what value can be recognized. Moreover, once this data is archived, it should be permanent, and according to consensus, it is impossible to tamper with it. This attribute is widely used in cryptocurrencies, where the permanent store contains the account key and its actual balance. However, it should be noted that cryptocurrencies are not the only possible blockchain applications. Basically, almost all systems that require globally consistent, permanent storage and verifiable transaction history can be implemented using blockchain in some way. Substrate provides efficient storage, is very easy to use, and is tightly integrated with the WebAssembly (Wasm) runtime.
Writing chain logic and smart contracts with Wasm means that you will have the best tools to execute logic in a fast and reliable way. However, Substrate has a way to execute code faster without any overhead from the virtual machine. The most revolutionary part of Substrate is that runtime images containing STF are stored on-chain along with other payloads. This means that the runtime and the business logic of the entire chain can be updated in a secure and verifiable manner. More importantly, because Substrate and its Runtime Module Library³ are written in the Rust programming language, they can be translated into native code and Wasm.
At any time, the client software has two copies of the compiled runtime: one is natively compiled in the software, and the other is a Wasm image to be executed in the VM. When the runtime function is executed, the client software checks if the on-chain Wasm version of the runtime matches its native built-in version. After doing so, the client software delegates execution of the runtime function to the native code version.
When the runtime image is updated on-chain, some clients have not updated their software. In this case, their node will execute the correct version by interpreting the runtime on Substrate's integrated Wasm virtual machine. Therefore, in any case, all nodes on the network are always able to correctly synchronize the chain (although the efficiency is different), thus preventing the chain from forking.
Blockchain relies on having many participants communicate over the network. The typical solution is to use peer-to-peer technology for this communication, and Substrate is no exception. P2P is a generic name for a technology used to create distributed network applications. The main idea is to establish a self-sustaining network environment in which each participant (usually called a node) can operate in the network without prior configuration or authorization. In order to ensure that nodes can join or leave the network at any time without affecting the connectivity of the entire network, Substrate uses libp2p (https://github.com/libp2p/rust-libp2p) implemented by Rust, which is very promising. Network stack, it has everything you need to build a distributed network environment.
In the simplest case, you don't even need to think about networking, because Substrate does everything for you. You only need to provide the state transition function of the blockchain and leave all network interactions to Substrate. However, if the blockchain requires custom messages to be sent, the network subsystem can be customized and extended by providing specific network protocols that clearly define the custom messages and their processing logic.
Having a state transition function that allows you to transition from one state to another is good, but not enough. You also need a way for all nodes to agree on what the next state should be. As the owner of a bank account, the last thing you want to happen is that you and the bank have a disagreement on "how much money is in your account". Blockchain allows parties to reach consensus without trusting each other (hence zero trust), even if malicious participants try to disrupt the system and steal your funds. This is done using a consensus algorithm with Byzantine fault tolerance (BFT) properties. If a system is BFT, this means that the nodes can agree, even if some of them perform poorly, including collusion, interception of messages, and offline. BFT consensus systems are able to resist varying degrees of network problems, in which messages can be reordered or delayed. Some BFT consensus systems are designed so that when nodes behave inappropriately (for example, voting for two blocks at a time), they can be punished and cut their tokens on the chain. For each consensus engine supported in Substrate, there will be a runtime module to handle proof of wrong behavior. The impact of evaluating wrong behavior can be determined by runtime. It is mathematically proven that the protocol is feasible as long as two thirds of the nodes in the network are not malicious and operate according to the protocol. This is one of the reasons why it is important to have many nodes in the network. Consensus is a key part of any blockchain application. Fortunately, Substrate provides an already available BFT consensus that can be used almost out of the box.
Depending on the usage scenario, you can use existing block authoring logic or provide your own block authoring logic. In the latter case, you can use the generic version of BFT and adapt it to your needs.
Substrate will continue to develop and more consensus algorithms will be added to it (https://medium.com/polkadot-network/grandpa-block-finality-in-polkadot-an-introduction-part-1-d08a24a021b5), including GRANDPA (GHOST-based Recursive Ancestor Deriving Prefix Agreement) finalized gadget developed by the Web3 Foundation.
In addition, since Substrate is a fully extensible and customizable framework, you can define your own custom consensus algorithms. In fact, Substrate is so flexible that it can support solutions that are not based on classic blockchain architecture. For example, we are studying how to solve the problem of blockchain throughput by designing a consensus that is not based on traditional block concepts.
Early blockchain implementations were designed like this: every node in the network maintains a complete blockchain database locally. This is now called a full client, which means that the client has everything it needs to run as a network node. Full clients (also called full nodes) are very important for chain security. But as the blockchain grows, customers' databases become larger and larger. Currently, mainstream cryptocurrencies have hundreds of gigabytes of databases. When the entire node is initialized, the first thing it needs to do is synchronize with the rest of the network. For security reasons, such a node cannot download the database from a node randomly as it is, but needs to build its own database from scratch and resynchronize all transactions since genesis (the first block of the chain). In addition to being computationally expensive, this task also requires the transmission of large amounts of data over the network. I think everyone will agree that downloading half a megabyte of data to your phone is just for a sandwich … It's impractical or it's ridiculous. That's why, almost from the beginning, blockchain developers have begun to consider ways to reduce node maintenance costs, including storage space and network throughput. In the end, the concept of light customers was born. In short, a light client is an operating mode of a blockchain node where only the most important data is stored locally, network interactions are reduced to a minimum, while acceptable security is maintained for almost all possible interactions. Moderate resource requirements ultimately allow light client nodes to be easily booted, so they can execute on mobile devices.
Unfortunately, integrating light client support into an existing blockchain is a serious task. It will be much easier to integrate light clients in the initial design of the blockchain. Substrate is specifically designed with light client support in mind. The blockchain built on Substrate has ready-made light client support.
In this article, we have skimmed the main goals and features of the Substrate framework. It will take longer to cover all aspects of Substrate, but hopefully you now have a general understanding of these concepts. Although this is not enough to start writing your code, at least for now you should know what to expect. To learn more, check out Jack Fransham's previous article "What is Substrate": https://www.parity.io/what-is-substrate/. If you want to dig deeper and start implementing your blockchain, the best place to start is the Parity Substrate Wiki (https://wiki.parity.io/). Also, don't hesitate to look at the source code, especially the ReadMe file: (https://github.com/paritytech/substrate).
- RNG stands for Random Number Generator. Not every RNG is suitable for blockchain applications.
- Substrate itself is now in a very active development phase. The code base is very unstable and documentation work is in progress. We are actively populating the wiki, this is a great place to look for more details: https://wiki.parity.io/
- The runtime module library is an optional set of large wooden boxes for Rust that handle common tasks such as parameter serialization and call dispatch, and help you build runtimes with minimal cost. This library is completely optional, so it's great to design your own runtime from scratch or use any language that can be compiled into Wasm. With the exception of Rust, only C and C ++ currently support Wasm as the target architecture.
- Welcome to Substrate: https://substrate.dev/
- Follow Substrate progress: https://github.com/paritytech/substrate
- Follow Polkadot progress: https://github.com/paritytech/polkadot
- Apply for Bootcamp: https://bootcamp.web3.foundation/