Skip to content

Commit

Permalink
Track total allocated stake in validator-sets pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Oct 20, 2023
1 parent c056b75 commit 3797679
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub mod pallet {
pub type InSet<T: Config> =
StorageMap<_, Identity, (NetworkId, [u8; 16], Public), (), OptionQuery>;

/// The total stake allocated to this network by the active set of validators.
#[pallet::storage]
pub type TotalAllocatedStake<T: Config> = StorageMap<_, Identity, NetworkId, Amount, OptionQuery>;

/// The current amount allocated to a validator set by a validator.
#[pallet::storage]
#[pallet::getter(fn allocation)]
Expand Down Expand Up @@ -275,6 +279,7 @@ pub mod pallet {
let mut iter = SortedAllocationsIter::<T>::new(network);
let mut participants = vec![];
let mut key_shares = 0;
let mut total_stake = 0;
while key_shares < u64::from(MAX_KEY_SHARES_PER_SET) {
let Some((key, amount)) = iter.next() else { break };

Expand All @@ -284,7 +289,9 @@ pub mod pallet {
// This can technically set key_shares to a value exceeding MAX_KEY_SHARES_PER_SET
// Off-chain, the key shares per validator will be accordingly adjusted
key_shares += amount.0 / allocation_per_key_share;
total_stake += amount.0;
}
TotalAllocatedStake::<T>::set(network, Some(Amount(total_stake)));

let set = ValidatorSet { network, session };
Pallet::<T>::deposit_event(Event::NewSet { set });
Expand Down Expand Up @@ -453,6 +460,13 @@ pub mod pallet {
}
}

if InSet::<T>::contains_key(Self::in_set_key(network, account)) {
TotalAllocatedStake::<T>::set(
network,
Some(Amount(TotalAllocatedStake::<T>::get(network).unwrap_or(Amount(0)).0 + amount.0)),
);
}

Ok(())
}

Expand Down Expand Up @@ -521,8 +535,15 @@ pub mod pallet {
}
}
}
// Also allow immediate deallocation of the key shares remain the same
// Also allow immediate deallocation if the key shares remain the same
if (!active) || (!decreased_key_shares) {
if active {
// Since it's being immediately deallocated, decrease TotalAllocatedStake
TotalAllocatedStake::<T>::set(
network,
Some(Amount(TotalAllocatedStake::<T>::get(network).unwrap_or(Amount(0)).0 - amount.0)),
);
}
return Ok(true);
}

Expand Down

0 comments on commit 3797679

Please sign in to comment.