Skip to content

Commit

Permalink
Merge pull request #83 from Cerebellum-Network/feature/ddc-validator-…
Browse files Browse the repository at this point in the history
…unit-tests

Feature/ddc validator unit tests
  • Loading branch information
Raid5594 authored Sep 22, 2023
2 parents cb6f457 + 39d8e69 commit 800d998
Show file tree
Hide file tree
Showing 3 changed files with 436 additions and 18 deletions.
5 changes: 4 additions & 1 deletion pallets/ddc-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ pub mod pallet {
BadState,
/// Current era not set during runtime
DDCEraNotSet,
/// Bucket with specified id doesn't exist
BucketDoesNotExist,
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -532,7 +534,8 @@ pub mod pallet {
let mut total_charged = BalanceOf::<T>::zero();

for bucket_details in paying_accounts.iter() {
let bucket: Bucket<T::AccountId> = Self::buckets(bucket_details.bucket_id).unwrap();
let bucket: Bucket<T::AccountId> = Self::buckets(bucket_details.bucket_id)
.ok_or(Error::<T>::BucketDoesNotExist)?;
let content_owner = bucket.owner_id;
let amount = bucket_details.amount * pricing.saturated_into::<BalanceOf<T>>();

Expand Down
44 changes: 38 additions & 6 deletions pallets/ddc-validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ pub mod pallet {
stakers_points: Vec<(T::AccountId, u64)>,
) -> DispatchResult {
let ddc_valitor_key = ensure_signed(origin)?;
let mut rewards_counter = 0;

ensure!(
DDCValidatorToStashKeys::<T>::contains_key(&ddc_valitor_key),
Expand All @@ -488,18 +489,21 @@ pub mod pallet {
<ddc_staking::pallet::ErasEdgesRewardPointsPerNode<T>>::mutate(
&staker,
|current_reward_points| {
let rewards =
let rewards: ddc_staking::EraRewardPointsPerNode =
ddc_staking::EraRewardPointsPerNode { era, points };
current_reward_points.push(rewards);
},
);
RewardPointsSetForNode::<T>::insert(era, staker, true);
rewards_counter += 1;
}
}
}
});

Self::deposit_event(Event::<T>::EraRewardPoints(era, stakers_points));
if rewards_counter > 0 {
Self::deposit_event(Event::<T>::EraRewardPoints(era, stakers_points));
}

Ok(())
}
Expand Down Expand Up @@ -911,25 +915,45 @@ pub mod pallet {
return Err("signing key not set")
}
// ToDo: replace local call by a call from `ddc-staking` pallet
let _tx_res: Option<(frame_system::offchain::Account<T>, Result<(), ()>)> =
let tx_res: Option<(frame_system::offchain::Account<T>, Result<(), ()>)> =
signer.send_signed_transaction(|_account| {
Call::charge_payments_content_owners {
paying_accounts: final_payments.clone(),
}
});

match tx_res {
Some((acc, Ok(()))) =>
log::debug!("[{:?}]: submit transaction success.", acc.id),
Some((acc, Err(e))) => log::debug!(
"[{:?}]: submit transaction failure. Reason: {:?}",
acc.id,
e
),
None => log::debug!("submit transaction failure."),
}

let final_res = dac::get_final_decision(validations_res);

let signer = Self::get_signer().unwrap();

let _tx_res =
let tx_res =
signer.send_signed_transaction(|_acct| Call::set_validation_decision {
era: current_ddc_era - 1,
cdn_node: utils::string_to_account::<T>(edge.clone()),
validation_decision: final_res.clone(),
});

log::debug!("final_res: {:?}", final_res);
match tx_res {
Some((acc, Ok(()))) =>
log::debug!("[{:?}]: submit transaction success.", acc.id),
Some((acc, Err(e))) => log::debug!(
"[{:?}]: submit transaction failure. Reason: {:?}",
acc.id,
e
),
None => log::debug!("submit transaction failure."),
}

cdn_nodes_reward_points.push((
utils::string_to_account::<T>(final_res.edge),
Expand All @@ -942,11 +966,19 @@ pub mod pallet {

// ToDo: replace local call by a call from `ddc-staking` pallet
if cdn_nodes_reward_points.len() > 0 {
let _tx_res =
let tx_res =
signer.send_signed_transaction(|_account| Call::set_era_reward_points {
era: current_ddc_era - 1,
stakers_points: cdn_nodes_reward_points.clone(),
});

match tx_res {
Some((acc, Ok(()))) =>
log::debug!("[{:?}]: submit transaction success.", acc.id),
Some((acc, Err(e))) =>
log::debug!("[{:?}]: submit transaction failure. Reason: {:?}", acc.id, e),
None => log::debug!("submit transaction failure."),
}
}

Ok(())
Expand Down
Loading

0 comments on commit 800d998

Please sign in to comment.