概述
Welcome to the wonderful world of blockchain development with Substrate! This is the Substrate Knowledge Base, the official documentation hub for Substrate developers. The purpose of this resource is to help readers understand the multi-disciplinary field of blockchain development with Substrate. This guide is broken down into several sections that explain the principles and design decisions that Substrate is built on as well as the specific skills needed to to be an effective Substrate blockchain developer.
一些专业知识是需要的 为了充分利用Substrate,您应该对计算机科学、区块链概念和基本密码学有很好的了解。 例如:区块头,区块,客户端,哈希,交易和签名等术语应该很熟悉。 Substrate is built on the Rust programming language, which makes use of novel design patterns to enable development of code that is safe and fast. Although you don't need to know Rust to get started with Substrate, a good understanding of Rust will allow you to be a better Substrate developer. Check out the excellent resources provided by the Rust community to build your Rust development skills.
Substrate takes a modular approach to blockchain development and defines a rich set of primitives that allows developers to make use of powerful, familiar programming idioms.
Architecture
The Substrate client is an application that runs a Substrate-based blockchain node - it consists of several components that include, but are not limited to, the following:
- Storage is used to persist the evolving state of the decentralized system represented by a blockchain. The blockchain network allows participants to reach trustless consensus about the state of storage. Substrate ships with a simple and highly efficient key-value storage mechanism.
- Runtime logic defines how blocks are processed, including state transition logic. In Substrate, runtime code is compiled to Wasm and becomes part of the blockchain's storage state - this enables one of the defining features of a Substrate-based blockchain: forkless runtime upgrades. Substrate clients may also include a "native runtime" that is compiled for the same platform as the client itself (as opposed to Wasm). The component of the client that dispatches calls to the runtime is known as the executor and it selects between the native code and interpreted Wasm. Although the native runtime may offer a performance advantage, the executor will select to interpret the Wasm runtime if it implements a newer version.
- Peer-to-peer network capabilities allow the client to communicate with other network participants. Substrate uses
the
libp2p
network stack. - Consensus engines provide logic that allows network participants to agree on the state of the blockchain. Substrate makes it possible to supply custom consensus engines and also ships with several consensus mechanisms that have been built on top of Web3 Foundation research.
- RPC (remote procedure call) capabilities allow blockchain users to interact with the network. Substrate provides HTTP and WebSocket RPC servers.
- Telemetry metrics are exposed by way of an embedded Prometheus server.
用法
Substrate 被设计可通过以下三种方式之一使用:
- 使用 Substrate 节点: 您可以运行预先设计好的Substrate节点并配置创世块。 在这种情况下,您只需要配置JSON文件就可以启动一个自己链。 此时拥有的很少的可定制性,仅仅允许你更改运行时模块的初始参数,比如:余额,质押(staking),出块周期,费用,治理。关于这部分的教程,请参考创建第一条Substrate链和用 Substrate启动一个私有网络
- 使用 Substrate FRAME: 您可以使用 FRAME (包含多个模块化的运行时组合框架)轻松创建自己的自定义运行时(Runtime)。 你有极大自由度来编写自己的区块链逻辑,你可以更改数据类型,从模块库(称为“pallets”)中进行选择或添加自己的自定义pallet。Substrate 开发者节点模板 非常适合开始类似的项目,你可以查看教程构建一个 dApp 和 添加一个 Pallet
- 使用 Substrate Core: 你可以忽略所有的FRAME,从零开始设计和实现运行时。可以使用任何能生成 WebAssembly的语言来完成。 如果运行时可以与Substrate节点的抽象区块生成逻辑兼容,那么您可以简单地从Wasm Blob构建一个新的创世纪块,并使用现有的基于Rust的Substrate客户端启动链。 如果没有,那么您将需要更改客户端的区块生成逻辑,甚至可能会更改区块头和块序列化格式。就开发工作而言,这是迄今为止使用Substrate最为困难的方法,同时也为您提供了最大的创新自由。
下一步
学习更多
- 阅读开发文档:使用 FRAME 进行模块开发.
- Refer to the developer documentation for the FRAME system for runtime development.
- Learn how to create rich client applications for any Substrate-based chain by using the Polkadot-JS family of libraries.
- Dive deep into advanced topics, like Substrate's SCALE encoding, consensus mechanisms, cryptography, and storage implementation.
示例
- 跟随 教程 to learn about building and running blockchains with Substrate and FRAME.
- Refer to the Substrate Recipes to find complete working examples that demonstrate solutions to common problems.
阅读手册
- 前往 Rust 手册 that ships with the Substrate code base.