Sub3.0模板进化1构建node-template官方模板--by Skyh0218

substrate 2.0 已经使用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

现在目录是这样


image.png

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

其中很多依赖是根据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出现问题了,基于codec的依赖

image.png

百思不得其解,参考其他项目,终于找到问题的可能所在,由于funty大改:
https://github.com/bitvecto-rs/bitvec/issues/105
在codec下面加入以下依赖就好
image.png

然后重新make build就好了(由于build时间相对较长,慢慢等待)

5. 以下

这是该系列的第一篇,以下将加入修改共识,加入治理模块和功能模块,也会加入些frame源码的阅读

你可能感兴趣的:(Sub3.0模板进化1构建node-template官方模板--by Skyh0218)