Skip to content

Commit

Permalink
fix: rewards will be also distributed on the basis of stake (#191)
Browse files Browse the repository at this point in the history
Signed-off-by: dung5ire <[email protected]>
Co-authored-by: dung5ire <[email protected]>
  • Loading branch information
arunjot12 and dung5ire authored Oct 3, 2024
1 parent c39b380 commit 50cc92a
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 30 additions & 21 deletions frame/reward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{ensure, pallet_prelude::DispatchResult};
pub use pallet::*;
use pallet_staking::{Rewards, CurrentEra, Validators, ErasRewardPoints, ErasStakers, IndividualExposure };
use pallet_staking::{BalanceOf, CurrentEra, ErasRewardPoints, ErasStakers, Exposure, IndividualExposure, Rewards, Validators };
use parity_scale_codec::Codec;
// use crate::migration::migrate_to_v1;
use frame_support::pallet_prelude::StorageVersion;
Expand Down Expand Up @@ -263,38 +263,29 @@ impl<T: Config> Rewards<T::AccountId> for Pallet<T> {
validators.iter().for_each(|validator_id| {
let validator = T::ValidatorId::convert(validator_id.clone()).unwrap();
let validator_points = Self::retrieve_validator_point(validator.clone());
let validator_exposure= ErasStakers::<T>::get(Self::current_era(), validator.clone());
let total_reward = Self::calculate_era_reward();
let reward = Self::calculate_validator_era_reward(
let validator_era_reward = Self::calculate_validator_era_reward(
validator_points.into(),
validator_exposure.total,
total_reward
);
let nominators = Self::check_nominators(validator.clone());
if nominators.is_empty() {
Self::allocate_rewards(
validator.clone(),
None,
Self::convert_float64_to_unsigned128(reward).into()
Self::convert_float64_to_unsigned128(validator_era_reward).into()
);
return;
}
let validator_commission = Self::validator_commission(validator.clone());
let validator_share = ((reward as f64) * (validator_commission as f64)) / 100.0;
let exposure = ErasStakers::<T>::get(Self::current_era(), validator.clone());
let total_stake = exposure.total;
let validator_stake = exposure.own;
let remaining_reward = reward - validator_share;
let validator_own_share_reward = Self::calculate_reward_share(
validator_stake.into(),
total_stake.into(),
remaining_reward
);
let total_validator_reward = validator_share + validator_own_share_reward;
let (total_validator_reward,remaining_reward_for_nominators) = Self::calculate_validator_commission_reward(validator.clone(),validator_era_reward,validator_exposure.clone());
Self::allocate_rewards(
validator.clone(),
None,
Self::convert_float64_to_unsigned128(total_validator_reward).into()
);
if remaining_reward.is_zero(){
if remaining_reward_for_nominators.is_zero(){
return;
}
nominators.iter().for_each(|nominator| {
Expand All @@ -306,8 +297,8 @@ impl<T: Config> Rewards<T::AccountId> for Pallet<T> {
let nominator_stake = nominator.value;
let nominator_reward = Self::calculate_reward_share(
nominator_stake.into(),
total_stake.into(),
remaining_reward.into()
validator_exposure.total.into(),
remaining_reward_for_nominators.into()
);
Self::allocate_rewards(
validator.clone(),
Expand Down Expand Up @@ -396,6 +387,22 @@ impl<T: Config> Pallet<T> {
total_reward
}

/// Calculates the total reward for a validator based on their commission and stake, as well as the remaining reward for nominators.
fn calculate_validator_commission_reward(validator: T::AccountId, validator_era_reward: f64, exposure : Exposure<<T as frame_system::Config>::AccountId, BalanceOf<T>>,) -> (f64,f64) {
let validator_commission = Self::validator_commission(validator.clone());
let validator_share = ((validator_era_reward as f64) * (validator_commission as f64)) / 100.0;
let total_stake = exposure.total;
let validator_stake = exposure.own;
let remaining_reward = validator_era_reward - validator_share;
let validator_own_share_reward = Self::calculate_reward_share(
validator_stake.into(),
total_stake.into(),
remaining_reward
);
let total_validator_reward = validator_share + validator_own_share_reward;
(total_validator_reward,remaining_reward)
}

/// Converts a floating-point number to an unsigned 128-bit integer.
pub fn convert_float64_to_unsigned128(value: f64) -> u128 {
let precision = T::Precision::get();
Expand Down Expand Up @@ -488,10 +495,12 @@ impl<T: Config> Pallet<T> {
}

/// Compute the reward of the validator
fn calculate_validator_era_reward(validator_points: u32, era_reward: f64) -> f64 {
fn calculate_validator_era_reward(validator_points: u32,validator_stake:T::CurrencyBalance, era_reward: f64) -> f64 {
let era_reward_points = <ErasRewardPoints<T>>::get(Self::active_era());
let total_points = era_reward_points.total as u32;
let reward = ((validator_points as f64) / (total_points as f64)) * (era_reward as f64);
let validator_points_stake = validator_points as u128 + validator_stake.into();
let total_stake = pallet_staking::ErasTotalStake::<T>::get(Self::current_era());
let total_points = era_reward_points.total as u128 + total_stake.into();
let reward = ((validator_points_stake as f64) / (total_points as f64)) * (era_reward as f64);
reward
}

Expand Down
1 change: 1 addition & 0 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub mod pallet {
+ MaybeSerializeDeserialize
+ sp_std::fmt::Debug
+ Default
+ From<u32>
+ From<u64>
+ From<u128>
+ Into<u128>
Expand Down
2 changes: 1 addition & 1 deletion runtime/firechain-mainnet-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "firechain-mainnet-runtime"
version = "1.1.1"
version = "1.1.2"
authors = ["5ire Team <[email protected]>"]
description = "5ire chain Mainnet runtime"
edition = "2021"
Expand Down
4 changes: 2 additions & 2 deletions runtime/firechain-mainnet-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 111,
spec_version: 112,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -2549,7 +2549,7 @@ pub mod migrations {
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = pallet_reward::migration::v1::MigrateToV1<Runtime>;
pub type Unreleased = ();
}


Expand Down
2 changes: 1 addition & 1 deletion runtime/firechain-qa-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "firechain-qa-runtime"
version = "1.1.0"
version = "1.1.1"
authors = ["5ire Team <[email protected]>"]
description = "5ire chain qa runtime"
edition = "2021"
Expand Down
4 changes: 2 additions & 2 deletions runtime/firechain-qa-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 110,
spec_version: 111,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -2547,7 +2547,7 @@ pub mod migrations {
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = pallet_reward::migration::v1::MigrateToV1<Runtime>;
pub type Unreleased = ();
}


Expand Down
2 changes: 1 addition & 1 deletion runtime/firechain-thunder-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "firechain-thunder-runtime"
version = "1.0.7"
version = "1.0.8"
authors = ["5ire Team <[email protected]>"]
description = "5ire chain Thunder Testnet runtime"
edition = "2021"
Expand Down
4 changes: 2 additions & 2 deletions runtime/firechain-thunder-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("firechain-node-thunder"),
impl_name: create_runtime_str!("5ire"),
authoring_version: 1,
spec_version: 107,
spec_version: 108,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -2553,7 +2553,7 @@ pub mod migrations {
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = pallet_reward::migration::v1::MigrateToV1<Runtime>;
pub type Unreleased = ();
}


Expand Down

0 comments on commit 50cc92a

Please sign in to comment.