2021-04-13 21:22:30 +00:00
|
|
|
pragma solidity ^0.8.0;
|
2021-02-26 19:53:43 +00:00
|
|
|
|
2021-04-13 21:22:30 +00:00
|
|
|
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
|
2021-02-26 19:53:43 +00:00
|
|
|
import "./interfaces/IDioneStaking.sol";
|
|
|
|
|
|
|
|
contract DioneDispute {
|
|
|
|
IDioneStaking public dioneStaking;
|
2021-04-20 21:39:47 +00:00
|
|
|
uint256 public voteWindowTime;
|
2021-02-26 19:53:43 +00:00
|
|
|
|
2021-04-27 18:10:40 +00:00
|
|
|
// Minimum amount of DIONE tokens required to vote to dispute
|
|
|
|
uint256 public minStake;
|
|
|
|
|
2021-02-26 19:53:43 +00:00
|
|
|
struct Dispute {
|
|
|
|
bytes32 dhash; // id of dispute - keccak256(_miner,_requestId,_timestamp)
|
2021-04-27 18:10:40 +00:00
|
|
|
int256 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
|
|
|
}
|
|
|
|
|
2021-04-27 18:10:40 +00:00
|
|
|
modifier onlyExistingDispute(bytes32 _dhash) {
|
|
|
|
require(disputes[_dhash].disputeInitiator != address(0), "dispute doesn't exist");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2021-02-26 19:53:43 +00:00
|
|
|
mapping(bytes32 => Dispute) disputes;
|
|
|
|
|
2021-03-15 20:39:52 +00:00
|
|
|
event NewDispute(bytes32 dhash, uint256 requestID, address indexed miner, address indexed disputeInitiator);
|
2021-02-26 19:53:43 +00:00
|
|
|
event NewVote(bytes32 dhash, address indexed votedMiner);
|
|
|
|
event DisputeFinished(bytes32 dhash, bool status);
|
|
|
|
|
2021-04-27 18:10:40 +00:00
|
|
|
constructor(IDioneStaking _dioneStaking, uint256 _voteWindowTime, uint256 _minStake) {
|
2021-02-26 19:53:43 +00:00
|
|
|
dioneStaking = _dioneStaking;
|
2021-04-20 21:39:47 +00:00
|
|
|
voteWindowTime = _voteWindowTime;
|
2021-04-27 18:10:40 +00:00
|
|
|
minStake = _minStake;
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function beginDispute(address miner, uint256 requestID) public {
|
2021-04-20 21:39:47 +00:00
|
|
|
bytes32 dhash = keccak256(abi.encodePacked(miner, requestID));
|
|
|
|
require(disputes[dhash].miner == address(0), "dispute already exists");
|
2021-03-04 18:22:05 +00:00
|
|
|
Dispute storage dispute = disputes[dhash];
|
|
|
|
dispute.dhash = dhash;
|
|
|
|
dispute.sum = 0;
|
|
|
|
dispute.finished = false;
|
|
|
|
dispute.disputeResult = false;
|
|
|
|
dispute.miner = miner;
|
2021-04-13 21:22:30 +00:00
|
|
|
dispute.timestamp = block.timestamp;
|
2021-03-04 18:22:05 +00:00
|
|
|
dispute.disputeInitiator = msg.sender;
|
2021-02-26 19:53:43 +00:00
|
|
|
|
|
|
|
disputes[dhash] = dispute;
|
|
|
|
|
2021-03-15 20:39:52 +00:00
|
|
|
emit NewDispute(dhash, requestID, miner, msg.sender);
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 18:10:40 +00:00
|
|
|
function vote(bytes32 dhash, bool voteStatus) public onlyExistingDispute(dhash) {
|
2021-04-20 21:39:47 +00:00
|
|
|
Dispute memory dispute = disputes[dhash];
|
2021-03-02 22:04:19 +00:00
|
|
|
require(dispute.finished == false, "dispute already finished");
|
2021-04-27 18:10:40 +00:00
|
|
|
require(msg.sender != dispute.disputeInitiator, "dispute initiator isn't allowed to vote");
|
2021-04-27 18:22:09 +00:00
|
|
|
require(msg.sender != dispute.miner, "the miner against whom dispute has beginned isn't allowed to vote");
|
2021-03-02 22:04:19 +00:00
|
|
|
require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner");
|
2021-04-27 18:10:40 +00:00
|
|
|
require(dioneStaking.minerStake(msg.sender) >= minStake, "miner doesn't have minimum stake to vote");
|
2021-03-04 18:22:05 +00:00
|
|
|
uint256 stake = dioneStaking.minerStake(msg.sender);
|
2021-02-26 19:53:43 +00:00
|
|
|
if (voteStatus) {
|
2021-04-27 18:10:40 +00:00
|
|
|
disputes[dhash].sum = disputes[dhash].sum + int256(stake);
|
2021-02-26 19:53:43 +00:00
|
|
|
} else {
|
2021-04-27 18:10:40 +00:00
|
|
|
disputes[dhash].sum = disputes[dhash].sum - int256(stake);
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
2021-04-20 21:39:47 +00:00
|
|
|
disputes[dhash].voted.push(msg.sender);
|
2021-02-26 19:53:43 +00:00
|
|
|
|
|
|
|
emit NewVote(dhash, msg.sender);
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:10:40 +00:00
|
|
|
function finishDispute(bytes32 dhash) public onlyExistingDispute(dhash) {
|
2021-04-20 21:39:47 +00:00
|
|
|
Dispute memory dispute = disputes[dhash];
|
|
|
|
require((block.timestamp - dispute.timestamp) >= voteWindowTime, "vote window hasn't passed yet");
|
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-04-20 21:39:47 +00:00
|
|
|
if (dispute.sum <= 0) {
|
|
|
|
disputes[dhash].disputeResult = false;
|
2021-02-26 19:53:43 +00:00
|
|
|
} else {
|
2021-04-20 21:39:47 +00:00
|
|
|
disputes[dhash].disputeResult = true;
|
|
|
|
disputes[dhash].voted.push(msg.sender);
|
|
|
|
dioneStaking.slashMiner(dispute.miner, disputes[dhash].voted);
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:39:47 +00:00
|
|
|
disputes[dhash].finished = true;
|
2021-03-02 22:04:19 +00:00
|
|
|
|
2021-04-20 21:39:47 +00:00
|
|
|
emit DisputeFinished(dhash, disputes[dhash].disputeResult);
|
2021-02-26 19:53:43 +00:00
|
|
|
}
|
|
|
|
}
|