fix(pallet-qpow): correct weight calculation for on_finalize#449
fix(pallet-qpow): correct weight calculation for on_finalize#449illuzen merged 1 commit intoQuantus-Network:mainfrom
on_finalize#449Conversation
|
@czareko I believe you have the most context on this. Would it be possible to take a look? |
|
Thank you @AdamMomen |
|
AI review: 1. Is this PR needed?Yes, it addresses a real issue. Your own codebase has a That said, the benefit is modest -- it frees up wasted block weight, not a critical fix. Nice cleanup, not urgent. 2. Correctness issues (the PR has bugs)I traced every storage access in the Actual Reads (7):
Actual Writes (4, not 5):
Problems with the PR:
3. Security assessmentLow risk, but worth noting:
4. RecommendationThe PR is directionally correct but has inaccuracies. I'd suggest:
|
717718a to
25c3f22
Compare
|
Fixed:
|
Ran benchmark with quantus-node to get accurate weights for on_finalize: - Storage reads: 4 (CurrentDifficulty, Timestamp::Now, LastBlockTime, BlockTimeEma) - Storage writes: 4 (CurrentDifficulty, LastBlockTime, BlockTimeEma, LastBlockDuration) - Execution time: 109 µs (updated from 217 µs) - Proof size: 1549 bytes (updated from 23445 bytes) Also renamed on_finalize_max_history -> on_finalize (the history system was replaced with EMA-based difficulty adjustment).
25c3f22 to
a21435b
Compare
illuzen
left a comment
There was a problem hiding this comment.
Thanks for taking care of that todo! Nice PR @AdamMomen !
Summary
pallet-qpow'son_finalizehook.on_finalize_max_historyfrom a deprecated block-time history implementation that was replaced with EMA (Exponential Moving Average), and the storage operation counts were significantly overstated.Problem
on_initializehook returned a weight claiming: 16 reads / 9 writeson_finalizeimplementation performs: 7 reads / 5 writesThis ~2x overestimation wastes block weight capacity.
Root Cause
The weight was inherited from commit 59c0781 which used a
BlockTimeHistoryring buffer for difficulty adjustment. When EMA replaced the history system:BlockTimeHistory,HistoryIndex,HistorySizewere removedBlockTimeEmawas addedChanges
weights.rson_finalize_max_history→on_finalize, corrected reads 16→7, writes 9→5, added missing storage docsbenchmarking.rslib.rsStorage Operations (corrected)
Reads (7):
Timestamp::NowLastBlockTimeCurrentDifficultySystem::block_numberTotalWorkBlockTimeEma(twice, inupdate_block_time_emaandadjust_difficulty)Writes (5):
TotalWorkLastBlockDuration(conditional)BlockTimeEmaLastBlockTimeCurrentDifficultyTesting
cargo test -p pallet-qpow
23 tests passed ✓
Impact
Note: Proper weight verification would require re-running benchmarks with
frame-omni-bencher. This PR corrects the documented storage operations based on code analysis. The execution time estimate (217 µs) is retained until fresh benchmarks can be run.