Skip to content

Commit

Permalink
pow-migration: Fix detection of switching validator in pre-stake
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsdanielh committed Apr 25, 2024
1 parent 0c1839f commit 58b5dd5
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions pow-migration/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
pub mod types;

use std::{collections::HashMap, ops::Range, str::FromStr, vec};
use std::{
collections::{hash_map::Entry, HashMap},
ops::Range,
str::FromStr,
vec,
};

use nimiq_bls::PublicKey as BlsPublicKey;
use nimiq_genesis_builder::config::{
Expand Down Expand Up @@ -392,27 +397,38 @@ pub async fn get_stakers(
log::info!(staker_address=txn.from_address, validator_address=%address, "Found pre-stake transaction for validator");
if let Ok(staker_address) = Address::from_str(&txn.from_address) {
let stake = Coin::from_u64_unchecked(txn.value);
if stake >= Coin::from_u64_unchecked(Policy::MINIMUM_STAKE) {
validator.total_stake += stake;
stakers
.entry(staker_address.clone())
.and_modify(|staker| {
// If we have an entry for this staker, treat this as a switch to another
// validator or an increase of the stake (if the validator isn't changed)
staker.delegation =
validator.validator.validator_address.clone();
staker.balance += stake;
})
.or_insert(GenesisStaker {
staker_address,
balance: stake,
delegation: validator
.validator
.validator_address
.clone(),
inactive_balance: Coin::ZERO,
inactive_from: None,
});
match stakers.entry(staker_address.clone()) {
Entry::Occupied(mut entry) => {
// If we have an entry for this staker, treat this as a switch to another
// validator or an increase of the stake (if the validator isn't changed)
let staker = entry.get_mut();
staker.delegation =
validator.validator.validator_address.clone();
staker.balance += stake;

// Update the validator total stake
validator.total_stake += stake;
}
Entry::Vacant(entry) => {
// If there wasn't a staker registered for this address, check minimum stake
if stake
>= Coin::from_u64_unchecked(Policy::MINIMUM_STAKE)
{
entry.insert(GenesisStaker {
staker_address,
balance: stake,
delegation: validator
.validator
.validator_address
.clone(),
inactive_balance: Coin::ZERO,
inactive_from: None,
});

// Update the validator total stake
validator.total_stake += stake;
}
}
}
} else {
log::error!(
Expand Down

0 comments on commit 58b5dd5

Please sign in to comment.