1

I am trying to get system time in scope of the extrinsic call execution of the substrate pallet. It should work independent on target arch (wasm, unix, etc.) and build mode (std, no_std)

Using std::time::SystemTime, chrono or wasm-timer libraries are not an option as they heavily depend on std.

Referencing timestamp pallet in scope of the custom pallet also not an option, as at any moment it provides on-chain timestamp, which is current or next block timestamp.

Questions:

  • is there a way to get system/actual-current timestamp in scope of pallet "functions" using substrate means

  • how the timestamp/now value of pallet-timestamp is initialized when node is started and incremented during runtime (what is the source of it)

  • could you suggest any library providing such means that can be linked with pallet without facing similar errors below

    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
user1051417
  • 43
  • 1
  • 1
  • 3

1 Answers1

1

is there a way to get system/actual-current timestamp in scope of pallet "functions" using substrate means

I don't know any other way than using pallet-timestamp or implementing similar pallet.

how the timestamp/now value of pallet-timestamp is initialized when node is started and incremented during runtime (what is the source of it)

It uses inherent extrinsic, the client of the block producer generates some inherent data which contains the current time, and with the help of the runtime api BlockBuilder::inherent_extrinsics it generates the inherent extrinsics to put into the block

The other validators will check that the inherent are good by generating some inherent data and checking the inherent extrinsics with BlockBuilder::check_inherents

In the case of pallet-timestamp, this will check that the inherent extrinsic setting the timestamp in the block set a sensible time. Not too far in the future, and above a valid minimum: https://docs.rs/pallet-timestamp/3.0.0/src/pallet_timestamp/lib.rs.html#227-245

thiolliere
  • 516
  • 2
  • 3
  • Thanks for the reply and comments. For our purpose value provided by pallet-timestamp does not suffice as we are aiming system-time but not on-chain time. But thanks to your hints "runtime_interface" API of substrate FWK helped to solve the issue. References: - https://crates.parity.io/sp_runtime_interface/index.html -https://github.com/PureStake/moonbeam/commit/c5f546631f9293a689512268dec99d232c870344 – user1051417 Sep 16 '21 at 07:00