如何获取 Substrate 中的当前区块号?
How do you get the current block number in Substrate?
我的问题是:
- 如何获取当前区块号?
- 如何将当前块号转换为 u32?
我正在尝试在模板托盘中编写一个函数,该函数采用 u32(输入参数)并将该数字添加到当前块编号。
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn revoke(origin: OriginFor<T>, number: u32) -> DispatchResult {
let caller = ensure_signed(origin)?;
// GET CURRENT BLOCK NUMBER
// CONVERT CURRENT BLOCK NUMBER TYPE TO U32
// TARGET_BLOCK = CURRENT_BLOCK_NUMBER + NUMBER
// STORE TARGET_BLOCK
Ok(())
}
我已阅读以下文档(以及更多),但我无法使代码正常工作。
- https://substrate.dev/rustdocs/latest/sp_runtime/offchain/storage_lock/trait.BlockNumberProvider.html#tymethod.current_block_number
- https://substrate.dev/rustdocs/latest/frame_system/pallet/struct.Pallet.html#method.current_block_number
- https://medium.com/paritytech/utxo-on-substrate-7f0e0576768e
这是我的尝试列表:
let y = System::block_number() as u32;
let z = sp_std::alloc::System::block_num() as u32;
let p = Self::block_number();
let h = BlockNumberProvider::current_block_number();
let h: BlockNumberFor<T> = BlockNumberProvider::current_block_number();
let p = BlockNumberProvider::current_block_number() as u32;
target_block = target_block + p;
let x = <T as sp_runtime::offchain::storage_lock::BlockNumberProvider>::current_block_number();
let x = <T as BlockNumberProvider>::current_block_number();
let j = frame_system::Pallet<Config>::block_number();
target_block = target_block + h;
let y = Self::BlockNumber;
let yy = <T as frame_system::Config>::BlockNumber;
let xc = frame_system::block_number();
let current_block = System::block_number()
.saturated_into::<u64>()
.saturating_sub(1);
let u = Number::<T>::get(T::BlockNumber);
let w = T::BlockNumber::block_number();
let n = <T as frame_system::Config>::BlockNumber::block_number();
let m = frame_system::<T>::BlockNumber;
let un = BlockNumberFor;
let current_block_number: u32 = <frame_system::Pallet<T>>::block_number();
let now_block_number = <frame_system::Module<T>>::block_number();
let now_block_number = <frame_system::Pallet<T>>::block_number();
let now_block_number_b: <T as frame_system::Config>::BlockNumber = <frame_system::Pallet<T>>::block_number();
T::BlockNumber;
let xp = now_block_number_b + 10;
let h: BlockNumberFor<T> = BlockNumberProvider::current_block_number();
let x: <T as Config>::BlockNumber = BlockNumberProvider::current_block_number();
let x = frame_system::Config::block_number();
let c = block_number();
let v = <T as frame_system::Config>::BlockNumber;
let x = block;
let q = block.into();
let x: Number<T> = block.into();
let f: u32 = x.into();
let v = 10.into();
let w: BlockNumber_<T> = block.into();
let target_block = target_block + f;
let current_block_number: Number<T> = <frame_system::Pallet<T>>::block_number();
let h: BlockNumberFor<T> = BlockNumberProvider::current_block_number();
let x: BlockNumber_<T> = frame_system::pallet::Pallet::current_block_number();
let y: BlockNumberFor<T> = frame_system::pallet::Pallet::current_block_number();
let z: T::BlockNumber = frame_system::pallet::Pallet::current_block_number();
如果您能提供任何帮助,我将不胜感激。
这是工作代码:
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn set_block_delay(origin: OriginFor<T>, block: T::BlockNumber) -> DispatchResult {
let caller = ensure_signed(origin)?;
let current_block_number = <frame_system::Pallet<T>>::block_number();
let block_delay: u32 = 10;
let target_block = current_block_number + block_delay.into();
ensure!(target_block < block, "fail");
Ok(())
}
我的问题是:
- 如何获取当前区块号?
- 如何将当前块号转换为 u32?
我正在尝试在模板托盘中编写一个函数,该函数采用 u32(输入参数)并将该数字添加到当前块编号。
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn revoke(origin: OriginFor<T>, number: u32) -> DispatchResult {
let caller = ensure_signed(origin)?;
// GET CURRENT BLOCK NUMBER
// CONVERT CURRENT BLOCK NUMBER TYPE TO U32
// TARGET_BLOCK = CURRENT_BLOCK_NUMBER + NUMBER
// STORE TARGET_BLOCK
Ok(())
}
我已阅读以下文档(以及更多),但我无法使代码正常工作。
- https://substrate.dev/rustdocs/latest/sp_runtime/offchain/storage_lock/trait.BlockNumberProvider.html#tymethod.current_block_number
- https://substrate.dev/rustdocs/latest/frame_system/pallet/struct.Pallet.html#method.current_block_number
- https://medium.com/paritytech/utxo-on-substrate-7f0e0576768e
这是我的尝试列表:
let y = System::block_number() as u32;
let z = sp_std::alloc::System::block_num() as u32;
let p = Self::block_number();
let h = BlockNumberProvider::current_block_number();
let h: BlockNumberFor<T> = BlockNumberProvider::current_block_number();
let p = BlockNumberProvider::current_block_number() as u32;
target_block = target_block + p;
let x = <T as sp_runtime::offchain::storage_lock::BlockNumberProvider>::current_block_number();
let x = <T as BlockNumberProvider>::current_block_number();
let j = frame_system::Pallet<Config>::block_number();
target_block = target_block + h;
let y = Self::BlockNumber;
let yy = <T as frame_system::Config>::BlockNumber;
let xc = frame_system::block_number();
let current_block = System::block_number()
.saturated_into::<u64>()
.saturating_sub(1);
let u = Number::<T>::get(T::BlockNumber);
let w = T::BlockNumber::block_number();
let n = <T as frame_system::Config>::BlockNumber::block_number();
let m = frame_system::<T>::BlockNumber;
let un = BlockNumberFor;
let current_block_number: u32 = <frame_system::Pallet<T>>::block_number();
let now_block_number = <frame_system::Module<T>>::block_number();
let now_block_number = <frame_system::Pallet<T>>::block_number();
let now_block_number_b: <T as frame_system::Config>::BlockNumber = <frame_system::Pallet<T>>::block_number();
T::BlockNumber;
let xp = now_block_number_b + 10;
let h: BlockNumberFor<T> = BlockNumberProvider::current_block_number();
let x: <T as Config>::BlockNumber = BlockNumberProvider::current_block_number();
let x = frame_system::Config::block_number();
let c = block_number();
let v = <T as frame_system::Config>::BlockNumber;
let x = block;
let q = block.into();
let x: Number<T> = block.into();
let f: u32 = x.into();
let v = 10.into();
let w: BlockNumber_<T> = block.into();
let target_block = target_block + f;
let current_block_number: Number<T> = <frame_system::Pallet<T>>::block_number();
let h: BlockNumberFor<T> = BlockNumberProvider::current_block_number();
let x: BlockNumber_<T> = frame_system::pallet::Pallet::current_block_number();
let y: BlockNumberFor<T> = frame_system::pallet::Pallet::current_block_number();
let z: T::BlockNumber = frame_system::pallet::Pallet::current_block_number();
如果您能提供任何帮助,我将不胜感激。
这是工作代码:
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn set_block_delay(origin: OriginFor<T>, block: T::BlockNumber) -> DispatchResult {
let caller = ensure_signed(origin)?;
let current_block_number = <frame_system::Pallet<T>>::block_number();
let block_delay: u32 = 10;
let target_block = current_block_number + block_delay.into();
ensure!(target_block < block, "fail");
Ok(())
}