原文作者:Mike Tang
Original source: Web3.0 Explorer
Compound (https://compund.finanice)在 2021 年 3 月正式上线了其独立链网络 Gateway(原名 Compound Chain)。Gateway 是一条独立的链,它的目标是成为一个统一的 Defi 枢纽,实现跨链的 Defi——你可以质押链 A 上的资产,在链 B 上借款——从而将所有链的资产全部打通,提升全网络资产利用率和流动性。
Gateway 的出现基于如下的判断:未来世界一定是一个多链的世界,即没有一条区块链能解决所有问题。未来会有很多条链,很多种可能性。于是人们的资产会被分散到各个独立的平台。如何提升这些分散的资产的利用率,就是 Gateway 要研究和解决的问题。
Gateway architecture diagram
As shown in the architecture diagram, the Gateway connects all the chains by developing a specific Starport (a combination of nodes or components on the Peer Chain) for each Chain.
This article will not go into much detail on Gateway's DEFI issues, but will provide a brief analysis of why Gateway chose Substrate to develop.
Compound 官方的说法:
(https://medium.com/compound-finance/gateway-623f6f48d2b6)
We choose Substrate so that we can focus on building application code rather than inventing consensus algorithms; And it's a modern framework, built on top of the modern language Rust.
Rust is a modern, security-oriented programming language. For those of you who are interested, read the Developer Survey 2020: Rust is the Most Popular Language Again. Rust has almost become the language of choice for blockchain development today. Things like Polkadot, Near, Solana, Dfinity, Filecoin's underlying layer are all implemented by Rust. The Rust language has the following notable features:
Low & have spent Memory security: With its unique ownership and lifetime design, it is theoretically guaranteed that no memory errors will occur. Solved 70% of the underlying system defects
Low & have spent Concurrency security: Concurrency security is guaranteed in concurrent programming (multithreading, multi-coroutine)
Low & have spent High Performance: It is the same level of performance as C/C++, the top of the current mainstream languages
Low & have spent Zero Overhead Abstraction: This feature allows Rust to seamlessly connect to the established ecology of the C language with no performance penalty
Low & have spent Powerful abstract expression: Rust borrows from the expressive power of many functional languages to make it more expressive
Low & have spent Modern engineering facilities: Modern auxiliary systems such as Cargo and Crates.io allow Rust to build complex projects with ease
Low & have spent Active Community: It's interesting that Rust has been developing iteratively in a community mode from the start, in stark contrast to Go's centralised development
Given these features of Rust, it is only natural to understand that Rust is almost certainly the language of choice for blockchain development today. The blockchain itself puts security first, which is entirely in line with Rust's design philosophy. Blockchain systems tend to be complex and require high levels of engineering-related infrastructure, and Rust's strong engineering design makes it particularly well suited for collaborative large-scale projects in teams and open source communities.
以注重安全性闻名的 Paritytech 公司,用 Rust 语言开发了以太坊的 parity 客户端。然后继续用 Rust 开发了新项目 Polkadot,在开发 Polkadot 的过程中,逐渐形成了一个重大决定:将区块链的所有功能,拆解成抽象的设计,实现到一个开源的、通用的区块链框架中,并以此框架为工具,构建 Polkadot 产品。而这个区块链框架的重大成果,就是本文的主角——Substrate。
Substrate 是一个用 Rust 语言开发的以通用性为目标的区块链开发框架。它的设计元素,比如密码学算法、存储结构 MPT 树,账户体系等,大部分借鉴自有史以来最成功的以太坊的基础设施,(这个可以理解,Paritytech 最早就是做以太坊客户端起家的,Gavin Wood 也是以太坊的联合创始人之一)。一个框架,要做到通用,就需要高度抽象。而高度抽象的代价往往会显得结构复杂,不易于使用。所以 Substrate 也提供了很多 DSL(领域特定语言),方便新手学习使用。简单归纳一下,Substrate 具有如下特点:
面向通用。其设计面向通用领域,而不是专为某一条链做开发的 SDK。每个团队都可以使用 Substrate 开发出一条完全独立的不依赖于任何既有网络的链出来(比如,使用 Substrate 开发的区块链可以与 Polkadot 无关,这也是 Paritytech 的设计目标之一) 功能全面。能覆盖区块链几乎所有的场景,可以说是目前市面上功能最全面的区块链框架 Runtime 代码编译成 wasm 执行。Wasm 是当今区块链业界主流的 VM 字节码选择 可定制性超强。Substrate 本身是一堆分散的组件,可以在一套规范约束下,自由替换组件,自由组合
Engineering design is a trade-off, and when it emphasizes one aspect, there will be a compromise on the other. Substrate has its drawbacks:
Low & have spent Some newly introduced concepts (such as extrinsic) need to be relearned and understood, and there is a certain threshold for beginners
Low & have spent The emphasis on generality leads to some abstractions and complex code visuals (for example, too many generic parameters). These abstractions, on the other hand, bring out Rust's advanced syntax features and make the code more noisy
Low & have spent The whole project code is large, dependent on a lot of packages (more than 1000 at present), resulting in a long compilation time (ordinary notebooks more than dozens of minutes), high performance requirements for the development machine
As you can see above, these deficiencies are primarily the result of the objective dependent complexity inherent in the design goal of Substrate: generality.
As one of the strongest blockchain development frameworks to date, Substrate is gaining popularity among more and more innovative teams, so it makes sense for the Compound team to choose Substrate for Gateway development.

Substrate overall frame drawing
As you can see from the architecture diagram, Substrate has the following major components:
Low & have spent P2P Networking
Low & have spent The Runtime Runtime
Low & have spent Storage store
Low & have spent Consensus agreement
Low & have spent RPC remote procedure call
Low & have spent Telemetry client monitoring tool
P2P network is an essential subsystem of blockchain system. Multiple nodes are connected to form the same network through P2P. By sending message data from node to node, the propagation of messages in the network may arrive after propagating through multiple relay nodes. In Substrate, use rust-libp2p (https://github.com/libp2p/rust-libp2p),此项目也主要是由 Paritytech 在负责维护。
Runtime,运行时,是区块链业务逻辑的实现部分。也就是说要用区块链干什么实际的事,就需要写在这里面。Substrate 支持将 Runtime 代码编译成 wasm 字节码或 native code 两种模式运行(随着 wasm vm 运行速度的提升,未来 native code 模式可能会被抛弃,这也能降低一些内部复杂性)。在 Runtime 中可以操作 Storage,实现状态的变更,因此 Runtime 整体也被称作状态转移函数(State Transition Function, STF)。
Storage,存储子系统,是区块链系统中不可缺少的组成部分。在 Substrate 中,Storage 用于持久化 Runtime 的逻辑对状态的变更,同时也支持对外的 RPC 状态读取接口,Runtime 中的 Event 在发出前,也会在 Storage 中做短暂的存储。共识系统的目标,也是对 Storage 中的状态达成一致。Storage 子系统在底层用的是 kv 数据库 rocksdb 或 paritydb。
Consensus, the Consensus subsystem, is used to reach agreement among the participants of the network on the status of the blockchain, also known as the "Consensus". Because it is a distributed system, the so-called state of blockchain does not have a macro perspective of God that can see a unified macro state. This state, is the state of the various nodes, each node has its own local state view, there is also a "local" global view. Each node through the local and global views, to make a decision. Consensus system to reach a consensus on each node state, pushing forward running system.
RPC,远程过程调用,用于向节点外部提供访问的接口。一个 Substrate node 本身可以作为一个服务而存在,外界可以通过这些 rpc 接口访问 node 的本地状态信息或向 node 提交变更请求。Substrate 同时提供 HTTP 和 Websocket 两种 rpc 通道。
Telemetry,客户端监测工具,用于搜集 node 的运行信息,发送到远端的 Prometheus 服务器。
Substrate 是一个通用开发框架。它为不同层次的开发者提供了三种开发风格。
1. Use Substrate's own Node to link directly. For developers who want to experience the results quickly, you can use Substrate's preset implementation of Node. You just need to modify a JSON configuration file to get running. You can customize the following:
Low & have spent The status of the original block
Low & have spent Account the Account
Low & have spent The Balance of the Balance
Low & have spent Staking ratio etc.
二:Runtime FRAME pallets 的开发。这种开发模式通过写 Runtime 中的 pallet 代码,将业务逻辑实现到 pallet 中。然后将自定义的 pallets 和其依赖的 Substrate 自带的 pallets 一起编译成 wasm 字节码运行,这是大部分 Substrate 开发者的选择。
III. In-depth customization based on Substrate Core. Substrate has been implemented as discrete components with sufficient abstraction and decoupling. For advanced developers, in certain scenarios, these components can be completely recombined from the ground up to achieve deep Node customization. For example, you can:
Low & have spent Use different cryptographic curves and hash algorithms
Low & have spent Use different serialization methods
Low & have spent Replace different consensus algorithms
Low & have spent Completely remove the Frame layer code and write the business in a different language, as long as it compiles to WASM and follows Substrate's specification
Low & have spent , etc.
The figure below shows the relationship between three levels of development difficulty and technical flexibility.

Using Substrate Node directly is the simplest, but the least flexible. Developing on Substrate Core is the most flexible, but the most difficult. Doing Frame Pallets development is in the middle. This is what most Substrate developers should do.
Substrate 的设计有很多优秀之处,我们来了解一下。
由于 Substrate 的 Runtime 代码编译为 wasm 运行,然后 wasm 字节码本身作为交易的数据直接提交到链上,再藉由链本身的 p2p 网络全网传播,实现业务逻辑的更新。每个节点在收到更新版本的 wasm 字节码后,将其更新到代码段,在某个块之后就使用新版本的 wasm 来执行逻辑。
With this hot update code mechanism, business code upgrades will no longer cause forks (soft or hard forks), that is, it will not cause network forks due to technical objective reasons (human active forks are still possible).
需要注意的是,这种升级仅限于 wasm 字节码能覆盖的部分——也即 Runtime 中的代码——的升级。如果改动了 node 代码本身(即 Runtime 外的部分),仍然需要通知所有节点进行手动或 devops 替换。
Substrate 同时支持几种密码学曲线:
Low & have spent ECDSA
Low & have spent Ed25519
Low & have spent SR25519
Several hashes are supported simultaneously:
Low & have spent Blake2
Low & have spent xxHash
Developers can choose to use it according to their own needs. If you don't have what you want, it's not hard to add new curves and Hash functions based on its architecture.
Substrate 的 Account Key 分三个层次:
Low & have spent Stash Key
Low & have spent Controller Key
Low & have spent Session Keys
Stash Key 是用来存资金的账户,其私钥部分应该尽可能安全地存储在冷钱包中。Controller Key 用来控制 Validator 的参数,也是 Stash Key 的一个中间代理账户,在更新验证人集合时非常有用。Session Keys 用来对共识相关的消息进行签名。Session Keys 可以有多个,每一个都有自己专门的用途,一般可组合在一起使用,如:
impl_opaque_keys! {
pub struct SessionKeys {
pub grandpa: Grandpa,
pub babe: Babe,
pub im_online: ImOnline,
pub authority_discovery: AuthorityDiscovery,
}
}
上述代码将 4 个独立的 Session Keys: Grandpa session key, Babe session key, ImOnline session key, AuthorityDiscovery session key 组合在一起成为一个大的 SessionKeys。
Session Keys can be understood as a unique Identification of the Validator. Session Keys should be changed once each Session has passed, so as to ensure maximum security.
Substrate 通过这种分层的 Account Key 的设计,既保证了安全性,又提供了充分的灵活性,基本能覆盖所有应用场景的需求。
Substrate 设计了一套共识框架,这套共识框架非常了得。它将块的生产(proposal)与敲定(finalize)分离,同时容纳了 Nakamoto 类(只有概率性敲定,无确定性敲定)共识和 BFT 类(有确定性敲定)共识。
Substrate 为出块提供了以下几种算法:
Low & have spent Aura: slot mode, in a known authority set, using the round robin mode to take turns to block
Low & have spent Babe: slot mode in which block nodes are randomly selected (there may be more than one block-producer per slot) using the verifiable random function VRF in a known authority set
Low & have spent PoW: Proof of work blocks
这几种出块算法,如果没有 Finalize Gadget 配合,只能做到概率性敲定(finalization),而无法做到确定性敲定。Substrate 提供了 Grandpa 这个 Finalize Gadget,用于确定性敲定。
Blocking algorithm and locking algorithm can be used together freely. So there are Aura/Grandpa, Babe/Grandpa, and even Pow/Grandpa. You can also use Grandpa for situations where you do not need to make sure. Substrate gives developers plenty of latitude.
Substrate 共识框架还提供了其它一些基础设施:
● Fork Choince Rules: Longest Chain Rule 或 GHOST Rule,用于决定在链有分叉的情况下,如何选择一个最好的链的算法
Low & have spent Import Queue: Import Queue
Low & have spent Trait: Block Import Trait
Low & have spent Block Import Pipeline
Substrate 还提供了在 Runtime 中对共识过程进行协调控制的功能。比如,在 Pow 运行过程中调整难度,在 PoA 中决定下一个是否轮转到,在 PoS 中动态修改 Stake 的权重等等。
In addition to Substrate's well-established infrastructure, developers can develop their own consensus engine in accordance with their specifications, introduce it into the framework, and have appropriate control at Runtime if their own consensus engine fails to meet their needs.
Off-Chain 特性是 Substrate 中提供的一套相当强大的基础设施。毕竟对区块链来说,链上的逻辑操作空间非常有限,有些事情必须通过链下来完成。在没有 Off-Chain Worker (OCW) 之前,这一类事情,通常是由预言机 Oracle 来完成。预言机是外部服务,通过区块链节点 RPC 接口向区块链提交交易从而把外界的信息传到链上去。这种方式虽然是可行的,但它在安全性、集成性、可扩展性和基础设施效率问题上面,仍然不够好。
To make integration of off-chain data more secure and efficient, Substrate provides off-chain related features. Its architecture diagram is as follows:

Off-chain Worker architecture diagram
Off-chain 特性包含三大组件:
Low & have spent Off-Chain Worker
Low & have spent Off-Chain Storage
Low & have spent Off-Chain Indexing
Off-Chain Worker 用于实现链下逻辑。其代码与 Runtime 代码写在一起,并被编译到同一个 wasm 字节码字符串中,在同一个交易中被传播到全网络。但是在执行的时候,Off-Chain Worker 的代码是在独立的 VM 中执行的,即与 Runtime 逻辑的执行完全隔离开。具体来说,Off-Chain Worker 能够实现如下功能:
Low & have spent The result of the calculation is submitted to the chain in the form of a transaction
Low & have spent Contains a fully functional HTTP client that can access data from external services
Low & have spent Access to the local node's keystore allows you to verify and sign transactions
Low & have spent The local KV database can be accessed, and this database is shared among all off-chain workers
Low & have spent A local secure source of entropy used to generate random numbers
Low & have spent The local time of the node can be accessed
Low & have spent Can sleep and resume work
Off-Chain Storage 是链下逻辑独立的存储空间,与链上的 Storage 是完全隔离开的。它具有如下特性:
● 能被 Off-Chain Worker 读取和写入
Low & have spent It is stored locally in Node and will not be passed to other nodes in the network and will not participate in the network consensus
● 被所有同时运行的 Off-Chain Workers 共享访问(因此需要锁操作)。因此,可以利用其在不同的 Workers 之间通信
Low & have spent Can be written by Runtime code, but not read. Therefore, some on-chain and off-chain interaction functions can be realized based on it
Low & have spent It can be read by code in Node outside of the WASM environment, so it can be read by RPC
Off-Chain Indexing 提供了在 Runtime 环境中,向 Off-Chain Storage 写入数据的能力。但是不能读取 Off-Chain Storage 中的数据。这为一些新的编程范式提供了可能性。
Others include, for example, a well-developed OCW integration testing framework.
Substrate 的 Off-chain 特性非常强大,令人印象深刻。
As anyone with Ethereum development experience knows, the Gas fee mechanism has been a very successful design and is very important for the safe and smooth operation of the chain. Almost all subsequent blockchains borrowed directly from this design. Substrate, on the other hand, provides a very comprehensive set of mechanisms and configuration parameters to help developers design their own Gas rate algorithms. The following GAS calculation and configuration parameters are built-in in Substrate:
Low & have spent Includinglength_fee and weight_fee: Includes length_fee and weight_fee
Low & have spent Fee Multiplier
Low & have spent Additional Fees, included
A. & have spent Bonds
A. & have spent Deposits
A. & have spent Burns
A. & have spent Limits
Low & have spent Default Weight Annotations
Low & have spent Dynamic Weights
Low & have spent Post Dispatch Weight Correction
Low & have spent Custom Fees
Low & have spent Custom Inclusion Fee
We will not explain each item in detail here, please refer to https://substrate.dev for the specific meaning.
As you can see, Substrate's design of the Gas charge calculation is very comprehensive and even slightly complex. The aim is still to achieve the goal of a universal blockchain framework -- everything should be there and work right out of the box.
Substrate 非常适合用于启动独立的链或者是面向 Web3.0 应用的 Appchain,一般这些链会在用户体验上下足功夫。比如对于应用的普通用户来讲,使用服务的过程中,可能会意识不到 Gas 费的存在,在这种场景下,Substrate 提供的上述丰富的 Gas 费机制,能做到 Gas 费置零或者设置为代付。这类特性便有机会让 Substrate 成为最适合 Web3.0 App 的开发框架之一。
Substrate 提供了一套 RPC 扩展框架,让开发者可以(在 Substrate 默认提供的接口之外)扩展开发自己的 RPC 接口。由于 RPC 实现代码是在 node 中,Runtime 之外,所以理论上来说,可以在 Substrate 中开发全功能(做任何事情)的 RPC 服务。这就使得 Substrate 成为了一个强大的 RPC 开发框架。
Often we need to interact with states in the Runtime, and that's where the Runtime API comes in. The Runtime API is a bridge between the inside and outside of the Runtime, or on and off the chain.
The combination of the RPC and the Runtime API makes it possible to complete the process of initiating external requests to obtain state on the chain. With custom Runtime API functionality and RPC extension interface functionality, developers have great flexibility and possibilities. It also lays the foundation for Substrate to become an all-in-one, integrated Web3.0 development framework.
To understand how Gateway uses Substrate, let's pull straight down to its source code for a brief analysis.
git clonehttps://github.com/compound-finance/gateway
cd gateway & & ls
As you can see, there are the following main directories (we have briefly commented them).
ethereum/ The Starport contract code on Ethereum
ethereum-client/ Ethereum related type definitions and basic utility functions
gateway-crypto/ Basic utility functions related to passwords used by the Gateway
integration/ Integration test code
node/ Substrate's custom Node code
our-std/ Gateway 的 std 代码,是对 sp_std 的简单封装,并添加了一点额外的东西
pallets/ The main business logic code for the Gateway stand-alone chain
runtime/ Substrate's runtime build code
types-derive/ A type-dependent procedure macro used by the Gateway
可以看到,目前 Gateway 只实现了到 ethereum 的 startport 连接,其它链的对接还在开发中,会逐渐加入。
The main business code is in the pallets/ directory, which has three subdirectories.
cash/ Main business logic
oracle/ Get the price code from the feeder
runtime-interfaces/ Some runtime interfaces
我们从 pallets/cash/src/lib.rs 看起。这是一个标准的 pallet 文件,其结构有
Low & have spent The definition of trait Config
Low & have spent decl_storage! Define the on-chain storage portion
Low & have spent decl_event! Define the event output section
Low & have spent decl_module! Define the module implementation part
In the module implementation section, there is the fn offchain_worker(block_number: T::BlockNumber) {} function. Its implementation consists mainly of these two function calls:
internal::events::track_chain_events::< T> (a)
internal::notices::process_notices::< T> (block_number)
As we know, the offchain_worker entry of Substrate is called when each block is imported into the local state database. Fn offchain_worker(block_number: T::BlockNumber) is executed by each block driver. Each block executes this function once. Therefore, for each increment of a block, the above code will handle both events and notice respectively, which are defined in the internal module.
继续追踪 pallets/cash/src/internal/events.rs 和 pallets/cash/src/internal/notices.rs 文件后,可以整理出大体的逻辑要点:
Low & have spent Offchain_worker is called once per block
● 在 offchain_worker 代码中,会调用到 ethereum-client/ 提供的以太坊基础类型和辅助工具,发 http 请求到以太坊的事件服务器,批量获取以太坊的 events,并分类处理
Low & have spent The processed result is submitted from the offchain_worker to the chain in the form of a transaction
Low & have spent Enter the Runtime business logic for processing
pallets/oracle/ 里面的逻辑流程也类似:在 offchain_worker 中,用 http 请求从喂价机地址获取数据,然后提交给链上使用。
理解了上述流程后,也就能比较轻松地理解 Compound 在 Ethereum 上做 Compound Governance Proposal 是如何影响到 Gateway 链上的验证人节点集合更新的了。其仍然是通过以太坊的事件向外抛出信息,事件服务器搜集到事件后,缓存下来。Gateway 这边的 Offchain worker 每新增一个块就去事件服务器上批量取一次最近的事件。获取到最近的事件后,按对应的逻辑迭代处理就行了。而 Substrate 中要更新验证人集合,需要用到 Controler account,以及 SetKeys 等相关由 Substrate 提供的功能。
Due to the limitation of space, we have reached the end of the analysis point here. Here we only briefly analyzed the logic process of Gateway Pallet's broad lines, and then focused on the Offchain Worker part.
As you can see, the Gateway takes full advantage of Substrate's infrastructure to easily implement business logic and the integration of on-chain code with off-chain code, as well as interactions with other chains. The whole code is very clear, worth learning from us.
In this article, we describe the overall architecture of Gateway, Substrate's functional modules, the features of the Substrate framework, and how Gateway makes full use of Substrate's infrastructure for the development of individual chains.
Limited by space, many places can only point to stop, the end of the article attached some links for you to expand the reading.
The author's level is limited. Readers are welcome to contact me via the following email: mike@oct.network.
Reference & NBSP;
Introducing Gateway: https://medium.com/compound-finance/gateway-623f6f48d2b6
Gateway Document: https://docs.google.com/document/d/1bp4uLYHt0_lUNO3dveFGelXYAC-M7RojqY-5jPDyUlM/edit#heading=h.ymt4jrq294bj
Compound’s Gateway: a deep dive into setting up a validator node. https://medium.com/ethereum-on-steroids/compounds-gateway-a-deep-dive-into-setting-up-a-validator-node-399a2817702d https://github.com/compound-finance/gateway
Substrate.dev: https://substrate.dev/docs/en/
Rust is again the most popular language:https://mp.weixin.qq.com/s/xGZqrXfzfthVSIUWIQWENA
The original link
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia