Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active November 13, 2018 23:19
Show Gist options
  • Save resilience-me/fadd6c4dddd10a763cdd2be23c823691 to your computer and use it in GitHub Desktop.
Save resilience-me/fadd6c4dddd10a763cdd2be23c823691 to your computer and use it in GitHub Desktop.
contract Labyrinth { function generateRandomNumber() external returns (uint) {} }
contract PseudonymPairs {
Labyrinth labyrinth = Labyrinth(0x73cf4f93b71ebceb9eeba15889c5b17887004572);
function bootstrapNetwork(address _nym) {
require(now < genesisTimestamp);
//require(msg.sender == );
sortMe(_nym);
}
struct ShuffleAlgorithm {
mapping(uint => uint) shufflingIndex;
uint counter;
}
ShuffleAlgorithm[4] shuffleUtility;
enum State { interlude, pseudonymEvent, eventFinished, endOfPeriod }
uint[4] timeperiods;
uint genesisTimestamp = 1545523200; // Sunday 23 December 2018 00:00:00, pseudonym events
// scheduled to random hour on saturdays from 00:40 to 23:40
uint eventCounter;
uint[24] hourIndex;
function setRandomHour() internal {
uint stepIn24HourCycle = eventCounter % 24;
uint randomHour = stepIn24HourCycle + labyrinth.generateRandomNumber() % (24 - stepIn24HourCycle);
if(hourIndex[stepIn24HourCycle] == 0) hourIndex[stepIn24HourCycle] = stepIn24HourCycle;
hourIndex[randomHour] = hourIndex[stepIn24HourCycle];
timeperiods[2] = 28 days - randomHour;
timeperiods[1] = 28 days - randomHour - 20 minutes;
}
modifier atTime(State _inState, State _nextState) {
require(now > getTime(_inState));
require(now < getTime(_nextState));
_;
}
function getTime(State _state) view returns (uint) {
return genesisTimestamp + (timeperiods[3] * eventCounter) + timeperiods[uint(_state)];
}
modifier incrementScheduler {
if(now > getTime(State.endOfPeriod)) {
eventCounter++;
if(eventCounter % 24 == 0) {
delete hourIndex;
}
setRandomHour();
}
_;
}
mapping(address => uint) pseudonymID;
mapping(uint => address) pseudonymIndex;
mapping(address => uint) immigrantID;
mapping(uint => address) immigrantIndex;
mapping(uint => bool) isTerminated;
constructor() {
genesisTimestamp = now;
timeperiods[3] = 28 days;
setRandomHour();
timeperiods[0] = 0;
}
function sortMe(address _nym) internal atTime(State.interlude, State.pseudonymEvent) {
uint8 idx;
if(totalSorted % 2 == 1) {
shuffleUtility[0].counter++;
idx = 0;
}
else {
shuffleUtility[1].counter++;
idx = 1;
}
uint totalSorted = shuffleUtility[0].counter + shuffleUtility[1].counter;
pseudonymID[_nym] = totalSorted;
pseudonymIndex[totalSorted] = _nym;
uint pos = shuffleUtility[idx].counter;
uint randomNumber = 1 + labyrinth.generateRandomNumber() % (pos - 1);
shuffleUtility[idx].shufflingIndex[pos] = randomNumber;
shuffleUtility[idx].shufflingIndex[randomNumber] = pos;
}
function getPair(address _nym) view atTime(State.pseudonymEvent, State.endOfPeriod) returns (uint) {
uint ID = pseudonymID[_nym];
uint8 idx;
uint nominator;
if(ID % 2 == 1) {
nominator += 1;
idx = 0;
}
else idx = 1;
uint pos = nominator / 2;
uint pairID = shuffleUtility[idx].shufflingIndex[pos];
return pairID;
}
function optIn() atTime(State.interlude, State.pseudonymEvent) {
uint totalPairs = shuffleUtility[1].counter;
shuffleUtility[2].counter++;
uint totalImmigrants = shuffleUtility[2].counter;
require(totalPairs >= totalImmigrants);
require(immigrantID[msg.sender] == 0);
//require(borderToken.balanceOf[msg.sender] >= 1);
//borderToken.balanceOf[msg.sender]--;
immigrantID[msg.sender] = totalImmigrants;
immigrantIndex[totalImmigrants] = msg.sender;
uint randomNumber = totalImmigrants + 1 + labyrinth.generateRandomNumber() % (totalPairs - totalImmigrants);
if(shuffleUtility[2].shufflingIndex[randomNumber] == 0) shuffleUtility[2].shufflingIndex[randomNumber] = randomNumber;
if(shuffleUtility[2].shufflingIndex[totalImmigrants] == 0) shuffleUtility[2].shufflingIndex[totalImmigrants] = totalImmigrants;
shuffleUtility[2].shufflingIndex[randomNumber] = shuffleUtility[2].shufflingIndex[totalImmigrants];
shuffleUtility[2].shufflingIndex[totalImmigrants] = shuffleUtility[2].shufflingIndex[randomNumber];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment