Skip to content

Commit

Permalink
fix: validator will receive rewards on both their commission and thei… (
Browse files Browse the repository at this point in the history
#150)

Signed-off-by: dung5ire <[email protected]>
Co-authored-by: dung5ire <[email protected]>
  • Loading branch information
arunjot12 and dung5ire authored Jun 4, 2024
1 parent e400c10 commit ad99305
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

58 changes: 36 additions & 22 deletions frame/reward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<T: Config> Rewards<T::AccountId> for Pallet<T> {
let validator_points = Self::retrieve_validator_point(validator.clone());
let era_reward = Self::calculate_era_reward();
let total_reward = era_reward as f64;
let reward = Self::calculate_validator_reward(validator_points.into(), total_reward);
let reward = Self::calculate_validator_era_reward(validator_points.into(), total_reward);
let nominators = Self::check_nominators(validator.clone());
if nominators.is_empty() {
Self::allocate_validator_rewards(
Expand All @@ -245,20 +245,27 @@ impl<T: Config> Rewards<T::AccountId> for Pallet<T> {
}
let validator_commission = Self::validator_commission(validator.clone());
let validator_share = ((reward as f64) * (validator_commission as f64)) / 100.0;
let nominator_share = reward - validator_share;
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_validator_reward_share(
validator_stake.into(),
total_stake.into(),
remaining_reward
);
let total_validator_reward = validator_share + validator_own_share_reward;
Self::allocate_validator_rewards(
validator.clone(),
Self::convert_float64_to_unsigned128(validator_share).into()
Self::convert_float64_to_unsigned128(total_validator_reward).into()
);

nominators.iter().for_each(|nominator| {
let nominator_stake = nominator.value;
let exposure = ErasStakers::<T>::get(Self::current_era(), validator.clone());
let total_stake = exposure.total;
let nominator_reward = Self::calculate_nominator_reward(
nominator_stake.into(),
total_stake.into(),
nominator_share.into()
remaining_reward.into()
);
Self::allocate_nominator_reward(
nominator.who.clone(),
Expand Down Expand Up @@ -328,6 +335,16 @@ impl<T: Config> Pallet<T> {
era_reward.into()
}

/// Compute the reward share for a validator based on their stake.
pub fn calculate_validator_reward_share(share: u128, total_stake: u128, reward: f64) -> f64 {
let precision = T::Precision::get();
let scaled_share = share / (10u128).pow(precision);
let scaled_total_stake: u64 = (total_stake / (10u128).pow(precision)) as u64;
let division: f64 = ((scaled_share as f64) / (scaled_total_stake as f64)) as f64;
let total_reward = division * reward;
total_reward
}

/// Compute the reward of the nominator
pub fn calculate_nominator_reward(share: u128, total_stake: u128, reward: f64) -> f64 {
let precision = T::Precision::get();
Expand Down Expand Up @@ -370,12 +387,12 @@ impl<T: Config> Pallet<T> {
/// Distributes rewards to the specified validator.
fn distribute_validator_reward(account: T::AccountId) -> DispatchResult {
let reward = ValidatorRewardAccounts::<T>::get(account.clone());
if reward.is_zero(){
return Ok(());
}
Self::transfer(Self::account_id(), account.clone(), reward, KeepAlive)?;
ValidatorRewardAccounts::<T>::remove(account.clone());

//BeneficialRewardRecord::<T>::insert(account.clone(), reward);
Self::store_reward_received(account,reward);

Ok(())
}

Expand All @@ -386,12 +403,8 @@ impl<T: Config> Pallet<T> {
return Ok(());
}
Self::transfer(Self::account_id(), account.clone(), reward, KeepAlive)?;
// let staking_account = T::LiquidStakeVault::staking_account();
// if account != staking_account {
// NominatorRewardAccounts::<T>::remove(account.clone());
// }
// BeneficialRewardRecord::<T>::insert(account.clone(), reward);
Self::store_reward_received(account,reward);
NominatorRewardAccounts::<T>::remove(account.clone());
Self::store_reward_received(account,reward);
Ok(())
}

Expand All @@ -406,15 +419,16 @@ impl<T: Config> Pallet<T> {
let era = active_era.index;
era
}
/// Store the received reward for a specific account.
fn store_reward_received(account:T::AccountId,reward:T::Balance){
let earlier_reward = BeneficialRewardRecord::<T>::get(account.clone());
let new_reward = reward + earlier_reward;
BeneficialRewardRecord::<T>::insert(account.clone(), new_reward);
}

/// Store the received reward for a specific account.
fn store_reward_received(account:T::AccountId,reward:T::Balance){
let earlier_reward = BeneficialRewardRecord::<T>::get(account.clone());
let new_reward = reward + earlier_reward;
BeneficialRewardRecord::<T>::insert(account.clone(), new_reward);
}

/// Compute the reward of the validator
fn calculate_validator_reward(validator_points: u32, era_reward: f64) -> f64 {
fn calculate_validator_era_reward(validator_points: u32, 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);
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.0.3"
version = "1.0.4"
authors = ["5ire Team <[email protected]>"]
description = "5ire chain qa runtime"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion 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: 103,
spec_version: 104,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
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.3"
version = "1.0.4"
authors = ["5ire Team <[email protected]>"]
description = "5ire chain Thunder Testnet runtime"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion 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: 103,
spec_version: 104,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down

0 comments on commit ad99305

Please sign in to comment.