Follow Us

Header Ad

Latest Posts

Categories

Here is the “random number Oracle by majority vote” I’m using in Pseudonym Pairs : ethereum

My dApp has to have secure random number generation. I came up with the idea to let the entire population converge, by vote, a bit like Casper, on a random number. This is the code I use for that, from Polyopia.sol.

contract Oracle 

    mapping (uint => mapping (uint => uint)) public points;
    mapping (uint => uint[]) public leaderboard;
    mapping (uint => mapping (uint => uint)) public leaderboardIndex;

    struct Score 
        uint start;
        uint end;
    
    mapping (uint => mapping (uint => Score)) public segments;

    function _vote(uint _id, uint _t) internal 

        uint score = points[_t][_id];

        if(score == 0) 
            leaderboard[_t].push(_id);
            leaderboardIndex[_t][_id] = leaderboard[_t].length;
            if(segments[_t][1].end == 0) segments[_t][1].end = leaderboard[_t].length;
            segments[_t][1].start = leaderboard[_t].length;
        
        else 
            uint index = leaderboardIndex[_t][_id];
            uint nextSegment = segments[_t][score].end;
            if(nextSegment != index) 
                leaderboardIndex[_t][_id] = nextSegment;
                leaderboardIndex[_t][leaderboard[_t][nextSegment-1]] = index;
                (leaderboard[_t][nextSegment - 1], leaderboard[_t][index - 1]) = (leaderboard[_t][index - 1], leaderboard[_t][nextSegment - 1]);
            
            if(segments[_t][score].start == nextSegment)  
                delete segments[_t][score].start; 
                delete segments[_t][score].end; 
            
            else segments[_t][score].end++;
            if(segments[_t][score+1].end == 0) segments[_t][score+1].end = nextSegment;
            segments[_t][score+1].start = nextSegment;
        
        points[_t][_id]++;
    


contract Polytopia is Oracle  ... 

    Leave Your Comment

    Your email address will not be published.*

    Forgot Password