scheduling in Pseudonym Pairs revisited

in pseudonympairs •  5 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++;
    }
    _;
}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!