Substrate中timestamp模块分析
Timestamp模块允许出块认证者在每个block中设置和验证一个时间戳。
使用inherents(内部固有)来设置timestamp数据,是出块者设置,其他节点来验证。在每个区块上只能设置一次,区块高度根据时间测量会引起问题,因为不断积累的计算错误,因此需要避免使用时间测量来作为区块高度。
Trait
/// 用来表达时间戳
type Moment: Parameter + Default + SimpleArithmetic
+ Scale<Self::BlockNumber, Output = Self::Moment> + Copy;
/// 当某件事情时间戳设置了,这件事情会被提示到
type OnTimestampSet: OnTimestampSet<Self::Moment>;
/// 最小的区块间隔
type MinimumPeriod: Get<Self::Moment>;
Module
fn set(origin, #[compact] now: T::Moment)
设置现在的时间,每个区块只能设置一次,出块者设置,设置的时间跟上一个区块设置的时间戳的时间差必须要大于MinimumPeriod
。
Store
/// Current time for the current block.
pub Now get(fn now) build(|_| 0.into()): T::Moment;
/// Did the timestamp get updated in this block?
DidUpdate: bool;
Module
- pub fn get() -> T::Moment
- 获取现在区块的时间戳
- pub fn set_timestamp(now: T::Moment)
- 测试网进行的设置
ProvideInherent
ProvideInherent是节点不需要签名的,内部固有,主要就是时间戳。
- fn create_inherent(data: &InherentData) -> Option<Self::Call>
- fn check_inherent(call: &Self::Call, data: &InherentData) -> result::Result<(), Self::Error>
网友评论