Substrate 介绍

github

和同事聊天的时候听他提到的一个区块链技术,据说可以一键发链(当然有点夸张),不过确实在 15 分钟之内就可以搭建一个全新的区块链出来。
很牛的设计。参考了 Web 框架类似的想法,把通用的部分抽象出来了。
看了之后很兴奋,这个就是我想要的东西。

使用 Rust 语言开发的。

由 Gavin Wood 带领的 parity 团队打造,Gavin Wood 是前以太坊 CTO

https://polkadot.network/#cover

What’s included in Substrate

many tools come working out-of-the-box, including:

  • Polkadot connectivity
  • Hot-swappable, pluggable Consensus
  • Hot-upgradeable, pluggable STF
  • Flexible and general sync
  • Pub/Sub WebSocket JSON-RPC
  • Light client
  • Transaction queue
  • Pervasive, secure networking
  • JavaScript implementation
  • Substrate Runtime Module Library (SRML)

Because of Substrate’s novel on-chain upgrade mechanism, you will be able “hot-swap” consensus algorithms or upgrade STFs without the need to hard fork. The latter, a forkless upgrade mechanism was first publicly demonstrated on the Polkadot “Krumme Lanke” testnet on July 17th, 2018, and continues as the PoC-2 testnet at the time of this article’s release.

Several consensus algorithms are supported in the 1.0-beta, with more are coming soon, including:

  • Rhododendron (instant finality)
  • SHAFT/GRANDPA (progressive, adaptive finality)
  • Aurand (probabilistic finality, constant blocktime)
  • Ouroboros (live, probabilistic finality) (planned)
  • PoW (e.g., Ethash) (planned)
  • Parachain (PoV, Polkadot consensus) (planned)
    Your can also design your own consensus algorithm with Substrate’s API.

特点

提供如下特性,开箱即用。

  • 共识机制,不可逆性,出块投票逻辑,拜占庭容错机制。
  • p2p 网络。比如 p2p 节点的搜索,同步等等。
  • 运行时的 WebAssembly 虚拟机。可以运行智能合约,或者其他基于 Substrate 开发的项目。
  • 在浏览器中运行一个节点,并与其他节点通信
  • 跨平台的数据库 / 文件存储系统,还支持浏览器环境
  • 客户端平滑更新。对于可能影响共识的更新,会将代码编译成 WebAssembly 的执行文件,然后部署时作为网络上的一条消息。 这样的机制提供了这种可能,就是你还可以本地保有多个版本的共识代码,并在本地执行一个与部署环境不同版本的共识,Substrate 会去处理本机环境与部署环境的差异。因为 WebAssembly 有回退机制,所以保证不会有不受控制的硬分叉或其他难以解决的共识导致的问题。
  • Polkadot 发布时立即接入你的项目。可以共享 Polkadot 的安全性和链之间的互操作性。

启动并运行一个完整的区块链需要做什么

  1. 初始化块的函数。该函数根据前一个块的头创建一个新的块。
    块的头包括:1. 块高度 ; 2. 块状态的加密索引,主要用于轻客户端验证块是否正确。 加密索引类似于 hash; 3. 块中二进制 BLOB 的加密索引 ; 4. 父块的 hash 值 ;
  2. 块头的自定义数据。轻客户端仅同步块头,更新轻客户端时,注意处理块头中自定义的数据。
  3. 将交易写入块的函数。 这个函数也更新链的状态(例如,账户余额);
  4. 生成块的函数。生成一个写好头、写好 BLOB 数据的块。这个块就可以广播出去了。
  5. 校验块的函数。 全节点运行这个函数,确认接收到的块在接受之前是有效的。 例如,在资产链中,你可以检查是否有人人花费超过其自身余额的钱。

模块

Substrate combines three new technologies: Rust, WebAssembly and Libp2p in order to create a framework that makes building upgradable, efficient and innovative blockchains easy. In short, Substrate helps you build the best blockchain you can.

AfG is an internal codename “Al’s Finality Gadget” due to @AlistairStewart who invented it

libp2p

f4e27cee.png
  • Transports:传输层,TCP,uTP,QUIC,SCTP……
  • Discovery:网络发现层,mDNS,bootstrap,DNS,Kad……
  • Peer Routing: 节点路由,mDNS, KadDHT……
  • NAT Traversal: NAT穿越层……
  • Content Routing: 内容寻址……

libp2p的主要功能是:

  • 发现节点
  • 连接节点
  • 发现数据
  • 传输数据

libp2p 规范及其实现的目标是:
允许使用各种:
传输协议: TCP、UDP、SCTP、UDT、UTP、QIC、SSH等
认证传输协议:TLS,DTLS,CurveCP,SSH等
高效使用套接字(连接重用)
使对等体之间的通信在一个套接字上复用(避免握手开销)
使用协商过程使多种协议和不同协议版本在对等体之间使用
向后兼容
能在现有系统中工作
利用当前网络技术的全部能力
有 NAT 穿透功能
允许中继连接
启用加密通道
有效利用底层传输(例如本地流复用、本地AUTH等)。

Substrate gives blockchain innovators the framework for maximum freedom with minimal effort. It is highly customisable, adaptable, and made by blockchain developers for blockchain developers. It allows developers to focus their effort on the thing they care most about—the state transition function—while providing them with the networking, consensus and security as well as an upgrade path to interoperability with Polkadot.

Substrate’s revolutionary generality and developability is evidenced through it being used to build four wildly different blockchains including Polkadot and Ethereum 2.0 simultaneously and in a fraction of the time it would otherwise take (an early version of Ethereum 2.0 was prototyped in a few weeks).

relative chapter
https://medium.com/polkadot-network/polkadot-poc-2-is-here-parachains-runtime-upgrades-and-libp2p-networking-7035bb141c25

module

Substrate is separated into Substrate Core and Substrate Runtime Module Library (SRML) and comes with a pre-assembled Substrate Node that is simple to configure.

Substrate Core

is the minimal Substrate base for building blockchains. Substrate Core gives you a foundation that is far easier than building a blockchain from scratch, though you don’t get a finished blockchain. It includes:

  • Pervasive and secure networking via libp2p
  • Storage
  • Telemetry
  • Light client
  • Block synchronisation
  • Extensible JSON-RPC API endpoints
  • Low-level JavaScript utils
  • Transaction queue and block production mechanism
  • Sandboxed WebAssembly interpreter
  • Interchain connectivity via the Polkadot protocol
  • Crypto primitives library
  • Chain specification and versioning
  • Pluggable consensus

Substrate Runtime Module Library (SRML)

The core architecture of Substrate separates the basic functionality of a network blockchain node and the chain-specific implementations by executing the latter in a separate WebAssembly environment. You have absolute freedom to write the chain logic in any language that can compile down to WebAssembly (Rust, C/C++, C#, Go, etc). You can add modules from the Substrate Runtime Module Library, or SRML. With SRML, you can take off-the-shelf components, plug them together, and build a blockchain that does what you want it to do. Modules include:

  • Accounts & Balances – basic cryptocurrency, including account management and viewing
  • Assets – simple, secure additional on-chain fungible assets
  • Consensus – setting and modifying runtime code and storage (i.e. the set of authorities as a list of session keys on-chain), and reporting offline or misbehaving validators
  • Contracts – turbo-charged Wasm-based smart contracts
  • Council – council election and proposals
  • Democracy – public proposals and referendums
  • Sessions – key rotation for authorities
  • Staking – Proof-of-Stake logic, including both staking and nominating of validator accounts
  • Timestamp – have your chain know about time
  • Treasury – decentralised grants, similar to a DAO

So, for example, if you want to create a Proof-of-Stake smart contract blockchain, you would take Substrate Core and plug in the Accounts & Balances, Staking, and Contracts modules. Once Polkadot launches, you could also make it interoperable by plugging it into Polkadot.

Substrate Node

Finally, Substrate Node allows you to deploy a blockchain with maximum ease. Soon you will be able to configure Substrate Node with a JSON file.