Skip to content

Commit

Permalink
0.7.6 fix clear tee (#304)
Browse files Browse the repository at this point in the history
* feat: add tee clearing mechanism

* fix: add polling cycle
  • Loading branch information
ytqaljn authored Feb 4, 2024
1 parent 9401205 commit 2333c22
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 8 deletions.
8 changes: 7 additions & 1 deletion pallets/audit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub mod pallet {
//Find the consensus of the current block
type FindAuthor: FindAuthor<Self::AccountId>;
//Judge whether it is the trait of the consensus node
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId>;
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId, BlockNumberFor<Self>>;
//It is used to increase or decrease the miners' computing power, space, and execute
// punishment
type MinerControl: MinerControl<Self::AccountId, BlockNumberFor<Self>>;
Expand Down Expand Up @@ -519,6 +519,9 @@ pub mod pallet {
Error::<T>::VerifyTeeSigFailed
);

let now = <frame_system::Pallet<T>>::block_number();
T::TeeWorkerHandler::update_work_block(now, &tee_puk)?;

let idle_result = Self::check_idle_verify_param(
idle_result,
front,
Expand Down Expand Up @@ -658,6 +661,9 @@ pub mod pallet {
Error::<T>::BloomFilterError,
);

let now = <frame_system::Pallet<T>>::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 {
Expand Down
10 changes: 9 additions & 1 deletion pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub mod pallet {
// Find the consensus of the current block
type FindAuthor: FindAuthor<Self::AccountId>;
// Used to find out whether the schedule exists
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId>;
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId, BlockNumberFor<Self>>;
// It is used to control the computing power and space of miners
type MinerControl: MinerControl<Self::AccountId, BlockNumberFor<Self>>;
// Interface that can generate random seeds
Expand Down Expand Up @@ -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 = <frame_system::Pallet<T>>::block_number();
T::TeeWorkerHandler::update_work_block(now, &puk)?;
}

Self::deposit_event(Event::<T>::CalculateReport{ miner: sender, file_hash: tag_sig_info.file_hash});
Expand Down Expand Up @@ -694,6 +696,9 @@ pub mod pallet {
Error::<T>::VerifyTeeSigFailed
);

let now = <frame_system::Pallet<T>>::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::<T>::MalformedSignature))?;

Expand Down Expand Up @@ -788,6 +793,9 @@ pub mod pallet {
Error::<T>::VerifyTeeSigFailed
);

let now = <frame_system::Pallet<T>>::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::<T>::MalformedSignature))?;

Expand Down
5 changes: 4 additions & 1 deletion pallets/sminer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub mod pallet {
/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;

type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId>;
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId, BlockNumberFor<Self>>;
/// The treasury's pallet id, used for deriving its sovereign account ID.
#[pallet::constant]
type FaucetId: Get<PalletId>;
Expand Down Expand Up @@ -901,6 +901,9 @@ pub mod pallet {
Error::<T>::VerifyTeeSigFailed
);

let now = <frame_system::Pallet<T>>::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::<T>::MalformedSignature))?;

Expand Down
2 changes: 1 addition & 1 deletion pallets/tee-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait Config:
frame_system::Config + sp_std::fmt::Debug
{
//...
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId>;
type TeeWorkerHandler: TeeWorkerHandler<Self::AccountId, BlockNumberFor<Self>>;
//...
}
```
Expand Down
37 changes: 37 additions & 0 deletions pallets/tee-worker/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,41 @@ impl<T: Config> Pallet<T> {
return false;
}
}

pub fn clear_mission(now: BlockNumberFor<T>) -> Weight {
let mut weight: Weight = Weight::zero();

let least = T::AtLeastWorkBlock::get();

for (pbk, last_block) in LastWork::<T>::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<Weight, DispatchError> {
let mut weight: Weight = Weight::zero();

let mut keyfairys = Keyfairies::<T>::get();
weight = weight.saturating_add(T::DbWeight::get().reads(1));
ensure!(keyfairys.len() > 1, Error::<T>::CannotRemoveLastKeyfairy);
Workers::<T>::remove(&pbk);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
WorkerAddedAt::<T>::remove(&pbk);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
Endpoints::<T>::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::<T>::put(keyfairys);

Ok(weight)
}
}
48 changes: 45 additions & 3 deletions pallets/tee-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -107,6 +107,9 @@ pub mod pallet {
#[pallet::constant]
type NoneAttestationEnabled: Get<bool>;

#[pallet::constant]
type AtLeastWorkBlock: Get<BlockNumberFor<Self>>;

/// Verify attestation
///
/// SHOULD NOT SET TO FALSE ON PRODUCTION!!!
Expand Down Expand Up @@ -282,6 +285,9 @@ pub mod pallet {
#[pallet::storage]
pub type Endpoints<T: Config> = StorageMap<_, Twox64Concat, WorkerPublicKey, alloc::string::String>;

#[pallet::storage]
pub type LastWork<T: Config> = StorageMap<_, Twox64Concat, WorkerPublicKey, BlockNumberFor<T>, ValueQuery>;

/// Ceseals whoes version less than MinimumCesealVersion would be forced to quit.
#[pallet::storage]
pub type MinimumCesealVersion<T: Config> = StorageValue<_, (u32, u32, u32), ValueQuery>;
Expand All @@ -293,6 +299,21 @@ pub mod pallet {
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(now: BlockNumberFor<T>) -> 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<T: Config> Pallet<T>
where
Expand Down Expand Up @@ -760,6 +781,18 @@ pub mod pallet {
Self::deposit_event(Event::<T>::MinimumCesealVersionChangedTo(major, minor, patch));
Ok(())
}

#[pallet::call_index(114)]
#[pallet::weight({0})]
pub fn migration_last_work(origin: OriginFor<T>) -> DispatchResult {
T::GovernanceOrigin::ensure_origin(origin)?;
let now = <frame_system::Pallet<T>>::block_number();
for (puk, _) in Workers::<T>::iter() {
<LastWork<T>>::insert(&puk, now);
}

Ok(())
}
}

impl<T: Config> ces_pallet_mq::MasterPubkeySupplier for Pallet<T> {
Expand Down Expand Up @@ -881,7 +914,7 @@ pub mod pallet {
}
}
}
pub trait TeeWorkerHandler<AccountId> {
pub trait TeeWorkerHandler<AccountId, Block> {
fn can_tag(pbk: &WorkerPublicKey) -> bool;
fn can_verify(pbk: &WorkerPublicKey) -> bool;
fn can_cert(pbk: &WorkerPublicKey) -> bool;
Expand All @@ -891,9 +924,10 @@ pub trait TeeWorkerHandler<AccountId> {
fn punish_scheduler(pbk: WorkerPublicKey) -> DispatchResult;
fn get_pubkey_list() -> Vec<WorkerPublicKey>; // get_controller_list
fn get_master_publickey() -> Result<MasterPublicKey, DispatchError>;
fn update_work_block(now: Block, pbk: &WorkerPublicKey) -> DispatchResult;
}

impl<T: Config> TeeWorkerHandler<AccountOf<T>> for Pallet<T> {
impl<T: Config> TeeWorkerHandler<AccountOf<T>, BlockNumberFor<T>> for Pallet<T> {
fn can_tag(pbk: &WorkerPublicKey) -> bool {
if let Ok(tee_info) = Workers::<T>::try_get(pbk) {
if WorkerRole::Marker == tee_info.role || WorkerRole::Full == tee_info.role {
Expand Down Expand Up @@ -968,4 +1002,12 @@ impl<T: Config> TeeWorkerHandler<AccountOf<T>> for Pallet<T> {

Ok(pk)
}

fn update_work_block(now: BlockNumberFor<T>, pbk: &WorkerPublicKey) -> DispatchResult {
<LastWork<T>>::try_mutate(pbk, |last_block| {
*last_block = now;

Ok(())
})
}
}
4 changes: 3 additions & 1 deletion standalone/chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -1273,6 +1274,7 @@ impl pallet_tee_worker::Config for Runtime {
type WeightInfo = pallet_tee_worker::weights::SubstrateWeight<Runtime>;
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;
Expand Down

0 comments on commit 2333c22

Please sign in to comment.