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
//! timestamp API for the sandbox.

use crate::{Runtime, Sandbox};

/// Generic Time type.
type MomentOf<R> = <R as pallet_timestamp::Config>::Moment;

impl<R> Sandbox<R>
where
    R: Runtime,
    R::Config: pallet_timestamp::Config,
{
    /// Return the timestamp of the current block.
    pub fn get_timestamp(&mut self) -> MomentOf<R::Config> {
        self.execute_with(pallet_timestamp::Pallet::<R::Config>::get)
    }

    /// Set the timestamp of the current block.
    ///
    /// # Arguments
    ///
    /// * `timestamp` - The new timestamp to be set.
    pub fn set_timestamp(&mut self, timestamp: MomentOf<R::Config>) {
        self.execute_with(|| pallet_timestamp::Pallet::<R::Config>::set_timestamp(timestamp))
    }
}

#[cfg(test)]
mod tests {
    use crate::{runtime::MinimalRuntime, Sandbox};

    #[test]
    fn getting_and_setting_timestamp_works() {
        let mut sandbox = Sandbox::<MinimalRuntime>::new().expect("Failed to create sandbox");
        for timestamp in 0..10 {
            assert_ne!(sandbox.get_timestamp(), timestamp);
            sandbox.set_timestamp(timestamp);
            assert_eq!(sandbox.get_timestamp(), timestamp);

            sandbox.build_block().expect("Failed to build block");
        }
    }
}