Keeper assignment and release in RanDAO realisation
Keeper assignment
Choice algorithm
The algorithm for a RanDAO-based choice of a next keeper is encoded in the _assignNextKeeper function. We outline it here
function _assignNextKeeper(bytes32 jobKey_) internal {
//get the randao realisation for the current block, stored in the block.difficulty
uint256 pseudoRandom = _getPseudoRandom();
//get the cardinality of the keeper set so that the index may be taken modulo it
uint256 totalActiveKeepers = activeKeepers.length();
//get the minimal CVP stake demanded of the keeper by the job
uint256 _jobMinKeeperCvp = jobMinKeeperCvp[jobKey_];
//obtain the index by taking the pseudorandom input, adding the job key so that variance in chosen keeper indices is observed within any single block, and mapping the result into the keeper index set by taking the modulo operation
//the resultant variable index is the index of the keeper selected for execution
uint256 index;
unchecked {
index = ((pseudoRandom + uint256(jobKey_)) % totalActiveKeepers);
}
//iterate over all the keepers, starting with the randomly selected one, until a keeper admissible for the job at hand is found or all gas has been expended
while (true) {
//effectively modulo operation
if (index >= totalActiveKeepers) {
index = 0;
}
//obtain the ID of the next keeper by the index selected
uint256 _nextExecutionKeeperId = activeKeepers.at(index);
//required stake is set to be the global minimal demanded CVP unless the job has a specification of its own
uint256 requiredStake = _jobMinKeeperCvp > 0 ? _jobMinKeeperCvp : minKeeperCvp;
//load the keeper object at the selected ID
Keeper memory keeper = keepers[_nextExecutionKeeperId];
//if the keeper is admissible, i.e. active and has sufficient stake, then assign him as the next keeper of the job and terminate the passage
if (keeper.isActive && keeper.cvpStake >= requiredStake) {
jobNextKeeperId[jobKey_] = _nextExecutionKeeperId;
keeperLocksByJob[_nextExecutionKeeperId].add(jobKey_);
emit KeeperJobLock(_nextExecutionKeeperId, jobKey_);
return;
}
index += 1;
}
}
}Calling the internal function of keeper assignment
Exogeneously
Exogeneous manual keeper assignment is available to the job(s) owner if needed:
Conditionally (on job credits)
Of more interest is the wrapped conditional keeper assignment, since we do not wish keepers to be assigned to jobs with below-minimum resources with which to pay them. This is handled by the internal function
Cases in which conditional assignment is invoked
The conditional keeper assignment happens when:
The job config is updated, with the credits source having changed or a previously inactive job activating
Credits are deposited to a Job (via the overridden hook
_afterDepositJobCredits(bytes32 jobKey)invoked in functiondepositJobCredits(bytes32 jobKey_)in the core contract)
Cases in which unconditional assignment is invoked
Unconditional keeper assignment happens when:
Job execution is successful, and the old keeper needs to be swapped for a new one (inside the
function _afterExecutionSucceeded(bytes32 jobKey, uint256 actualKeeperId_, uint256 binJob_) internalhook).After a job is registered (inside the
function _afterRegisterJob(bytes32 jobKey) internalhook)
Keeper release
Technical implementation of releasing a keeper
Keeper release is performed by the internal function _releaseKeeper(bytes32 jobKey_, uint256 keeperId_) and consists of clearing the Job parameters pertaining to the next keeper and possible slashing and removing the job from the keeper's pending pool:
Calling the keeper release function
We need to exercise utmost care in calling such function so that it does not provide malicious keepers with a way to escape punishment.
Firstly, there is a defined conditional release on insufficiency of Job credits, wrapped into a function releaseKeeperIfRequired, itself wrapping releaseKeeperIfRequiredBinJob. Since releaseKeeperIfRequired is a simple wrapping of a call to releaseKeeperIfRequiredBinJob with a freshly read binary job representation passed as an argument, we only describe the latter function.
This function is called when:
Assigning a keeper (in keeper assignment, a check of
_releaseKeeperIfRequiredBinJob(with thecheckAlreadyReleasedflag passed asFalse) beingFalseensures no keeper is assigned for an job with too few tokens)Job credits are withdrawn (via the hook
_afterWithdrawJobCredits(bytes32 jobKey_) internal)releaseJobis called by a keeper admin (the check happens before any slashing checks and is the only way for a job to be released without satisfying these checks, e.g. when the admissibility period is not over yet, and the keeper is technically locked by the possibility of being slashed, he still can be released from his duty if the job does not meet the minimum credit demand)The job config is updated, with the credits source having changed, and
_assignNextKeeperIfRequiredreturningFalse.
Unwrapped _releaseKeeperIfRequiredBinJob is called when:
Assigning a new keeper;
checkAlreadyReleasedis passed asFalse, so this has the meaning of exiting the keeper assignment process when the job/owner does not have sufficient tokens.
Unconditional release is called when:
The job owner calls
releaseJobThe keeper admin calls
releaseJob, the Job has sufficient (from the Agent's standpoint) credits, and the admissibility period of slashing has elapsedThe keeper is disabled
After execution of the job being automated reverts
The job is deactivated
After execution of the job being automated succeeds
Last updated
Was this helpful?