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源码的阅读

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容