Substrate Contracts Node 项目教程
1. 项目的目录结构及介绍
substrate-contracts-node/
├── Cargo.lock
├── Cargo.toml
├── README.md
├── bin/
│ └── substrate-contracts-node
├── node/
│ ├── Cargo.toml
│ └── src/
│ ├── benchmarking.rs
│ ├── chain_spec.rs
│ ├── cli.rs
│ ├── command.rs
│ ├── main.rs
│ ├── rpc.rs
│ └── service.rs
├── primitives/
│ ├── Cargo.toml
│ └── src/
│ └── lib.rs
├── runtime/
│ ├── Cargo.toml
│ └── src/
│ └── lib.rs
└── scripts/
└── init.sh
目录结构介绍
- Cargo.lock: 锁定依赖版本的文件。
- Cargo.toml: 项目的依赖和元数据配置文件。
- README.md: 项目的基本介绍和使用说明。
- bin/: 包含可执行文件
substrate-contracts-node
。 - node/: 包含节点的主要代码,包括启动文件、配置文件等。
- src/: 节点的源代码目录。
- main.rs: 节点的入口文件。
- chain_spec.rs: 链的配置文件。
- cli.rs: 命令行接口的实现。
- command.rs: 命令处理逻辑。
- rpc.rs: RPC 接口的实现。
- service.rs: 节点服务的实现。
- src/: 节点的源代码目录。
- primitives/: 包含项目的基本数据结构和类型定义。
- runtime/: 包含运行时的逻辑和配置。
- scripts/: 包含一些初始化脚本。
2. 项目的启动文件介绍
node/src/main.rs
main.rs
是 Substrate Contracts Node 的入口文件,负责启动整个节点。它包含了节点的初始化逻辑、命令行参数解析、以及服务的启动。
fn main() -> sc_cli::Result<()> {
let version = sc_cli::VersionInfo {
name: "Substrate Contracts Node",
commit: env!("VERGEN_SHA_SHORT"),
version: env!("CARGO_PKG_VERSION"),
executable_name: "substrate-contracts-node",
author: "Parity Technologies <admin@parity.io>",
description: "Substrate node configured for smart contracts",
support_url: "https://github.com/paritytech/substrate-contracts-node/issues",
copyright_start_year: 2019,
};
substrate_contracts_node::command::run(version)
}
启动命令
substrate-contracts-node --dev
--dev
: 启动开发模式,自动生成开发链。
3. 项目的配置文件介绍
node/src/chain_spec.rs
chain_spec.rs
文件定义了链的配置,包括创世块的配置、节点配置等。
pub fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
) -> GenesisConfig {
GenesisConfig {
// 其他配置项...
pallet_contracts: Some(ContractsConfig {
current_schedule: pallet_contracts::Schedule {
enable_println,
..Default::default()
},
}),
}
}
node/Cargo.toml
Cargo.toml
文件是 Rust 项目的依赖和元数据配置文件,定义了项目的依赖库、版本、作者等信息。
[package]
name = "substrate-contracts-node"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
# 依赖库...
runtime/src/lib.rs
lib.rs
文件定义了运行时的逻辑和配置,包括模块的注册、配置项的定义等。
impl pallet_contracts::Config for Runtime {
type Time = Timestamp;
type Randomness = RandomnessCollectiveFlip;
type Currency = Balances;
type Event = Event;
type Call = Call;
type CallFilter = frame_support::traits::Everything;
type WeightInfo = ();
type ChainExtension = ();
type Schedule = pallet_contracts::Schedule;
type CallStack = [pallet_contracts::Frame<Self>; 31];
type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = ();
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 }>;
}
通过以上配置文件,可以定制化 Substrate Contracts Node 的行为和功能。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考