mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-03 09:29:46 +00:00
fix total allocated stake update in wrong time (#518)
Some checks failed
Full Stack Tests / build (push) Has been cancelled
Lint / clippy (macos-13) (push) Has been cancelled
Lint / clippy (macos-14) (push) Has been cancelled
Lint / clippy (ubuntu-latest) (push) Has been cancelled
Lint / clippy (windows-latest) (push) Has been cancelled
Lint / deny (push) Has been cancelled
Lint / fmt (push) Has been cancelled
Lint / machete (push) Has been cancelled
Reproducible Runtime / build (push) Has been cancelled
Tests / test-infra (push) Has been cancelled
Tests / test-substrate (push) Has been cancelled
Tests / test-serai-client (push) Has been cancelled
Some checks failed
Full Stack Tests / build (push) Has been cancelled
Lint / clippy (macos-13) (push) Has been cancelled
Lint / clippy (macos-14) (push) Has been cancelled
Lint / clippy (ubuntu-latest) (push) Has been cancelled
Lint / clippy (windows-latest) (push) Has been cancelled
Lint / deny (push) Has been cancelled
Lint / fmt (push) Has been cancelled
Lint / machete (push) Has been cancelled
Reproducible Runtime / build (push) Has been cancelled
Tests / test-infra (push) Has been cancelled
Tests / test-substrate (push) Has been cancelled
Tests / test-serai-client (push) Has been cancelled
* fix total allocated stake update in wrong time * Restore mid-set increases * Correct typo I introduced --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
parent
4d9c2df38c
commit
3e99d68cfe
1 changed files with 26 additions and 12 deletions
|
@ -124,7 +124,9 @@ pub mod pallet {
|
||||||
#[pallet::getter(fn allocation_per_key_share)]
|
#[pallet::getter(fn allocation_per_key_share)]
|
||||||
pub type AllocationPerKeyShare<T: Config> =
|
pub type AllocationPerKeyShare<T: Config> =
|
||||||
StorageMap<_, Identity, NetworkId, Amount, OptionQuery>;
|
StorageMap<_, Identity, NetworkId, Amount, OptionQuery>;
|
||||||
/// The validators selected to be in-set.
|
/// The validators selected to be in-set (and their key shares), regardless of if removed.
|
||||||
|
///
|
||||||
|
/// This method allows iterating over all validators and their stake.
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn participants_for_latest_decided_set)]
|
#[pallet::getter(fn participants_for_latest_decided_set)]
|
||||||
pub(crate) type Participants<T: Config> = StorageMap<
|
pub(crate) type Participants<T: Config> = StorageMap<
|
||||||
|
@ -134,8 +136,10 @@ pub mod pallet {
|
||||||
BoundedVec<(Public, u64), ConstU32<{ MAX_KEY_SHARES_PER_SET }>>,
|
BoundedVec<(Public, u64), ConstU32<{ MAX_KEY_SHARES_PER_SET }>>,
|
||||||
OptionQuery,
|
OptionQuery,
|
||||||
>;
|
>;
|
||||||
/// The validators selected to be in-set, regardless of if removed, with the ability to perform a
|
/// The validators selected to be in-set, regardless of if removed.
|
||||||
/// check for presence.
|
///
|
||||||
|
/// This method allows quickly checking for presence in-set and looking up a validator's key
|
||||||
|
/// shares.
|
||||||
// Uses Identity for NetworkId to avoid a hash of a severely limited fixed key-space.
|
// Uses Identity for NetworkId to avoid a hash of a severely limited fixed key-space.
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
pub(crate) type InSet<T: Config> =
|
pub(crate) type InSet<T: Config> =
|
||||||
|
@ -364,7 +368,6 @@ pub mod pallet {
|
||||||
let allocation_per_key_share = Self::allocation_per_key_share(network).unwrap().0;
|
let allocation_per_key_share = Self::allocation_per_key_share(network).unwrap().0;
|
||||||
|
|
||||||
let mut participants = vec![];
|
let mut participants = vec![];
|
||||||
let mut total_stake = 0;
|
|
||||||
{
|
{
|
||||||
let mut iter = SortedAllocationsIter::<T>::new(network);
|
let mut iter = SortedAllocationsIter::<T>::new(network);
|
||||||
let mut key_shares = 0;
|
let mut key_shares = 0;
|
||||||
|
@ -376,7 +379,6 @@ pub mod pallet {
|
||||||
participants.push((key, these_key_shares));
|
participants.push((key, these_key_shares));
|
||||||
|
|
||||||
key_shares += these_key_shares;
|
key_shares += these_key_shares;
|
||||||
total_stake += amount.0;
|
|
||||||
}
|
}
|
||||||
amortize_excess_key_shares(&mut participants);
|
amortize_excess_key_shares(&mut participants);
|
||||||
}
|
}
|
||||||
|
@ -384,7 +386,6 @@ pub mod pallet {
|
||||||
for (key, shares) in &participants {
|
for (key, shares) in &participants {
|
||||||
InSet::<T>::set(network, key, Some(*shares));
|
InSet::<T>::set(network, key, Some(*shares));
|
||||||
}
|
}
|
||||||
TotalAllocatedStake::<T>::set(network, Some(Amount(total_stake)));
|
|
||||||
|
|
||||||
let set = ValidatorSet { network, session };
|
let set = ValidatorSet { network, session };
|
||||||
Pallet::<T>::deposit_event(Event::NewSet { set });
|
Pallet::<T>::deposit_event(Event::NewSet { set });
|
||||||
|
@ -523,11 +524,16 @@ pub mod pallet {
|
||||||
Err(Error::<T>::AllocationWouldPreventFaultTolerance)?;
|
Err(Error::<T>::AllocationWouldPreventFaultTolerance)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if InSet::<T>::contains_key(network, account) {
|
// If they're in the current set, and the current set has completed its handover (so its
|
||||||
TotalAllocatedStake::<T>::set(
|
// currently being tracked by TotalAllocatedStake), update the TotalAllocatedStake
|
||||||
network,
|
if let Some(session) = Self::session(network) {
|
||||||
Some(Amount(TotalAllocatedStake::<T>::get(network).unwrap_or(Amount(0)).0 + amount.0)),
|
if InSet::<T>::contains_key(network, account) && Self::handover_completed(network, session)
|
||||||
);
|
{
|
||||||
|
TotalAllocatedStake::<T>::set(
|
||||||
|
network,
|
||||||
|
Some(Amount(TotalAllocatedStake::<T>::get(network).unwrap_or(Amount(0)).0 + amount.0)),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -695,7 +701,7 @@ pub mod pallet {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serai network slashes are handled by BABE/GRANDPA
|
// Serai doesn't set keys and network slashes are handled by BABE/GRANDPA
|
||||||
if set.network != NetworkId::Serai {
|
if set.network != NetworkId::Serai {
|
||||||
// This overwrites the prior value as the prior to-report set's stake presumably just
|
// This overwrites the prior value as the prior to-report set's stake presumably just
|
||||||
// unlocked, making their report unenforceable
|
// unlocked, making their report unenforceable
|
||||||
|
@ -707,6 +713,14 @@ pub mod pallet {
|
||||||
Self::deposit_event(Event::AcceptedHandover {
|
Self::deposit_event(Event::AcceptedHandover {
|
||||||
set: ValidatorSet { network: set.network, session: Session(set.session.0 + 1) },
|
set: ValidatorSet { network: set.network, session: Session(set.session.0 + 1) },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Take the amount deallocatable.
|
/// Take the amount deallocatable.
|
||||||
|
|
Loading…
Reference in a new issue