Substrate 代码debug

前言

整理在开发pallet过程中使用到的代码打印控制台。官方资料:https://docs.substrate.io/test/debug/#logging-utilities
主要有以下几种:

创建项目

进入到/substrate-node-template中,接着进入到目录pallets中,创建我们自己的lfhuang-debug-pallet。并且将这pallet加载到runtime中。

使用Logging utilities

通过使用 log crate 代码包在代码中使用log输出日志,包括debug和info等,和Java类似用法。
需要引入的依赖:

[package]
name = "lfhuang-debug-pallet"
description = "FRAME pallet template for debug"
authors = ["lfhuang"]
...

[dependencies]
log = "0.4.17"
...

接着可以在lib.rs业务代码使用log进行打印。

#[pallet::weight(0)]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
	let who = ensure_signed(origin)?;
	<Something<T>>::put(something);
    // Debug方式一 log
	log::info!(
		"🥸 🥸 🥸 ######################## called by something:{:?} and signer:{:?}",
		something,
		who
	);
	Self::deposit_event(Event::SomethingStored(something, who));
	Ok(())
}

通过调用交易凭证能看到输出日志信息如下:
image.png

使用Printable trait

通过使用Printable trait 代码包实现它,下面通过Error类型实现打印输出日志信息。需要开启debug环境变量模式启动。
当前非test调试代码,所以sp-runtime的相关依赖需要放到[dependencies]依赖下。

...

[dependencies]
sp-runtime = { version = "7.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" }
...
// 引入依赖
use sp_runtime::print;
use sp_runtime::traits::Printable;

...
    
#[pallet::error]
pub enum Error<T> {
	/// Value was None
	NoneValue,
	/// Value reached maximum and cannot be incremented further
	StorageOverflow,
}
impl<T: Config> Printable for Error<T> {
	fn print(&self) {
		match self {
			Error::NoneValue => "Invalid Value".print(),
			Error::StorageOverflow => "Value Exceeded and Overflowed".print(),
			_ => "Invalid Error Case".print(),
		}
	}
}

...

#[pallet::weight(0)]
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
	// 检查是否已签名
	let _who = ensure_signed(origin)?;
	print("🥸 🥸 🥸 ########### 错误原因... #############");
	match <Something<T>>::get() {
		None => {
			print(Error::<T>::NoneValue);	// Debug方式二,默认是debug级别,所以启动时候把环境变量调整到debug启动
			Err(Error::<T>::NoneValue)?
		},
		Some(old) => {
			let new = old.checked_add(1).ok_or({
				print(Error::<T>::StorageOverflow);
				Error::<T>::StorageOverflow
			})?;
			<Something<T>>::put(new);
			Ok(())
		},
	}
}

首次调用cause_error没有值则会报Error::NoneValue错误,需要调用do_something方法设置一个值,接着再掉用cause_error方法则存储值会加1,同时也会报Error::StorageOverflow错误。
image.png

使用print

使用print函数进行调试记录runtime执行状态。需要导入use sp_runtime::print;和上面的Printable trait方式差不多,输出打印日志都使用到了print();需要开启debug环境变量模式启动。
官网例子中的print!();编译报错。部分代码例子如下:

#[pallet::weight(0)]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
	let who = ensure_signed(origin)?; // 检查是否已签名
	print("🥸 🥸 🥸 开始设置值..."); // Debug方式三 print,默认是debug级别,所以启动时候把环境变量调整到debug启动
	<Something<T>>::put(something);
	print("🥸 🥸 🥸 存储值完成...");
	// Debug方式一 log
	log::info!(
		"🥸 🥸 🥸 ######################## called by something:{:?} and signer:{:?}",
		something,
		who
	);
	Self::deposit_event(Event::SomethingStored(something, who));
	Ok(())
}

使用if_std

使用if_std宏的方式实现调试。

...

[dependencies]
sp-std = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" }
...
// 引入依赖
use sp_std::if_std;

println!语句应该在if_std宏的内部。

#[pallet::weight(0)]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
	let who = ensure_signed(origin)?; // 检查是否已签名
	print("🥸 🥸 🥸 开始设置值..."); // Debug方式三 print,默认是debug级别,所以启动时候把环境变量调整到debug启动
	<Something<T>>::put(something);
	print("🥸 🥸 🥸 存储值完成...");
	// Debug方式一 log
	log::info!(
		"🥸 🥸 🥸 ######################## called by something:{:?} and signer:{:?}",
		something,
		who
	);
	// Debug方式四 if_std!宏
	if_std! {
		println!("😂 😂 😂 ################## Hello native world! something value: {}",something);
		println!("😂 😂 😂 ################## 调用者的交易账户是: {:#?}", who);
	}
	Self::deposit_event(Event::SomethingStored(something, who));
	Ok(())
}

image.png

检查编译代码

cargo check -p node-template-runtime --release

编译节点模板

返回到项目根目录下/substrate-node-template

cargo build --package node-template --release

启动节点

运行一个临时节点,该节点将在流程结束时删除所有配置,指定开发链

./target/release/node-template --tmp --dev

使用RUST_LOG环境变量运行节点二进制文件以打印值。

RUST_LOG=runtime=debug ./target/release/node-template --tmp --dev

连接节点

1、在浏览器中输入https://polkadot.js.org/apps/#/explorer
2、点击左上角会展开 
3、在展开的菜单中点击 DEVELOPMENT 
4、点击 Local Node 
5、点击 switch (转换到本地网络)
*** Key Features *** * Learn to deliver superior server-to-server communication through the networking channels * Gain expertise of the networking features of your own applications to support various network architectures such as client/server and peer-to-peer * Explore the issues that impact scalability, affect security, and allow applications to work in a heterogeneous environment *** Book Description *** Network-aware applications are becoming more prevalent and play an ever-increasing role in the world today. Connecting and using an Internet-based service is a frequent requirement for many applications. Java provides numerous classes that have evolved over the years to meet evolving network needs. These range from low-level socket and IP-based approaches to those encapsulated in software services. This book explores how Java supports networks, starting with the basics and then advancing to more complex topics. An overview of each relevant network technology is presented followed by detailed examples of how to use Java to support these technologies. We start with the basics of networking and then explore how Java supports the development of client/server and peer-to-peer applications. The NIO packages are examined as well as multitasking and how network applications can address practical issues such as security. A discussion on networking concepts will put many network issues into perspective and let you focus on the appropriate technology for the problem at hand. The examples used will provide a good starting point to develop similar capabilities for many of your network needs. *** What you will learn *** * Connect to other applications using sockets * Use channels and buffers to enhance communication between applications * Access network services and develop client/server applications * Explore the critical elements of peer-to-peer applications and current technologies available * Use UDP to perform multicasting * Address scalability through the use of co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值