This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Documentation for the timestamp module #1927
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
679ec03
docs for timestamp module
f7c4248
addressed review comments in timestamp module docs
1cf17db
fixed typos
b695b9b
uppercase readme file name
2f526c1
merge with master
48288ab
merge
a8c1ade
fixed build failure
d3bc702
removed md file
ac865a8
updated as per the latest template
a10e516
merge
024c0a1
updated docs
642f53f
merge with master
6508415
updated docs
aaab62c
addressed review comments
4de6d41
merge
843a2aa
addressed review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Timestamp Module | ||
|
|
||
| The timestamp module provides functionality to get and set the on-chain time. | ||
|
|
||
| ## Overview | ||
|
|
||
| The timestamp module allows the validators to set and validate a timestamp with each block. It uses timestamp data as an inherent which is provided by the block author and validated/verified by other validators. | ||
|
|
||
| It is expected that the timestamp is set by the validator in the beginning of each block, typically one of the first extrinsics. The timestamp can be set only once per block and must be set each block. | ||
|
|
||
| Note, that there might be a constraint on how much time must pass before setting the new timestamp, specified by the `tim:block_period` storage entry. | ||
|
|
||
| The timestamp module is the recommended way to query the on-chain time instead of using an approach based on block numbers. The block numbers based time measurement can cause issues because of cummulative calculation errors and hence it should be avoided. | ||
|
|
||
| ## Public Interface | ||
|
|
||
| ### Types | ||
|
|
||
| * `Moment` - Represents the current timestamp. | ||
|
|
||
| ### Storage Items | ||
|
|
||
| * `Now`: `Moment` - The current timestamp represented as **total seconds from the unix epoch**. | ||
| * `BlockPeriod`: `Moment` - The minimum (and advised) period between blocks. | ||
|
|
||
| ### Public Immutable functions | ||
|
|
||
| #### get() | ||
|
|
||
| Get the current time for the current block. If this function is called prior the setting to timestamp, it will return the timestamp of the previous block. | ||
|
|
||
| Returns the timestamp as `Moment`. | ||
|
|
||
| #### block_period() | ||
|
|
||
| Get the block period for the chain. Return the block period as the `Moment` type. | ||
|
|
||
| ### Public Mutable functions | ||
|
|
||
| #### set(origin, now: T::Moment) | ||
|
|
||
| Sets the current time. This call should be invoked exactly once per block. It will panic at the finalization phase, if this call hasn't been invoked by that time. | ||
|
|
||
| The timestamp should be greater than the previous one by the amount specified by `block_period`. | ||
|
|
||
| ##### Errors: | ||
|
|
||
| * Timestamp must be updated only once in the block | ||
| * Timestamp must increment by at least `BlockPeriod` between sequential blocks | ||
|
|
||
| ### Inherent Data | ||
|
|
||
| The timestamp module manages the block timestamp using InherentData. To identify the timestamp inherent, it defines an `InherentIdentifier`. | ||
|
|
||
| The timestamp module defines and implements the trait `TimestampInherentData` for `InherentData` to query the timestamp inherent using the timestamp `InherentIdentifier`. | ||
|
|
||
| ## Usage | ||
|
|
||
| The following example shows how to use the timestamp module in your custom module to query the current timestamp. | ||
|
|
||
| In your custom module, after importing the `timestamp` module and deriving your module's configuration trait with the timestamp trait, call the timestamp module's `get` function to get the current timestamp, | ||
|
|
||
| ``` | ||
| let now = <timestamp::Module<T>>::get(); | ||
| ``` | ||
|
|
||
| Similarly, the `block_period` function can be called. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.