2

In my pallet I use the pallet_balances, through loose coupling via trait Currency. I use T::Currency::transfer in my on_initialize hook. In this hook I'm supposed to keep track of the weight I'm consuming, and return it at the end of the hook call.

I know that the weight for a call to Balances::transfer is set to:

// Storage: System Account (r:1 w:1)
    fn transfer() -> Weight {
        (41_860_000 as Weight)
            .saturating_add(T::DbWeight::get().reads(1 as Weight))
            .saturating_add(T::DbWeight::get().writes(1 as Weight))
    }

because I looked at it in the pallet weight.rs file.

What I want to do is access this value in my hook, so I can increase my weight counter and return an accurate measurement of what I used.

How can I achieve this? Where is the data available?

1 Answer 1

1

You should simply write a benchmark for your on_initialize hook, which when executed, will also run the appropriate balance transfers, and automatically add their weight into your final weight output.

This is the same for any pallet hook you use, since benchmarks simply execute the written code, any of those hooks should execute and be measured as a part of the benchmarking process.

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.