tellor-io / tellor-pallet

GNU General Public License v3.0
7 stars 3 forks source link

refactor: optimise extrinsics #111

Closed evilrobot-01 closed 1 year ago

evilrobot-01 commented 1 year ago

Optimises extrinsics and storage to read storage in a more efficient manner to reduce extrinsic weights.

A summary of changes:

Additional notes/observations:

evilrobot-01 commented 1 year ago
* Does `report.is_disputed = true;` get reversed and the value placed back in the series if execute vote determines the dispute fails or is invalid?

Forgive me if I have missed it, but I havent seen anywhere in the contracts where a disputed value is reversed. I just double checked the governance contract again and can only see it being removed, the disputed value being copied to the dispute and no restoration. Could you possibly provide a link if I have indeed missed it please?

* Could you provide some more explanation about this: `Includes the actual number of iterations done by the binary search so caller can be refunded based on weight difference.`? I'm not clearly understanding the workflow of the caller and the situations where they'd be doing this.

The binary search is used when looking up the timestamp index before some supplied timestamp, which may not match the actual timestamp of a submitted value. update_stake_amount uses this twice, once for determining the price for the staking token query id and again for the other query id used for the dispute fee.

Each iteration of the binary search requires additional reads, which results in additional weight/cost. Based on the u32::MAX value as the maximum number of timestamps for a query id, the maximum iterations for a binary search will be 32, but depending on the number of timestamps which exist under a query id the number of iterations can vary. We benchmark to generate weight functions which can be called prior to dispatchable function execution (example) to ensure the caller has sufficient balance to cover the worst case fees and then called again at the end of the dispatchable function (example) to calculate actual weights used to have the caller refunded accordingly.

No action is required by the caller, its just gas metering ahead of time to generate a lightweight weight function (example) rather than the overhead of measuring in realtime, which is then callable with observed parameter values depending on use case. A simpler example is that of claiming timestamps, where claiming 100 will require more compute as opposed to 10 and we therefore need to charge accordingly.

oraclown commented 1 year ago

For the first part, that's my mistake. Thanks for clarifying. Appreciate the further explanation of the number of binary search iterations and where it's used. Sounds good.