Substrate 安裝 + 創建測驗鏈 + 啟動私有網路
- 前言
- 一、substrate 環境安裝
- 1.Build Dependencies
- 2.Rust Developer Environment
- 二、Create Your First Substrate Chain
- 1.Compiling the Node Template
- 2.安裝 Front-End Template
- 3.啟動節點
- 三、啟動私有網路
- 1.啟動 Alice
- 2.連接UI
- 3.BOb 加入
- 4.生成 Subkey 密鑰
- 5.創建自定義的 chain spec
- 6.創建私有網路
- 7.將密鑰添加到密鑰庫
- 8.其他參與者加入
- BUG總結
前言
此檔案用于 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
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 密鑰
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/281694.html
標籤:區塊鏈
