I have a substrate pallet with a number of extrinsics, some simple, some more complex. Depending on the chain state and the input variables, the amount of computation and the number of DB reads for the same extrinsic call can be quite different.
I've done my weight annotations based on a worst-case scenario. I'm happy with those for estimating the transaction fees in advance.
Now I'm trying to return the actual weight via DispatchResultWithPostInfo so that the transaction-payment pallet can calculate the real fee and return the difference to the user. I, perhaps naively, thought that the actual weight of an extrinsic call would be calculated for me somewhere and I could just pass this back in DispatchResultWithPostInfo, but this doesn't seem to be the case.
So I've created a weight variable in each extrinsic which I update each time there is a DB read/write operation so that I have something to create my PostInfo that reflects what actually happened. This raises a few questions for me:
1/ When do DB reads get cached, and should therefore not add extra weight?
2/ How can I get the actual computation time? Or can I not and have to use estimates from benchmarking?
3/ Is this really the best way to calculate the actual weight? I feel I have to be missing something.
Thanks in advance for any help.