Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.24;
- import "./SafeMath.sol";
- import "./Random.sol";
- contract Platform {
- /*
- * | commit | reveal | reward |
- */
- uint public commitT;
- uint public revealT;
- uint public rewardT;
- uint public periodT;
- uint public startT;
- uint public commitPrice;
- uint public bonusPrice;
- uint public depositLimit;
- uint public scoreScale;
- struct PeerStatus {
- uint reward;
- uint times;
- }
- // slot => task => score => count
- mapping(uint => mapping(address => mapping(uint => uint))) slotTasks;
- // slot => agent => task => score
- mapping(uint => mapping(address => mapping(address => uint))) slotReports;
- // slot => agent => commitments
- mapping(uint => mapping(address => mapping(address => bytes32))) slotCommitments;
- mapping(uint => mapping(address => mapping(address => uint))) slotCommitRandom;
- // slot => agent => status
- mapping(uint => mapping(address => PeerStatus)) slotPeerStatus;
- // slot => status
- mapping(uint => PeerStatus) slotStatus;
- mapping(address => uint) public deposit;
- constructor() public {
- commitT = 60;
- revealT = 60;
- rewardT = 60;
- periodT = 180;
- startT = block.timestamp;
- commitPrice = 0;
- bonusPrice = 1000;
- depositLimit = 100000;
- scoreScale = 3;
- }
- function platActive() public payable {
- deposit[msg.sender] = SafeMath.add(deposit[msg.sender], msg.value);
- }
- function platWithdraw(uint amount) public {
- require(amount <= deposit[msg.sender]);
- deposit[msg.sender] -= amount;
- msg.sender.transfer(amount);
- }
- function platCheckActive(address addr) public view returns (bool) {
- return (deposit[addr] > depositLimit);
- }
- function platGetSlot() public view returns (uint) {
- uint nowTime = block.timestamp;
- uint slot = SafeMath.div(nowTime - startT, periodT);
- return slot;
- }
- // 0: before; 1: commit; 2: reveal; 3: reward; 4: after
- function platCheckSlot(uint slot) public view returns (uint) {
- uint nowTime = block.timestamp;
- uint slotStart = SafeMath.add(SafeMath.mul(slot, commitT), startT);
- if (nowTime < slotStart) {
- return 0;
- } else if (nowTime < slotStart + commitT) {
- return 1;
- } else if (nowTime < slotStart + commitT + revealT) {
- return 2;
- } else if (nowTime < slotStart + commitT + revealT + rewardT) {
- return 3;
- } else {
- return 4;
- }
- }
- function platCheckScore(uint score) internal view returns (bool) {
- return (score > 0 && score <= scoreScale);
- }
- function platCommit(address task, bytes32 commitment) public {
- // require(platCheckActive(msg.sender));
- uint slot = platGetSlot();
- slotCommitments[slot][msg.sender][task] = commitment;
- slotCommitRandom[slot][msg.sender][task] = Random.randInt256(1);
- deposit[msg.sender] = SafeMath.sub(deposit[msg.sender], commitPrice);
- }
- function platCommitPack(address[] tasks, bytes32[] commitments) public {
- for (uint i = 0; i < tasks.length; i++) {
- platCommit(tasks[i], commitments[i]);
- }
- }
- function platReveal(uint slot, address task, uint score, uint random) public {
- // require(regAllCheck(msg.sender));
- require(platCheckScore(score));
- // require(platCheckSlot(slot) == 2);
- // slot nounce of commitment
- bytes32 commitment = slotCommitments[slot][msg.sender][task];
- // check commitment
- require(keccak256(abi.encodePacked(score, random)) == commitment);
- // require(slotReports[slot][msg.sender][task] == 0);
- slotReports[slot][msg.sender][task] = score;
- // update plattation
- slotplattations[slot][task][score] = SafeMath.add(slotplattations[slot][task][score], 1);
- plattations[task][score] = SafeMath.add(plattations[task][score], 1);
- slotTotalplattations[slot][score] = SafeMath.add(slotTotalplattations[slot][score], 1);
- slotTotalplattations[slot][0] = SafeMath.add(slotTotalplattations[slot][0], 1);
- }
- function platRevealPack(uint slot, address[] tasks, uint[] scores, uint[] randoms) public {
- for (uint i = 0; i < tasks.length; i++) {
- platReveal(slot, tasks[i], scores[i], randoms[i]);
- }
- }
- function platPrediction(uint slot, address task) public {
- // require(regAllCheck(msg.sender));
- // require(platCheckSlot(slot) == 3);
- uint reward = computeReward(slot, msg.sender, task, slotReports[slot][msg.sender][task]);
- // update status
- slotPeerStatus[slot][msg.sender].reward = SafeMath.add(slotPeerStatus[slot][msg.sender].reward, reward);
- slotPeerStatus[slot][msg.sender].times = SafeMath.add(slotPeerStatus[slot][msg.sender].times, 1);
- slotStatus[slot].reward = SafeMath.add(slotStatus[slot].reward, reward);
- slotStatus[slot].times = SafeMath.add(slotStatus[slot].times, 1);
- }
- function platPredictionPack(uint slot, address[] tasks) public {
- for (uint i = 0; i < tasks.length; i++) {
- platPrediction(slot, tasks[i]);
- }
- }
- function platReward(uint slot) public {
- // require(regAllCheck(msg.sender));
- // require(platCheckSlot(slot) == 4);
- uint tradeoff = SafeMath.div(SafeMath.mul(slotPeerStatus[slot][msg.sender].times, slotStatus[slot].reward), slotStatus[slot].times);
- deposit[msg.sender] = SafeMath.sub(
- SafeMath.add(
- SafeMath.add(
- deposit[msg.sender],
- slotPeerStatus[slot][msg.sender].reward
- ),
- commitPrice
- ),
- tradeoff
- );
- }
- // peer prediction RPTS
- function computeReward(uint _slot, address _agent, address _task, uint _score) internal view returns (uint) {
- // select reference peer
- uint selected_score = selectRandomSlotReport(_slot, _agent, _task);
- uint score_amount;
- for (uint i = 1; i <= scoreScale; i++) {
- score_amount = SafeMath.add(score_amount, slotTotalplattations[_slot][i]);
- }
- // compute result
- if (slotTotalplattations[_slot][_score] == 0) return 1000000000;
- else if (selected_score == _score) {
- return SafeMath.div(SafeMath.mul(1000000000, score_amount), slotTotalplattations[_slot][_score]);
- }
- else {
- return 0;
- }
- }
- function selectRandomSlotReport(uint slot, address agent, address task) internal view returns (uint) {
- uint[] memory score_list = new uint[](scoreScale);
- uint selected_score;
- uint score_amount;
- for (uint i = 1; i <= scoreScale; i++) {
- score_list[i-1] = slotplattations[slot][task][i];
- score_amount = SafeMath.add(score_amount, slotplattations[slot][task][i]);
- }
- uint r = slotCommitRandom[slot][agent][task] % score_amount;
- for (i = 1; i <= scoreScale; i++) {
- if (r < score_list[i-1]) {
- selected_score = i;
- break;
- }
- r = SafeMath.sub(r, score_list[i-1]);
- }
- require(platCheckScore(selected_score));
- return selected_score;
- }
- function test(uint slot, address task, uint score) public {
- slotplattations[slot][task][score] = SafeMath.add(slotplattations[slot][task][score], 1);
- plattations[task][score] = SafeMath.add(plattations[task][score], 1);
- slotTotalplattations[slot][score] = SafeMath.add(slotTotalplattations[slot][score], 1);
- }
- }
Add Comment
Please, Sign In to add comment