scheduling in Pseudonym Pairs revisited

in #pseudonympairs5 years ago (edited)

Pseudonym Pairs is built around a simple schedule, and all actions are contained within the period your "proof-of-personhood" is valid. In the reference implementation I am writing, a modifier scheduler(), is called every time anyone does anything, and it keeps the schedule.

uint schedulePeriod = 198000 + ((block.timestamp - 198000)/ 7 days) * 7 days + 7 days; // nearest Saturday at 07:00 UTC

uint[] clockwork;
uint hour;

function scheduleHour() internal {
    if(clockwork.length == 0) clockwork.length = 24;
    uint randomHour = getRandomNumber() % clockwork.length;
    if(clockwork[randomHour] == 0) hour = randomHour;
    else hour = clockwork[randomHour];
    if(clockwork[clockwork.length - 1] == 0) clockwork[randomHour] = clockwork.length - 1;
    else clockwork[randomHour]  = clockwork[clockwork.length - 1];
    clockwork.length--;
}

modifier scheduler() {
    if(block.timestamp > schedulePeriod + hour) {
        schedulePeriod += 28 days;
        scheduleHour();
        data.length++;
    }
    _;
}

Coin Marketplace

STEEM 0.21
TRX 0.25
JST 0.038
BTC 95686.04
ETH 3320.33
USDT 1.00
SBD 3.08