3

As the title suggests, I've been trying to call assert_last_event on the tests.rs class without success. The problem first happened when developing the kitties tutorial and after several toolchain version changes later, even more problems started to appear. I've noticed XLC's Kitties repo doesn't emit events in the frontend, although the tests.rs class runs successfully despite calling assert_last_event. I've also tried Substrate Node Template's repo, in all the branches that mention 'kitties', but all of them fire the following error:

error: failed to get `frame-benchmarking` as a dependency of package `node-template v4.0.0-dev (/home/frkmnt/Documents/Rust/kitties/substrate-node-template/node)`

Caused by:
failed to load source for dependency `frame-benchmarking`

Caused by:
Unable to update https://github.com/paritytech/substrate.git?tag=devhub/latest#e6fbbd5c

Caused by:
object not found - no match for id (e6fbbd5cdf72a5ed7fd65138072ed1f8a320a33d); class=Odb (9); code=NotFound (-3)

Attempting to update the toolchain as mentioned yields no result and updating the required versions only creates more dependency errors. I also tried it in a blank project, using the template pallet using the latest stable toolchain 1.59.0, along with several others, and modified the existing test to assert the last event as shown:

#[test]
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
    assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));
    assert_eq!(TemplateModule::something(), Some(42));
    System::assert_last_event(self::Event::TemplateModule(crate::Event::<Test>::SomethingStored(1, 42)))
});
}

My original project fired this same error, even though this is the only version that successfully ran:

---- tests::it_works_for_default_value stdout ----
thread 'tests::it_works_for_default_value' panicked at 'events expected', 
/mnt/dados/andre/.cargo/git/checkouts/substrate- 
7e08433d4c370a21/32a4fe0/frame/system/src/lib.rs:1442:42

I have no warnings and all my code compiles successfully. Before this, it also emitted events and worked perfectly in the frontend when ran with cargo run -- --dev.

1 Answer 1

4

I believe the problem here is small and easy to miss.

Substrate chains do not emit events when the block number is 0. This is to avoid hundreds or even thousands of events being generated in the genesis block of a Substrate chain.

So if you want to do any Runtime tests where you check events, make sure you first do:

frame_system::Pallet::<T>::set_block_number(1);

You can also automate this for all of your tests via with:

pub fn new_test_ext() -> sp_io::TestExternalities {
    let t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
    let mut ext = sp_io::TestExternalities::new(t);
    ext.execute_with(|| System::set_block_number(1));
    ext
}

Here you see we always will set the block number to 1 before giving you the text externalities environment.

2
  • Is there a way of enabling Events for genesis? We need it for indexing.
    – vovacha
    Nov 8, 2022 at 4:23
  • 1
    We explicitly disabled events on genesis because it created a very heavy and slow block. So no, we have no such functionality today.
    – Shawn Tabrizi
    Nov 8, 2022 at 6:17

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.