From 8957f2f826902339e8dad864c7e5492befc316d3 Mon Sep 17 00:00:00 2001 From: 1xstj <106580853+1xstj@users.noreply.github.com> Date: Mon, 27 Nov 2023 22:03:46 +0000 Subject: [PATCH] add set_permitted_caller --- pallets/jobs/src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++ precompiles/jobs/src/lib.rs | 27 ------------------------ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/pallets/jobs/src/lib.rs b/pallets/jobs/src/lib.rs index 3c9799759..845c519ce 100644 --- a/pallets/jobs/src/lib.rs +++ b/pallets/jobs/src/lib.rs @@ -121,6 +121,8 @@ pub mod module { ValidatorMetadataNotFound, /// Unexpected result provided ResultNotExpectedType, + /// No permission to change permitted caller + NoPermission, } #[pallet::event] @@ -552,5 +554,45 @@ pub mod module { Ok(()) } + + /// Withdraw rewards accumulated by the caller. + /// + /// # Parameters + /// + /// - `origin`: The origin of the call (typically a signed account). + /// + /// # Errors + /// + /// This function can return an error if: + /// + /// - The caller is not authorized. + /// - No rewards are available for the caller. + /// - The reward transfer operation fails. + /// + /// # Details + /// + /// This function allows a caller to withdraw rewards that have been accumulated in their + /// account. + #[pallet::call_index(4)] + #[pallet::weight(T::WeightInfo::withdraw_rewards())] + pub fn set_permitted_caller( + origin: OriginFor, + job_key: JobKey, + job_id: JobId, + new_permitted_caller: T::AccountId, + ) -> DispatchResult { + let caller = ensure_signed(origin)?; + + KnownResults::::try_mutate(job_key, job_id, |job| -> DispatchResult { + let job = job.as_mut().ok_or(Error::::JobNotFound)?; + + // ensure the caller is the current permitted caller + ensure!(job.permitted_caller == Some(caller), Error::::NoPermission); + + job.permitted_caller = Some(new_permitted_caller); + + Ok(()) + }) + } } } diff --git a/precompiles/jobs/src/lib.rs b/precompiles/jobs/src/lib.rs index 26ec50b72..93ad99403 100644 --- a/precompiles/jobs/src/lib.rs +++ b/precompiles/jobs/src/lib.rs @@ -84,31 +84,4 @@ where event.record(handle)?; Ok(hash) } - - /// Clear an unrequested preimage from the runtime storage. - /// - /// Parameters: - /// * hash: The preimage cleared from storage - #[precompile::public("unnotePreimage(bytes32)")] - fn unnote_preimage(handle: &mut impl PrecompileHandle, hash: H256) -> EvmResult { - let event = log1( - handle.context().address, - SELECTOR_LOG_PREIMAGE_UNNOTED, - solidity::encode_arguments(hash), - ); - handle.record_log_costs(&[&event])?; - - let hash: Runtime::Hash = hash - .try_into() - .map_err(|_| RevertReason::custom("H256 is Runtime::Hash").in_field("hash"))?; - let origin = Runtime::AddressMapping::into_account_id(handle.context().caller); - - let call = PreimageCall::::unnote_preimage { hash }; - - >::try_dispatch(handle, Some(origin).into(), call)?; - - event.record(handle)?; - - Ok(()) - } }