迈向 Substrate 的第一步

选对工具,搭建区块链一点也不难。

在 Substrate 诞生之前,想搭建一条自己的定制化私有区块链并非易事。我看到的大多做法都基本是先 clone 现有成熟区块链的代码,然后在那个基础上直接修改。这种方式虽然直接,但对于开发队伍的要求并不低,而且还需要小心地避免改到不该改动的代码,免得运行时出问题。 Substrate 的出现则彻底改变了这种原始地工作方式,它以一种模块化的方式让你可以按需选择来构建自己的区块链。同时,由于相关部分已经进行了封装,这使得你可以只专注于添加或修改自己的逻辑,完全避免了之前的“人肉规避”做法。 一言以蔽之:**Substrate 就是区块链世界的“ Spring Boot ”**。 采用 Substrate 开发区块链的主要优势: - 系出名门,代码质量可期:Substrate 诞生于 Polkadot(当前最红的跨链项目,其创始人是以太坊的联合创始人 Gavin Wood) 的创建过程中,也是 Polkadot 的基础。 - 采用 Rust 语言开发,安全、稳定、可靠。 - 模块化设计,开发者可以根据自己的情况进行裁剪。 - 健全的开发生态:提供了从运行时,到前端交互,到智能合约的一整套开发工具。 - 可无缝对接 Polkadot,融入更大的生态,不必从头开始构建自己的社区。 从使用方式来讲,Substrate 有三种使用方式,如下图: ![image.png](https://img.learnblockchain.cn/attachments/2020/06/qI9gEJ1a5ef00396ead04.png) 从技术的自由度讲,“ Substrate Core ”最大,你完全可以采用另一种语言来开发,只需要它能支持 WebAssembly 就好,前题是你要遵循 Substrate 的设计规范和相关协议。类似于只需要符合 PG 的网络接口标准,你可以用任何语言来编写 PG 的客户端一样。 从开发的简单程度来讲,“ Substrate Node ”最简单,只需稍作配置,你就可以立刻启动一个 Substrate 节点。 “ Substrate SRML ”则处于中间位置,既给你提供了一定的技术自由度,同时也没有那么难。一般情况下这都涉及到编写一定的 Rust 代码,有点类似你用 Spring Boot 开发时的情形。同时,采用这种方式你可以自行剪裁需要的模块,搭建自己钟意的“链条”。 作为迈向 Substrate 的第一步,本文不打算讲太多关于 Substrate 太多的概念,更多地关注于环境的搭建和最基本的示例。至于其他(比如涉及到的 rust 语言、概念、模板工程组成、合约等内容),则会留到后续文章再谈。 就搭建一个 Substrate 节点而言,其实过程并不复杂,总结起来有以下几步: - 搭建 rust 环境(关于 rust 的基础知识可以参见[我的这个 PPT](https://mp.weixin.qq.com/s/l2NHAEJNNrVopL0N1Q4UNQ)) - clone node-template - 修改、编译、测试、运行 - 访问:https://polkadot.js.org/apps/#/settings?rpc=ws://127.0.0.1:9944 ,即可看到区块的情况 这些内容参考下面的文档就能轻易地完成: - [Getting Started](https://substrate.dev/docs/en/overview/getting-started) - [Creating Your First Substrate Chain](https://substrate.dev/docs/en/tutorials/creating-your-first-substrate-chain/) - [Build a PoE Decentralized Application](https://substrate.dev/docs/en/tutorials/build-a-dapp/) - [Start a Private Network](https://substrate.dev/docs/en/tutorials/start-a-private-network/) 即使不太明白其中的代码含义也没有关系,只需按其步骤进行即可。 对于 Mac 用户需要注意一下,因为直接按照文章所说的运行: ```shell curl https://getsubstrate.io -sSf | bash -s -- --fast ``` 你会得到以下错误: ```shell $curl https://getsubstrate.io -sSf | bash -s -- --fast Mac OS (Darwin) detected. remote: Repository not found. fatal: repository 'https://github.com/Homebrew/homebrew-versions/' not found ``` 此时,你只需要按照“ [Getting Started](https://substrate.dev/docs/en/overview/getting-started) ”中的手动安装部分进行即可。访问 “https://getsubstrate.io/” 时你可以发现,其内容也就是安装和配置 rust 开发环境而已。 同时,按照文中命令(见下)也是无法正常运行的: ```shell ./target/release/substrate ``` 会报以下错误: ```shell 2020-05-10 17:15:03 Substrate Node 2020-05-10 17:15:03 ️ version 2.0.0-dev-c386dea20-x86_64-macos 2020-05-10 17:15:03 ️ by Parity Technologies <admin@parity.io>, 2017-2020 2020-05-10 17:15:03 Chain specification: Flaming Fir 2020-05-10 17:15:03 Node name: real-battle-0753 2020-05-10 17:15:03 Role: FULL 2020-05-10 17:15:03 Database: RocksDb at /Users/foxgem/Library/Application Support/substrate/chains/flamingfir6/db 2020-05-10 17:15:03 Native runtime: node-244 (substrate-node-3.tx1.au10) 2020-05-10 17:15:03 Loading GRANDPA authority set from genesis on what appears to be first startup. 2020-05-10 17:15:03 Cannot create a runtime: Instantiation("Instantiation: Export ext_storage_child_root_version_1 not found") Error: Service(Client(Execution(Other("Instantiation: Export ext_storage_child_root_version_1 not found")))) ``` 请使用以下命令运行: ```shell ./target/release/substrate purge-chain --dev ./target/release/substrate --dev ``` 第一条命令本质上相当于清除数据库的作用,假如你之前曾经运行过但想重新完全开始,可以考虑这条命令。 在接下来的文章中,我会谈谈 Substrate 开发需要的 rust 知识、相关概念,以及 node-template 结构。敬请期待吧,。 [原文链接](https://blog.dteam.top/posts/2020-05/substrate-step1.html) --- ![作者名片](https://blog.dteam.top/images/mingpian.png)

在 Substrate 诞生之前,想搭建一条自己的定制化私有区块链并非易事。我看到的大多做法都基本是先 clone 现有成熟区块链的代码,然后在那个基础上直接修改。这种方式虽然直接,但对于开发队伍的要求并不低,而且还需要小心地避免改到不该改动的代码,免得运行时出问题。

Substrate 的出现则彻底改变了这种原始地工作方式,它以一种模块化的方式让你可以按需选择来构建自己的区块链。同时,由于相关部分已经进行了封装,这使得你可以只专注于添加或修改自己的逻辑,完全避免了之前的“人肉规避”做法。

一言以蔽之:Substrate 就是区块链世界的“ Spring Boot ”

采用 Substrate 开发区块链的主要优势:

  • 系出名门,代码质量可期:Substrate 诞生于 Polkadot(当前最红的跨链项目,其创始人是以太坊的联合创始人 Gavin Wood) 的创建过程中,也是 Polkadot 的基础。
  • 采用 Rust 语言开发,安全、稳定、可靠。
  • 模块化设计,开发者可以根据自己的情况进行裁剪。
  • 健全的开发生态:提供了从运行时,到前端交互,到智能合约的一整套开发工具。
  • 可无缝对接 Polkadot,融入更大的生态,不必从头开始构建自己的社区。

从使用方式来讲,Substrate 有三种使用方式,如下图:

从技术的自由度讲,“ Substrate Core ”最大,你完全可以采用另一种语言来开发,只需要它能支持 WebAssembly 就好,前题是你要遵循 Substrate 的设计规范和相关协议。类似于只需要符合 PG 的网络接口标准,你可以用任何语言来编写 PG 的客户端一样。

从开发的简单程度来讲,“ Substrate Node ”最简单,只需稍作配置,你就可以立刻启动一个 Substrate 节点。

“ Substrate SRML ”则处于中间位置,既给你提供了一定的技术自由度,同时也没有那么难。一般情况下这都涉及到编写一定的 Rust 代码,有点类似你用 Spring Boot 开发时的情形。同时,采用这种方式你可以自行剪裁需要的模块,搭建自己钟意的“链条”。

作为迈向 Substrate 的第一步,本文不打算讲太多关于 Substrate 太多的概念,更多地关注于环境的搭建和最基本的示例。至于其他(比如涉及到的 rust 语言、概念、模板工程组成、合约等内容),则会留到后续文章再谈。

就搭建一个 Substrate 节点而言,其实过程并不复杂,总结起来有以下几步:

  • 搭建 rust 环境(关于 rust 的基础知识可以参见我的这个 PPT)
  • clone node-template
  • 修改、编译、测试、运行
  • 访问:https://polkadot.js.org/apps/#/settings?rpc=ws://127.0.0.1:9944 ,即可看到区块的情况

这些内容参考下面的文档就能轻易地完成:

  • Getting Started
  • Creating Your First Substrate Chain
  • Build a PoE Decentralized Application
  • Start a Private Network

即使不太明白其中的代码含义也没有关系,只需按其步骤进行即可。

对于 Mac 用户需要注意一下,因为直接按照文章所说的运行:

curl https://getsubstrate.io -sSf | bash -s -- --fast

你会得到以下错误:

$curl https://getsubstrate.io -sSf | bash -s -- --fast
Mac OS (Darwin) detected.
remote: Repository not found.
fatal: repository 'https://github.com/Homebrew/homebrew-versions/' not found

此时,你只需要按照“ Getting Started ”中的手动安装部分进行即可。访问 “https://getsubstrate.io/” 时你可以发现,其内容也就是安装和配置 rust 开发环境而已。

同时,按照文中命令(见下)也是无法正常运行的:

./target/release/substrate

会报以下错误:

2020-05-10 17:15:03 Substrate Node
2020-05-10 17:15:03 ️  version 2.0.0-dev-c386dea20-x86_64-macos
2020-05-10 17:15:03 ️  by Parity Technologies &lt;admin@parity.io>, 2017-2020
2020-05-10 17:15:03  Chain specification: Flaming Fir
2020-05-10 17:15:03   Node name: real-battle-0753
2020-05-10 17:15:03  Role: FULL
2020-05-10 17:15:03  Database: RocksDb at /Users/foxgem/Library/Application Support/substrate/chains/flamingfir6/db
2020-05-10 17:15:03   Native runtime: node-244 (substrate-node-3.tx1.au10)
2020-05-10 17:15:03  Loading GRANDPA authority set from genesis on what appears to be first startup.
2020-05-10 17:15:03 Cannot create a runtime: Instantiation("Instantiation: Export ext_storage_child_root_version_1 not found")
Error: Service(Client(Execution(Other("Instantiation: Export ext_storage_child_root_version_1 not found"))))

请使用以下命令运行:

./target/release/substrate purge-chain --dev
./target/release/substrate  --dev

第一条命令本质上相当于清除数据库的作用,假如你之前曾经运行过但想重新完全开始,可以考虑这条命令。

在接下来的文章中,我会谈谈 Substrate 开发需要的 rust 知识、相关概念,以及 node-template 结构。敬请期待吧,。

原文链接

区块链技术网。

  • 发表于 2020-05-10 17:46
  • 阅读 ( 1467 )
  • 学分 ( 66 )
  • 分类:Polkadot

评论