Skip to content

fix(arion): bound the settlement hook's weight by what it iterates (#79) - #82

Open
faizsarwar wants to merge 5 commits into
mainfrom
fix/arion-settlement-weight-bound
Open

faizsarwar wants to merge 5 commits into
mainfrom
fix/arion-settlement-weight-bound

Conversation

@faizsarwar

Copy link
Copy Markdown
Collaborator

What

pallet-arion's miner payment settlement runs in on_initialize (mandatory block
weight) and moves funds, but its declared weight —
miner_payment_settlement_hook(MaxChildrenTotal, MaxFamilies) — claimed a bound
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.

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 FamilyChildren instead of ChildRegistrations::iter(). FamilyChildren
holds exactly the active children (pushed on register_child, swap_removed on
deregister_child) and its key set is the current families (dropped when a
family's active count hits zero), so the walk is provably ≤ MaxFamilies keys ×
MaxChildrenPerFamily = MaxChildrenTotal. The per-child registration read +
Active check stay (same cost the old iter() 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 FamilyArrears row is moved into a new
DepartedFamilyArrears ledger (both teardown paths:
cleanup_family_when_no_active_children and do_deregister_family). The live
arrears walk then only ever sees active families (≤ MaxFamilies). Nothing owed
is dropped — the settlement pays the departed ledger down automatically:

  • from bank surplus (active families settled first; departed debt drains from
    what's left),
  • in free balance (a departed family isn't bonded),
  • a bounded MaxDepartedPayoutsPerBlock (10) slice per tick via
    DepartedArrearsCursor, so that walk is bounded too however large the ledger
    grows.

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_version 92022 → 92023.

Tests

Integration tests in runtime/mainnet/tests/miner_payments.rs (the pallet has no
mock runtime; these drive the real runtime — balances, staking, bank):

  • departed debt is preserved on departure and paid from later surplus in free
    balance, without adding to the family's existing stake;
  • the per-tick payout cap holds and the cursor drains a 25-row backlog over
    successive ticks;
  • migration v3→v4 moves stale rows while leaving active families' arrears live;
  • deregister_family (wholesale teardown) also moves arrears to the ledger;
  • 50 Unbonding rows with fat accruals are never walked — only the active child
    is paid.

Settlement test helpers now seed FamilyChildren for active children, matching
the live register_child invariant the bounded walk relies on; three
storage-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_* / weight
suites).

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 (true
for anything via register_child). Worth a sanity check that there's no legacy
cohort of active ChildRegistrations predating FamilyChildren maintenance, which
would otherwise stop being paid until re-registered.

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arion: settlement hook weight is not bounded by what it iterates (unbonding children + stale arrears)

1 participant