Skip to content

Commit

Permalink
Ensure TotalAllocatedStake is set for the first set
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Jul 4, 2024
1 parent ba244e8 commit 52bb918
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,15 @@ pub mod pallet {
}
}

fn set_total_allocated_stake(network: NetworkId) {
let participants = Participants::<T>::get(network)
.expect("setting TotalAllocatedStake for a network without participants");
let total_stake = participants.iter().fold(0, |acc, (addr, _)| {
acc + Allocations::<T>::get((network, addr)).unwrap_or(Amount(0)).0
});
TotalAllocatedStake::<T>::set(network, Some(Amount(total_stake)));
}

// TODO: This is called retire_set, yet just starts retiring the set
// Update the nomenclature within this function
pub fn retire_set(set: ValidatorSet) {
Expand All @@ -715,12 +724,7 @@ pub mod pallet {
});

// Update the total allocated stake to be for the current set
let participants =
Participants::<T>::get(set.network).expect("set retired without a new set");
let total_stake = participants.iter().fold(0, |acc, (addr, _)| {
acc + Allocations::<T>::get((set.network, addr)).unwrap_or(Amount(0)).0
});
TotalAllocatedStake::<T>::set(set.network, Some(Amount(total_stake)));
self.set_total_allocated_stake(set.network);
}

/// Take the amount deallocatable.
Expand Down Expand Up @@ -907,6 +911,11 @@ pub mod pallet {

Keys::<T>::set(set, Some(key_pair.clone()));

// If this is the first ever set for this network, set TotalAllocatedStake now
if session == Session(0) {
self.set_total_allocated_stake(network);
}

// This does not remove from TotalAllocatedStake or InSet in order to:
// 1) Not decrease the stake present in this set. This means removed participants are
// still liable for the economic security of the external network. This prevents
Expand Down

0 comments on commit 52bb918

Please sign in to comment.