Skip to content

Commit

Permalink
add set_permitted_caller
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Nov 27, 2023
1 parent 03f1626 commit 8957f2f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
42 changes: 42 additions & 0 deletions pallets/jobs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub mod module {
ValidatorMetadataNotFound,
/// Unexpected result provided
ResultNotExpectedType,
/// No permission to change permitted caller
NoPermission,
}

#[pallet::event]
Expand Down Expand Up @@ -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<T>,
job_key: JobKey,
job_id: JobId,
new_permitted_caller: T::AccountId,
) -> DispatchResult {
let caller = ensure_signed(origin)?;

KnownResults::<T>::try_mutate(job_key, job_id, |job| -> DispatchResult {
let job = job.as_mut().ok_or(Error::<T>::JobNotFound)?;

// ensure the caller is the current permitted caller
ensure!(job.permitted_caller == Some(caller), Error::<T>::NoPermission);

job.permitted_caller = Some(new_permitted_caller);

Ok(())
})
}
}
}
27 changes: 0 additions & 27 deletions precompiles/jobs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Runtime>::unnote_preimage { hash };

<RuntimeHelper<Runtime>>::try_dispatch(handle, Some(origin).into(), call)?;

event.record(handle)?;

Ok(())
}
}

0 comments on commit 8957f2f

Please sign in to comment.