Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active January 17, 2019 16:12
Show Gist options
  • Save resilience-me/0ee4bb41ff272064b8874867c004f7d0 to your computer and use it in GitHub Desktop.
Save resilience-me/0ee4bb41ff272064b8874867c004f7d0 to your computer and use it in GitHub Desktop.
contract PseudonymPairs {
mapping(address => uint) pseudonymID;
mapping(uint => address) pseudonymIndex;
struct PairUtility {
mapping(uint => uint) index;
uint counter;
}
PairUtility[2] pairingUtility;
mapping(uint => uint) pairLookup;
// sortingHat() is invoked after ring signature key transfer is complete, and will cost you a tiny amount of GAS
// can be invoked continously at any time from the end of the previous event, up until the next event, and forms
// complete pairs regardless of wether or not every single person "commits" to the next event
function sortingHat() internal {
uint8 idx;
uint totalSorted = pairingUtility[0].counter + pairingUtility[1].counter;
// every other person is sorted in pairingUtility[1], instead of pairingUtility[0],
// the two combined form a complete list of pairs when the event begins
idx = totalSorted % 2;
totalSorted++;
pseudonymID[msg.sender] = totalSorted;
pseudonymIndex[totalSorted] = msg.sender;
// the pairingUtility counts how many have been sorted in the pairingUtility you are
// assigned to, and then randomly places you in the position another person had, moving
// that other person to the end of the list. this shuffles the pairs with each person sorted
pairingUtility[idx].counter++;
uint counter = pairingUtility[idx].counter;
uint randomNumber = getRandomNumber() % counter;
uint personShuffled = pairingUtility[idx].index[randomNumber];
pairingUtility[idx].index[randomNumber] = pseudonymID[msg.sender];
pairLookup[totalSorted] = randomNumber;
pairingUtility[idx].index[counter] = personShuffled;
pairLookup[personShuffled] = counter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment