如何在衬底模块中将块编号转换为Integer类型? [英] How can I convert the block number into the Integer type in substrate module?
本文介绍了如何在衬底模块中将块编号转换为Integer类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在测试底层离链工人,我想做的是接收当前的块数,然后做一些计算,就像下面的代码if (get_block / 10 == 0)
,我得到了一些错误。如何将块编号转换为整数类型?
我的代码
use frame_support::{decl_storage, decl_module, dispatch::DispatchResult, debug};
use frame_system::{ensure_signed, offchain};
use sp_runtime::{
offchain::http,
transaction_validity::{
TransactionValidity, TransactionLongevity, ValidTransaction, InvalidTransaction
}
};
pub trait Trait: frame_system::Trait {}
decl_storage! {
trait Store for Module<T: Trait> as Runtime_example {
SubjectCount: u32;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn offchain_worker(block: T::BlockNumber){
let get_block = block;
if (get_block / 10 == 0) {
debug::info!("print !!!!!!!!!!!!!!!!");
}
}
}
}
错误日志
error[E0308]: mismatched types
--> /home/substrate-node-template/runtime/src/runtime_example.rs:32:29
|
32 | if (get_block / 10 == 0) {
| ^ expected associated type, found integer
|
= note: expected associated type `<T as frame_system::Trait>::BlockNumber`
found type `{integer}`
= help: consider constraining the associated type `<T as frame_system::Trait>::BlockNumber` to `{integer}` or calling a method that returns `<T as frame_system::Trait>::BlockNumber`
error[E0308]: mismatched types
--> /home/substrate-node-template/runtime/src/runtime_example.rs:32:34
|
32 | if (get_block / 10 == 0) {
| ^ expected associated type, found integer
|
= note: expected associated type `<T as frame_system::Trait>::BlockNumber`
found type `{integer}`
= help: consider constraining the associated type `<T as frame_system::Trait>::BlockNumber` to `{integer}` or calling a method that returns `<T as frame_system::Trait>::BlockNumber`
推荐答案
@kmdreko是对的。您不希望将块编号转换为整数,而是将该整数转换为块编号,然后进行计算。
因此替换:
get_block / 10 == 0
使用:
(get_block / 10.into()).is_zero()
这篇关于如何在衬底模块中将块编号转换为Integer类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文