mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-12 14:09:25 +00:00
add initial reward era test
This commit is contained in:
@@ -27,11 +27,8 @@ frame-system = { git = "https://github.com/serai-dex/substrate", default-feature
|
||||
frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-session = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-runtime = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
pallet-babe = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
coins-pallet = { package = "serai-coins-pallet", path = "../../coins/pallet", default-features = false }
|
||||
validator-sets-pallet = { package = "serai-validator-sets-pallet", path = "../../validator-sets/pallet", default-features = false }
|
||||
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
|
||||
@@ -50,11 +47,8 @@ std = [
|
||||
"frame-support/std",
|
||||
|
||||
"sp-std/std",
|
||||
"sp-session/std",
|
||||
"sp-runtime/std",
|
||||
|
||||
"pallet-babe/std",
|
||||
|
||||
"coins-pallet/std",
|
||||
"validator-sets-pallet/std",
|
||||
"dex-pallet/std",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding)]
|
||||
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding, clippy::empty_docs)]
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
@@ -8,27 +8,21 @@ pub mod pallet {
|
||||
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
|
||||
|
||||
use sp_std::{vec, vec::Vec, collections::btree_map::BTreeMap};
|
||||
use sp_session::ShouldEndSession;
|
||||
use sp_runtime;
|
||||
|
||||
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
||||
use dex_pallet::{Config as DexConfig, Pallet as Dex};
|
||||
|
||||
use validator_sets_pallet::{Pallet as ValidatorSets, Config as ValidatorSetsConfig};
|
||||
use pallet_babe::{Pallet as Babe, Config as BabeConfig};
|
||||
|
||||
use serai_primitives::{NetworkId, NETWORKS, *};
|
||||
use validator_sets_primitives::MAX_KEY_SHARES_PER_SET;
|
||||
use validator_sets_primitives::{MAX_KEY_SHARES_PER_SET, Session};
|
||||
use genesis_liquidity_primitives::GENESIS_PERIOD_BLOCKS;
|
||||
use emissions_primitives::*;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config:
|
||||
frame_system::Config<AccountId = PublicKey>
|
||||
+ ValidatorSetsConfig
|
||||
+ BabeConfig
|
||||
+ CoinsConfig
|
||||
+ DexConfig
|
||||
frame_system::Config<AccountId = PublicKey> + ValidatorSetsConfig + CoinsConfig + DexConfig
|
||||
{
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
@@ -37,7 +31,7 @@ pub mod pallet {
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
/// Networks to spawn Serai with.
|
||||
pub networks: Vec<NetworkId>,
|
||||
pub networks: Vec<(NetworkId, Amount)>,
|
||||
/// List of participants to place in the initial validator sets.
|
||||
pub participants: Vec<T::AccountId>,
|
||||
}
|
||||
@@ -50,10 +44,7 @@ pub mod pallet {
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
GenesisPeriodEnded,
|
||||
AmountOverflowed,
|
||||
NotEnoughLiquidity,
|
||||
CanOnlyRemoveFullAmount,
|
||||
MintFailed,
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
@@ -73,13 +64,17 @@ pub mod pallet {
|
||||
>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn epoch_begin_block)]
|
||||
pub(crate) type EpochBeginBlock<T: Config> = StorageMap<_, Identity, u64, u64, ValueQuery>;
|
||||
#[pallet::getter(fn session_begin_block)]
|
||||
pub(crate) type SessionBeginBlock<T: Config> = StorageMap<_, Identity, u32, u64, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn session)]
|
||||
pub type CurrentSession<T: Config> = StorageMap<_, Identity, NetworkId, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn economic_security_reached)]
|
||||
pub(crate) type EconomicSecurityReached<T: Config> =
|
||||
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, ValueQuery>;
|
||||
StorageMap<_, Identity, NetworkId, bool, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn last_swap_volume)]
|
||||
@@ -88,57 +83,60 @@ pub mod pallet {
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
for id in self.networks.clone() {
|
||||
for (id, stake) in self.networks.clone() {
|
||||
let mut participants = vec![];
|
||||
for p in self.participants.clone() {
|
||||
participants.push((p, 0u64));
|
||||
participants.push((p, stake.0));
|
||||
}
|
||||
Participants::<T>::set(id, Some(participants.try_into().unwrap()));
|
||||
CurrentSession::<T>::set(id, 0);
|
||||
EconomicSecurityReached::<T>::set(id, false);
|
||||
}
|
||||
|
||||
EpochBeginBlock::<T>::set(0, 0);
|
||||
SessionBeginBlock::<T>::set(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
/// Since we are on `on_finalize`, session should have already rotated.
|
||||
/// We can distribute the rewards for the last set.
|
||||
fn on_finalize(n: BlockNumberFor<T>) {
|
||||
// wait 1 extra block to actually see genesis changes
|
||||
let genesis_ended = n >= (GENESIS_PERIOD_BLOCKS + 1).into();
|
||||
|
||||
// we accept we reached economic security once we can mint smallest amount of a network's coin
|
||||
for coin in COINS {
|
||||
let existing = EconomicSecurityReached::<T>::get(coin.network());
|
||||
if existing == 0u32.into() &&
|
||||
<T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) })
|
||||
let check = !Self::economic_security_reached(coin.network()) && genesis_ended;
|
||||
if check && <T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) })
|
||||
{
|
||||
EconomicSecurityReached::<T>::set(coin.network(), n);
|
||||
EconomicSecurityReached::<T>::set(coin.network(), true);
|
||||
}
|
||||
}
|
||||
|
||||
// emissions start only after genesis period and happens once per epoch
|
||||
// check wif we got a new session
|
||||
let mut session_changed = false;
|
||||
let session = ValidatorSets::<T>::session(NetworkId::Serai).unwrap_or(Session(0)).0;
|
||||
if session > Self::session(NetworkId::Serai) {
|
||||
session_changed = true;
|
||||
CurrentSession::<T>::set(NetworkId::Serai, session);
|
||||
}
|
||||
|
||||
// emissions start only after genesis period and happens once per session.
|
||||
// so we don't do anything before that time.
|
||||
if !(n >= GENESIS_PERIOD_BLOCKS.into() && T::ShouldEndSession::should_end_session(n)) {
|
||||
if !(genesis_ended && session_changed) {
|
||||
return;
|
||||
}
|
||||
|
||||
// figure out the amount of blocks in the last epoch
|
||||
// TODO: we use epoch index here but should we use SessionIndex since this is how we decide
|
||||
// whether time to distribute the rewards or not? Because apparently epochs != Sessions
|
||||
// since we can skip some epochs if the chain is offline more than epoch duration??
|
||||
let epoch = Babe::<T>::current_epoch().epoch_index - 1;
|
||||
let block_count = n.saturated_into::<u64>() - Self::epoch_begin_block(epoch);
|
||||
// figure out the amount of blocks in the last session. Session is at least 1
|
||||
// if we come here.
|
||||
let current_block = n.saturated_into::<u64>();
|
||||
let block_count = current_block - Self::session_begin_block(session - 1);
|
||||
|
||||
// get total reward for this epoch
|
||||
let pre_ec_security = Self::pre_ec_security();
|
||||
let mut distances = BTreeMap::new();
|
||||
let mut total_distance: u64 = 0;
|
||||
let reward_this_epoch = if Self::initial_period(n) {
|
||||
// rewards are fixed for initial period
|
||||
block_count * INITIAL_REWARD_PER_BLOCK
|
||||
} else if pre_ec_security {
|
||||
let reward_this_epoch = if pre_ec_security {
|
||||
// calculate distance to economic security per network
|
||||
let mut total_required: u64 = 0;
|
||||
let mut total_current: u64 = 0;
|
||||
for n in NETWORKS {
|
||||
if n == NetworkId::Serai {
|
||||
continue;
|
||||
@@ -150,11 +148,10 @@ pub mod pallet {
|
||||
current = required;
|
||||
}
|
||||
|
||||
distances.insert(n, required - current);
|
||||
total_required = total_required.saturating_add(required);
|
||||
total_current = total_current.saturating_add(current);
|
||||
let distance = required - current;
|
||||
distances.insert(n, distance);
|
||||
total_distance = total_distance.saturating_add(distance);
|
||||
}
|
||||
total_distance = total_required.saturating_sub(total_current);
|
||||
|
||||
// add serai network portion(20%)
|
||||
let new_total_distance =
|
||||
@@ -162,10 +159,15 @@ pub mod pallet {
|
||||
distances.insert(NetworkId::Serai, new_total_distance - total_distance);
|
||||
total_distance = new_total_distance;
|
||||
|
||||
// rewards for pre-economic security is
|
||||
// (STAKE_REQUIRED - CURRENT_STAKE) / blocks_until(SECURE_BY).
|
||||
let block_reward = total_distance / Self::blocks_until(SECURE_BY);
|
||||
block_count * block_reward
|
||||
if Self::initial_period(n) {
|
||||
// rewards are fixed for initial period
|
||||
block_count * INITIAL_REWARD_PER_BLOCK
|
||||
} else {
|
||||
// rewards for pre-economic security is
|
||||
// (STAKE_REQUIRED - CURRENT_STAKE) / blocks_until(SECURE_BY).
|
||||
let block_reward = total_distance / Self::blocks_until(SECURE_BY);
|
||||
block_count * block_reward
|
||||
}
|
||||
} else {
|
||||
// post ec security
|
||||
block_count * REWARD_PER_BLOCK
|
||||
@@ -201,7 +203,11 @@ pub mod pallet {
|
||||
.map(|(n, distance)| {
|
||||
let reward = if pre_ec_security {
|
||||
// calculate how much each network gets based on distance to ec-security
|
||||
reward_this_epoch.saturating_mul(distance) / total_distance
|
||||
u64::try_from(
|
||||
u128::from(reward_this_epoch).saturating_mul(u128::from(distance)) /
|
||||
u128::from(total_distance),
|
||||
)
|
||||
.unwrap()
|
||||
} else {
|
||||
// 20% of the reward goes to the Serai network and rest is distributed among others
|
||||
// based on swap-volume.
|
||||
@@ -209,7 +215,12 @@ pub mod pallet {
|
||||
reward_this_epoch / 5
|
||||
} else {
|
||||
let reward = reward_this_epoch - (reward_this_epoch / 5);
|
||||
reward.saturating_mul(*volume_per_network.get(&n).unwrap_or(&0)) / total_volume
|
||||
u64::try_from(
|
||||
u128::from(reward)
|
||||
.saturating_mul(u128::from(*volume_per_network.get(&n).unwrap_or(&0))) /
|
||||
u128::from(total_volume),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
};
|
||||
(n, reward)
|
||||
@@ -218,35 +229,46 @@ pub mod pallet {
|
||||
|
||||
// distribute the rewards within the network
|
||||
for (n, reward) in rewards_per_network {
|
||||
// calculate pool vs validator share
|
||||
let capacity = ValidatorSets::<T>::total_allocated_stake(n).unwrap_or(Amount(0)).0;
|
||||
let required = ValidatorSets::<T>::required_stake_for_network(n);
|
||||
let unused_capacity = capacity.saturating_sub(required);
|
||||
let (validators_reward, pool_reward) = if n == NetworkId::Serai {
|
||||
(reward, 0)
|
||||
} else {
|
||||
// calculate pool vs validator share
|
||||
let capacity = ValidatorSets::<T>::total_allocated_stake(n).unwrap_or(Amount(0)).0;
|
||||
let required = ValidatorSets::<T>::required_stake_for_network(n);
|
||||
let unused_capacity = capacity.saturating_sub(required);
|
||||
|
||||
let distribution = unused_capacity.saturating_mul(ACCURACY_MULTIPLIER) / capacity;
|
||||
let total = DESIRED_DISTRIBUTION.saturating_add(distribution);
|
||||
let distribution = unused_capacity.saturating_mul(ACCURACY_MULTIPLIER) / capacity;
|
||||
let total = DESIRED_DISTRIBUTION.saturating_add(distribution);
|
||||
|
||||
let validators_reward = DESIRED_DISTRIBUTION.saturating_mul(reward) / total;
|
||||
let pool_reward = total - validators_reward;
|
||||
let validators_reward = DESIRED_DISTRIBUTION.saturating_mul(reward) / total;
|
||||
let pool_reward = total - validators_reward;
|
||||
(validators_reward, pool_reward)
|
||||
};
|
||||
|
||||
// distribute validators rewards
|
||||
Self::distribute_to_validators(n, validators_reward);
|
||||
if Self::distribute_to_validators(n, validators_reward).is_err() {
|
||||
// TODO: log the failure
|
||||
continue;
|
||||
}
|
||||
|
||||
// send the rest to the pool
|
||||
let coin_count = u64::try_from(n.coins().len()).unwrap();
|
||||
for c in n.coins() {
|
||||
// TODO: we just print a warning here instead of unwrap?
|
||||
// assumes reward is equally distributed between network coins.
|
||||
Coins::<T>::mint(
|
||||
if Coins::<T>::mint(
|
||||
Dex::<T>::get_pool_account(*c),
|
||||
Balance { coin: Coin::Serai, amount: Amount(pool_reward / coin_count) },
|
||||
)
|
||||
.unwrap();
|
||||
.is_err()
|
||||
{
|
||||
// TODO: log the failure
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set the begin block and participants
|
||||
EpochBeginBlock::<T>::set(epoch, n.saturated_into::<u64>());
|
||||
SessionBeginBlock::<T>::set(session, current_block);
|
||||
for n in NETWORKS {
|
||||
// TODO: `participants_for_latest_decided_set` returns keys with key shares but we
|
||||
// store keys with actual stake amounts. Pr https://github.com/serai-dex/serai/pull/518
|
||||
@@ -273,14 +295,14 @@ pub mod pallet {
|
||||
continue;
|
||||
}
|
||||
|
||||
if Self::economic_security_reached(n) == 0u32.into() {
|
||||
if !Self::economic_security_reached(n) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn distribute_to_validators(n: NetworkId, reward: u64) {
|
||||
fn distribute_to_validators(n: NetworkId, reward: u64) -> DispatchResult {
|
||||
// distribute among network's set based on
|
||||
// -> (key shares * stake per share) + ((stake % stake per share) / 2)
|
||||
let stake_per_share = ValidatorSets::<T>::allocation_per_key_share(n).unwrap().0;
|
||||
@@ -296,10 +318,17 @@ pub mod pallet {
|
||||
|
||||
// stake the rewards
|
||||
for (p, score) in scores {
|
||||
let p_reward = reward.saturating_mul(score) / total_score;
|
||||
// TODO: print a warning here?
|
||||
let _ = ValidatorSets::<T>::deposit_stake(n, p, Amount(p_reward));
|
||||
let p_reward = u64::try_from(
|
||||
u128::from(reward).saturating_mul(u128::from(score)) / u128::from(total_score),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) })
|
||||
.map_err(|_| Error::<T>::MintFailed)?;
|
||||
ValidatorSets::<T>::deposit_stake(n, p, Amount(p_reward))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user