首页 > 解决方案 > 获取基板托盘范围内的系统时间

问题描述

我试图在基板托盘的外部调用执行范围内获取系统时间。它应该独立于目标架构(wasm、unix 等)和构建模式(std、no_std)

使用 std::time::SystemTime、chrono 或 wasm-timer 库不是一个选项,因为它们严重依赖于 std。

在自定义托盘范围内引用时间戳托盘也不是一种选择,因为它随时提供链上时间戳,即当前或下一个区块时间戳。

问题:

    Compiling pallet-template v3.0.0-monthly-2021-08 (/workspace/supra/examples/concurrency_poc/node/pallets/template)
 error: duplicate lang item in crate `std` (which `wasm_timer` depends on): `panic_impl`.
   |
   = note: the lang item is first defined in crate `sp_io` (which `frame_support` depends on)
   = note: first definition in `sp_io` loaded from /workspace/supra/examples/concurrency_poc/node/target/debug/wbuild/node-template-runtime/target/wasm32-unknown-unknown/release/deps/libsp_io-e941cef754f8762e.rmeta
   = note: second definition in `std` loaded from /home/areg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libstd-0c448d560077210c.rlib

 error: duplicate lang item in crate `std` (which `wasm_timer` depends on): `oom`.
   |
   = note: the lang item is first defined in crate `sp_io` (which `frame_support` depends on)
   = note: first definition in `sp_io` loaded from /workspace/supra/examples/concurrency_poc/node/target/debug/wbuild/node-template-runtime/target/wasm32-unknown-unknown/release/deps/libsp_io-e941cef754f8762e.rmeta
   = note: second definition in `std` loaded from /home/areg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libstd-0c448d560077210c.rlib

标签: timestampsubstratepallet

解决方案


有没有办法使用基板手段在托盘“功能”范围内获取系统/实际当前时间戳

除了使用托盘时间戳或实施类似托盘之外,我不知道任何其他方式。

当节点启动并在运行时递增时如何初始化托盘时间戳的时间戳/现在值(它的来源是什么)

它使用固有外部,块生产者的客户端生成一些包含当前时间的固有数据,并在运行时 api BlockBuilder::inherent_extrinsics的帮助下生成固有外部放入块中

其他验证器将通过生成一些内在数据并使用BlockBuilder::check_inherents检查内在外在来检查内在是否良好

在托盘时间戳的情况下,这将检查块中固有的外部设置时间戳是否设置了一个合理的时间。不久的将来,并且超过有效的最小值:https ://docs.rs/pallet-timestamp/3.0.0/src/pallet_timestamp/lib.rs.html#227-245


推荐阅读