0

I am trying to fast forward time to do some tests for a custom runtime module. I have looked at the answer from this thread and followed the answer to use Timestamp, however, I am unable to access the set_timestamp method.

setup:

#[cfg(test)]
mod tests {
    use super::*;
    use support::dispatch::Vec;
    use runtime_primitives::traits::{Hash};
    use runtime_io::with_externalities;
    use primitives::{H256, Blake2Hasher};
    use timestamp;
    use support::{impl_outer_origin, assert_ok, assert_noop};
    use runtime_primitives::{
        BuildStorage,
        traits::{BlakeTwo256, IdentityLookup},
        testing::{Digest, DigestItem, Header}
    };

    impl_outer_origin! {
        pub enum Origin for Test {}
    }

    #[derive(Clone, Eq, PartialEq)]
    pub struct Test;
    impl system::Trait for Test {
        type Origin = Origin;
        type Index = u64;
        type BlockNumber = u64;
        type Hash = H256;
        type Hashing = BlakeTwo256;
        type Digest = Digest;
        type AccountId = u64;
        type Lookup = IdentityLookup<Self::AccountId>;
        type Header = Header;
        type Event = ();
        type Log = DigestItem;
    }
    impl super::Trait for Test {
        type Event = ();
    }
    impl timestamp::Trait for Test {
        type Moment = u64;
        type OnTimestampSet = ();
    }

    type Pizza = Module<Test>;

And the error is below:

error[E0599]: no function or associated item named `set_timestamp` found for type 
`srml_timestamp::Module<tests::Test>` in the current scope


    |
254 |  let now = <timestamp::Module<tests::Test>>::set_timestamp(9);
    |                                              ^^^^^^^^^^^^^ function or associated item 
                                            not found in `srml_timestamp::Module<tests::Test>`

2 Answers2

0

In Substrate v1.0, the set_timestamp function has a #[cfg(feature = "std")] attribute on it:

https://github.com/paritytech/substrate/blob/v1.0/srml/timestamp/src/lib.rs#L276

This means it will only be visible if you are compiling with std. When you write tests, this should work, but I assume that this issue is appearing because you are trying to call it from within the runtime environment, which much be no_std.

If for some reason you do need to modify the timestamp from within your runtime, you should be able to do so directly:

https://github.com/paritytech/substrate/blob/v1.0/srml/timestamp/src/lib.rs#L249

<timestamp::Module<T>>::Now::put(new_time)

(I haven't tested this, but something like it should work).

Let me know if this helps.

Shawn Tabrizi
  • 12,206
  • 1
  • 38
  • 69
  • Hi, thank you for your answer, you were correct, without compiling with std, `set_timestamp()` was not available. I added `timestamp/std` to the cargo.toml features section with default['std'] to compile with `std`. I did not need to modify timestamp within the runtime but only for tests. I was unable to access `>::Now`, it is also not found. I resolved using `set_timestamp()`. – whalelephant Feb 03 '20 at 02:37
-1

In Substrate v1.0 you can declare

type Moment = timestamp::Module<Test>;

Then use it to set a specific timestamp.

Moment::set_timestamp(9);

If you want to get the timestamp value, you can do:

let now_timestamp = Moment::now();
Community
  • 1
  • 1
Ricardo Rius
  • 121
  • 2