From f432231b86477eef287fbf01c6cfa658b2155e91 Mon Sep 17 00:00:00 2001 From: Elden Young <59600396+ytqaljn@users.noreply.github.com> Date: Fri, 10 May 2024 16:29:31 +0800 Subject: [PATCH] refactor: cancel return error event (#348) * refactor: cancle return error event * style: clear useless event --- pallets/tee-worker/src/lib.rs | 148 +++------------------------------- 1 file changed, 13 insertions(+), 135 deletions(-) diff --git a/pallets/tee-worker/src/lib.rs b/pallets/tee-worker/src/lib.rs index 7c6d3330..be3ac688 100644 --- a/pallets/tee-worker/src/lib.rs +++ b/pallets/tee-worker/src/lib.rs @@ -133,6 +133,7 @@ pub mod pallet { attestation_provider: Option, confidence_level: u8, }, + WorkerUpdated { pubkey: WorkerPublicKey, attestation_provider: Option, @@ -141,16 +142,6 @@ pub mod pallet { MinimumCesealVersionChangedTo(u32, u32, u32), - NotBond, - - WrongTeeType, - - CesealAlreadyExists, - - ValidateError, - - BoundedVecError, - CesealBinAdded(H256), CesealBinRemoved(H256), @@ -288,73 +279,6 @@ pub mod pallet { where T: ces_pallet_mq::Config, { - /// Update the TEE Worker MR Enclave Whitelist - /// - /// This function allows the root or superuser to update the whitelist of Trusted Execution Environment (TEE) - /// Worker MR (Measurement and Report) Enclaves. Each MR Enclave represents a specific instance of a TEE worker. - /// By adding an MR Enclave to the whitelist, the user ensures that the associated TEE worker can participate in - /// network activities. - /// - /// Parameters: - /// - `origin`: The origin from which the function is called, representing the user's account. Only the root - /// user is authorized to call this function. - /// - `mr_enclave`: A fixed-size array of 64 bytes representing the MR Enclave of the TEE worker to be added to - /// the whitelist. - - /// Exit a TEE Worker from the Network - /// - /// This function allows a TEE (Trusted Execution Environment) Worker to voluntarily exit from the network. - /// When a TEE Worker exits, it will no longer participate in network activities and will be removed from the - /// list of active TEE Workers. - /// - /// Parameters: - /// - `origin`: The origin from which the function is called, representing the account of the TEE Worker. This - /// should be the controller account of the TEE Worker. - // #[pallet::call_index(2)] - // #[pallet::weight(Weight::zero())] - // pub fn exit( - // origin: OriginFor, - // payload: WorkerAction, - // sig: BoundedVec> - // ) -> DispatchResult { - // ensure_signed(origin)?; - - // if let WorkerAction::Exit(payload) = payload { - // ensure!(sig.len() == 64, Error::::InvalidSignatureLength); - // let sig = - // sp_core::sr25519::Signature::try_from(sig.as_slice()).or(Err(Error::::MalformedSignature))?; - // let encoded_data = payload.encode(); - // let data_to_sign = wrap_content_to_sign(&encoded_data, SignedContentType::EndpointInfo); - // ensure!( - // sp_io::crypto::sr25519_verify(&sig, &data_to_sign, &payload.pubkey), - // Error::::InvalidSignature - // ); - - // ensure!(>::count() > 1, Error::::LastWorker); - // ensure!(>::contains_key(&payload.pubkey), Error::::WorkerNotFound); - - // Workers::::remove(&payload.pubkey); - // WorkerAddedAt::::remove(&payload.pubkey); - // Endpoints::::remove(&payload.pubkey); - - // let mut keyfairys = Keyfairies::::get(); - // ensure!(keyfairys.len() > 1, Error::::CannotRemoveLastKeyfairy); - // keyfairys.retain(|g| *g != payload.pubkey); - // Keyfairies::::put(keyfairys); - - // ensure!( - // Self::check_time_unix(&payload.signing_time), - // Error::::InvalidEndpointSigningTime - // ); - - // Self::deposit_event(Event::::Exit { tee: payload.pubkey }); - // } else { - // return Err(Error::::PayloadError)? - // } - - // Ok(()) - // } - /// Force register a worker with the given pubkey with sudo permission /// /// For test only. @@ -432,41 +356,18 @@ pub mod pallet { T::VerifyCeseal::get(), CesealBinAllowList::::get(), ) - .map_err({ - Self::deposit_event(Event::::ValidateError); - Into::>::into - })?; + .map_err(Into::>::into)?; // Update the registry let pubkey = ceseal_info.pubkey; - - if Workers::::contains_key(&pubkey) { - Self::deposit_event(Event::::CesealAlreadyExists); - return Err(Error::::CesealAlreadyExists)?; - } + ensure!(!Workers::::contains_key(&pubkey), Error::::CesealAlreadyExists); match &ceseal_info.operator { Some(acc) => { - let result = >::bonded(acc).ok_or(Error::::NotBond); - if result.is_err() { - Self::deposit_event(Event::::NotBond); - return Err(Error::::NotBond)?; - } - - // ensure!(ceseal_info.role == WorkerRole::Verifier || ceseal_info.role == WorkerRole::Full, Error::::WrongTeeType); - if ceseal_info.role != WorkerRole::Verifier && ceseal_info.role != WorkerRole::Full { - Self::deposit_event(Event::::WrongTeeType); - return Err(Error::::WrongTeeType)?; - } - }, - - None => { - if ceseal_info.role != WorkerRole::Marker { - Self::deposit_event(Event::::WrongTeeType); - return Err(Error::::WrongTeeType)?; - } - // ensure!(ceseal_info.role == WorkerRole::Marker, Error::::WrongTeeType) + >::bonded(acc).ok_or(Error::::NotBond)?; + ensure!(ceseal_info.role == WorkerRole::Verifier || ceseal_info.role == WorkerRole::Full, Error::::WrongTeeType); }, + None => ensure!(ceseal_info.role == WorkerRole::Marker, Error::::WrongTeeType), }; let worker_info = WorkerInfo { @@ -489,7 +390,7 @@ pub mod pallet { ValidationTypeList::::mutate(|puk_list| -> DispatchResult { puk_list .try_push(pubkey) - .map_err(|_| {Self::deposit_event(Event::::BoundedVecError); Error::::BoundedVecError})?; + .map_err(|_| Error::::BoundedVecError)?; Ok(()) })?; } @@ -529,41 +430,18 @@ pub mod pallet { CesealBinAllowList::::get(), T::NoneAttestationEnabled::get(), ) - .map_err({ - Self::deposit_event(Event::::ValidateError); - Into::>::into - })?; + .map_err(Into::>::into)?; // Update the registry let pubkey = ceseal_info.pubkey; - - if Workers::::contains_key(&pubkey) { - Self::deposit_event(Event::::CesealAlreadyExists); - return Err(Error::::CesealAlreadyExists)?; - } + ensure!(!Workers::::contains_key(&pubkey), Error::::CesealAlreadyExists); match &ceseal_info.operator { Some(acc) => { - let result = >::bonded(acc).ok_or(Error::::NotBond); - if result.is_err() { - Self::deposit_event(Event::::NotBond); - return Err(Error::::NotBond)?; - } - - // ensure!(ceseal_info.role == WorkerRole::Verifier || ceseal_info.role == WorkerRole::Full, Error::::WrongTeeType); - if ceseal_info.role != WorkerRole::Verifier && ceseal_info.role != WorkerRole::Full { - Self::deposit_event(Event::::WrongTeeType); - return Err(Error::::WrongTeeType)?; - } - }, - - None => { - if ceseal_info.role != WorkerRole::Marker { - Self::deposit_event(Event::::WrongTeeType); - return Err(Error::::WrongTeeType)?; - } - // ensure!(ceseal_info.role == WorkerRole::Marker, Error::::WrongTeeType) + >::bonded(acc).ok_or(Error::::NotBond)?; + ensure!(ceseal_info.role == WorkerRole::Verifier || ceseal_info.role == WorkerRole::Full, Error::::WrongTeeType); }, + None => ensure!(ceseal_info.role == WorkerRole::Marker, Error::::WrongTeeType), }; let worker_info = WorkerInfo { @@ -586,7 +464,7 @@ pub mod pallet { ValidationTypeList::::mutate(|puk_list| -> DispatchResult { puk_list .try_push(pubkey) - .map_err(|_| {Self::deposit_event(Event::::BoundedVecError); Error::::BoundedVecError})?; + .map_err(|_| Error::::BoundedVecError)?; Ok(()) })?; }