2021-02-26 19:53:43 +00:00
|
|
|
pragma solidity ^0.6.12;
|
|
|
|
|
|
|
|
import "@openzeppelin/contracts/math/SafeMath.sol";
|
|
|
|
import "./interfaces/IDioneStaking.sol";
|
|
|
|
|
|
|
|
contract DioneDispute {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
|
|
|
|
IDioneStaking public dioneStaking;
|
|
|
|
|
|
|
|
struct Dispute {
|
|
|
|
bytes32 dhash; // id of dispute - keccak256(_miner,_requestId,_timestamp)
|
2021-03-04 18:22:05 +00:00
|
|
|
uint256 sum; // vote measure (for/against this dispute)
|
2021-02-26 19:53:43 +00:00
|
|
|
bool finished; // dispute was finished (closed) or not
|
|
|
|
bool disputeResult; // true - dispute had basis, false - dispute was false
|
|
|
|
address miner; // the miner against whom the dispute
|
|
|
|
address disputeInitiator; // the miner who started the dispute
|
2021-03-02 22:04:19 +00:00
|
|
|
uint256 timestamp; // dispute creation timestamp
|
2021-03-04 18:22:05 +00:00
|
|
|
address[] voted; // map of miners who vote for/against
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mapping(bytes32 => Dispute) disputes;
|
|
|
|
|
|
|
|
event NewDispute(bytes32 dhash, address indexed miner, address indexed disputeInitiator);
|
|
|
|
event NewVote(bytes32 dhash, address indexed votedMiner);
|
|
|
|
event DisputeFinished(bytes32 dhash, bool status);
|
|
|
|
|
|
|
|
constructor(IDioneStaking _dioneStaking) public {
|
|
|
|
dioneStaking = _dioneStaking;
|
|
|
|
}
|
|
|
|
|
|
|
|
function beginDispute(address miner, uint256 requestID) public {
|
2021-03-04 18:22:05 +00:00
|
|
|
bytes32 dhash = keccak256(abi.encodePacked(miner, requestID, now));
|
|
|
|
require(disputes[dhash].dhash.length != 0, "dispute already exists");
|
|
|
|
Dispute storage dispute = disputes[dhash];
|
|
|
|
dispute.dhash = dhash;
|
|
|
|
dispute.sum = 0;
|
|
|
|
dispute.finished = false;
|
|
|
|
dispute.disputeResult = false;
|
|
|
|
dispute.miner = miner;
|
|
|
|
dispute.timestamp = now;
|
|
|
|
dispute.disputeInitiator = msg.sender;
|
2021-02-26 19:53:43 +00:00
|
|
|
|
|
|
|
disputes[dhash] = dispute;
|
|
|
|
|
|
|
|
emit NewDispute(dhash, miner, msg.sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
function vote(bytes32 dhash, bool voteStatus) public {
|
2021-03-04 18:22:05 +00:00
|
|
|
require(disputes[dhash].dhash.length == 0, "dispute doesn't exist");
|
2021-02-26 19:53:43 +00:00
|
|
|
Dispute storage dispute = disputes[dhash];
|
2021-03-02 22:04:19 +00:00
|
|
|
require(dispute.finished == false, "dispute already finished");
|
|
|
|
require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner");
|
2021-03-04 18:22:05 +00:00
|
|
|
uint256 stake = dioneStaking.minerStake(msg.sender);
|
2021-02-26 19:53:43 +00:00
|
|
|
if (voteStatus) {
|
|
|
|
dispute.sum.sub(stake);
|
|
|
|
} else {
|
|
|
|
dispute.sum.add(stake);
|
|
|
|
}
|
2021-03-04 18:22:05 +00:00
|
|
|
dispute.voted.push(msg.sender);
|
2021-02-26 19:53:43 +00:00
|
|
|
|
|
|
|
emit NewVote(dhash, msg.sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
function finishDispute(bytes32 dhash) public {
|
2021-03-04 18:22:05 +00:00
|
|
|
require(disputes[dhash].dhash.length == 0, "dispute doesn't exist");
|
2021-02-26 19:53:43 +00:00
|
|
|
Dispute storage dispute = disputes[dhash];
|
2021-03-04 18:22:05 +00:00
|
|
|
require((now - dispute.timestamp) >= 2 hours, "vote window must be two hours");
|
2021-03-02 22:04:19 +00:00
|
|
|
require(dispute.finished == false, "dispute already finished");
|
|
|
|
require(dispute.disputeInitiator == msg.sender, "only dispute initiator can call this function");
|
2021-02-26 19:53:43 +00:00
|
|
|
if (dispute.sum < 0) {
|
|
|
|
dispute.disputeResult = false;
|
|
|
|
} else {
|
|
|
|
dispute.disputeResult = true;
|
2021-03-04 18:22:05 +00:00
|
|
|
dispute.voted.push(msg.sender);
|
|
|
|
dioneStaking.slashMiner(dispute.miner, dispute.voted);
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 22:04:19 +00:00
|
|
|
dispute.finished = true;
|
|
|
|
|
2021-02-26 19:53:43 +00:00
|
|
|
emit DisputeFinished(dhash, dispute.disputeResult);
|
|
|
|
}
|
|
|
|
}
|