Sub3.0模板進化1構建node-template官方模板--by Skyh0218

substrate 2.0 已經(jīng)使用1年了,剛好最近有時間,就從3.0開始搭建node-tempate腳手架,并加入官方治理pallet,成為基礎鏈

1. node-template模板目錄

官方目錄在 https://github.com/substrate-developer-hub/substrate-node-template

但是查看Cargo.toml還停留在2.0.1


image.png

其實最新版的node-template在substrate倉庫里的子文件夾
https://github.com/paritytech/substrate/tree/master/bin/node-template

2. 構建本地最新的最新版node-template

首先先把substrate clone下來

git clone https://github.com/paritytech/substrate.git

如果想拆出新項目,把該文件夾拷出來

cp -r ./substrate/bin/node-template/ ./node-template

現(xiàn)在目錄是這樣


image.png

3. 添加Cargo.toml和修改依賴

其中很多依賴是根據(jù)substrate相對定位來確定依賴的,如果要成為獨立項目,必須把相對定位改成遠程


image.png

3.1 在根目錄添加Cargo.toml,全部依賴指向master(太多有可能有warning)

[profile.release]
panic = 'unwind'

[workspace]
members = [
    'node',
    'pallets/*',
    'runtime',
]

[patch.crates-io]
frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-executive = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-support-procedural = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-support-procedural-tools = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-support-procedural-tools-derive = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-authorship = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-aura = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-babe = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-balances = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-bounties = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-collective = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-grandpa = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-indices = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-im-online = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-membership = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-multisig = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-offences = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-proxy = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-recovery = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-scheduler = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-session = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-staking = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-sudo = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-tips = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-treasury = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
pallet-utility = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-block-builder = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-basic-authorship = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus-aura = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-chain-spec = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-cli = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-client-api = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-client-db = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus-babe = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus-babe-rpc = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus-slots = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus-manual-seal = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-consensus-epochs = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-executor = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-finality-grandpa = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-finality-grandpa-rpc = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-informant = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-keystore = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-network = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-offchain = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-rpc = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-rpc-api = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-service = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-telemetry = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sc-transaction-pool = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-api = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-application-crypto = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-arithmetic = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-block-builder = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-consensus = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-consensus-babe = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-externalities = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-inherents = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-keystore = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-offchain = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-rpc = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-runtime-interface = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-state-machine = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-session = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-staking = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-storage = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-timestamp = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-tracing = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-trie = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-version = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
substrate-frame-rpc-system = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }
sp-runtime-interface-proc-macro = { git = "https://github.com/paritytech/substrate.git", branch="master", default-features=false }

根目錄添加Makefile

.PHONY: init
init:
    ./scripts/init.sh

.PHONY: check
check:
    SKIP_WASM_BUILD=1 cargo check --release

.PHONY: test
test:
    SKIP_WASM_BUILD=1 cargo test --release --all

.PHONY: run
run:
     cargo run --release -- --dev --tmp

.PHONY: build
build:
     cargo build --release

添加完應該目錄變成


image.png

3.2 修改子文件夾相對路徑, 具體有runtime, node, pallets文件夾
我一般先把, path.", 換成, 然后再用, path."換成空

image.png

注意自己的文件夾依賴
image.png

或者把我以下三個文件分別拷入
runtime

[package]
name = "node-template-runtime"
version = "3.0.0"
authors = ["Anonymous"]
edition = "2018"
license = "Unlicense"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
funty = { version = "=1.1.0", default-features = false } # https://github.com/bitvecto-rs/bitvec/issues/105

pallet-aura = { version = "3.0.0", default-features = false }
pallet-balances = { version = "3.0.0", default-features = false }
frame-support = { version = "3.0.0", default-features = false }
pallet-grandpa = { version = "3.0.0", default-features = false }
pallet-randomness-collective-flip = { version = "3.0.0", default-features = false }
pallet-sudo = { version = "3.0.0", default-features = false }
frame-system = { version = "3.0.0", default-features = false }
pallet-timestamp = { version = "3.0.0", default-features = false }
pallet-transaction-payment = { version = "3.0.0", default-features = false }
frame-executive = { version = "3.0.0", default-features = false }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
sp-api = { version = "3.0.0", default-features = false }
sp-block-builder = { default-features = false, version = "3.0.0"}
sp-consensus-aura = { version = "0.9.0", default-features = false }
sp-core = { version = "3.0.0", default-features = false }
sp-inherents = { default-features = false, version = "3.0.0"}
sp-offchain = { version = "3.0.0", default-features = false }
sp-runtime = { version = "3.0.0", default-features = false }
sp-session = { version = "3.0.0", default-features = false }
sp-std = { version = "3.0.0", default-features = false }
sp-transaction-pool = { version = "3.0.0", default-features = false }
sp-version = { version = "3.0.0", default-features = false }

# Used for the node template's RPCs
frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false }

# Used for runtime benchmarking
frame-benchmarking = { version = "3.0.0", default-features = false, optional = true }
frame-system-benchmarking = { version = "3.0.0", default-features = false, optional = true }
hex-literal = { version = "0.3.1", optional = true }

template = { version = "3.0.0", default-features = false, package = "pallet-template", path = "../pallets/template" }

[build-dependencies]
substrate-wasm-builder = { version = "4.0.0" }

[features]
default = ["std"]
std = [
    "codec/std",
    "frame-executive/std",
    "frame-support/std",
    "pallet-aura/std",
    "pallet-balances/std",
    "pallet-grandpa/std",
    "pallet-randomness-collective-flip/std",
    "pallet-sudo/std",
    "pallet-timestamp/std",
    "pallet-transaction-payment/std",
    "pallet-transaction-payment-rpc-runtime-api/std",
    "serde",
    "sp-api/std",
    "sp-block-builder/std",
    "sp-consensus-aura/std",
    "sp-core/std",
    "sp-inherents/std",
    "sp-offchain/std",
    "sp-runtime/std",
    "sp-session/std",
    "sp-std/std",
    "sp-transaction-pool/std",
    "sp-version/std",
    "frame-system/std",
    "frame-system-rpc-runtime-api/std",
    "template/std",
]
runtime-benchmarks = [
    "sp-runtime/runtime-benchmarks",
    "frame-benchmarking",
    "frame-support/runtime-benchmarks",
    "frame-system-benchmarking",
    "hex-literal",
    "frame-system/runtime-benchmarks",
    "pallet-balances/runtime-benchmarks",
    "pallet-timestamp/runtime-benchmarks",
]

node:

[package]
name = "node-template"
version = "2.0.0"
authors = ["Anonymous"]
description = "A new FRAME-based Substrate node, ready for hacking."
edition = "2018"
license = "Unlicense"
build = "build.rs"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[[bin]]
name = "node-template"

[dependencies]
structopt = "0.3.8"

sc-cli = { version = "0.9.0", features = ["wasmtime"] }
sc-executor = { version = "0.9.0", features = ["wasmtime"] }
sc-service = { version = "0.9.0", features = ["wasmtime"] }
sp-core = "3.0.0"
sc-telemetry = "3.0.0"
sc-keystore = "3.0.0"
sp-inherents = "3.0.0"
sc-transaction-pool = "3.0.0"
sp-transaction-pool = "3.0.0"
sc-consensus-aura = "0.9.0"
sp-consensus-aura = "0.9.0"
sp-consensus = "0.9.0"
sc-consensus = "0.9.0"
sc-finality-grandpa = "0.9.0"
sp-finality-grandpa = "3.0.0"
sc-client-api = "3.0.0"
sp-runtime = "3.0.0"

# These dependencies are used for the node template's RPCs
jsonrpc-core = "15.1.0"
sc-rpc = "3.0.0"
sp-api = "3.0.0"
sc-rpc-api = "0.9.0"
sp-blockchain = "3.0.0"
sp-block-builder = "3.0.0"
sc-basic-authorship = "0.9.0"
substrate-frame-rpc-system = "3.0.0"
pallet-transaction-payment-rpc = "3.0.0"

# These dependencies are used for runtime benchmarking
frame-benchmarking = "3.0.0"
frame-benchmarking-cli = "3.0.0"

node-template-runtime = { version = "3.0.0", path = "../runtime" }

[build-dependencies]
substrate-build-script-utils = "3.0.0"

[features]
default = []
runtime-benchmarks = [
    "node-template-runtime/runtime-benchmarks",
]

pallets:

[package]
authors = ['Anonymous']
edition = '2018'
name = 'pallet-template'
version = "3.0.0"
license = "Unlicense"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME pallet template for defining custom runtime logic."
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }

frame-support = { version = "3.0.0", default-features = false }
frame-system = { version = "3.0.0", default-features = false }
sp-runtime = { version = "3.0.0", default-features = false }

[dev-dependencies]
serde = { version = "1.0.101" }
sp-core = { version = "3.0.0", default-features = false }
sp-io = { version = "3.0.0", default-features = false }

[features]
default = ['std']
std = [
    'codec/std',
    'frame-support/std',
    'frame-system/std'
]

4. Build

好可以開始build項目了,可以使用

cargo build --release
或者
make build

坑:build第一次bitvec出現(xiàn)問題了,基于codec的依賴

image.png

百思不得其解,參考其他項目,終于找到問題的可能所在,由于funty大改:
https://github.com/bitvecto-rs/bitvec/issues/105
在codec下面加入以下依賴就好
image.png

然后重新make build就好了(由于build時間相對較長,慢慢等待)

5. 以下

這是該系列的第一篇,以下將加入修改共識,加入治理模塊和功能模塊,也會加入些frame源碼的閱讀

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容