Skip to content

nft-credits: Weigh build_credit_tree per award chunk and regenerate its weights - #155

Open
shawntabrizi wants to merge 11 commits into
mainfrom
fix/743-build-credit-tree-proof-size
Open

shawntabrizi wants to merge 11 commits into
mainfrom
fix/743-build-credit-tree-proof-size

Conversation

@shawntabrizi

@shawntabrizi shawntabrizi commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Closes https://github.com/paritytech/srlabs_findings/issues/743

When #50 chunked NftClaimCreditAwards into 32-award chunks and raised a tree to AWARDS_PER_TREE = 2048, the build_credit_tree weights were renamed by hand but not regenerated. They still described one NftClaimCreditAwards read over [1, 1200], with a fixed proof size of 81_479 bytes. A full tree reads 64 chunks, which is 133_184 bytes of proof. Regenerating over awards alone is not enough: one chunk read per 32 awards is a slope of 1/32 read per award, which the generated weight rounds to zero, so the reads stay fixed.

The benchmark now sweeps the number of full award chunks, c: Linear<1, CHUNKS_PER_TREE>, and build_credit_tree is charged for the chunks block_awards reads. Each chunk then adds one read and its proof size. The integrity_test also asserts that the worst-case weight covers the proof size of every chunk in a full tree, so stale weights fail the runtime tests.

Changes

  • build_credit_tree benchmark: component n: Linear<1, AWARDS_PER_TREE> → c: Linear<1, CHUNKS_PER_TREE>, with c * AWARDS_PER_CHUNK awards.
  • Pallet::build_credit_tree: weighs buffered.div_ceil(AWARDS_PER_CHUNK) chunks instead of the leaf count. A tree with a partly full last chunk pays for a full chunk.
  • integrity_test_credits: calls the weight with CHUNKS_PER_TREE and asserts proof_size >= CHUNKS_PER_TREE * MaxEncodedLen(chunk). Zero weights (the mock) are skipped.
  • Regenerated indiv_pallet_nft_credits weights (pallet weights.rs and next-people-paseo runtime), by the bench bot.

The `build_credit_tree` weights were renamed by hand when awards were
chunked (#50) but never regenerated. They still charge one
`NftClaimCreditAwards` read of the old single-vector layout and a fixed
proof size of 81_479 bytes over `[1, 1200]`, while a full tree of
`AWARDS_PER_TREE = 2048` awards reads 64 chunks, 133_184 bytes.

The `integrity_test` now asserts that the worst-case weight declares at
least the proof of every chunk a tree reads, so stale weights fail the
runtime tests rather than under-charge `on_initialize`.

Fixes paritytech/srlabs_findings#743

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@shawntabrizi
shawntabrizi requested a review from a team September 23, 2026 13:08
@shawntabrizi shawntabrizi added D2-no audit No audit is needed C0-non-breaking This PR is not a breaking change for any runtime consuming it. labels Sep 23, 2026
@shawntabrizi

Copy link
Copy Markdown
Member Author

/cmd bench --pallet indiv_pallet_nft_credits

@github-actions

Copy link
Copy Markdown
Contributor

Command "bench --pallet indiv_pallet_nft_credits" has started 🚀 See logs here

@command-bot-individuality-commun

Copy link
Copy Markdown

Command "bench --pallet indiv_pallet_nft_credits" has finished ✅ See logs here

Runtime weight deviation:
Base:    upstream/main @ a4e2cb4e344d  (merge-base)
Current: HEAD

[!] 2 extrinsics above threshold (|Δ| ≥ 10% in ref_time or proof_size)

── runtimes/next-people-paseo · weights/indiv_pallet_nft_credits.rs ────────────

   bench machine (base -> current)
     CPU       Intel(R) Xeon(R) CPU @ 2.60GHz   (same)
     hostname  parity-weights-1ecf9779a5ae1fca -> parity-weights-a57c01dfa0265114
     date      2026-09-04 -> 2026-09-23
     sampling  STEPS: 50, REPEAT: 20   (same)

🔺 indiv_pallet_nft_credits::build_credit_tree                    ref_time +447.9% (5.5× slower) · proof_size +623.4%

   METRIC       BASE       CURRENT       DELTA
   ref_time     2.06 ms   -> 11.27 ms     +9.21 ms    (+447.9%)  ◀
   proof_size   81,479    -> 589,400      +507,921    (+623.4%)  ◀
   db reads     5         -> 69           +64         (+1280.0%)
   db writes    7         -> 70           +63         (+900.0%)

   time breakdown
     computation   +1.31 ms
     db access     +7.90 ms

   storage (base -> current)
   • NftCredits::NftClaimCreditAwards  r:1 w:1  ->  r:128 w:64    reads ×128, writes ×64

🔺 indiv_pallet_nft_credits::build_credit_tree_empty              proof_size +133.7%

   METRIC       BASE       CURRENT       DELTA
   ref_time     0.03 ms   -> 0.03 ms      +0.00 ms    (+3.6%)
   proof_size   1,493     -> 3,489        +1,996      (+133.7%)  ◀
   db reads     1         -> 1            unchanged
   db writes    0         -> 0            unchanged

   time breakdown
     computation   +0.00 ms
     db access     +0.00 ms

   storage (base -> current)
   (no storage-access change)

1 file scanned · 2 extrinsics above 10% threshold
measured at documented component maxima - pass --max NAME=VALUE for charge-site maxima

The generated weight declared a fixed `reads(69)`: a chunk read per 32
awards is a slope of 1/32 read per award, which rounds to zero. The
benchmark now sweeps the number of full chunks, and `build_credit_tree`
is charged for the chunks `block_awards` reads, so each chunk adds one
read and its proof size.

Also shortens the integrity check's comment and assert.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@shawntabrizi

Copy link
Copy Markdown
Member Author

/cmd bench --pallet indiv_pallet_nft_credits

@shawntabrizi shawntabrizi changed the title nft-credits: Regenerate build_credit_tree weights and assert they cover every award chunk nft-credits: Weigh build_credit_tree per award chunk and regenerate its weights Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Command "bench --pallet indiv_pallet_nft_credits" has started 🚀 See logs here

@command-bot-individuality-commun

Copy link
Copy Markdown

Command "bench --pallet indiv_pallet_nft_credits" has finished ✅ See logs here

Runtime weight deviation:
Base:    upstream/main @ a4e2cb4e344d  (merge-base)
Current: HEAD

[!] 2 extrinsics above threshold (|Δ| ≥ 10% in ref_time or proof_size)

── runtimes/next-people-paseo · weights/indiv_pallet_nft_credits.rs ────────────

   bench machine (base -> current)
     CPU       Intel(R) Xeon(R) CPU @ 2.60GHz   (same)
     hostname  parity-weights-1ecf9779a5ae1fca -> parity-weights-4629cb577dc99a90
     date      2026-09-04 -> 2026-09-23
     sampling  STEPS: 50, REPEAT: 20   (same)

🔺 indiv_pallet_nft_credits::build_credit_tree                    ref_time +524.0% (6.2× slower) · proof_size +620.7%

   METRIC       BASE       CURRENT       DELTA
   ref_time     2.06 ms   -> 12.83 ms     +10.77 ms   (+524.0%)  ◀
   proof_size   81,479    -> 587,230      +505,751    (+620.7%)  ◀
   db reads     5         -> 132          +127        (+2540.0%)
   db writes    7         -> 70           +63         (+900.0%)

   time breakdown
     computation   +1.30 ms
     db access     +9.47 ms

   storage (base -> current)
   • NftCredits::NftClaimCreditAwards  r:1 w:1  ->  r:128 w:64    reads ×128, writes ×64

🔺 indiv_pallet_nft_credits::build_credit_tree_empty              proof_size +133.7%

   METRIC       BASE       CURRENT       DELTA
   ref_time     0.03 ms   -> 0.03 ms      +0.00 ms    (+3.8%)
   proof_size   1,493     -> 3,489        +1,996      (+133.7%)  ◀
   db reads     1         -> 1            unchanged
   db writes    0         -> 0            unchanged

   time breakdown
     computation   +0.00 ms
     db access     +0.00 ms

   storage (base -> current)
   (no storage-access change)

1 file scanned · 2 extrinsics above 10% threshold
measured at documented component maxima - pass --max NAME=VALUE for charge-site maxima

Comment thread pallets/nft-credits/src/lib.rs Resolved
@tindzk
tindzk requested a review from Zebedeusz October 1, 2026 08:18
Comment thread pallets/nft-credits/src/lib.rs Resolved
shawntabrizi and others added 3 commits November 14, 2026 12:08
Co-authored-by: theo <37565691+agustinustheo@users.noreply.github.com>
Signed-off-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Tim Nieradzik <tim.n@parity.io>
Signed-off-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Takes main's regenerated nft-credits weights except build_credit_tree,
which keeps the per-chunk weight until the bench bot reruns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shawntabrizi

Copy link
Copy Markdown
Member Author

/cmd bench --pallet indiv_pallet_nft_credits

@github-actions

Copy link
Copy Markdown
Contributor

Command "bench --pallet indiv_pallet_nft_credits" has started 🚀 See logs here

@command-bot-individuality-commun

Copy link
Copy Markdown

Command "bench --pallet indiv_pallet_nft_credits" has finished ✅ See logs here

Runtime weight deviation:
Base:    upstream/main @ 3c4c1de7ec86  (merge-base)
Current: HEAD

[!] 1 extrinsic above threshold (|Δ| ≥ 10% in ref_time or proof_size)

── runtimes/next-people-paseo · weights/indiv_pallet_nft_credits.rs ────────────

   bench machine (base -> current)
     CPU       Intel(R) Xeon(R) CPU @ 2.60GHz   (same)
     hostname  parity-weights-78b82649b38c8d4b -> parity-weights-0775d6dea49c4964
     date      2026-09-15 -> 2026-11-14
     sampling  STEPS: 50, REPEAT: 20   (same)

🔺 indiv_pallet_nft_credits::build_credit_tree                    ref_time +50.9% (1.5× slower)

   METRIC       BASE       CURRENT       DELTA
   ref_time     3.19 ms   -> 4.82 ms      +1.63 ms    (+50.9%)  ◀
   proof_size   297,423   -> 297,679      +256        (+0.1%)
   db reads     4         -> 67           +63         (+1575.0%)
   db writes    6         -> 6            unchanged

   time breakdown
     computation   +0.05 ms
     db access     +1.57 ms

   storage (base -> current)
   (no storage-access change)

1 file scanned · 1 extrinsic above 10% threshold
measured at documented component maxima - pass --max NAME=VALUE for charge-site maxima

…tree-proof-size

# Conflicts:
#	pallets/nft-credits/src/weights.rs
#	runtimes/next-people-paseo/src/weights/indiv_pallet_nft_credits.rs
@shawntabrizi
shawntabrizi enabled auto-merge (squash) November 25, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C0-non-breaking This PR is not a breaking change for any runtime consuming it. D2-no audit No audit is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants