Skip to content

Commit

Permalink
0.7.6 calculate credit point (#275)
Browse files Browse the repository at this point in the history
* feat: re calculate credit point

* fix: some bug

* fix: fix complete error
  • Loading branch information
ytqaljn authored Dec 14, 2023
1 parent 9a9d4de commit 67577ba
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions crates/scheduler-credit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
use frame_support::dispatch::DispatchResult;
/// API necessary for Scheduler record ops about credit.
pub trait SchedulerCreditCounter<SchedulerCtrlAccountId> {
fn increase_point_for_tag(scheduler_id: &SchedulerCtrlAccountId, space: u128) -> DispatchResult;

fn increase_point_for_cert(scheduler_id: &SchedulerCtrlAccountId, space: u128) -> DispatchResult;

fn increase_point_for_idle_verify(scheduler_id: &SchedulerCtrlAccountId, space: u128) -> DispatchResult;

fn increase_point_for_service_verify(scheduler_id: &SchedulerCtrlAccountId, space: u128) -> DispatchResult;

fn increase_point_for_replace(scheduler_id: &SchedulerCtrlAccountId, space: u128) -> DispatchResult;

/// Records the number of file bytes processed by the scheduler
fn record_proceed_block_size(scheduler_id: &SchedulerCtrlAccountId, block_size: u64) -> DispatchResult;
Expand Down
15 changes: 6 additions & 9 deletions pallets/audit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,10 @@ pub mod pallet {
.rear
.checked_sub(challenge_info.miner_snapshot.space_proof_info.front)
.ok_or(Error::<T>::Overflow)?;
T::CreditCounter::record_proceed_block_size(&tee_acc, count)?;

let space = IDLE_SEG_SIZE.checked_mul(count as u128).ok_or(Error::<T>::Overflow)?;
let bond_stash = T::TeeWorkerHandler::get_stash(&tee_acc)?;
T::CreditCounter::increase_point_for_idle_verify(&bond_stash, space)?;

Self::deposit_event(Event::<T>::SubmitIdleVerifyResult {
tee: tee_acc.clone(),
Expand Down Expand Up @@ -701,14 +704,8 @@ pub mod pallet {
<CountedServiceFailed<T>>::insert(&sender, count);
}

let count = challenge_info
.miner_snapshot
.service_space
.checked_div(IDLE_SEG_SIZE)
.ok_or(Error::<T>::Overflow)?
.checked_add(1)
.ok_or(Error::<T>::Overflow)?;
T::CreditCounter::record_proceed_block_size(&tee_acc, count as u64)?;
let bond_stash = T::TeeWorkerHandler::get_stash(&tee_acc)?;
T::CreditCounter::increase_point_for_idle_verify(&bond_stash, challenge_info.miner_snapshot.service_space)?;

Self::deposit_event(Event::<T>::SubmitServiceVerifyResult {
tee: tee_acc.clone(),
Expand Down
34 changes: 31 additions & 3 deletions pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,23 @@ pub mod pallet {
#[pallet::weight(Weight::zero())]
pub fn calculate_report(
origin: OriginFor<T>,
file_hash: Hash,
tee_sig: TeeRsaSignature,
tag_sig_info: TagSigInfo<AccountOf<T>>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

<File<T>>::try_mutate(&file_hash, |file_info_opt| -> DispatchResult {
ensure!(
T::TeeWorkerHandler::can_tag(&tag_sig_info.tee_acc),
Error::<T>::TeeNoPermission
);
let idle_sig_info_encode = tag_sig_info.encode();
let original = sp_io::hashing::sha2_256(&idle_sig_info_encode);
let tee_puk = T::TeeWorkerHandler::get_tee_publickey()?;

ensure!(verify_rsa(&tee_puk, &original, &tee_sig), Error::<T>::VerifyTeeSigFailed);
ensure!(tag_sig_info.miner == sender, Error::<T>::VerifyTeeSigFailed);

<File<T>>::try_mutate(&tag_sig_info.file_hash, |file_info_opt| -> DispatchResult {
let file_info = file_info_opt.as_mut().ok_or(Error::<T>::NonExistent)?;
let now = <frame_system::Pallet<T>>::block_number();
let mut count: u128 = 0;
Expand All @@ -582,7 +594,12 @@ pub mod pallet {
T::MinerControl::unlock_space_to_service(&sender, unlock_space)?;
T::MinerControl::insert_service_bloom(&sender, hash_list)?;

Self::deposit_event(Event::<T>::CalculateReport{ miner: sender, file_hash: file_hash});
if T::TeeWorkerHandler::is_bonded(&tag_sig_info.tee_acc) {
let bond_stash = T::TeeWorkerHandler::get_stash(&tag_sig_info.tee_acc)?;
T::CreditCounter::increase_point_for_tag(&bond_stash, unlock_space)?;
}

Self::deposit_event(Event::<T>::CalculateReport{ miner: sender, file_hash: tag_sig_info.file_hash});

Ok(())
})?;
Expand Down Expand Up @@ -634,6 +651,12 @@ pub mod pallet {
)?;
let replace_space = IDLE_SEG_SIZE.checked_mul(count.into()).ok_or(Error::<T>::Overflow)?;
T::MinerControl::decrease_replace_space(&sender, replace_space)?;

if T::TeeWorkerHandler::is_bonded(&tee_acc) {
let bond_stash = T::TeeWorkerHandler::get_stash(&tee_acc)?;
T::CreditCounter::increase_point_for_replace(&bond_stash, replace_space)?;
}

Self::deposit_event(Event::<T>::ReplaceIdleSpace { acc: sender.clone(), space: replace_space });

Ok(())
Expand Down Expand Up @@ -711,6 +734,11 @@ pub mod pallet {
tee_sig,
)?;

if T::TeeWorkerHandler::is_bonded(&tee_acc) {
let bond_stash = T::TeeWorkerHandler::get_stash(&tee_acc)?;
T::CreditCounter::increase_point_for_cert(&bond_stash, idle_space)?;
}

T::StorageHandle::add_total_idle_space(idle_space)?;

Self::deposit_event(Event::<T>::IdleSpaceCert{ acc: sender, space: idle_space });
Expand Down
7 changes: 7 additions & 0 deletions pallets/file-bank/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ pub struct RestoralOrderInfo<T: Config> {
pub(super) deadline: BlockNumberFor<T>,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct TagSigInfo<AccountId> {
pub(super) miner: AccountId,
pub(super) file_hash: Hash,
pub(super) tee_acc: AccountId,
}

1 change: 1 addition & 0 deletions pallets/scheduler-credit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ frame-system = { workspace = true }
#lock dependencies
cp-scheduler-credit = { workspace = true }
cessp-consensus-rrsc = { workspace = true }
cp-cess-common = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }
Expand Down
98 changes: 98 additions & 0 deletions pallets/scheduler-credit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,32 @@ use cessp_consensus_rrsc::traits::ValidatorCredits;
use sp_std::{collections::btree_map::BTreeMap, prelude::*};

use cp_scheduler_credit::{SchedulerCreditCounter, SchedulerStashAccountFinder};
use cp_cess_common::*;

pub use pallet::*;

pub type CreditScore = u32;

pub const CERT_BASE_SIZE: u128 = 16 * G_BYTE;

pub const CERT_BASE_POINT: u64 = 4;

pub const TAG_BASE_SIZE: u128 = 16 * M_BYTE;

pub const TAG_BASE_POINT: u64 = 180;

pub const REPLACE_BASE_SIZE: u128 = 64 * M_BYTE;

pub const REPLACE_BASE_POINT: u64 = 1;

pub const IDLE_VERIFY_BASE_SIZE: u128 = 16 * G_BYTE;

pub const IDLE_VERIFY_BASE_POINT: u64 = 1;

pub const SERVICE_VERIFY_BASE_SIZE: u128 = 1 * G_BYTE;

pub const SERVICE_VERIFY_BASE_POINT: u64 = 12;

pub const FULL_CREDIT_SCORE: u32 = 1000;
const LOG_TARGET: &str = "scheduler-credit";
/// The weight of credit value when figure credit score.
Expand Down Expand Up @@ -100,6 +121,8 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
Overflow,

PointOverflow,
}

#[pallet::storage]
Expand Down Expand Up @@ -229,6 +252,81 @@ impl<T: Config> Pallet<T> {
}

impl<T: Config> SchedulerCreditCounter<T::AccountId> for Pallet<T> {
fn increase_point_for_tag(scheduler_id: &T::AccountId, space: u128) -> DispatchResult {
let mut base_count: u64 = space
.checked_div(TAG_BASE_SIZE).ok_or(Error::<T>::PointOverflow)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if space % TAG_BASE_SIZE != 0 {
base_count = base_count.checked_add(1).ok_or(Error::<T>::PointOverflow)?;
}

let point: u64 = base_count.checked_mul(TAG_BASE_POINT).ok_or(Error::<T>::PointOverflow)?;

Self::record_proceed_block_size(scheduler_id, point)?;

Ok(())
}

fn increase_point_for_cert(scheduler_id: &T::AccountId, space: u128) -> DispatchResult {
let mut base_count: u64 = space
.checked_div(CERT_BASE_SIZE).ok_or(Error::<T>::PointOverflow)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if space % CERT_BASE_SIZE != 0 {
base_count = base_count.checked_add(1).ok_or(Error::<T>::PointOverflow)?;
}

let point: u64 = base_count.checked_mul(CERT_BASE_POINT).ok_or(Error::<T>::PointOverflow)?;

Self::record_proceed_block_size(scheduler_id, point)?;

Ok(())
}

fn increase_point_for_idle_verify(scheduler_id: &T::AccountId, space: u128) -> DispatchResult {
let mut base_count: u64 = space
.checked_div(IDLE_VERIFY_BASE_SIZE).ok_or(Error::<T>::PointOverflow)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if space % IDLE_VERIFY_BASE_SIZE != 0 {
base_count = base_count.checked_add(1).ok_or(Error::<T>::PointOverflow)?;
}

let point: u64 = base_count.checked_mul(IDLE_VERIFY_BASE_POINT).ok_or(Error::<T>::PointOverflow)?;

Self::record_proceed_block_size(scheduler_id, point)?;

Ok(())
}

fn increase_point_for_service_verify(scheduler_id: &T::AccountId, space: u128) -> DispatchResult {
let mut base_count: u64 = space
.checked_div(SERVICE_VERIFY_BASE_SIZE).ok_or(Error::<T>::PointOverflow)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if space % SERVICE_VERIFY_BASE_SIZE != 0 {
base_count = base_count.checked_add(1).ok_or(Error::<T>::PointOverflow)?;
}

let point: u64 = base_count.checked_mul(SERVICE_VERIFY_BASE_POINT).ok_or(Error::<T>::PointOverflow)?;

Self::record_proceed_block_size(scheduler_id, point)?;

Ok(())
}

fn increase_point_for_replace(scheduler_id: &T::AccountId, space: u128) -> DispatchResult {
let mut base_count: u64 = space
.checked_div(REPLACE_BASE_SIZE).ok_or(Error::<T>::PointOverflow)?
.try_into().map_err(|_| Error::<T>::Overflow)?;
if space % REPLACE_BASE_SIZE != 0 {
base_count = base_count.checked_add(1).ok_or(Error::<T>::PointOverflow)?;
}

let point: u64 = base_count.checked_mul(REPLACE_BASE_POINT).ok_or(Error::<T>::PointOverflow)?;

Self::record_proceed_block_size(scheduler_id, point)?;

Ok(())
}

fn record_proceed_block_size(scheduler_id: &T::AccountId, block_size: u64) -> DispatchResult {
Pallet::<T>::record_proceed_block_size(scheduler_id, block_size)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion pallets/sminer/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<T: Config> Pallet<T> {
})?;

<RewardMap<T>>::remove(acc);
<PendingReplacements<T>>::remove(miner);
<PendingReplacements<T>>::remove(acc);

Ok(())
}
Expand Down
23 changes: 18 additions & 5 deletions pallets/tee-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub mod pallet {
let _ = <pallet_cess_staking::Pallet<T>>::bonded(acc).ok_or(Error::<T>::NotBond)?;
ensure!(tee_type == TeeType::Verifier || tee_type == TeeType::Full, Error::<T>::WrongTeeType);
},
None => ensure!(tee_type == TeeType::Certifier || tee_type == TeeType::Marker, Error::<T>::WrongTeeType),
None => ensure!(tee_type == TeeType::Marker, Error::<T>::WrongTeeType),
};

ensure!(!TeeWorkerMap::<T>::contains_key(&worker_account), Error::<T>::AlreadyRegistration);
Expand Down Expand Up @@ -213,6 +213,7 @@ pub mod pallet {
).map_err(Into::<Error<T>>::into)?;

let tee_worker_info = TeeWorkerInfo::<T> {
worker_account: worker_account.clone(),
peer_id: peer_id.clone(),
bond_stash: stash_account,
end_point,
Expand Down Expand Up @@ -317,6 +318,7 @@ pub mod pallet {
let _ = ensure_root(origin)?;

let tee_worker_info = TeeWorkerInfo::<T> {
worker_account: controller_account.clone(),
peer_id: peer_id.clone(),
bond_stash: stash_account,
end_point,
Expand Down Expand Up @@ -350,7 +352,8 @@ pub trait TeeWorkerHandler<AccountId> {
fn can_verify(acc: &AccountId) -> bool;
fn can_cert(acc: &AccountId) -> bool;
fn contains_scheduler(acc: AccountId) -> bool;
fn is_bonded(acc: AccountId) -> bool;
fn is_bonded(acc: &AccountId) -> bool;
fn get_stash(acc: &AccountId) -> Result<AccountId, DispatchError>;
fn punish_scheduler(acc: AccountId) -> DispatchResult;
fn get_controller_list() -> Vec<AccountId>;
fn get_tee_publickey() -> Result<Podr2Key, DispatchError>;
Expand Down Expand Up @@ -379,7 +382,7 @@ impl<T: Config> TeeWorkerHandler<<T as frame_system::Config>::AccountId> for Pal

fn can_cert(acc: &AccountOf<T>) -> bool {
if let Ok(tee_info) = TeeWorkerMap::<T>::try_get(acc) {
if TeeType::Certifier == tee_info.tee_type || TeeType::Full == tee_info.tee_type {
if TeeType::Marker == tee_info.tee_type || TeeType::Full == tee_info.tee_type {
return true;
}
}
Expand All @@ -391,15 +394,25 @@ impl<T: Config> TeeWorkerHandler<<T as frame_system::Config>::AccountId> for Pal
TeeWorkerMap::<T>::contains_key(&acc)
}

fn is_bonded(acc: AccountOf<T>) -> bool {
if let Ok(tee_info) = TeeWorkerMap::<T>::try_get(&acc) {
fn is_bonded(acc: &AccountOf<T>) -> bool {
if let Ok(tee_info) = TeeWorkerMap::<T>::try_get(acc) {
let result = tee_info.bond_stash.is_some();
return result;
}

false
}

fn get_stash(acc: &AccountOf<T>) -> Result<AccountOf<T>, DispatchError> {
let tee_info = TeeWorkerMap::<T>::try_get(acc).map_err(|_| Error::<T>::NonTeeWorker)?;

if let Some(bond_stash) = tee_info.bond_stash {
return Ok(bond_stash)
}

Err(Error::<T>::NonTeeWorker.into())
}

fn punish_scheduler(acc: <T as frame_system::Config>::AccountId) -> DispatchResult {
let tee_worker = TeeWorkerMap::<T>::try_get(&acc).map_err(|_| Error::<T>::NonTeeWorker)?;
if let Some(stash_account) = tee_worker.bond_stash {
Expand Down
2 changes: 1 addition & 1 deletion pallets/tee-worker/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::*;
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct TeeWorkerInfo<T: pallet::Config> {
pub worker_account: AccountOf<T>,
pub peer_id: PeerId,
pub bond_stash: Option<AccountOf<T>>,
pub end_point: EndPoint,
Expand All @@ -20,7 +21,6 @@ pub struct SgxAttestationReport {
#[derive(PartialEq, Eq, Encode, Decode, Clone, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub enum TeeType {
Full,
Certifier,
Verifier,
Marker,
}

0 comments on commit 67577ba

Please sign in to comment.