创建第一条 Substrate 链

  |   0 评论   |   0 浏览

基础环境:

虚拟机操作系统:Ubuntu 20.04

虚拟机ID地址:172.16.1.20

环境配置

安装依赖

1sudo apt update
2sudo apt install -y git clang curl libssl-dev

安装 Rust

 1# 安装
 2curl https://sh.rustup.rs -sSf | sh
 3
 4# 配置
 5source ~/.cargo/env
 6
 7# 将默认工具链配置为最新稳定版
 8rustup default stable
 9rustup update
10
11# 安装 nightly 编译链
12rustup update nightly
13
14# 给 nightly 编译链添加 wasm 编译target
15rustup target add wasm32-unknown-unknown --toolchain nightly

编译 Node Template

Node Template 是 Substrate Developer Hub 提供的一个基于 Substrate 框架的模板程序。

1# 拷贝 Node Template (version v3.0.0).
2git clone -b v3.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-node-template
3
4# 编译
5cd substrate-node-template
6cargo build --release

安装 Front-End Template

Front-End Template 是 Substrate Developer Hub 提供的一个基于 ReactJS 的前端应用,用来与 Substrate 区块链进行交互。

安装 Node.js

安装:

1curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
2sudo apt-get install -y nodejs

确认:

1wangzk@ubuntu:~$ node -v
2v14.16.1
3wangzk@ubuntu:~$ npm -v
46.14.12

安装 Yarn

安装:

1sudo npm install --global yarn

确认:

1wangzk@ubuntu:~$ yarn -v
21.22.10

安装 Front-End

1# 拷贝
2git clone -b v3.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-front-end-template
3
4# 安装依赖
5cd substrate-front-end-template
6yarn install

与节点交互

启动 Node

1cd substrate-node-template
2
3# 在开发模式下启动一个临时节点
4./target/release/node-template --dev --tmp

其中:

  • --dev -- 指定开发模式
  • --tmp -- 节点数据存放在临时目录
 1wangzk@ubuntu:~/substrate-node-template$ ./target/release/node-template --dev --tmp
 22021-04-25 18:06:34  Running in --dev mode, RPC CORS has been disabled.
 32021-04-25 18:06:34  Substrate Node
 42021-04-25 18:06:34  ✌️  version 3.0.0-8370ddd-x86_64-linux-gnu
 52021-04-25 18:06:34  ❤️  by Substrate DevHub <https://github.com/substrate-developer-hub>, 2017-2021
 62021-04-25 18:06:34  📋 Chain specification: Development
 72021-04-25 18:06:34  🏷 Node name: dreary-spiders-7993
 82021-04-25 18:06:34  👤 Role: AUTHORITY
 92021-04-25 18:06:34  💾 Database: RocksDb at /tmp/substratej1rcoJ/chains/dev/db
102021-04-25 18:06:34  ⛓  Native runtime: node-template-100 (node-template-1.tx1.au1)
112021-04-25 18:06:34  🔨 Initializing Genesis block/state (state: 0x6d10…2825, header-hash: 0x94e8…3adb)
122021-04-25 18:06:34  👴 Loading GRANDPA authority set from genesis on what appears to be first startup.
132021-04-25 18:06:34  ⏱  Loaded block-time = 6000 milliseconds from genesis on first-launch
142021-04-25 18:06:34  Using default protocol ID "sup" because none is configured in the chain specs
152021-04-25 18:06:34  🏷 Local node identity is: 12D3KooWR5XALiyiCRAxfg7iuro63QMZdUVBgfMifgU9CmihaNrP
162021-04-25 18:06:34  📦 Highest known block at #0
172021-04-25 18:06:34  〽️ Prometheus server started at 127.0.0.1:9615
182021-04-25 18:06:34  Listening for new connections on 127.0.0.1:9944.
192021-04-25 18:06:36  🙌 Starting consensus session on top of parent 0x94e8821914882c9a650b6648ef0af4e7561cdfbb1585b7613eb9f153b3663adb
202021-04-25 18:06:36  Timeout fired waiting for transaction pool at block #0. Proceeding with production.
21......

输出中包含:

  • Database: RocksDb at /tmp/substratej1rcoJ/chains/dev/db
  • Local node identity is: 12D3KooWR5XALiyiCRAxfg7iuro63QMZdUVBgfMifgU9CmihaNrP

启动 Front-End

1cd substrate-front-end-template
2yarn start

启动成功后的输出信息:

1Compiled successfully!
2
3You can now view substrate-front-end-template in the browser.
4
5  Local:            http://localhost:8000/substrate-front-end-template
6  On Your Network:  http://172.16.1.20:8000/substrate-front-end-template
7
8Note that the development build is not optimized.
9To create a production build, use yarn build.

交互

Front-End 在启动成功时,会自动打开浏览器展示下面的页面:
image20210426084920905.png

遇到的问题

在虚拟机外部无法访问 http://172.16.1.20:8000/substrate-front-end-template,浏览器提示下面的报错信息:
image20210426082542041.png

解决方法

首先,在启动 Node 时,添加 --ws-external 参数。

1./target/release/node-template --dev --tmp --ws-external

然后,在启动 Front-End 前,修改 development.json 文件。

1cd substrate-front-end-template/src/config
2vim development.json

将配置文件中的 1237.0.0.1 改为实际的 IP 地址。

1{
2  "PROVIDER_SOCKET": "ws://127.0.0.1:9944"
3}
4// 改为
5{
6  "PROVIDER_SOCKET": "ws://172.16.1.20:9944" // 这是我的虚拟机的IP
7}

相关资料

Create Your First Substrate Chain

Fixing "Error Connecting to Substrate" message in Substrate Front End Template