The bonded caller pool of the Ethereum Alarm Clock: callers post a bond, take turns being the designated executor, and forfeit it if they miss.
Historical Significance
The Ethereum Alarm Clock answered a problem the EVM has by design: nothing on chain runs unless an account sends a transaction, so a contract cannot wake itself at a future block. Piper Merriam's answer was to pay whoever does send it. A scheduler holds the requester's deposit, anyone may execute the call once its block arrives, and the executor is reimbursed for gas plus a margin. That shape, a public bounty for executing someone else's scheduled work, backed by bonded callers who get first refusal, is the one keeper networks settled on later.
Context
The Alarm Clock was built on mainnet through the autumn of 2015, and each revision was deployed as a complete new set rather than upgraded in place, so several generations of the service sit on chain side by side. The code is Frontier Solidity: no events are emitted, the log statements are commented out, and a failed check signals an error by reading past the end of an empty array so the transaction consumes its gas and reverts.
Key Facts
Description
CallerPool decides who gets first refusal on executing a scheduled call in the Ethereum Alarm Clock. A caller deposits a bond, joins the pool, and from then on each call's grace period is divided into four-block windows. The pool picks a starting offset from the call key and steps through its members one window at a time, so a different caller is designated in each window; the last two windows are open to anyone.
A caller who lets their window pass forfeits the minimum bond to whoever executes the call instead. The minimum bond is not a fixed number: it is the current gas price multiplied by the block gas limit, so it tracks what executing a call actually costs. Joining and leaving take effect in a later generation of the pool rather than at once, which keeps the membership frozen for longer than any call's grace period and stops the designated caller changing under a call already in flight.
The pool is created by the scheduler's constructor and accepts pool accounting calls only from that scheduler.
Source Verified
Sourcify partial match (chain 1, address 0xd3b727dc4F38F9FCD2059Ca2cB1B3451A20206A9). Compiled with solc 0.1.3+commit.028f561d, optimizer off, contract CallerPool.
Heuristic Analysis
The following characteristics were detected through bytecode analysis and may not be accurate.
Frontier Era
The initial release of Ethereum. A bare-bones implementation for technical users.
Bytecode Overview
Verified Source Available
This contract has verified source code.
View Verification ProofShow source code (Solidity)
// Recovered by EthereumHistory (ethereumhistory.com) -sw
contract Relay {
address operator;
function Relay() {
operator = msg.sender;
}
function relayCall(address contractAddress, bytes4 abiSignature, bytes data) public returns (bool) {
if (msg.sender != operator) {
__throw();
}
return contractAddress.call(abiSignature, data);
}
function __throw() internal {
int[] x;
x[1];
}
}
contract CallerPoolAlarmAPI {
/*
* Abstract contract for the caller pool to be able to access the
* Alarm api.
*/
function getCallGracePeriod(bytes32 callKey) public returns (uint);
function getCallTargetBlock(bytes32 callKey) public returns (uint);
}
contract CallerPool {
address operator;
CallerPoolAlarmAPI alarm;
function CallerPool() {
operator = msg.sender;
alarm = CallerPoolAlarmAPI(operator);
}
/*
* Caller bonding
*/
mapping (address => uint) public callerBonds;
function getMinimumBond() public returns (uint) {
return tx.gasprice * block.gaslimit;
}
function _deductFromBond(address callerAddress, uint value) internal {
/*
* deduct funds from a bond value without risk of an
* underflow.
*/
if (value > callerBonds[callerAddress]) {
// Prevent Underflow.
__throw();
}
callerBonds[callerAddress] -= value;
}
function _addToBond(address callerAddress, uint value) internal {
/*
* Add funds to a bond value without risk of an
* overflow.
*/
if (callerBonds[callerAddress] + value < callerBonds[callerAddress]) {
// Prevent Overflow
__throw();
}
callerBonds[callerAddress] += value;
}
function depositBond() public {
_addToBond(msg.sender, msg.value);
}
function withdrawBond(uint value) public {
/*
* Only if you are not in either of the current call pools.
*/
if (isInAnyPool(msg.sender)) {
// Prevent underflow
if (value > callerBonds[msg.sender]) {
__throw();
}
// Don't allow withdrawl if this would drop the bond
// balance below the minimum.
if (callerBonds[msg.sender] - value < getMinimumBond()) {
return;
}
}
_deductFromBond(msg.sender, value);
if (!msg.sender.send(value)) {
// Potentially sending money to a contract that
// has a fallback function. So instead, try
// tranferring the funds with the call api.
if (!msg.sender.call.gas(msg.gas).value(value)()) {
// Revert the entire transaction. No
// need to destroy the funds.
__throw();
}
}
}
function() {
/*
* Fallback function that allows depositing bond funds just by
* sending a transaction.
*/
_addToBond(msg.sender, msg.value);
}
/*
* API used by Alarm service
*/
function getDesignatedCaller(bytes32 callKey, uint targetBlock, uint8 gracePeriod, uint blockNumber) public returns (address) {
/*
* Returns the caller from the current call pool who is
* designated as the executor of this call.
*/
if (blockNumber < targetBlock || blockNumber > targetBlock + gracePeriod) {
// blockNumber not within call window.
return 0x0;
}
// Pool used is based on the starting block for the call. This
// allows us to know that the pool cannot change for at least
// POOL_FREEZE_NUM_BLOCKS which is kept greater than the max
// grace period.
uint poolNumber = getPoolKeyForBlock(targetBlock);
if (poolNumber == 0) {
// No pool currently in operation.
return 0x0;
}
var pool = callerPools[poolNumber];
uint numWindows = gracePeriod / 4;
uint blockWindow = (blockNumber - targetBlock) / 4;
if (blockWindow + 2 > numWindows) {
// We are within the free-for-all period.
return 0x0;
}
uint offset = uint(callKey) % pool.length;
return pool[(offset + blockWindow) % pool.length];
}
//event AwardedMissedBlockBonus(address indexed fromCaller, address indexed toCaller, uint indexed poolNumber, bytes32 callKey, uint blockNumber, uint bonusAmount);
function _doBondBonusTransfer(address fromCaller, address toCaller) internal returns (uint) {
uint bonusAmount = getMinimumBond();
uint bondBalance = callerBonds[fromCaller];
// If the bond balance is lower than the award
// balance, then adjust the reward amount to
// match the bond balance.
if (bonusAmount > bondBalance) {
bonusAmount = bondBalance;
}
// Transfer the funds fromCaller => toCaller
_deductFromBond(fromCaller, bonusAmount);
_addToBond(toCaller, bonusAmount);
return bonusAmount;
}
function awardMissedBlockBonus(address toCaller, bytes32 callKey, uint targetBlock, uint8 gracePeriod) public {
if (msg.sender != operator) {
return;
}
uint poolNumber = getPoolKeyForBlock(targetBlock);
var pool = callerPools[poolNumber];
uint i;
uint bonusAmount;
address fromCaller;
uint numWindows = gracePeriod / 4;
uint blockWindow = (block.number - targetBlock) / 4;
// Check if we are within the free-for-all period. If so, we
// award from all pool members.
if (blockWindow + 2 > numWindows) {
address firstCaller = getDesignatedCaller(callKey, targetBlock, gracePeriod, targetBlock);
for (i = targetBlock; i <= targetBlock + gracePeriod; i += 4) {
fromCaller = getDesignatedCaller(callKey, targetBlock, gracePeriod, i);
if (fromCaller == firstCaller && i != targetBlock) {
// We have already gone through all of
// the pool callers so we should break
// out of the loop.
break;
}
if (fromCaller == toCaller) {
continue;
}
bonusAmount = _doBondBonusTransfer(fromCaller, toCaller);
// Log the bonus was awarded.
//AwardedMissedBlockBonus(fromCaller, toCaller, poolNumber, callKey, block.number, bonusAmount);
}
return;
}
// Special case for single member and empty pools
if (pool.length < 2) {
return;
}
// Otherwise the award comes from the previous caller.
for (i = 0; i < pool.length; i++) {
// Find where the member is in the pool and
// award from the previous pool members bond.
if (pool[i] == toCaller) {
fromCaller = pool[(i + pool.length - 1) % pool.length];
bonusAmount = _doBondBonusTransfer(fromCaller, toCaller);
// Log the bonus was awarded.
//AwardedMissedBlockBonus(fromCaller, toCaller, poolNumber, callKey, block.number, bonusAmount);
// Remove the caller from the next pool.
if (getNextPoolKey() == 0) {
// This is the first address to modify the
// current pool so we need to setup the next
// pool.
_initiateNextPool();
}
_removeFromPool(fromCaller, getNextPoolKey());
return;
}
}
}
/*
* Caller Pool Management
*/
uint[] public poolHistory;
mapping (uint => address[]) callerPools;
function getPoolKeyForBlock(uint blockNumber) public returns (uint) {
if (poolHistory.length == 0) {
return 0;
}
for (uint i = 0; i < poolHistory.length; i++) {
uint poolStartBlock = poolHistory[poolHistory.length - i - 1];
if (poolStartBlock <= blockNumber) {
return poolStartBlock;
}
}
return 0;
}
function getActivePoolKey() public returns (uint) {
return getPoolKeyForBlock(block.number);
}
function getPoolSize(uint poolKey) returns (uint) {
return callerPools[poolKey].length;
}
function getNextPoolKey() public returns (uint) {
if (poolHistory.length == 0) {
return 0;
}
uint latestPool = poolHistory[poolHistory.length - 1];
if (latestPool > block.number) {
return latestPool;
}
return 0;
}
function isInAnyPool(address callerAddress) public returns (bool) {
/*
* Returns boolean whether the `callerAddress` is in either
* the current active pool or the next pool.
*/
return isInPool(msg.sender, getActivePoolKey()) || isInPool(msg.sender, getNextPoolKey());
}
function isInPool(address callerAddress, uint poolNumber) public returns (bool) {
/*
* Returns boolean whether the `callerAddress` is in the
* poolNumber.
*/
if (poolNumber == 0 ) {
// Nobody can be in pool 0
return false;
}
var pool = callerPools[poolNumber];
// Nobody is in the pool.
if (pool.length == 0) {
return false;
}
for (uint i = 0; i < pool.length; i++) {
// Address is in the pool and thus is allowed to exit.
if (pool[i] == callerAddress) {
return true;
}
}
return false;
}
// Ten minutes into the future.
uint constant POOL_FREEZE_NUM_BLOCKS = 256;
//uint constant POOL_FREEZE_NUM_BLOCKS = 40;
function getPoolFreezeDuration() public returns (uint) {
return POOL_FREEZE_NUM_BLOCKS;
}
function getPoolMinimumLength() public returns (uint) {
return 2 * POOL_FREEZE_NUM_BLOCKS;
}
function canEnterPool(address callerAddress) public returns (bool) {
/*
* Returns boolean whether `callerAddress` is allowed to enter
* the next pool (which may or may not already have been
* created.
*/
// Not allowed to join if you are in either the current
// active pool or the next pool.
if (isInAnyPool(callerAddress)) {
return false;
}
// Next pool begins within the POOL_FREEZE_NUM_BLOCKS grace
// period so no changes are allowed.
if (getNextPoolKey() != 0 && block.number >= (getNextPoolKey() - POOL_FREEZE_NUM_BLOCKS)) {
return false;
}
// Account bond balance is too low.
if (callerBonds[callerAddress] < getMinimumBond()) {
return false;
}
return true;
}
function canExitPool(address callerAddress) public returns (bool) {
/*
* Returns boolean whether `callerAddress` is allowed to exit
* the current active pool.
*/
// Can't exit if we aren't in the current active pool.
if (!isInPool(callerAddress, getActivePoolKey())) {
return false;
}
// There is a next pool coming up.
if (getNextPoolKey() != 0) {
// Next pool begins within the POOL_FREEZE_NUM_BLOCKS
// window and thus can't be modified.
if (block.number >= (getNextPoolKey() - POOL_FREEZE_NUM_BLOCKS)) {
return false;
}
// Next pool was already setup and callerAddress isn't
// in it which indicates that they already left.
if (!isInPool(callerAddress, getNextPoolKey())) {
return false;
}
}
// They must be in the current pool and either the next pool
// hasn't been initiated or it has but this user hasn't left
// yet.
return true;
}
function _initiateNextPool() internal {
if (getNextPoolKey() != 0) {
// If there is already a next pool, we shouldn't
// initiate a new one until it has become active.
__throw();
}
// Set the next pool to start at double the freeze block number
// in the future.
uint nextPool = block.number + 2 * POOL_FREEZE_NUM_BLOCKS;
// Copy the current pool into the next pool.
callerPools[nextPool] = callerPools[getActivePoolKey()];
// Randomize the pool order
_shufflePool(nextPool);
// Push the next pool into the pool history.
poolHistory.length += 1;
poolHistory[poolHistory.length - 1] = nextPool;
}
function _shufflePool(uint poolNumber) internal {
var pool = callerPools[poolNumber];
uint swapIndex;
address buffer;
for (uint i = 0; i < pool.length; i++) {
swapIndex = uint(sha3(block.blockhash(block.number), i)) % pool.length;
if (swapIndex == i) {
continue;
}
buffer = pool[i];
pool[i] = pool[swapIndex];
pool[swapIndex] = buffer;
}
}
//event AddedToPool(address indexed callerAddress, uint indexed pool);
//event RemovedFromPool(address indexed callerAddress, uint indexed pool);
function _addToPool(address callerAddress, uint poolNumber) internal {
if (poolNumber == 0 ) {
// This shouldn't be called with 0;
__throw();
}
// already in the pool.
if (isInPool(callerAddress, poolNumber)) {
return;
}
var pool = callerPools[poolNumber];
pool.length += 1;
pool[pool.length - 1] = callerAddress;
// Log the addition.
//AddedToPool(callerAddress, poolNumber);
}
function _removeFromPool(address callerAddress, uint poolNumber) internal {
if (poolNumber == 0 ) {
// This shouldn't be called with 0;
__throw();
}
// nothing to remove.
if (!isInPool(callerAddress, poolNumber)) {
return;
}
var pool = callerPools[poolNumber];
// special case length == 1
if (pool.length == 1) {
pool.length = 0;
}
for (uint i = 0; i < pool.length; i++) {
// When we find the index of the address to remove we
// shift the last person to that location and then we
// truncate the last member off of the end.
if (pool[i] == callerAddress) {
pool[i] = pool[pool.length - 1];
pool.length -= 1;
break;
}
}
// Log the addition.
//RemovedFromPool(callerAddress, poolNumber);
}
function enterPool() public {
/*
* Request to be added to the call pool.
*/
if (canEnterPool(msg.sender)) {
if (getNextPoolKey() == 0) {
// This is the first address to modify the
// current pool so we need to setup the next
// pool.
_initiateNextPool();
}
_addToPool(msg.sender, getNextPoolKey());
}
}
function exitPool() public {
/*
* Request to be removed from the call pool.
*/
if (canExitPool(msg.sender)) {
if (getNextPoolKey() == 0) {
// This is the first address to modify the
// current pool so we need to setup the next
// pool.
_initiateNextPool();
}
_removeFromPool(msg.sender, getNextPoolKey());
}
}
function __throw() internal {
int[] x;
x[1];
}
}External Links
Related contracts
MemberRegistry
Same deployerOwner-controlled dynamic-array member registry.
0x1f3b19...7c161aAugust 13, 2015Members
Same deployerOwner-controlled member registry with array + mapping tracking.
0xaeb2ac...a5fd37August 14, 2015MembershipRoster
Same deployerAn Ethereum Public Trust roster contract for adding, removing, and checking members.
0xf1d327...aa0455August 14, 2015KillMul
Same deployerTest/utility contract with `suicide(address)` kill switch and a `multiply2` helper.
0x3e5e1f...7c78cdAugust 14, 2015Members
Same deployerOwner-controlled member registry using sequential id + mapping storage.
0xc6810e...db4e6fAugust 14, 2015CallerPool
Same deployerThe bonded caller pool of the Ethereum Alarm Clock: callers post a bond, take turns being the designated executor, and forfeit it if they miss.
0x329219...fc3e8bSeptember 22, 2015