Repository navigation
fix(arion): bound the settlement hook's weight by what it iterates (#79) - #82
Open
faizsarwar wants to merge 5 commits into
Open
faizsarwar wants to merge 5 commits into
faizsarwar wants to merge 5 commits into
Conversation
The miner payment settlement runs in on_initialize (mandatory weight) and moves funds, but its declared weight claimed a bound — miner_payment_settlement_hook(MaxChildrenTotal, MaxFamilies) — that the actual iteration could exceed: - it walked ChildRegistrations::iter(), which keeps Unbonding/stale rows (only active children are capped), and - it walked the whole FamilyArrears map, whose rows outlive active families. So on a chain with enough unbonding children or stale arrears the hook could produce a heavier-than-declared block. Children: iterate FamilyChildren (the bounded active index) instead of ChildRegistrations. It holds exactly the active children and at most MaxFamilies keys, so the walk is provably <= MaxChildrenTotal. The per-child registration read + Active check stay (same cost as the old iter), keeping the "only Active is paid" guarantee against a stale index. Arrears: when a family goes fully inactive its FamilyArrears row is moved to a new DepartedFamilyArrears ledger (both teardown paths), so the live arrears walk only ever sees active families (<= MaxFamilies). Nothing owed is dropped: the settlement pays the departed ledger down from bank surplus, in free balance, a bounded MaxDepartedPayoutsPerBlock slice per tick via DepartedArrearsCursor — so that walk is bounded too however large the ledger grows. Active families keep priority; departed debt drains from whatever the bank has left after they are settled. The hook's declared weight now includes the departed-payout slice. Storage migration v3 -> v4 sweeps pre-existing stale arrears (inactive families) into the departed ledger so the bound holds regardless of rows already on chain. spec_version 92022 -> 92023. Tests (runtime/mainnet/tests/miner_payments.rs): departed debt is preserved on departure and paid from later surplus in free balance; the per-tick payout cap holds and the cursor drains a backlog; the migration moves stale rows while leaving active families' arrears live. Settlement test helpers now seed FamilyChildren for active children, matching the live register_child invariant the bounded walk relies on. Benchmark re-measurement on reference hardware remains a follow-up (the pallet has no frame-benchmarking harness on this branch).
…ound (#79) Close the two acceptance-criteria gaps from the #79 fix: - deregister_family (the wholesale teardown path) also moves FamilyArrears into the departed-debt ledger, so no fund-moving teardown drops what a family is owed. - Settlement does not walk Unbonding ChildRegistrations rows beyond the active set: 50 stale rows with fat accruals are left in the map and none leak into the payout, since the walk iterates FamilyChildren.
…enchmark Stand up benchmarking for pallet-arion so #79's declared settlement weight can be measured on reference hardware instead of hand-estimated. - pallet: frame-benchmarking dep + runtime-benchmarks feature; benchmarking.rs with a #[benchmarks] module; an ArionBenchmarkHelper Config type (behind runtime-benchmarks) for the cross-pallet state the crate can't reach alone. - miner_payment_settlement_hook: posed at the worst case over children x families x departed payouts, measured via #[block] on settle_miner_payments with the payout source primed so every payment + bond actually runs. - register_child (paid path, real ed25519 node sig), deregister_child, claim_unbonded: the child-deposit fund-moving lifecycle. - runtime: [pallet_arion, Arion] in define_benchmarks!, the ArionBenchHelper impl (funds the bank, whitelists the requester, sets the token price, authorizes the proxy child), and pallet-arion/runtime-benchmarks wiring. Measured weights are the follow-up: run `benchmark pallet` on reference hardware and replace the hand-written SubstrateWeight impl in weights.rs.
Measured on AMD EPYC 9255 (WASM compiled, chain `benchmark`, steps 50 / repeat 20) and merged into both the SubstrateWeight<T> and () impls: - miner_payment_settlement_hook: base 19.0ms, +9_993_349/child, +71_131_128/ family; departed payouts carry no per-unit ref-time term (cost is in the reads/writes). Reads 21 +4c +7f +2d, writes 8 +1c +6f +2d, plus PoV sizes. - register_child: 96.2ms, r23 w11 (paid path, real ed25519 node sig) - deregister_child: 52.9ms, r11 w19 (only-child family-cleanup branch) - claim_unbonded: 37.3ms, r5 w9 The other entries stay hand-estimated. This closes #79's "re-measured on reference hardware" criterion and covers the #37 miner-payment paths.
Resolve conflicts in the arion pallet's settlement benchmark, which both branches had rewritten. Combined the two designs: - Kept this branch's 3-dimension settlement weight (miner_payment_settlement_hook(max_children_total, max_families, max_departed)) and the DepartedFamilyArrears bounding (#79), which the merged lib.rs and WeightInfo trait are built on. - Folded in main's correctness concern: the settlement benchmark now pre-bonds and nominates every paid family so it measures the heavier bond_extra + bags-rebag path, not a first-time bond. - Unified the two helper traits into pallet_arion::ArionBenchmarkHelper (set_token_price, prime_payout_source, authorize_child, prebond_family) with a () no-op impl; dropped the duplicate BenchmarkHelper trait. - Kept every benchmark from both sides: register_child, deregister_child, claim_unbonded (this branch) and submit_miner_stats, set_miner_price (main). - Cargo.toml feature lists and the runtime benchmark registration merged as unions (sp-staking, pallet-hippocampus). weights.rs miner_payment_settlement_hook: took main's measured bond_extra + bags-rebag figures for the per-child (c) and per-family (f) terms, kept the 3-arg signature, and retained this branch's departed-payout (d) term. The d term is bond-path independent (departed families are paid in free balance, no bond), so it is unaffected by the bond_extra change and the two measurements compose into a correct linear weight. Verified: pallet-arion and hippius-mainnet-runtime both cargo check with and without runtime-benchmarks; the arion runtime test suites (arion_family_count_and_unbond, miner_payments, miner_payment_adversarial) pass (44 tests).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
pallet-arion's miner payment settlement runs in
on_initialize(mandatory blockweight) and moves funds, but its declared weight —
miner_payment_settlement_hook(MaxChildrenTotal, MaxFamilies)— claimed a boundthe actual iteration could exceed:
ChildRegistrations::iter(), which keepsUnbonding/stale rows(only active children are capped), and
FamilyArrearsmap, whose rows outlive active families.On a chain with enough unbonding children or stale arrears the hook could do more
work than declared, i.e. produce a heavier-than-declared block. Since it's in
on_initialize, overweight isn't rejected — the block is just produced heavy.Closes #79.
Changes
Child walk — bounded active index.
Iterate
FamilyChildreninstead ofChildRegistrations::iter().FamilyChildrenholds exactly the active children (pushed on
register_child,swap_removed onderegister_child) and its key set is the current families (dropped when afamily's active count hits zero), so the walk is provably ≤
MaxFamilieskeys ×MaxChildrenPerFamily=MaxChildrenTotal. The per-child registration read +Activecheck stay (same cost the olditer()already paid), preserving the"only an Active child is paid" guarantee even against a stale index.
Arrears walk — bounded, no funds forfeited.
When a family goes fully inactive, its
FamilyArrearsrow is moved into a newDepartedFamilyArrearsledger (both teardown paths:cleanup_family_when_no_active_childrenanddo_deregister_family). The livearrears walk then only ever sees active families (≤
MaxFamilies). Nothing owedis dropped — the settlement pays the departed ledger down automatically:
what's left),
MaxDepartedPayoutsPerBlock(10) slice per tick viaDepartedArrearsCursor, so that walk is bounded too however large the ledgergrows.
Declared weight extended to include the departed-payout slice.
Migration v3 → v4 sweeps pre-existing stale arrears (rows for inactive
families) into the departed ledger, so the bound holds regardless of rows already
on chain.
spec_version92022 → 92023.Tests
Integration tests in
runtime/mainnet/tests/miner_payments.rs(the pallet has nomock runtime; these drive the real runtime — balances, staking, bank):
balance, without adding to the family's existing stake;
successive ticks;
deregister_family(wholesale teardown) also moves arrears to the ledger;Unbondingrows with fat accruals are never walked — only the active childis paid.
Settlement test helpers now seed
FamilyChildrenfor active children, matchingthe live
register_childinvariant the bounded walk relies on; threestorage-version assertions bumped to 4. Full arion test set stays green
(
miner_payments,miner_payment_adversarial,tub_lock_poc,audit_probe,uid_bootstrap_poc,arion_family_count_and_unbond, and the pay_* / weightsuites).
Follow-up (not in this PR)
The hook's declared weight is still a conservative hand-estimate (as its existing
TODO notes). Re-measuring on reference hardware needs a frame-benchmarking harness,
which this pallet doesn't have on this branch — tracked as the remaining #79 item.
Reviewer note
The bounded walk assumes every active child is present in
FamilyChildren(truefor anything via
register_child). Worth a sanity check that there's no legacycohort of active
ChildRegistrationspredatingFamilyChildrenmaintenance, whichwould otherwise stop being paid until re-registered.