rust - 如何在基板模块中将块号转换为整数类型?
问题描述
我正在测试substrate off-chain worker,我想要做的是接收当前的块号,然后做一些计算,就像下面的代码一样if (get_block / 10 == 0)
,我得到了一些错误。如何将块号转换为 Integer 类型?
我的代码
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()
推荐阅读
- firebase - 奇怪的 Firestore 控制台情况
- c++ - 我在读取变量、比较它然后在使用循环时打印输出时遇到问题
- database - 检查并比较两个表并更新表2在访问数据库中如何可能
- postgresql - 如何使用带有函数 pgr_dijkstra 的循环
- javascript - 可点击的信息框
- spring - spring boot 数据 cassandra 反应式 JmxReporter 问题
- html - 如何通过一个渐变填充多个 svg 路径?
- nexus - 如何获取 Nexus 创建工件的日期?
- python - 我需要什么样的对象才能使用 fix_multiple_files 的 options 参数?
- json - 如何在 alamofire 中使用 JSON 元素