Substrate开发学习

学习内容

  • 搭建 substrate 的开发环境;
  • 使用 template 运行基于 substrate 的区块链;
  • 使用 substrate 的前端模板和我们搭建的区块链进行交互;

什么是 Substrate

从高层看,一个区块链节点包含以下几个关键组件(key components)

  • Storage 存储
    • key-value 存储机制
  • P2P 网络
    • 允许客户端直接与其他网络参与者通信。
  • 共识的能力
  • 外部或“外部”信息的数据处理能能力
  • 一个 Runtime
    • 定义了区块的处理方式,主要是状态转换的逻辑。
    • 在 Substrate 中,runtime code 被编译为 wasm 作为区块链存储状态的一部分。

因为构建上述组件比较复杂,所以目前大多数区块链项目都是从现有的区块链项目派生出来的。

例如,比特币仓库分叉创建:莱特币、ZCash、Namecoin和比特币现金。类似地,以太坊仓库分叉创建Quorum、POA Network、KodakCoin和Musicoin。

Substrate 是用于构建区块链的开源、模块化和可扩展的框架。

Substrate 是设计灵活,允许使用者设计和构建满足其需求的区块链网络。它提供了构建自定义区块链节点所需要的所有核心组件(core components)。

一些基础概念

pallet

  • 使用 FRAME 开发框架构建的模块
  • 作用:向 node template 中添加指定 pallet(即向 node 的 runtime 中添加),就是添加一个指定功能的模块。
  • 是通过 crate 的形式呈现的,所以使用时需要在 Cargo.toml 中添加到 dependencies

搭建开发环境

以下命令为 Ubuntu 环境,更多环境参考官方文档

安装基础依赖软件

1
sudo apt update && sudo apt install -y git clang curl libssl-dev llvm libudev-dev

安装 rust 相关

安装 rust

1
curl https://sh.rustup.rs -sSf | sh

加载 cargo 环境

1
source ~/.cargo/env

配置 rust 工具链默认为最新的稳定版本

1
2
rustup default stable
rustup update

添加 nightly release和 nightly WebAssembly (wasm) 目标

1
2
rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly

确定安装信息

1
2
rustc --version
rustup show

使用模板搭建 Substrate 节点开发环境

使用 node template

1
2
3
4
5
6
7
8
git clone https://github.com/substrate-developer-hub/substrate-node-template

cd substrate-node-template
# We want to use the `latest` tag throughout all of this tutorial
git checkout latest

# 对模板内容编译
cargo build --release

编译如果出错,查看 遇到问题

看到 finalized 的数量在增加,说明节点启动好了。

安装 front-end template

前端模板

安装 nodejs

要求 nodejs 版本 >= v14

添加指定版本源

1
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

setup_16.x 是指 16 这个大版本,如果版本到了 17 就改成 setup_17.x 。具体版本可以的 https://nodejs.org 查看最新版本。

执行安装

1
sudo apt-get install -y nodejs

验证版本号

1
node -v

安装完 nodejs 后,npm 也会一并安全了。

验证或安装 yarn

如果有 yarn 则不需要安装,没有就安装

1
npm install -g yarn

验证是否安装,查看版本号

1
yarn --version

下载前端模板

1
2
3
4
5
6
7
git clone https://github.com/substrate-developer-hub/substrate-front-end-template

cd substrate-front-end-template
# We want to use the `latest` tag throughout all of this tutorial
git checkout latest

yarn install

启动本地 Substrate 节点

打开一个终端

1
2
cd substrate-node-template
./target/release/node-template --dev

启动前端模板

再开一个终端

1
2
cd substrate-front-end-template
yarn start

启动后,打开浏览器

1
http://localhost:8000

添加一个 Pallet 到 Runtime

什么是 pallet ?

添加 pallet-nicks 依赖到项目中

添加 pallet-nicks crate 到 runtime 的 Cargo.toml 文件中 [dependencies] 段落中

1
pallet-nicks = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.20" }

添加 pallet-nicksfeatures 段落中

1
2
3
4
5
6
7
8
9
[features]
default = ['std']
std = [
...
'pallet-aura/std',
'pallet-balances/std',
'pallet-nicks/std', # 添加这行
...
]

检查依赖是否正常

1
cargo check -p node-template-runtime

模块名可以通过查看 Cargo.toml 中的 package name 获得

检查 Nicks pallet 的 configuration trait

每个 pallet 都有一个被称为 Config 的Rust trait,用于配置 pallet 在运行时需要的参数和类型。

Runtime 要实现指定 pallet 的 Config

trace the accounts that hold balances.

跟踪持有余额的账户。

balance 在金融领域,有余额的含义。

record an account’s balance.

记录一个账户的余额。

如何做?

查看 pallet_nick 的 Config 定义文档;

查看 Balance pallet 的 Config 的实现(implementation);

遇到问题

按照 Substrate官网入门教程编译节点模板

1
spoon@spoon-vm:~/devops/codes/substrate-node-template$ cargo build --release

报错如下

1
2
3
4
5
error: failed to run custom build command for `tikv-jemalloc-sys v0.4.3+5.2.1-patched.2`

Caused by:
process didn't exit successfully: `/home/spoon/devops/codes/substrate-node-template/target/release/build/tikv-jemalloc-sys-df85f4a5b8101ca7/build-script-build` (exit status: 101)
--- stdout

解决方法

安装 cmake

1
sudo apt-get install cmake

然后重新编译即可。

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2023 ligongzhao
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信