diff --git a/Cargo.lock b/Cargo.lock index 7261de91..32077067 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6884,6 +6884,7 @@ name = "pallet-scheduler-credit" version = "0.1.0" dependencies = [ "cessp-consensus-rrsc", + "cp-cess-common", "cp-scheduler-credit", "frame-support", "frame-system", diff --git a/crates/scheduler-credit/src/lib.rs b/crates/scheduler-credit/src/lib.rs index 405fba34..81ac4e99 100644 --- a/crates/scheduler-credit/src/lib.rs +++ b/crates/scheduler-credit/src/lib.rs @@ -5,6 +5,15 @@ use frame_support::dispatch::DispatchResult; /// API necessary for Scheduler record ops about credit. pub trait SchedulerCreditCounter { + 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; diff --git a/pallets/audit/src/lib.rs b/pallets/audit/src/lib.rs index 849479d6..7ee46a25 100644 --- a/pallets/audit/src/lib.rs +++ b/pallets/audit/src/lib.rs @@ -575,7 +575,10 @@ pub mod pallet { .rear .checked_sub(challenge_info.miner_snapshot.space_proof_info.front) .ok_or(Error::::Overflow)?; - T::CreditCounter::record_proceed_block_size(&tee_acc, count)?; + + let space = IDLE_SEG_SIZE.checked_mul(count as u128).ok_or(Error::::Overflow)?; + let bond_stash = T::TeeWorkerHandler::get_stash(&tee_acc)?; + T::CreditCounter::increase_point_for_idle_verify(&bond_stash, space)?; Self::deposit_event(Event::::SubmitIdleVerifyResult { tee: tee_acc.clone(), @@ -701,14 +704,8 @@ pub mod pallet { >::insert(&sender, count); } - let count = challenge_info - .miner_snapshot - .service_space - .checked_div(IDLE_SEG_SIZE) - .ok_or(Error::::Overflow)? - .checked_add(1) - .ok_or(Error::::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::::SubmitServiceVerifyResult { tee: tee_acc.clone(), diff --git a/pallets/file-bank/src/lib.rs b/pallets/file-bank/src/lib.rs index 04c0c3f1..ddccf3d6 100755 --- a/pallets/file-bank/src/lib.rs +++ b/pallets/file-bank/src/lib.rs @@ -558,11 +558,23 @@ pub mod pallet { #[pallet::weight(Weight::zero())] pub fn calculate_report( origin: OriginFor, - file_hash: Hash, + tee_sig: TeeRsaSignature, + tag_sig_info: TagSigInfo>, ) -> DispatchResult { let sender = ensure_signed(origin)?; - >::try_mutate(&file_hash, |file_info_opt| -> DispatchResult { + ensure!( + T::TeeWorkerHandler::can_tag(&tag_sig_info.tee_acc), + Error::::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::::VerifyTeeSigFailed); + ensure!(tag_sig_info.miner == sender, Error::::VerifyTeeSigFailed); + + >::try_mutate(&tag_sig_info.file_hash, |file_info_opt| -> DispatchResult { let file_info = file_info_opt.as_mut().ok_or(Error::::NonExistent)?; let now = >::block_number(); let mut count: u128 = 0; @@ -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::::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::::CalculateReport{ miner: sender, file_hash: tag_sig_info.file_hash}); Ok(()) })?; @@ -634,6 +651,12 @@ pub mod pallet { )?; let replace_space = IDLE_SEG_SIZE.checked_mul(count.into()).ok_or(Error::::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::::ReplaceIdleSpace { acc: sender.clone(), space: replace_space }); Ok(()) @@ -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::::IdleSpaceCert{ acc: sender, space: idle_space }); diff --git a/pallets/file-bank/src/types.rs b/pallets/file-bank/src/types.rs index 70945e8c..cb27437e 100755 --- a/pallets/file-bank/src/types.rs +++ b/pallets/file-bank/src/types.rs @@ -125,3 +125,10 @@ pub struct RestoralOrderInfo { pub(super) deadline: BlockNumberFor, } +#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)] +pub struct TagSigInfo { + pub(super) miner: AccountId, + pub(super) file_hash: Hash, + pub(super) tee_acc: AccountId, +} + diff --git a/pallets/scheduler-credit/Cargo.toml b/pallets/scheduler-credit/Cargo.toml index 329dd600..2823ea57 100644 --- a/pallets/scheduler-credit/Cargo.toml +++ b/pallets/scheduler-credit/Cargo.toml @@ -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 } diff --git a/pallets/scheduler-credit/src/lib.rs b/pallets/scheduler-credit/src/lib.rs index 65a84dff..89af4d34 100644 --- a/pallets/scheduler-credit/src/lib.rs +++ b/pallets/scheduler-credit/src/lib.rs @@ -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. @@ -100,6 +121,8 @@ pub mod pallet { #[pallet::error] pub enum Error { Overflow, + + PointOverflow, } #[pallet::storage] @@ -229,6 +252,81 @@ impl Pallet { } impl SchedulerCreditCounter for Pallet { + 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::::PointOverflow)? + .try_into().map_err(|_| Error::::Overflow)?; + if space % TAG_BASE_SIZE != 0 { + base_count = base_count.checked_add(1).ok_or(Error::::PointOverflow)?; + } + + let point: u64 = base_count.checked_mul(TAG_BASE_POINT).ok_or(Error::::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::::PointOverflow)? + .try_into().map_err(|_| Error::::Overflow)?; + if space % CERT_BASE_SIZE != 0 { + base_count = base_count.checked_add(1).ok_or(Error::::PointOverflow)?; + } + + let point: u64 = base_count.checked_mul(CERT_BASE_POINT).ok_or(Error::::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::::PointOverflow)? + .try_into().map_err(|_| Error::::Overflow)?; + if space % IDLE_VERIFY_BASE_SIZE != 0 { + base_count = base_count.checked_add(1).ok_or(Error::::PointOverflow)?; + } + + let point: u64 = base_count.checked_mul(IDLE_VERIFY_BASE_POINT).ok_or(Error::::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::::PointOverflow)? + .try_into().map_err(|_| Error::::Overflow)?; + if space % SERVICE_VERIFY_BASE_SIZE != 0 { + base_count = base_count.checked_add(1).ok_or(Error::::PointOverflow)?; + } + + let point: u64 = base_count.checked_mul(SERVICE_VERIFY_BASE_POINT).ok_or(Error::::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::::PointOverflow)? + .try_into().map_err(|_| Error::::Overflow)?; + if space % REPLACE_BASE_SIZE != 0 { + base_count = base_count.checked_add(1).ok_or(Error::::PointOverflow)?; + } + + let point: u64 = base_count.checked_mul(REPLACE_BASE_POINT).ok_or(Error::::PointOverflow)?; + + Self::record_proceed_block_size(scheduler_id, point)?; + + Ok(()) + } + fn record_proceed_block_size(scheduler_id: &T::AccountId, block_size: u64) -> DispatchResult { Pallet::::record_proceed_block_size(scheduler_id, block_size)?; Ok(()) diff --git a/pallets/sminer/src/helper.rs b/pallets/sminer/src/helper.rs index fd46f26a..7da313b9 100644 --- a/pallets/sminer/src/helper.rs +++ b/pallets/sminer/src/helper.rs @@ -260,7 +260,7 @@ impl Pallet { })?; >::remove(acc); - >::remove(miner); + >::remove(acc); Ok(()) } diff --git a/pallets/tee-worker/src/lib.rs b/pallets/tee-worker/src/lib.rs index e8d5549b..3d3e38b4 100644 --- a/pallets/tee-worker/src/lib.rs +++ b/pallets/tee-worker/src/lib.rs @@ -182,7 +182,7 @@ pub mod pallet { let _ = >::bonded(acc).ok_or(Error::::NotBond)?; ensure!(tee_type == TeeType::Verifier || tee_type == TeeType::Full, Error::::WrongTeeType); }, - None => ensure!(tee_type == TeeType::Certifier || tee_type == TeeType::Marker, Error::::WrongTeeType), + None => ensure!(tee_type == TeeType::Marker, Error::::WrongTeeType), }; ensure!(!TeeWorkerMap::::contains_key(&worker_account), Error::::AlreadyRegistration); @@ -213,6 +213,7 @@ pub mod pallet { ).map_err(Into::>::into)?; let tee_worker_info = TeeWorkerInfo:: { + worker_account: worker_account.clone(), peer_id: peer_id.clone(), bond_stash: stash_account, end_point, @@ -317,6 +318,7 @@ pub mod pallet { let _ = ensure_root(origin)?; let tee_worker_info = TeeWorkerInfo:: { + worker_account: controller_account.clone(), peer_id: peer_id.clone(), bond_stash: stash_account, end_point, @@ -350,7 +352,8 @@ pub trait TeeWorkerHandler { 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; fn punish_scheduler(acc: AccountId) -> DispatchResult; fn get_controller_list() -> Vec; fn get_tee_publickey() -> Result; @@ -379,7 +382,7 @@ impl TeeWorkerHandler<::AccountId> for Pal fn can_cert(acc: &AccountOf) -> bool { if let Ok(tee_info) = TeeWorkerMap::::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; } } @@ -391,8 +394,8 @@ impl TeeWorkerHandler<::AccountId> for Pal TeeWorkerMap::::contains_key(&acc) } - fn is_bonded(acc: AccountOf) -> bool { - if let Ok(tee_info) = TeeWorkerMap::::try_get(&acc) { + fn is_bonded(acc: &AccountOf) -> bool { + if let Ok(tee_info) = TeeWorkerMap::::try_get(acc) { let result = tee_info.bond_stash.is_some(); return result; } @@ -400,6 +403,16 @@ impl TeeWorkerHandler<::AccountId> for Pal false } + fn get_stash(acc: &AccountOf) -> Result, DispatchError> { + let tee_info = TeeWorkerMap::::try_get(acc).map_err(|_| Error::::NonTeeWorker)?; + + if let Some(bond_stash) = tee_info.bond_stash { + return Ok(bond_stash) + } + + Err(Error::::NonTeeWorker.into()) + } + fn punish_scheduler(acc: ::AccountId) -> DispatchResult { let tee_worker = TeeWorkerMap::::try_get(&acc).map_err(|_| Error::::NonTeeWorker)?; if let Some(stash_account) = tee_worker.bond_stash { diff --git a/pallets/tee-worker/src/types.rs b/pallets/tee-worker/src/types.rs index 995bcde4..ccdb2516 100644 --- a/pallets/tee-worker/src/types.rs +++ b/pallets/tee-worker/src/types.rs @@ -4,6 +4,7 @@ use super::*; #[scale_info(skip_type_params(T))] #[codec(mel_bound())] pub struct TeeWorkerInfo { + pub worker_account: AccountOf, pub peer_id: PeerId, pub bond_stash: Option>, pub end_point: EndPoint, @@ -20,7 +21,6 @@ pub struct SgxAttestationReport { #[derive(PartialEq, Eq, Encode, Decode, Clone, RuntimeDebug, MaxEncodedLen, TypeInfo)] pub enum TeeType { Full, - Certifier, Verifier, Marker, } \ No newline at end of file