Skip to content

Commit 5166d82

Browse files
committed
node: add PoS migration to fix total active stake
1 parent 0e8878a commit 5166d82

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

crates/node/src/shell/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,22 @@ where
818818
_ => None,
819819
};
820820

821+
// Temporary migration code to fix the total active stake value
822+
if let Some(height) =
823+
std::env::var("NAMADA_MIGRATION_HEIGHT").ok().map(|height| {
824+
<BlockHeight as std::str::FromStr>::from_str(&height.trim())
825+
.expect(
826+
"Invalid block height set in `NAMADA_MIGRATION_HEIGHT` env var",
827+
)
828+
})
829+
{
830+
if height == height_to_commit {
831+
proof_of_stake::fix_total_active_stake::<_, governance::Store<_>>(
832+
&mut self.state,
833+
).expect("Must be able to fix total active stake")
834+
}
835+
}
836+
821837
self.state
822838
.commit_block()
823839
.expect("Encountered a storage error while committing a block");

crates/proof_of_stake/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,6 +3277,47 @@ fn prune_old_delegations(
32773277
Ok(())
32783278
}
32793279

3280+
/// Temporary migration code to fix the total active stake value
3281+
pub fn fix_total_active_stake<S, Gov>(storage: &mut S) -> Result<()>
3282+
where
3283+
S: StorageRead + StorageWrite,
3284+
Gov: governance::Read<S>,
3285+
{
3286+
let epoch = storage.get_block_epoch()?;
3287+
let validators = storage::read_all_validator_addresses(storage, epoch)?;
3288+
let mut total_stake = token::Amount::zero();
3289+
let params = read_pos_params::<_, Gov>(storage)?;
3290+
for validator in validators {
3291+
let state = storage::read_validator_state::<_, Gov>(
3292+
storage, &validator, epoch,
3293+
)?;
3294+
let is_active = matches!(
3295+
state,
3296+
Some(
3297+
ValidatorState::Consensus
3298+
| ValidatorState::BelowCapacity
3299+
| ValidatorState::BelowThreshold,
3300+
)
3301+
);
3302+
if is_active {
3303+
let stake =
3304+
read_validator_stake(storage, &params, &validator, epoch)?;
3305+
total_stake =
3306+
total_stake.checked_add(stake).expect("Must not overflow");
3307+
}
3308+
}
3309+
3310+
let total_active_deltas = storage::total_active_deltas_handle();
3311+
total_active_deltas.set::<S, Gov>(
3312+
storage,
3313+
total_stake.change(),
3314+
epoch,
3315+
0,
3316+
)?;
3317+
3318+
Ok(())
3319+
}
3320+
32803321
#[cfg(any(test, feature = "testing"))]
32813322
/// PoS related utility functions to help set up tests.
32823323
pub mod test_utils {

0 commit comments

Comments
 (0)