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 May 10, 2024
1 parent 3071188 commit 7ef415f
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 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 @@ -388,31 +393,38 @@ pub async fn get_stakers(
if let Ok(address_bytes) = hex::decode(data) {
if let Ok(address_str) = std::str::from_utf8(&address_bytes) {
if let Ok(address) = Address::from_str(address_str) {
if let Some(validator) = validators.get_mut(address_str) {
if let Some(validator) = validators.get(address_str) {
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();

// Update the staker entry
staker.delegation =
validator.validator.validator_address.clone();
staker.balance += 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,
});
}
}
}
} else {
log::error!(
Expand All @@ -433,6 +445,14 @@ pub async fn get_stakers(
}
}

// Now that we have the stakers, update the validator's total stake
for staker in stakers.values() {
let validator = validators
.get_mut(&staker.delegation.to_string())
.expect("Delegated validator must exist");
validator.total_stake += staker.balance;
}

Ok((
stakers.into_values().collect(),
validators.into_values().collect(),
Expand Down

0 comments on commit 7ef415f

Please sign in to comment.