2

I'm writing a test case for the on_initalize hook!

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_initialize(_now: T::BlockNumber) -> Weight {
         
             let time: u64 = T::TimeProvider::now().as_secs() - 180 ;
            
            <RootOracleEvent<T>>::mutate(|event_list| {
                if let Some(index) = event_list.iter().position(|member| member.time_stamp <= time) {
                    event_list.remove(index);
                    
                }
                
            });     
        
                    
            T::DbWeight::get().writes(1)
        }

    
    }

And, when I write a test case, its throws an error: ---- tests::valid_weights stdout ---- thread 'tests::valid_weights' panicked at 'attempt to subtract with overflow', pallets/pallet_oracle_feed/src/lib.rs:86:29 note: run with the RUST_BACKTRACE=1 environment variable to display a backtrace

#[test]
fn valid_weights() {
    use frame_support::pallet_prelude::*;
    use frame_support::weights::RuntimeDbWeight;

    new_test_ext().execute_with(|| {
      let db_weights: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get();
     

        assert_eq!(
            ClubPalletTesting::on_initialize(1),
            db_weights.writes(2));
    })
}

3
  • Do you not use the current block number now? I have an assumption what could be the cause, but it would help if you could share the code of the on_initialize or the whole pallet, if possible. Commented Sep 28, 2022 at 17:22
  • No, Im not using the value of 'now' , inside my logic.. Commented Sep 28, 2022 at 17:35
  • 1
    I have updated my code...@OliverTale-Yazdi Commented Sep 28, 2022 at 17:36

1 Answer 1

1

Three things:

  1. Could be that you are not initializing the block number, it is therefore 0. This can lead to various errors when subtracting from it. You can easily fix this by modifying your new_test_ext function to do so. 1 may not be enough in your case.
pub fn new_test_ext() -> sp_io::TestExternalities {
    let mut ext: sp_io::TestExternalities = system::GenesisConfig::default().build_storage::<Test>().unwrap().into();
    ext.execute_with(|| System::set_block_number(1));
    ext
}
  1. Ensure that the TimeProvider is initialized before your pallet. A good example is in pallet-gilt. I don't know what your time provider does, but it seems to return something less than 180 secs, hence the error.
  2. Always use saturating or checked arithmetic operations in the runtime.
    You can change the code to let time: u64 = T::TimeProvider::now().as_secs().saturating_sub(180);.
2
  • 1
    2nd was the issue.. Commented Sep 28, 2022 at 18:06
  • Okay great! Then please mark this question as solved. Commented Sep 28, 2022 at 19:32

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.