Skip to content

Commit

Permalink
Merge branch 'deployment-0.9.30' into feat/dev-backport-19-12-2023
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Dec 19, 2023
2 parents f859474 + 51b208b commit 6d2eeb4
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 27 deletions.
56 changes: 42 additions & 14 deletions pallets/ddc-payouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) mod mock;
#[cfg(test)]
mod tests;

use ddc_primitives::{ClusterId, DdcEra};
use ddc_primitives::{ClusterId, DdcEra, MILLICENTS};
use ddc_traits::{
cluster::{ClusterCreator as ClusterCreatorType, ClusterVisitor as ClusterVisitorType},
customer::{
Expand All @@ -43,7 +43,7 @@ use frame_support::{
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use sp_runtime::{PerThing, Perquintill};
use sp_runtime::{traits::Convert, PerThing, Perquintill};
use sp_std::prelude::*;

type BatchIndex = u16;
Expand Down Expand Up @@ -96,9 +96,14 @@ pub struct CustomerCharge {
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

pub type VoteScoreOf<T> =
<<T as pallet::Config>::NominatorsAndValidatorsList as frame_election_provider_support::SortedListProvider<
<T as frame_system::Config>::AccountId,
>>::Score;

parameter_types! {
pub MaxBatchesCount: u16 = 1000;
pub MaxDust: u16 = 20000;
pub MaxDust: u128 = MILLICENTS;
pub MaxBatchSize: u16 = 1000;
}

Expand All @@ -124,9 +129,10 @@ pub mod pallet {
type CustomerDepositor: CustomerDepositorType<Self>;
type TreasuryVisitor: PalletVisitorType<Self>;
type ClusterVisitor: ClusterVisitorType<Self>;
type ValidatorList: SortedListProvider<Self::AccountId>;
type NominatorsAndValidatorsList: SortedListProvider<Self::AccountId>;
type ClusterCreator: ClusterCreatorType<Self, BalanceOf<Self>>;
type WeightInfo: WeightInfo;
type VoteScoreToU64: Convert<VoteScoreOf<Self>, u64>;
}

#[pallet::event]
Expand Down Expand Up @@ -230,6 +236,7 @@ pub mod pallet {
ArithmeticOverflow,
NotExpectedClusterState,
BatchSizeIsOutOfBounds,
ScoreRetrievalError,
}

#[pallet::storage]
Expand Down Expand Up @@ -672,6 +679,7 @@ pub mod pallet {
Error::<T>::BatchIndexAlreadyProcessed
);

let max_dust = MaxDust::get().saturated_into::<BalanceOf<T>>();
let mut updated_billing_report = billing_report.clone();
for payee in payees {
let node_reward = get_node_reward::<T>(
Expand Down Expand Up @@ -699,7 +707,7 @@ pub mod pallet {
) - <T as pallet::Config>::Currency::minimum_balance();

if reward > vault_balance {
if reward - vault_balance > MaxDust::get().into() {
if reward - vault_balance > max_dust {
Self::deposit_event(Event::<T>::NotDistributedReward {
cluster_id,
era,
Expand Down Expand Up @@ -775,8 +783,7 @@ pub mod pallet {
})()
.ok_or(Error::<T>::ArithmeticOverflow)?;

if expected_amount_to_reward - billing_report.total_distributed_reward >
MaxDust::get().into()
if expected_amount_to_reward - billing_report.total_distributed_reward > MaxDust::get()
{
Self::deposit_event(Event::<T>::NotDistributedOverallReward {
cluster_id,
Expand Down Expand Up @@ -847,20 +854,41 @@ pub mod pallet {
)
}

fn get_current_exposure_ratios<T: Config>(
) -> Result<Vec<(T::AccountId, Perquintill)>, DispatchError> {
let mut total_score = 0;
let mut individual_scores: Vec<(T::AccountId, u64)> = Vec::new();
for staker_id in T::NominatorsAndValidatorsList::iter() {
let s = T::NominatorsAndValidatorsList::get_score(&staker_id)
.map_err(|_| Error::<T>::ScoreRetrievalError)?;
let score = T::VoteScoreToU64::convert(s);
total_score += score;

individual_scores.push((staker_id, score));
}

let mut result = Vec::new();
for (staker_id, score) in individual_scores {
let ratio = Perquintill::from_rational(score, total_score);
result.push((staker_id, ratio));
}

Ok(result)
}

fn charge_validator_fees<T: Config>(
validators_fee: u128,
vault: &T::AccountId,
) -> DispatchResult {
let amount_to_deduct = validators_fee
.checked_div(T::ValidatorList::count().try_into().unwrap())
.ok_or(Error::<T>::ArithmeticOverflow)?
.saturated_into::<BalanceOf<T>>();
let stakers = get_current_exposure_ratios::<T>()?;

for (staker_id, ratio) in stakers.iter() {
let amount_to_deduct = *ratio * validators_fee;

for validator_account_id in T::ValidatorList::iter() {
<T as pallet::Config>::Currency::transfer(
vault,
&validator_account_id,
amount_to_deduct,
staker_id,
amount_to_deduct.saturated_into::<BalanceOf<T>>(),
ExistenceRequirement::AllowDeath,
)?;
}
Expand Down
21 changes: 17 additions & 4 deletions pallets/ddc-payouts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
traits::{BlakeTwo256, Identity, IdentityLookup},
DispatchError, Perquintill,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -110,8 +110,10 @@ impl crate::pallet::Config for Test {
type CustomerDepositor = TestCustomerDepositor;
type ClusterVisitor = TestClusterVisitor;
type TreasuryVisitor = TestTreasuryVisitor;
type ValidatorList = TestValidatorVisitor<Self>;
type NominatorsAndValidatorsList = TestValidatorVisitor<Self>;
type ClusterCreator = TestClusterCreator;

type VoteScoreToU64 = Identity;
type WeightInfo = ();
}

Expand Down Expand Up @@ -192,6 +194,11 @@ pub const TREASURY_ACCOUNT_ID: AccountId = 888;
pub const VALIDATOR1_ACCOUNT_ID: AccountId = 111;
pub const VALIDATOR2_ACCOUNT_ID: AccountId = 222;
pub const VALIDATOR3_ACCOUNT_ID: AccountId = 333;

pub const VALIDATOR1_SCORE: u64 = 30;
pub const VALIDATOR2_SCORE: u64 = 45;
pub const VALIDATOR3_SCORE: u64 = 25;

pub const PARTIAL_CHARGE: u128 = 100;
pub const USER3_BALANCE: u128 = 1000;

Expand Down Expand Up @@ -268,8 +275,14 @@ impl<T: frame_system::Config> SortedListProvider<T::AccountId> for TestValidator
// nothing to do on insert.
Ok(())
}
fn get_score(_id: &T::AccountId) -> Result<Self::Score, Self::Error> {
unimplemented!()
fn get_score(validator_id: &T::AccountId) -> Result<Self::Score, Self::Error> {
if *validator_id == create_account_id_from_u128::<T>(VALIDATOR1_ACCOUNT_ID) {
Ok(VALIDATOR1_SCORE)
} else if *validator_id == create_account_id_from_u128::<T>(VALIDATOR2_ACCOUNT_ID) {
Ok(VALIDATOR2_SCORE)
} else {
Ok(VALIDATOR3_SCORE)
}
}
fn on_update(_: &T::AccountId, _weight: Self::Score) -> Result<(), Self::Error> {
// nothing to do on update.
Expand Down
Loading

0 comments on commit 6d2eeb4

Please sign in to comment.