2

Let's say I have a pallet_x. In this pallet i have an extrinsic where I get the active_era using the below code :

            Staking::<T>::active_era()
                          .map(|e| e.index)
                          .ok_or(Error::<T>::FailedToGetActiveEra)?

So when I write benchmark for this extrinsic it fails with this error FailedToGetActiveEra

My question now is how to insert value to ActiveEra storage in pallet_staking from pallet_x ?

1
  • 1
    does pallet_staking::ActiveEra::<T>::mutate/insert( .. ) help? Also make sure to include T: pallet_staking::Config Aug 8 at 13:01

1 Answer 1

2

You can do it the same way as in tests, by inserting storage items like @Purple_Turtle posted in the comment;

pallet_staking::ActiveEra::<T>::put(…)

One example would be from the nomination-pools benchmark where it fudges the staking stoarge.

Normally it is better to have pallets communicate with each other via traits, since otherwise storage invariants can get invalidated when pallet_x just modifies the storage of pallet_y without its knowledge. But if you only do it in testing/benchmarks, then its fine.

Since that type has private fields, you can hack around it by decoding it from its struct elements. It's not ideal, but better than having to go through an upstream change:

#[test]
fn hack() {
    use codec::{Encode, Decode};
    
    new_test_ext().execute_with(|| {
        let raw = (4 as sp_staking::EraIndex, Some(10u64)).encode();
        let info = pallet_staking::ActiveEraInfo::decode(&mut &raw[..]).unwrap();
        // Now you can put it into stoarge.
        pallet_staking::ActiveEra::<Test>::put(&info);
    });
}
2
  • the problem is one of the fields in ActiveEraInfo struct is private which is not allowing me to storage put. pub struct ActiveEraInfo { pub index: EraIndex, start: Option<u64>, } Aug 8 at 16:57
  • 1
    Ah that is annoying indeed. I extended the answer. Aug 8 at 19:14

Your Answer

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

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