簡介
- 使用 https://substrate 礦建
- 參考文檔:https://substrate.dev/docs/en/tutorials/create-your-first-substrate-chain/setup
- 參考文檔:https://substrate.dev/docs/en/knowledgebase/getting-started/
- 參考文檔:https://www.rust-lang.org/ (https://www.rust-lang.org/zh-CN/tools/install 這個文檔還有中文版本)
- 參考文檔:https://rust-cli.github.io/book/tutorial/setup.html
- 我的電腦是 macOS 所以如下操作默認都是在 macOS 上進行
安裝 Rust
命令行輸入:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
輸入1 進行默認安裝
-
安裝過程很順利削茁,最后提示 Rust is installed now. Great!
-
查看可執(zhí)行文件
ls ~/.cargo/bin
為了可以在命令行中直接使用 rustc 命令可以
source ~/.profile
或者
vim ~/.bash_profile
# 在最后一行添加
$ export PATH="$HOME/.cargo/bin:$PATH"
-
操作演示如下圖。
-
最后通過
rustup default stable
命令掉房,將rustup 設(shè)定為穩(wěn)定版本
如上圖所示我們安裝的nightly版本是 2021-02-10茧跋,為了之后的學(xué)習(xí)我們需要把版本切換到
nightly-2020-10-06
。
# 輸入命令:
rustup install nightly-2020-10-06
# 上面命令后會進入安裝該版本的環(huán)節(jié)卓囚,安裝后使用 `rustup default nightly-2020-10-06` 命令切換到該版本瘾杭,這個很重要。
-
操作截圖:
至此Rust 安裝并配置完畢
安裝 wasm32-unknown-unknown 即可:安裝方法
# 主要是安裝這個哪亿。
rustup target add wasm32-unknown-unknown
# 順便把下面兩個一并安裝。
cargo install wasm-pack
cargo install https
安裝
- Mac 需要安裝 brew,一般來講Mac用戶都會安裝這個命令襟雷,如果已經(jīng)安裝直接
brew update
和brew install openssl cmake
即可呼寸,如下是全部流程:
# 此摘要源自:https://substrate.dev/docs/en/knowledgebase/getting-started/
# Install Homebrew if necessary https://brew.sh/
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# Make sure Homebrew is up-to-date, install openssl and cmake
brew update
brew install openssl cmake
- 如果安裝出錯可能是國外源被屏蔽了可以使用國內(nèi)的工具包:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
brew update
brew install openssl cmake
-
brew install openssl cmake
如果報錯的解決辦法
# 如果 出錯可以使用
brew install --build-from-source cmake 通過源碼安裝試試(不過這個超級慢做好心理準(zhǔn)備)
# 編譯大概需要1個小時左右,根據(jù)機器性能的不同,編譯后需要添加環(huán)境變量。
echo 'export PATH="/usr/local/opt/sphinx-doc/bin:$PATH"' >> ~/.bash_profile
安裝 Notejs
- 參考文檔(可選):https://nodejs.org/en/download/
- 下載Mac 安裝包 https://nodejs.org/dist/v14.16.0/node-v14.16.0.pkg
# Mac 下可以使用 curl 命令下載,或者瀏覽器直接訪問安裝包地址恤左。
curl https://nodejs.org/dist/v14.16.0/node-v14.16.0.pkg -o ~/Desktop/node-v14.16.0.pkg
# 下載完成后Mac 上雙擊安裝即可
- 驗證版本:
node -v
顯示 v14.16.0 表示安裝成功。
安裝Yarn
- Yarn 是更牛逼的npm 包管理器搀绣,如果你沒有安裝的化也需要安一個飞袋。
# 我這里使用cnpm 如果你不知道cnpm 是什么baidu一下吧,如果你有國外代理直接使用 npm 也可以
cnpm install --global yarn
-
驗證 yarn --version 安裝
編譯Substrate
- 經(jīng)過上面漫長的配置等待链患,接下來開始編譯 Substrate 巧鸭,為了和大家保持一致,Substrate和Substrate-node-template版本:v2.0.0麻捻,這個版本很重要否則后面可能出現(xiàn)莫名其妙的問題纲仍,上面的Rust 使用的是 nightly-2020-10-06 版本,這個也很重要贸毕。
- 克隆代碼倉庫
# 首先切換到自己常用的代碼倉庫郑叠, cd ~/ 找個合適的位置
# 然后下載 2.0.0 版本代碼
git clone -b v2.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-node-template
# 進入這個 substrate-node-template
git tag # 如果顯示 v2.0.0 表示對了。
# 然后開始 make 項目
make init
make build
# 然后切換到上級目錄后下載前端代碼倉庫
cd ..
git clone -b v2.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-front-end-template
cd substrate-front-end-template/ # git tag 應(yīng)該 v2.0.0
yarn install # 安裝前端項目的依賴包
-
最終效果截圖:
啟動后端節(jié)點
- 啟動節(jié)點
cd ../substrate-node-template/
./target/release/node-template --dev --tmp
啟動前端節(jié)點
## 啟動前端
要與您的本地節(jié)點交互明棍,我們將使用 [Substrate 開發(fā)者 Front-end Template](https://github.com/substrate-developer-hub/substrate-front-end-template)乡革,它集合了常規(guī)情況下需要的各種前端交互組件。
如果您已經(jīng)安裝了 Front-end Template摊腋,請在 Front-end Template 所在的根目錄中執(zhí)行以下命令:
確保在 Front-end Template 根目錄中運行此命令
cd ../substrate-front-end-template/
yarn start
配置開發(fā)環(huán)境
-
下載安裝 vscode沸版,然后點擊插件,我們安裝插件
- 安裝如下插件兴蒸,逐個搜索并安裝如下:
Rust-in-peace Extension Pack
vscode-rust-syntax
Rust syntax # 這個有可能沒有视粮,請翻墻
-
需要安裝的插件大致如下:
可能遇到的問題
rust-analyzer加載時間過長
- 參考文檔:https://segmentfault.com/a/1190000039375746
- 參考文檔:https://github.com/rust-analyzer/rust-analyzer/issues/2653
- 開發(fā)環(huán)境: vscode+rust-analyzer
- 問題:vscode一直卡在fetching metadata階段。
- 方法橙凳,運行cargo metadata,發(fā)現(xiàn) :
Blocking waiting for file lock on package cache
- 運行
rm -rf ~/.cargo/.package-cache
cargo metadata
- 之后重啟 vscode
cargo metadata 過慢
- 參考:https://learnku.com/articles/49977
- 首先進入電腦的 cargo 目錄蕾殴,MAC OS 默認安裝在~/.cargo 下:
cd ~/.cargo
- 創(chuàng)建一個 config 文件,這里使用 vim 編輯器:
vim config
- 將下面的代碼貼進去:
# 放到 `$HOME/.cargo/config` 文件中
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 替換成你偏好的鏡像源
replace-with = 'sjtu'
#replace-with = 'ustc'
# 清華大學(xué)
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# 中國科學(xué)技術(shù)大學(xué)
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
# 上海交通大學(xué)
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
# rustcc社區(qū)
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"
結(jié)束
- 感謝閱讀痕惋,接下來會進入SETP1環(huán)節(jié),參考下一篇文章娃殖。