1.環境準備
1.1.Rust
參照官網即可,我的系統是macOS Mojave,不過官網中有對其他系統的說明,運行如下腳本:
curl https://sh.rustup.rs -sSf | sh
為了讓下載更順暢,可以配置國內源,原因都懂的。在~/.profile
中添加如下兩行:
export RUSTUP_DIST_SERVER=https://mirrors.sjtug.sjtu.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.sjtug.sjtu.edu.cn/rust-static/rustup
也可以使用rustcc的源
[source.crates-io]
replace-with = "rustcc"
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"
項目源碼在https://github.com/rustcc/lernaean-deploy
1.2.Pre-requirement
參照substrate官網,編譯安裝substrate需要一些預裝依賴。substrate提供了快速安裝和完全安裝兩種形式,都是傻瓜式一鍵安裝,不過我個人更喜歡手動安裝,能比較清晰的了解其中的步驟。姑且也把兩種方式列出來吧。
- 快速安裝,直接執行腳本
curl https://getsubstrate.io -sSf | bash -s -- --fast
會順帶安裝所有需要的依賴
- 完全安裝,其實就是去掉了
--fast
參數
curl https://getsubstrate.io -sSf | bash
如果足夠幸運,到目前為止安裝過程已經完成。
- 手動安裝
查看安裝腳本,可以發現需要預先安裝的依賴,不同的系統都有不同的預裝,mac直接使用brew:
brew install openssl cmake llvm
網絡有問題,還得配代理。。。我也是折騰了半天
2.編譯安裝
第一步沒什么說的,先拿到代碼
git clone https://github.com/paritytech/substrate
接下來就可以編譯安裝了。編譯substrate過程中,需要從crates.io下載包,再次更換國內源,在~/.cargo
目錄下添加config
文件,寫入如下內容:
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'sjtug'
[source.sjtug]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
正式開始吧
- 編譯安裝substrate
cd ~/rustptah/src/substrate
./scripts/init.sh
./scripts/build.sh
cargo install --force --path . substrate
成功后,可以在~/.cargo/bin
下看到substrate
可執行文件
- 編譯安裝subkey
Subkey是什么?
Subkey is a commandline utility included with Substrate that generates or restores Substrate keys.
繼續在上一步的substrate根目錄下
cargo install --force --path ./subkey subkey
- 安裝substrate提供的快捷工具腳本
f=`mktemp -d`
git clone https://github.com/paritytech/substrate-up $f
cp -a $f/substrate-* ~/.cargo/bin
cp -a $f/polkadot-* ~/.cargo/bin
3.驗證
3.1.基本
3.2.啟動一個新的節點
使用剛剛安裝好的腳本substrate-node-new <node-name> <author>
substrate-node-new testnode garry
成功后可以看到不斷有新的區塊生成。
在第一次執行這個指令的時候,可能會重新同步
substrate repo
,速度感人,且沒什么必要,將runtime/wasm
下的Cargo.lock
刪除,再重新執行即可。
說到刪除
Cargo.lock
,之前還碰到了一個問題:在vscode中配置了rls,但打開substrate項目,下方的狀態條顯示RLS一直卡在Starting轉圈,并不會自動構建,查資料有人在16年使用RLS時就遇到過,官方回復說是bug,但不影響RLS工作-_-||...在刪除了項目根目錄下的Cargo.lock后,RLS也恢復了正常。
3.3.通過Polkadot.js UI查看
Polkadot.js提供了一個UI界面,可以在瀏覽器中訪問如下網址
https://polkadot.js.org/apps/#/explorer
然后在左側面板中選擇Settings
標簽,選擇連接本地節點,就可以看到剛剛啟動的node了
在UI的Explorer中可以直觀的看到當前block和event信息,其他標簽也可以愉快的自行探索啦!
如果想重新啟動,清除上一次的運行數據,使用如下命令
./target/release/testnode purge-chain --dev -y