Substrate 安装 + 创建测试链 + 启动私有网络
前言
此文档用于 substrate 学习记录,官方文档链接在此。
一、substrate 环境安装
1.Build Dependencies
sudo apt update
# May prompt for location information
sudo apt install -y git clang curl libssl-dev
2.Rust Developer Environment
# Install
curl https://sh.rustup.rs -sSf | sh
# Configure
source ~/.cargo/env
rustup default stable
rustup update
rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly
一遍不行执行两遍!
二、Create Your First Substrate Chain
这是官方给出的一个创链过程,可以使用 Win10 子系统配合 Win10 浏览器使用。
1.Compiling the Node Template
git clone -b v3.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-node-template
cd substrate-node-template
# NOTE: you should always use the `--release` flag
cargo build --release
# ^^ this will take a while!
2.安装 Front-End Template
可以在 Win10 上进行,需要安装 nodejs 和 Yarn 后继续。
# Clone the frontend template from github
git clone -b v3.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-front-end-template
# Install the dependencies
cd substrate-front-end-template
yarn install
3.启动节点
# 在开发模式运行一个临时节点
./target/release/node-template --dev --tmp
三、启动私有网络
1.启动 Alice
# Purge any chain data from previous runs
# You will be prompted to type `y`
./target/release/node-template purge-chain --base-path /tmp/alice --chain local
# Start Alice's node
./target/release/node-template \
--base-path /tmp/alice \
--chain local \
--alice \
--port 30333 \
--ws-port 9945 \
--rpc-port 9933 \
--node-key 0000000000000000000000000000000000000000000000000000000000000001 \
--telemetry-url 'wss://telemetry.polkadot.io/submit/ 0' \
--validator
2.连接UI
3.BOb 加入
./target/release/node-template purge-chain --base-path /tmp/bob --chain local
./target/release/node-template \
--base-path /tmp/bob \
--chain local \
--bob \
--port 30334 \
--ws-port 9946 \
--rpc-port 9934 \
--telemetry-url 'wss://telemetry.polkadot.io/submit/ 0' \
--validator \
--bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp
flag 的更多细节,可以通过运行以下代码获取:
./target/release/node-template --help
4.生成 Subkey 密钥
sr25519:
# subkey command
subkey generate --scheme sr25519
# subkey output
Secret phrase `infant salmon buzz patrol maple subject turtle cute legend song vital leisure` is account:
Secret seed: 0xa2b0200f9666b743402289ca4f7e79c9a4a52ce129365578521b0b75396bd242
Public key (hex): 0x0a11c9bcc81f8bd314e80bc51cbfacf30eaeb57e863196a79cccdc8bf4750d21
Account ID: 0x0a11c9bcc81f8bd314e80bc51cbfacf30eaeb57e863196a79cccdc8bf4750d21
SS58 Address: 5CHucvTwrPg8L2tjneVoemApqXcUaEdUDsCEPyE7aDwrtR8D
ed25519:
# subkey command
subkey inspect --scheme ed25519 "infant salmon buzz patrol maple subject turtle cute legend song vital leisure"
# subkey output
Secret phrase `infant salmon buzz patrol maple subject turtle cute legend song vital leisure` is account:
Secret seed: 0xa2b0200f9666b743402289ca4f7e79c9a4a52ce129365578521b0b75396bd242
Public key (hex): 0x1a0e2bf1e0195a1f5396c5fd209a620a48fe90f6f336d89c89405a0183a857a3
Account ID: 0x1a0e2bf1e0195a1f5396c5fd209a620a48fe90f6f336d89c89405a0183a857a3
SS58 Address: 5CesK3uTmn4NGfD3oyGBd1jrp4EfRyYdtqL3ERe9SXv8jUHb
5.创建自定义的 chain spec
我们无需从头开始写新的 chain spec,可以对之前使用的 chain spec 进行一些修改。 首先,我们需要将 chain spec 导出到一个名为 customSpec.json 的文件中:
# Export the local chain spec to json
./target/release/node-template build-spec --disable-default-bootnode --chain local > customSpec.json
打开文件可以看到如下两个重要位置,Aura 为生产区块验证,Grandpa 为达成区块的最终确定性验证。
我们通过上面步骤生产出两份密钥,修改对应位置内容即可。
当 chain spec 准备好后,我们将其转换为 “原生” chain spec。 原生 chain spec 包含着所有相同的信息,但它同时也包含节点用于引用本地存储数据的已编码的存储密钥。 部署原生 chain spec 可以确保每个节点用适当的存储密钥对数据进行存储:
./target/release/node-template build-spec --chain=customSpec.json --raw --disable-default-bootnode > customSpecRaw.json
6.创建私有网络
# purge chain (only required for new/modified dev chain spec)
./target/release/node-template purge-chain --base-path /tmp/node01 --chain local -y
# start node01
./target/release/node-template \
--base-path /tmp/node01 \
--chain ./customSpecRaw.json \
--port 30333 \
--ws-port 9945 \
--rpc-port 9933 \
--telemetry-url 'wss://telemetry.polkadot.io/submit/ 0' \
--validator \
--rpc-methods Unsafe \
--name MyNode01
7.将密钥添加到密钥库
在网络中的每一个节点添加密钥,包括了 Aura 和 GRANDPA 两个密钥,两个密钥的作用已经在上文提到。用浏览器打开 Polkadot-JS Apps 可以看到当前的节点信息,链接为:https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9945#/explorer。
找到 rpc calls 添加 aura 和 grandpa 密钥。
8.其他参与者加入
# purge chain (only required for new/modified dev chain spec)
./target/release/node-template purge-chain --base-path /tmp/node02 --chain local -y
# start node02
./target/release/node-template \
--base-path /tmp/node02 \
--chain ./customSpecRaw.json \
--port 30334 \
--ws-port 9946 \
--rpc-port 9934 \
--telemetry-url 'wss://telemetry.polkadot.io/submit/ 0' \
--validator \
--rpc-methods Unsafe \
--name MyNode02 \
--bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWAvdwXzjmRpkHpz8PzUTaX1o23SdpgAWVyTGMSQ68QXK6
# you MUST fill the correct info in the line above:
# --bootnodes /ip4/<IP Address>/tcp/<p2p Port>/p2p/<Peer ID>
同样需要导入 arua 和 grandpa 密钥,本地 APPS UI 上修改端口即可进入对应节点,加入密钥后重启开始产块。
BUG总结
- substrate 安装 Rust – update crates.io过慢
解决:在实际开发中,为了更快速下载第三方包,我们需要把crates.io换国内的镜像源,否则在拉取 crates.io 仓库代码会非常慢,Updating crates.io index 卡很久,很多次超时导致引用库没法编译,我们首先创建一个 config 文件放到 $HOME/.cargo/config 文件中,这里使用 vim 编辑器:vim config 加入代码:
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 替换成你偏好的镜像源
replace-with = 'sjtu'
#replace-with = 'ustc'
# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
# rustcc社区
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"
- substrate 安装 报错 Blocking waiting for file lock on package cache
解决:Cargo.lock被其他程序正在写入,独占了。一般关掉那个程序就行。原来是在cargo文件夹下面,.package_cache被加锁阻塞,所以进入对应的cargo文件夹下面,删除.package_cache文件即可。 - Alice and bob test chain 报错:error: Found argument ‘tmp/bob’ which wasn’t expected, or isn’t valid in this context
解决:官方文档代码错误,bob加入代码:
./target/release/node-template purge-chain --base-path / tmp/bob --chain local
可以发现 tmp 前多一个空格,删除即可。
- 参与者进入时:The bootnode you want to connect to at … provided a different peer ID than the one you expect: …
解决:
# you MUST fill the correct info in the line above:
# --bootnodes /ip4/<IP Address>/tcp/<p2p Port>/p2p/<Peer ID>