0

I want to test my pallet that get digest info to collect the author of the current block. So, is it possible to set, for example, Alice as a block author in the mock.rs so I can test it?

I set up the mock.rs in this way configuring pallet_session, pallet_node_authorization and pallet_aura.

but when I try to call a test with cargo test:

#[test]
fn hash_work() {
    new_test_ext().execute_with(|| {

        run_to_block(10);
        assert_ok!(ComputationalWork::hash_work(Origin::signed(get_account_id_from_seed::<sr25519::Public>("Alice"))));
    })
}

I get this error:

thread 'tests::check_x_block_index_change_by_1' panicked at 'called `Option::unwrap()` on a `None` value', pallets/computational-work/src/lib.rs:140:62

the lines that cause the error:

// Get the block author.
let block_digest = <frame_system::Pallet<T>>::digest();
let digests = block_digest.logs.iter().filter_map(|d| d.as_pre_runtime());
let author = T::FindAuthor::find_author(digests).unwrap();

I think the problem is author that is none in this context.

1 Answer 1

1

Add these to your mock file.

impl pallet_computational_work::Config for Test {
    type Event = Event;
    type FindAuthor = AuthorGiven;
}

pub struct AuthorGiven;
impl frame_support::traits::FindAuthor<YourAccountIdType> for AuthorGiven {
    fn find_author<'a, I>(digests: I) -> Option<YourAccountIdType>
    where
        I: 'a + IntoIterator<Item = (sp_runtime::ConsensusEngineId, &'a [u8])>,
    {
        Some(AliceOrAnyYouWant)
    }
}
1
  • Ok it's working, thank you!
    – viac92
    Oct 17, 2022 at 16:40

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.