daily pastebin goal
71%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.4.24;
  2. import "./SafeMath.sol";
  3. import "./Random.sol";
  4.  
  5. contract Platform {
  6.  
  7.     /*
  8.      * |     commit     | reveal | reward |
  9.      */
  10.     uint public commitT;
  11.     uint public revealT;
  12.     uint public rewardT;
  13.     uint public periodT;
  14.     uint public startT;
  15.     uint public commitPrice;
  16.     uint public bonusPrice;
  17.     uint public depositLimit;
  18.     uint public scoreScale;
  19.  
  20.     struct PeerStatus {
  21.         uint reward;
  22.         uint times;
  23.     }
  24.  
  25.     // slot => task => score => count
  26.     mapping(uint => mapping(address => mapping(uint => uint))) slotTasks;
  27.     // slot => agent => task => score
  28.     mapping(uint => mapping(address => mapping(address => uint))) slotReports;
  29.     // slot => agent => commitments
  30.     mapping(uint => mapping(address => mapping(address => bytes32))) slotCommitments;
  31.     mapping(uint => mapping(address => mapping(address => uint))) slotCommitRandom;
  32.     // slot => agent => status
  33.     mapping(uint => mapping(address => PeerStatus)) slotPeerStatus;
  34.     // slot => status
  35.     mapping(uint => PeerStatus) slotStatus;
  36.  
  37.     mapping(address => uint) public deposit;
  38.  
  39.     constructor() public {
  40.         commitT = 60;
  41.         revealT = 60;
  42.         rewardT = 60;
  43.         periodT = 180;
  44.         startT = block.timestamp;
  45.         commitPrice = 0;
  46.         bonusPrice = 1000;
  47.         depositLimit = 100000;
  48.         scoreScale = 3;
  49.     }
  50.  
  51.     function platActive() public payable {
  52.         deposit[msg.sender] = SafeMath.add(deposit[msg.sender], msg.value);
  53.     }
  54.  
  55.     function platWithdraw(uint amount) public {
  56.         require(amount <= deposit[msg.sender]);
  57.         deposit[msg.sender] -= amount;
  58.         msg.sender.transfer(amount);
  59.     }
  60.  
  61.     function platCheckActive(address addr) public view returns (bool) {
  62.         return (deposit[addr] > depositLimit);
  63.     }
  64.  
  65.     function platGetSlot() public view returns (uint) {
  66.         uint nowTime = block.timestamp;
  67.         uint slot = SafeMath.div(nowTime - startT, periodT);
  68.         return slot;
  69.     }
  70.  
  71.     // 0: before; 1: commit; 2: reveal; 3: reward; 4: after
  72.     function platCheckSlot(uint slot) public view returns (uint) {
  73.         uint nowTime = block.timestamp;
  74.         uint slotStart = SafeMath.add(SafeMath.mul(slot, commitT), startT);
  75.         if (nowTime < slotStart) {
  76.             return 0;
  77.         } else if (nowTime < slotStart + commitT) {
  78.             return 1;
  79.         } else if (nowTime < slotStart + commitT + revealT) {
  80.             return 2;
  81.         } else if (nowTime < slotStart + commitT + revealT + rewardT) {
  82.             return 3;
  83.         } else {
  84.             return 4;
  85.         }
  86.     }
  87.  
  88.     function platCheckScore(uint score) internal view returns (bool) {
  89.         return (score > 0 && score <= scoreScale);
  90.     }
  91.  
  92.     function platCommit(address task, bytes32 commitment) public {
  93.         // require(platCheckActive(msg.sender));
  94.         uint slot = platGetSlot();
  95.         slotCommitments[slot][msg.sender][task] = commitment;
  96.         slotCommitRandom[slot][msg.sender][task] = Random.randInt256(1);
  97.         deposit[msg.sender] = SafeMath.sub(deposit[msg.sender], commitPrice);
  98.     }
  99.  
  100.     function platCommitPack(address[] tasks, bytes32[] commitments) public {
  101.         for (uint i = 0; i < tasks.length; i++) {
  102.             platCommit(tasks[i], commitments[i]);
  103.         }
  104.     }
  105.  
  106.     function platReveal(uint slot, address task, uint score, uint random) public {
  107.         // require(regAllCheck(msg.sender));
  108.         require(platCheckScore(score));
  109.         // require(platCheckSlot(slot) == 2);
  110.         // slot nounce of commitment
  111.         bytes32 commitment = slotCommitments[slot][msg.sender][task];
  112.         // check commitment
  113.         require(keccak256(abi.encodePacked(score, random)) == commitment);
  114.         // require(slotReports[slot][msg.sender][task] == 0);
  115.         slotReports[slot][msg.sender][task] = score;
  116.         // update plattation
  117.         slotplattations[slot][task][score] = SafeMath.add(slotplattations[slot][task][score], 1);
  118.         plattations[task][score] = SafeMath.add(plattations[task][score], 1);
  119.         slotTotalplattations[slot][score] = SafeMath.add(slotTotalplattations[slot][score], 1);
  120.         slotTotalplattations[slot][0] = SafeMath.add(slotTotalplattations[slot][0], 1);
  121.     }
  122.  
  123.     function platRevealPack(uint slot, address[] tasks, uint[] scores, uint[] randoms) public {
  124.         for (uint i = 0; i < tasks.length; i++) {
  125.             platReveal(slot, tasks[i], scores[i], randoms[i]);
  126.         }
  127.     }
  128.  
  129.     function platPrediction(uint slot, address task) public {
  130.         // require(regAllCheck(msg.sender));
  131.         // require(platCheckSlot(slot) == 3);
  132.         uint reward = computeReward(slot, msg.sender, task, slotReports[slot][msg.sender][task]);
  133.         // update status
  134.         slotPeerStatus[slot][msg.sender].reward = SafeMath.add(slotPeerStatus[slot][msg.sender].reward, reward);
  135.         slotPeerStatus[slot][msg.sender].times = SafeMath.add(slotPeerStatus[slot][msg.sender].times, 1);
  136.         slotStatus[slot].reward = SafeMath.add(slotStatus[slot].reward, reward);
  137.         slotStatus[slot].times = SafeMath.add(slotStatus[slot].times, 1);
  138.     }
  139.  
  140.     function platPredictionPack(uint slot, address[] tasks) public {
  141.         for (uint i = 0; i < tasks.length; i++) {
  142.             platPrediction(slot, tasks[i]);
  143.         }
  144.     }
  145.  
  146.     function platReward(uint slot) public {
  147.         // require(regAllCheck(msg.sender));
  148.         // require(platCheckSlot(slot) == 4);
  149.         uint tradeoff = SafeMath.div(SafeMath.mul(slotPeerStatus[slot][msg.sender].times, slotStatus[slot].reward), slotStatus[slot].times);
  150.         deposit[msg.sender] = SafeMath.sub(
  151.             SafeMath.add(
  152.                 SafeMath.add(
  153.                     deposit[msg.sender],
  154.                     slotPeerStatus[slot][msg.sender].reward
  155.                 ),
  156.                 commitPrice
  157.             ),
  158.             tradeoff
  159.         );
  160.     }
  161.  
  162.     // peer prediction RPTS
  163.     function computeReward(uint _slot, address _agent, address _task, uint _score) internal view returns (uint) {  
  164.         // select reference peer
  165.         uint selected_score = selectRandomSlotReport(_slot, _agent, _task);
  166.         uint score_amount;
  167.         for (uint i = 1; i <= scoreScale; i++) {
  168.             score_amount = SafeMath.add(score_amount, slotTotalplattations[_slot][i]);
  169.         }
  170.         // compute result
  171.         if (slotTotalplattations[_slot][_score] == 0) return 1000000000;
  172.         else if (selected_score == _score) {
  173.             return SafeMath.div(SafeMath.mul(1000000000, score_amount), slotTotalplattations[_slot][_score]);
  174.         }
  175.         else {
  176.             return 0;
  177.         }
  178.     }
  179.  
  180.     function selectRandomSlotReport(uint slot, address agent, address task) internal view returns (uint) {
  181.         uint[] memory score_list = new uint[](scoreScale);
  182.         uint selected_score;
  183.         uint score_amount;
  184.         for (uint i = 1; i <= scoreScale; i++) {
  185.             score_list[i-1] = slotplattations[slot][task][i];
  186.             score_amount = SafeMath.add(score_amount, slotplattations[slot][task][i]);
  187.         }
  188.         uint r = slotCommitRandom[slot][agent][task] % score_amount;
  189.        
  190.         for (i = 1; i <= scoreScale; i++) {
  191.             if (r < score_list[i-1]) {
  192.                 selected_score = i;
  193.                 break;
  194.             }
  195.             r = SafeMath.sub(r, score_list[i-1]);
  196.         }
  197.         require(platCheckScore(selected_score));
  198.         return selected_score;
  199.     }
  200.    
  201.     function test(uint slot, address task, uint score) public {
  202.         slotplattations[slot][task][score] = SafeMath.add(slotplattations[slot][task][score], 1);
  203.         plattations[task][score] = SafeMath.add(plattations[task][score], 1);
  204.         slotTotalplattations[slot][score] = SafeMath.add(slotTotalplattations[slot][score], 1);
  205.     }
  206. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top