2

I'm trying to use pallet_timestamp on function run_to_block when testing PalletA. Here is my code

pub fn run_to_block(n: u64) {
    while System::block_number() < n {
        if System::block_number() > 1 {
            PalletA::on_finalize(System::block_number());
            System::on_finalize(System::block_number());
        }
        System::set_block_number(System::block_number() + 1);
        System::on_initialize(System::block_number());
        PalletA::on_initialize(System::block_number());
        Timestamp::on_initialize(System::block_number());
       //Timestamp::on_finalize(System::block_number());  // Got error 'Timestamp must be updated once in the block'
    }
}

But on hooks of PalletA the '_now' value is always 0

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_finalize(_block_number: BlockNumberFor<T>) {
            let _now = <timestamp::Pallet<T>>::get();

            println!("now: {:?}", _now);
        }
    }
0

1 Answer 1

3

Found solution dealing with pallet_timestamp on unittest:

pub fn run_to_block(n: u64) {
    while System::block_number() < n {
        if System::block_number() > 1 {
            PalletA::on_finalize(System::block_number());
            System::on_finalize(System::block_number());
        }
        System::set_block_number(System::block_number() + 1);
        System::on_initialize(System::block_number());
        PalletA::on_initialize(System::block_number());
        Timestamp::set_timestamp((System::block_number() as u64 * MILLISECS_PER_BLOCK) + INIT_TIMESTAMP); // New line
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.