Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pow-migration: Fix detection of switching validator in pre-stake #2403

Open
wants to merge 1 commit into
base: albatross
Choose a base branch
from

Conversation

jsdanielh
Copy link
Contributor

Fix the detection of switching validator in pre-stake as it was filtering always transactions that didn't have the minimum stake but for switching to another validator in pre-stake, the stake is already above the minimum since it was already counted to another validator.

This fixes #2399.

Pull request checklist

  • All tests pass. The project builds and runs.
  • I have resolved any merge conflicts.
  • I have resolved all clippy and rustfmt warnings.

@jsdanielh jsdanielh added this to the Nimiq PoS Mainnet milestone Apr 25, 2024
@Eligioo
Copy link
Member

Eligioo commented May 2, 2024

@jsdanielh I do think this is still prone to incorrect outcomes. I believe decrease_validator_total_stake should only be set to Some if the staker is actually switching between validators. It can very well be that the staker doesn't change validator and only simply increase his stake. In this scenario the stakers.entry will be Occupied as you process the top-up transaction after his initial pre-stake transaction. But right now this will result in validator.total_stake to reduced in the end, which shouldn't happen.

To make this easier to reason about, and since stake can jump around validators quite a lot, is that you compute the total stake of a validator in a second loop after you've done processing all the transactions in for (_, txns) in txns_by_sender.iter_mut(). Instead of trying to keep track of the total stake as you process the txns_by_sender.

@jsdanielh
Copy link
Contributor Author

@jsdanielh I do think this is still prone to incorrect outcomes. I believe decrease_validator_total_stake should only be set to Some if the staker is actually switching between validators. It can very well be that the staker doesn't change validator and only simply increase his stake. In this scenario the stakers.entry will be Occupied as you process the top-up transaction after his initial pre-stake transaction. But right now this will result in validator.total_stake to reduced in the end, which shouldn't happen.

To make this easier to reason about, and since stake can jump around validators quite a lot, is that you compute the total stake of a validator in a second loop after you've done processing all the transactions in for (_, txns) in txns_by_sender.iter_mut(). Instead of trying to keep track of the total stake as you process the txns_by_sender.

Makes sense. Just made the change to implement this.

pow-migration/src/state/mod.rs Outdated Show resolved Hide resolved
pow-migration/src/state/mod.rs Outdated Show resolved Hide resolved
Fix the detection of switching validator in pre-stake as it was
filtering always transactions that didn't have the minimum stake but
for switching to another validator in pre-stake, the stake is already
above the minimum since it was already counted to another validator.

This fixes #2399.
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.

Migration: Validator change in pre-staking window not detected
2 participants