gpt4 book ai didi

rust - 如何在基板模块中将 block 号转换为整数类型?

转载 作者:行者123 更新时间:2023-12-03 11:26:03 24 4
gpt4 key购买 nike

我正在测试substrate off-chain worker,我想要做的是接收当前的 block 号,然后做一些计算,就像下面的代码if (get_block / 10 == 0) ,我得到一些错误。如何将 block 号转换为 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 是对的。您不想将 block 号转换为整数,而是将整数转换为 block 号,然后进行数学运算。
所以替换:

get_block / 10 == 0
和:
(get_block / 10.into()).is_zero()

关于rust - 如何在基板模块中将 block 号转换为整数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65402395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com