Can I use the ic_cdk::api::canister_balance() when enter/exist a function to measure how much cycles consumed in this method?

In the basic_bitcoin’s lib.rs, multiple_sha256() is added

each function is executed once, the logs looks like

it is very strange, in get_balance(), cycle_used is not zero, but in multiple_sha256(), cycle_used is zero

Has it been considered to measure instructions instead, using ic_cdk::api::performance_counter?

They are not exactly the same thing since cycles can be charged for different reasons, but I imagine within the same method you mostly just care about the instructions.

In my case, I hope caller pay exact cycles consumed, just like ethereum does.
Though I can use ic_cdk::api::performance_counter calculate how many instructions used. I can not convert the instructions consumed to cycles consumed.

canister_balance won’t help you. You need to use the gas cost table together with performance_counter and a few heuristics to get the cycles cost. The problem with canister_balance is that it doesn’t count live cycles deductions, and if you await as part of your function then there’s a bunch of causes outside of the running function why the canister_balance would fluctuate

2 Likes

@Severin Thanks for you clear instructions. I will try this method。