1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[allow(missing_copy_implementations)]
/// Data inherent to an invocation. Meant to be supplied by a runtime, not a user.
#[must_use]
#[non_exhaustive]
pub struct InherentData {
/// The seed to associate with an invocation.
pub seed: u64,
/// The timestamp to associate with an invocation.
pub timestamp: u64,
}
impl InherentData {
/// Constructor for [InherentData]
pub const fn new(seed: u64, timestamp: u64) -> Self {
Self { seed, timestamp }
}
#[cfg(all(feature = "rng", not(target_family = "wasm")))]
pub fn next(&self) -> Self {
Self {
seed: seeded_random::Random::from_seed(seeded_random::Seed::unsafe_new(self.seed)).gen(),
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
.try_into()
.unwrap(),
}
}
/// Create a new [InherentData] with the current time and a random seed.
///
/// This is not "unsafe" in the Rust sense. It is unsafe because it should
/// only be used if you are sure you know what you're doing. If you don't know why this is unsafe, don't use it.
#[cfg(all(feature = "rng", feature = "std", not(target_family = "wasm")))]
pub fn unsafe_default() -> Self {
Self {
seed: seeded_random::Random::new().gen(),
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
.try_into()
.unwrap(),
}
}
/// Clone the [InherentData] struct.
///
/// This is not "unsafe" in the Rust sense. It is unsafe because it should
/// only be used if you are sure you know what you're doing. If you don't know why this is unsafe, don't use it.
pub const fn unsafe_clone(&self) -> Self {
Self {
seed: self.seed,
timestamp: self.timestamp,
}
}
}
#[cfg(test)]
mod tests {}