Pseudonym Pairs reference implementation

in pseudonympairs •  5 years ago  (edited)

Roughly how I'm structuring the Pseudonym Pairs reference implementation at the moment. I'm not a coder, I'm learning as I go along. To implement this is harder for me than for an experienced person.

The dApp has 2 classes of people, the peers within the pairs, and, people assigned to pairs as "courts", either when opting-in ("immigrating"), or, when there has been a dispute and a pair has been broken up. The game theory is that people are in pairs, unless there is a problem, and then "mob rule" is used, 2 people deciding over 1. That makes the protocol maximally egalitarian.

struct Reg {
    bool rank;
    uint id;
}
mapping(address => Reg) registry;

uint[][2] index;
uint[] courtUtility;
function incrementCourtUtility() internal { courtUtility.push(index[true].length/2 + index[false].length)); }

function assignID(bool _rank) internal {
    index[_rank].length++;
    registry[msg.sender].rank = _rank;
    registry[msg.sender].id = index[_rank].length;
}
function shuffle(bool _rank) internal returns (uint id) {
    if(index[_rank].length != 0) id = getRandomNumber() % index[_rank].length;
    assignID(_rank);
    index[_rank][index[_rank].length - 1] = index[_rank][id];
    return id;
}
function register() {
    index[true][shuffle(true)] = index[true].length;
    if(index[true].length%2 == 0) incrementCourtUtility(); 
}
function assignCourt(bool _reassign) internal {
    uint height = index[true].length/2 - index[false].length % index[true].length/2;
    uint id = getRandomNumber() % height;
    if(_reassign == false) {
        index[false][shuffle(false)] = courtUtility[id];
    }
    else {
        assignID(false);
        index[false][index[false].length - 1] = courtUtility[id];
    }
    courtUtility[id] = courtUtility[height-1];
    courtUtility[height-1] = courtUtility[courtUtility.length - 1];
    courtUtility.length--;
    incrementCourtUtility();
}
function immigrate() { assignCourt(false); }
function reassign() { assignCourt(true); }
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!
Sort Order:  

thanks for sharing this :)