polkadot-sdk icon indicating copy to clipboard operation
polkadot-sdk copied to clipboard

FRAME: why not refund the weight used for storage access accurately with a new `DataBaseAccessExt`

Open gui1117 opened this issue 1 year ago • 3 comments

Context:

Transactions are paying for their weight. Weight has 2 part: ref_time and proof_size.

ref_time is calculated using benchmarks. It benchmarks for compute time and also database access. Database access are then converted into some ref_time.

We usually provide benchmark for the worst case and for the best cases. And the call give some refund in case transaction execute in the best cases.

But benchmarks are hard. And benchmarking accurate best cases is even harder considering Database cache.

PVM will give us accurate compute time. But we can already get the accurate database usage after the transaction.

Proposition

I propose we have a new runtime extension similar to ProofSizeExt: DataBaseAccessExt

And DispatchInfo is modified to be more precise: ref_time is split into ref_time_compute and ref_time_db.

So that:

  • we can give way more accurate refund due to less database access and more cache hits than expected.
  • the refund is more accurate even when no specific best-cases benchmark are provided.

Additionally

  • Some extrinsic could want to reserve some weight to be used in on_finalize hook, I guess we can provide some add_reserved_weight in frame-system. also such reserved weight would be good for StorageWeightReclaim extension.

gui1117 avatar Aug 02 '24 23:08 gui1117