Skip to content

Staking (EPMB): Add defensive error handling to voter snapshot creation and solution verification#8687

Merged
sigurpol merged 11 commits intomasterfrom
sigurpol/epmb_no_unwrap_on_init
May 30, 2025
Merged

Staking (EPMB): Add defensive error handling to voter snapshot creation and solution verification#8687
sigurpol merged 11 commits intomasterfrom
sigurpol/epmb_no_unwrap_on_init

Conversation

@sigurpol
Copy link
Copy Markdown
Contributor

@sigurpol sigurpol commented May 28, 2025

  • Refactor snapshot creation to emit events and triggers defensive panic on failure
  • Replace unwrap() with defensive_unwrap_or(u32::MAX) to ensure solution fails verification gracefully when desired_targets is unavailable rather than panicking.
  • Add error events for failed target and voter snapshots

Close #8685.

@sigurpol sigurpol requested a review from a team as a code owner May 28, 2025 08:41
@sigurpol sigurpol added the T2-pallets This PR/Issue is related to a particular pallet. label May 28, 2025
@sigurpol sigurpol requested review from Ank4n and kianenigma May 28, 2025 08:58
@sigurpol sigurpol changed the title Staking (EPMB): Add defensive error handling to voter snapshot creation Staking (EPMB): Add defensive error handling to voter snapshot creation and solution verification May 28, 2025
@paritytech-workflow-stopper
Copy link
Copy Markdown

All GitHub workflows were cancelled due to failure one of the required jobs.
Failed workflow url: https://github.com/paritytech/polkadot-sdk/actions/runs/15296719861
Failed job name: test-linux-stable-runtime-benchmarks

.and_then(|(final_score, winner_count)| {
let desired_targets = crate::Snapshot::<T>::desired_targets().unwrap();
let desired_targets =
crate::Snapshot::<T>::desired_targets().defensive_unwrap_or(u32::MAX);
Copy link
Copy Markdown
Contributor

@Ank4n Ank4n May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code path is a bit messy because:
DataProvider::desired_targets() returns a Result, but it’s infallible.
Staking::ValidatorCount is a ValueQuery and defaults to 0 if not set.
• From this pallet’s pov, DesiredTargets might be unset, so we default to u32::MAX, whereas staking defaults to 0.

Practically this is of no significance — this code path should never be hit. But it’s a bit amusing and maybe a sign that our interfaces could use some cleanup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - maybe something to address next week when we do a complete review / cleanup of the staking code?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think a good follow-up would be to actually create_targets_snapshot and create_voters_snapshot to not return a (pointless) Result<_>, and handle everything internally.

If any of them fail, it should handle triggering a defensive panic + emit Unexpected event.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here -> 045b713

sigurpol added 7 commits May 30, 2025 11:38
Replace unwrap() with defensive_unwrap_or_default() in the on_initialize
path to gracefully handle runtime misconfigurations where voter snapshot
conversion fails, preventing potential panics during block initialization.
Replace unwrap() with defensive_unwrap_or(u32::MAX) to
ensure solution fails verification gracefully when desired_targets
is unavailable rather than panicking.
The commit adds new events to track when snapshot creation fails. This helps
with monitoring and debugging failures in target/voter snapshot operations.
@sigurpol sigurpol force-pushed the sigurpol/epmb_no_unwrap_on_init branch from e4dea84 to b6d96c4 Compare May 30, 2025 09:50
sigurpol added 3 commits May 30, 2025 13:39
Snapshot creation functions now emit failure events and trigger a defensive
panic instead of returning errors. Call sites updated accordingly.
@sigurpol sigurpol added this pull request to the merge queue May 30, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 30, 2025
@sigurpol sigurpol added this pull request to the merge queue May 30, 2025
Merged via the queue into master with commit 3d0c061 May 30, 2025
265 of 285 checks passed
@sigurpol sigurpol deleted the sigurpol/epmb_no_unwrap_on_init branch May 30, 2025 15:56
ordian added a commit that referenced this pull request Jun 4, 2025
* master:
  omni-node: fix `benchmark pallet` to work with `--runtime` (#8594)
  Handle and suppress "New unknown `FromSwarm` libp2p event" warning (#8731)
  Implement detailed logging for XCM failures (#8724)
  [pallet-revive] contract's nonce starts at 1 (#8734)
  sync/fix: Clear gap sync on known imported blocks (#8445)
  [PoP] Add personhood tracking pallets (#8164)
  client/net: Use litep2p as the default network backend (#8461)
  Unflake `returns_status_for_pruned_blocks` (#8709)
  [AHM] Report the weights of epmb pallet to expose kusama and polkadot weights (#8704)
  Remove all XCM dependencies from `pallet-revive` (#8584)
  Docker master image tag fix (#8711)
  Record ed as part of the storage deposit (#8718)
  [pallet-revive] update dry-run logic (#8662)
  feat: add collator peer ID to ParachainInherentData (#8708)
  Nest errors in pallet-xcm (#7730)
  pallet-assets ERC20 precompile (#8554)
  Broker: Introduce min price + adjust renewals to lower market. (#8630)
  [AHM] Staking async fixes for XCM and election planning (#8422)
  Staking (EPMB): Add defensive error handling to voter snapshot creation and solution verification (#8687)
pgherveou pushed a commit that referenced this pull request Jun 11, 2025
…on and solution verification (#8687)

- Refactor snapshot creation to emit events and triggers defensive panic
on failure
- Replace unwrap() with defensive_unwrap_or(u32::MAX) to ensure solution
fails verification gracefully when desired_targets is unavailable rather
than panicking.
- Add error events for failed target and voter snapshots

Close #8685.
alvicsam pushed a commit that referenced this pull request Oct 17, 2025
…on and solution verification (#8687)

- Refactor snapshot creation to emit events and triggers defensive panic
on failure
- Replace unwrap() with defensive_unwrap_or(u32::MAX) to ensure solution
fails verification gracefully when desired_targets is unavailable rather
than panicking.
- Add error events for failed target and voter snapshots

Close #8685.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T2-pallets This PR/Issue is related to a particular pallet.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Staking AHM] [EPMB] No unwrap on EPMB's on-init path

3 participants