編譯和運行substrate
rust環境搭建
這裏以ubuntu18.04爲例
如下爲腳本源碼,國內網絡比較差,只能這樣了
#!/usr/bin/env bash
# check if user is root
if [ $(id -u) -eq "0" ]; then
Echo_Red "Error: 請使用普通賬號運行! "
exit 1
fi
# 安裝gcc
sudo apt -y install gcc
sudo apt -y install curl
# 安裝USTC 的rust mirror
echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static" >> ~/.bashrc
echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup" >> ~/.bashrc
source ~/.bashrc
# 安裝rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 使rust環境生效
source ~/.cargo/env
# 使用cargo代理,增快rust下載速度
cat >~/.cargo/config <<EOF
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
EOF
# 安裝rust nightly版本,並默認使用nightly版本
rustup install nightly
rustup default nightly
# 安裝RLS組建
rustup component add rls --toolchain nightly
rustup component add rust-analysis --toolchain nightly
rustup component add rust-src --toolchain nightly
# 安裝racer
cargo install racer
安裝必要插件
安裝依賴的插件
sudo apt install -y cmake pkg-config libssl-dev git gcc build-essential clang libclang-dev
查看cmake版本
sudo apt install build-essential libssl-dev
sudo snap install cmake --classic
cmake --version
升級cmake:(非必須)
sudo apt autoremove cmake
wget https://cmake.org/files/v3.17/cmake-3.17.0-Linux-x86_64.tar.gz
tar -zxvf cmake-3.17.0-Linux-x86_64.tar.gz
sudo mv cmake-3.17.0-Linux-x86_64 /usr/local/cmake3.17.0
sudo ln /usr/local/cmake3.17.0/bin/* /usr/bin/
cmake --version
clone substrate
安裝git :
sudo apt install git
clone substrate
clone https://github.com/paritytech/substrate.git
編譯substrate
cd substrate
cargo build --release
運行substrate單節點
./target/release/substrate
出現如下所示,說明單節點跑起來了,如果不是環境問題,不用15分鐘吧
運行多節點
開啓兩個終端,進入到substrate目錄下:
啓動Alice節點
./target/release/substrate --alice --chain local --base-path /tmp/alice
這時候是不會出塊的
啓動bob節點
./target/release/node-template --bob --chain local --base-path /tmp/bob
至少兩個節點一起啓動,纔會出塊
local 網絡只有alice和bob是驗證人
substrate 命令
xxx@xxx:~/workspace/substracte/substrate-node-template$ ./target/release/node-template -h
node-template 2.0.0-rc2-93862bd-x86_64-linux-gnu
Substrate DevHub <https://github.com/substrate-developer-hub>
All core commands that are provided by default.
The core commands are split into multiple subcommands and `Run` is the default subcommand. From the CLI user
perspective, it is not visible that `Run` is a subcommand. So, all parameters of `Run` are exported as main executable
parameters.
(核心命令分爲多個子命令,“run”是默認的子命令。在終端運行時,“run”是默認執行的。因此,“run”的所有參數都被導出爲主要可執行文件的參數)
USAGE:
node-template [FLAGS] [OPTIONS]
node-template <SUBCOMMAND>
FLAGS:
--alice Shortcut for `--name Alice --validator` with session keys for `Alice` added to keystore
(local默認驗證人)
--bob Shortcut for `--name Bob --validator` with session keys for `Bob` added to keystore
(local默認驗證人)
--charlie Shortcut for `--name Charlie --validator` with session keys for `Charlie` added to keystore
(local用不上)
--dave Shortcut for `--name Dave --validator` with session keys for `Dave` added to keystore
(local用不上)
--dev Specify the development chain
--discover-local Enable peer discovery on local networks
--eve Shortcut for `--name Eve --validator` with session keys for `Eve` added to keystore
--ferdie Shortcut for `--name Ferdie --validator` with session keys for `Ferdie` added to keystore
--force-authoring Enable authoring even when offline
-h, --help Prints help information
--legacy-network-protocol Use the legacy "pre-mainnet-launch" networking protocol. Enable if things seem
broken. This option will be removed in the future
--light Experimental: Run in light client mode
--no-grandpa Disable GRANDPA voter when running in validator mode, otherwise disable the GRANDPA
observer
--no-mdns Disable mDNS discovery
--no-private-ipv4 Forbid connecting to private IPv4 addresses (as specified in
[RFC1918](https://tools.ietf.org/html/rfc1918)), unless the address was passed with
`--reserved-nodes` or `--bootnodes`
--no-prometheus Do not expose a Prometheus metric endpoint
--no-telemetry Disable connecting to the Substrate telemetry server
--no-yamux-flow-control Disable the yamux flow control. This option will be removed in the future once
there is enough confidence that this feature is properly working
--one Shortcut for `--name One --validator` with session keys for `One` added to keystore
--password-interactive Use interactive shell for entering the password used by the keystore
--prometheus-external Listen to all Prometheus data source interfaces
--reserved-only Whether to only allow connections to/from reserved nodes
--rpc-external Listen to all RPC interfaces
--two Shortcut for `--name Two --validator` with session keys for `Two` added to keystore
--unsafe-pruning Force start with unsafe pruning settings
--unsafe-rpc-external Listen to all RPC interfaces
--unsafe-ws-external Listen to all Websocket interfaces
--validator Enable validator mode
-V, --version Prints version information
--ws-external Listen to all Websocket interfaces
OPTIONS:
-d, --base-path <PATH> Specify custom base path
--bootnodes <ADDR>... Specify a list of bootnodes
--chain <CHAIN_SPEC>
Specify the chain specification (one of dev, local, or staging)
--database <DB> Select database backend to use
--db-cache <MiB> Limit the memory the database cache can use
--offchain-worker <ENABLED>
Should execute offchain workers on every block [default: WhenValidating] [possible values: Always, Never,
WhenValidating]
--execution <STRATEGY>
The execution strategy that should be used by all execution contexts [possible values: Native, Wasm, Both,
NativeElseWasm]
--execution-block-construction <STRATEGY>
The means of execution used when calling into the runtime while constructing blocks [default: Wasm]
[possible values: Native, Wasm, Both, NativeElseWasm]
--execution-import-block <STRATEGY>
The means of execution used when calling into the runtime while importing blocks [default: NativeElseWasm]
[possible values: Native, Wasm, Both, NativeElseWasm]
--execution-offchain-worker <STRATEGY>
The means of execution used when calling into the runtime while using an off-chain worker [default: Native]
[possible values: Native, Wasm, Both, NativeElseWasm]
--execution-other <STRATEGY>
The means of execution used when calling into the runtime while not syncing, importing or constructing
blocks [default: Native] [possible values: Native, Wasm, Both, NativeElseWasm]
--execution-syncing <STRATEGY>
The means of execution used when calling into the runtime while syncing blocks [default: NativeElseWasm]
[possible values: Native, Wasm, Both, NativeElseWasm]
--in-peers <COUNT>
Specify the maximum number of incoming connections we're accepting [default: 25]
--enable-offchain-indexing <ENABLE_OFFCHAIN_INDEXING>
Enable Offchain Indexing API, which allows block import to write to Offchain DB
--keystore-path <PATH> Specify custom keystore path
--listen-addr <LISTEN_ADDR>... Listen on this multiaddress
-l, --log <LOG_PATTERN>...
Sets a custom logging filter. Syntax is <target>=<level>, e.g. -lsync=debug
--max-parallel-downloads <COUNT>
Maximum number of peers from which to ask for the same blocks in parallel [default: 5]
--max-runtime-instances <max-runtime-instances> The size of the instances cache for each runtime
--name <NAME> The human-readable name for this node
--node-key <KEY> The secret key to use for libp2p networking
--node-key-file <FILE>
The file from which to read the node's secret key to use for libp2p networking
--node-key-type <TYPE>
The type of secret key to use for libp2p networking [default: Ed25519] [possible values: Ed25519]
--out-peers <COUNT>
Specify the number of outgoing connections we're trying to maintain [default: 25]
--password <password> Password used by the keystore
--password-filename <PATH> File that contains the password used by the keystore
--pool-kbytes <COUNT>
Maximum number of kilobytes of all transactions stored in the pool [default: 20480]
--pool-limit <COUNT>
Maximum number of transactions in the transaction pool [default: 8192]
--port <PORT> Specify p2p protocol TCP port
--prometheus-port <PORT> Specify Prometheus data source server TCP Port
--pruning <PRUNING_MODE>
Specify the state pruning mode, a number of blocks to keep or 'archive'
--public-addr <PUBLIC_ADDR>...
The public address that other nodes will use to connect to it. This can be used if there's a proxy in front
of this node
--reserved-nodes <ADDR>... Specify a list of reserved node addresses
--rpc-cors <ORIGINS>
Specify browser Origins allowed to access the HTTP & WS RPC servers
--rpc-methods <METHOD SET>
RPC methods to expose. [default: Auto] [possible values: Auto, Safe, Unsafe]
--rpc-port <PORT> Specify HTTP RPC server TCP port
--sentry <sentry>... Enable sentry mode
--sentry-nodes <ADDR>... Specify a list of sentry node public addresses
--state-cache-size <Bytes> Specify the state cache size [default: 67108864]
--telemetry-url <URL VERBOSITY>... The URL of the telemetry server to connect to
--tracing-receiver <RECEIVER>
Receiver to process tracing messages [default: Log] [possible values: Log, Telemetry]
--tracing-targets <TARGETS> Comma separated list of targets for tracing
--wasm-execution <METHOD>
Method for executing Wasm runtime code [default: Interpreted] [possible values: Interpreted]
--ws-max-connections <COUNT> Maximum number of WS RPC server connections
--ws-port <PORT> Specify WebSockets RPC server TCP port
SUBCOMMANDS:
build-spec Build a spec.json file, outputs to stdout
check-block Validate a single block
export-blocks Export blocks to a file
export-state Export state as raw chain spec
help Prints this message or the help of the given subcommand(s)
import-blocks Import blocks from file
purge-chain Remove the whole chain data
revert Revert chain to the previous state
異常處理
刪除緩存:
./target/release/substrate purge-chain --dev
刪除指定數據庫:
rm -rf /tmp/bob
rm -rf /tmp/alise
檢查9944端口是否被佔用
lsof -i:9944