Updating the block_timestamp after each function call

Viewed 57

I want to update the block_timestamp after each function call. This is how I am updating the context after each function call in tests.

#[test]
    fn set_then_get_greeting() {
        let context = get_context(vec![], false);
        testing_env!(context);
        let mut contract = Welcome::new();
        contract.set_record("7dd".to_owned());
        let context = get_context(vec![], false);
        testing_env!(context);
        contract.update_record("7dd".to_owned());
        
    }

But on state update it gives follow error:

panicked at 'called `Result::unwrap()` on an `Err` value: InconsistentStateError(IntegerOverflow)'

How can I update the block_timestamp with every function call?

Code: https://gateway.ipfs.io/ipfs/QmTNHuRryBoDmTp7wqmNsCcJW8Gu7G6dz3cE3F4pynv6V9

2 Answers

Not sure what get_context is doing, but you can modify context before calling testing_env! and then pass a cloned copy.

For example:

#[test]
    fn set_then_get_greeting() {
        let mut context = get_context(vec![], false);
        testing_env!(context.clone());

        let mut contract = Welcome::new();
        contract.set_record("7dd".to_owned());

        context.block_timestamp += 1000;
        testing_env!(context.clone());

        contract.update_record("7dd".to_owned());
    }

The error you see might be unrelated to the context because it complains about inconsistent state, which likely means something is off with the persistent collections.

Related