From 2333c2212ea12294682f250919fdb90929252a73 Mon Sep 17 00:00:00 2001 From: Elden Young <59600396+ytqaljn@users.noreply.github.com> Date: Sun, 4 Feb 2024 10:31:48 +0800 Subject: [PATCH] 0.7.6 fix clear tee (#304) * feat: add tee clearing mechanism * fix: add polling cycle --- pallets/audit/src/lib.rs | 8 ++++- pallets/file-bank/src/lib.rs | 10 +++++- pallets/sminer/src/lib.rs | 5 ++- pallets/tee-worker/README.md | 2 +- pallets/tee-worker/src/functions.rs | 37 ++++++++++++++++++++++ pallets/tee-worker/src/lib.rs | 48 +++++++++++++++++++++++++++-- standalone/chain/runtime/src/lib.rs | 4 ++- 7 files changed, 106 insertions(+), 8 deletions(-) diff --git a/pallets/audit/src/lib.rs b/pallets/audit/src/lib.rs index 96452fd3..240710cb 100644 --- a/pallets/audit/src/lib.rs +++ b/pallets/audit/src/lib.rs @@ -178,7 +178,7 @@ pub mod pallet { //Find the consensus of the current block type FindAuthor: FindAuthor; //Judge whether it is the trait of the consensus node - type TeeWorkerHandler: TeeWorkerHandler; + type TeeWorkerHandler: TeeWorkerHandler>; //It is used to increase or decrease the miners' computing power, space, and execute // punishment type MinerControl: MinerControl>; @@ -519,6 +519,9 @@ pub mod pallet { Error::::VerifyTeeSigFailed ); + let now = >::block_number(); + T::TeeWorkerHandler::update_work_block(now, &tee_puk)?; + let idle_result = Self::check_idle_verify_param( idle_result, front, @@ -658,6 +661,9 @@ pub mod pallet { Error::::BloomFilterError, ); + let now = >::block_number(); + T::TeeWorkerHandler::update_work_block(now, &tee_puk)?; + service_prove.verify_result = Some(service_result); if let Some(idle_prove) = &challenge_info.prove_info.idle_prove { diff --git a/pallets/file-bank/src/lib.rs b/pallets/file-bank/src/lib.rs index c5e5afbe..5e9e0f50 100755 --- a/pallets/file-bank/src/lib.rs +++ b/pallets/file-bank/src/lib.rs @@ -116,7 +116,7 @@ pub mod pallet { // Find the consensus of the current block type FindAuthor: FindAuthor; // Used to find out whether the schedule exists - type TeeWorkerHandler: TeeWorkerHandler; + type TeeWorkerHandler: TeeWorkerHandler>; // It is used to control the computing power and space of miners type MinerControl: MinerControl>; // Interface that can generate random seeds @@ -642,6 +642,8 @@ pub mod pallet { let bond_stash = T::TeeWorkerHandler::get_stash(&puk)?; T::CreditCounter::increase_point_for_tag(&bond_stash, FRAGMENT_SIZE * (*count as u128))?; } + let now = >::block_number(); + T::TeeWorkerHandler::update_work_block(now, &puk)?; } Self::deposit_event(Event::::CalculateReport{ miner: sender, file_hash: tag_sig_info.file_hash}); @@ -694,6 +696,9 @@ pub mod pallet { Error::::VerifyTeeSigFailed ); + let now = >::block_number(); + T::TeeWorkerHandler::update_work_block(now, &tee_puk)?; + let sig = sp_core::sr25519::Signature::try_from(tee_sig.as_slice()).or(Err(Error::::MalformedSignature))?; @@ -788,6 +793,9 @@ pub mod pallet { Error::::VerifyTeeSigFailed ); + let now = >::block_number(); + T::TeeWorkerHandler::update_work_block(now, &tee_puk)?; + let sig = sp_core::sr25519::Signature::try_from(tee_sig.as_slice()).or(Err(Error::::MalformedSignature))?; diff --git a/pallets/sminer/src/lib.rs b/pallets/sminer/src/lib.rs index 9dd2d457..52ab2a34 100644 --- a/pallets/sminer/src/lib.rs +++ b/pallets/sminer/src/lib.rs @@ -91,7 +91,7 @@ pub mod pallet { /// The currency trait. type Currency: ReservableCurrency; - type TeeWorkerHandler: TeeWorkerHandler; + type TeeWorkerHandler: TeeWorkerHandler>; /// The treasury's pallet id, used for deriving its sovereign account ID. #[pallet::constant] type FaucetId: Get; @@ -901,6 +901,9 @@ pub mod pallet { Error::::VerifyTeeSigFailed ); + let now = >::block_number(); + T::TeeWorkerHandler::update_work_block(now, &tee_puk)?; + let sig = sp_core::sr25519::Signature::try_from(tee_sig.as_slice()).or(Err(Error::::MalformedSignature))?; diff --git a/pallets/tee-worker/README.md b/pallets/tee-worker/README.md index 571bccef..8b3bac64 100755 --- a/pallets/tee-worker/README.md +++ b/pallets/tee-worker/README.md @@ -35,7 +35,7 @@ pub trait Config: frame_system::Config + sp_std::fmt::Debug { //... - type TeeWorkerHandler: TeeWorkerHandler; + type TeeWorkerHandler: TeeWorkerHandler>; //... } ``` diff --git a/pallets/tee-worker/src/functions.rs b/pallets/tee-worker/src/functions.rs index 7c3a8374..f6cb07e0 100644 --- a/pallets/tee-worker/src/functions.rs +++ b/pallets/tee-worker/src/functions.rs @@ -10,4 +10,41 @@ impl Pallet { return false; } } + + pub fn clear_mission(now: BlockNumberFor) -> Weight { + let mut weight: Weight = Weight::zero(); + + let least = T::AtLeastWorkBlock::get(); + + for (pbk, last_block) in LastWork::::iter() { + weight = weight.saturating_add(T::DbWeight::get().reads(1)); + if last_block.saturating_add(least) < now { + if let Ok(temp_weight) = Self::execute_exit(pbk) { + weight.saturating_add(temp_weight); + } + } + } + + return weight + } + + pub fn execute_exit(pbk: WorkerPublicKey) -> Result { + let mut weight: Weight = Weight::zero(); + + let mut keyfairys = Keyfairies::::get(); + weight = weight.saturating_add(T::DbWeight::get().reads(1)); + ensure!(keyfairys.len() > 1, Error::::CannotRemoveLastKeyfairy); + Workers::::remove(&pbk); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + WorkerAddedAt::::remove(&pbk); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + Endpoints::::remove(&pbk); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + keyfairys.retain(|g| *g != pbk); + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); + Keyfairies::::put(keyfairys); + + Ok(weight) + } } \ No newline at end of file diff --git a/pallets/tee-worker/src/lib.rs b/pallets/tee-worker/src/lib.rs index a3c9c06b..71ccbf6d 100644 --- a/pallets/tee-worker/src/lib.rs +++ b/pallets/tee-worker/src/lib.rs @@ -20,7 +20,7 @@ pub use pallet::*; use scale_info::TypeInfo; use sp_runtime::{DispatchError, RuntimeDebug, SaturatedConversion}; use sp_std::{convert::TryInto, prelude::*}; - +use sp_runtime::Saturating; use cp_cess_common::*; use cp_scheduler_credit::SchedulerCreditCounter; use ces_types::{WorkerPublicKey, MasterPublicKey, WorkerRole}; @@ -107,6 +107,9 @@ pub mod pallet { #[pallet::constant] type NoneAttestationEnabled: Get; + #[pallet::constant] + type AtLeastWorkBlock: Get>; + /// Verify attestation /// /// SHOULD NOT SET TO FALSE ON PRODUCTION!!! @@ -282,6 +285,9 @@ pub mod pallet { #[pallet::storage] pub type Endpoints = StorageMap<_, Twox64Concat, WorkerPublicKey, alloc::string::String>; + #[pallet::storage] + pub type LastWork = StorageMap<_, Twox64Concat, WorkerPublicKey, BlockNumberFor, ValueQuery>; + /// Ceseals whoes version less than MinimumCesealVersion would be forced to quit. #[pallet::storage] pub type MinimumCesealVersion = StorageValue<_, (u32, u32, u32), ValueQuery>; @@ -293,6 +299,21 @@ pub mod pallet { #[pallet::without_storage_info] pub struct Pallet(_); + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(now: BlockNumberFor) -> Weight { + let weight: Weight = Weight::zero(); + + let least = T::AtLeastWorkBlock::get(); + if now % least == 0u32.saturated_into() { + weight + .saturating_add(Self::clear_mission(now)); + } + + weight + } + } + #[pallet::call] impl Pallet where @@ -760,6 +781,18 @@ pub mod pallet { Self::deposit_event(Event::::MinimumCesealVersionChangedTo(major, minor, patch)); Ok(()) } + + #[pallet::call_index(114)] + #[pallet::weight({0})] + pub fn migration_last_work(origin: OriginFor) -> DispatchResult { + T::GovernanceOrigin::ensure_origin(origin)?; + let now = >::block_number(); + for (puk, _) in Workers::::iter() { + >::insert(&puk, now); + } + + Ok(()) + } } impl ces_pallet_mq::MasterPubkeySupplier for Pallet { @@ -881,7 +914,7 @@ pub mod pallet { } } } -pub trait TeeWorkerHandler { +pub trait TeeWorkerHandler { fn can_tag(pbk: &WorkerPublicKey) -> bool; fn can_verify(pbk: &WorkerPublicKey) -> bool; fn can_cert(pbk: &WorkerPublicKey) -> bool; @@ -891,9 +924,10 @@ pub trait TeeWorkerHandler { fn punish_scheduler(pbk: WorkerPublicKey) -> DispatchResult; fn get_pubkey_list() -> Vec; // get_controller_list fn get_master_publickey() -> Result; + fn update_work_block(now: Block, pbk: &WorkerPublicKey) -> DispatchResult; } -impl TeeWorkerHandler> for Pallet { +impl TeeWorkerHandler, BlockNumberFor> for Pallet { fn can_tag(pbk: &WorkerPublicKey) -> bool { if let Ok(tee_info) = Workers::::try_get(pbk) { if WorkerRole::Marker == tee_info.role || WorkerRole::Full == tee_info.role { @@ -968,4 +1002,12 @@ impl TeeWorkerHandler> for Pallet { Ok(pk) } + + fn update_work_block(now: BlockNumberFor, pbk: &WorkerPublicKey) -> DispatchResult { + >::try_mutate(pbk, |last_block| { + *last_block = now; + + Ok(()) + }) + } } diff --git a/standalone/chain/runtime/src/lib.rs b/standalone/chain/runtime/src/lib.rs index c81b738b..a3e02a92 100644 --- a/standalone/chain/runtime/src/lib.rs +++ b/standalone/chain/runtime/src/lib.rs @@ -176,7 +176,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 102, + spec_version: 104, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, @@ -1262,6 +1262,7 @@ parameter_types! { pub const MaxWhitelist: u32 = 200; pub const NoneAttestationEnabled: bool = if cfg!(not(feature = "only-attestation")) { true } else { false }; pub const VerifyCeseal: bool = if cfg!(not(feature = "verify-cesealbin")) { false } else { true }; + pub const AtLeastWorkBlock: BlockNumber = DAYS; } impl pallet_tee_worker::Config for Runtime { @@ -1273,6 +1274,7 @@ impl pallet_tee_worker::Config for Runtime { type WeightInfo = pallet_tee_worker::weights::SubstrateWeight; type CreditCounter = SchedulerCredit; type MaxWhitelist = MaxWhitelist; + type AtLeastWorkBlock = AtLeastWorkBlock; // type AuthorityId = pallet_tee_worker::ed25519::AuthorityId; type LegacyAttestationValidator = pallet_tee_worker::IasValidator; type NoneAttestationEnabled = NoneAttestationEnabled;