Add simple miner slashing algorithm, add error messages to DioneDispute.sol

This commit is contained in:
ChronosX88 2021-03-03 01:04:19 +03:00
parent 21e50dd13a
commit 8ead31a45e
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 30 additions and 6 deletions

View File

@ -15,6 +15,7 @@ contract DioneDispute {
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
uint256 timestamp; // dispute creation timestamp
mapping(address => bool) voted; // map of miners who vote for/against
}
@ -29,6 +30,7 @@ contract DioneDispute {
}
function beginDispute(address miner, uint256 requestID) public {
require(!disputes[dhash], "dispute already exists");
bytes32 dhash = keccak256(miner, requestID, now);
Dispute dispute = Dispute(
{
@ -37,6 +39,7 @@ contract DioneDispute {
finished: false,
disputeResult: true,
miner: miner,
timesyamp: now,
disputeInitiator: msg.sender,
}
);
@ -47,10 +50,10 @@ contract DioneDispute {
}
function vote(bytes32 dhash, bool voteStatus) public {
require(disputes[dhash]);
require(disputes[dhash], "dispute doesn't exist");
Dispute storage dispute = disputes[dhash];
require(dispute.finished == false);
require(dioneStaking.isMiner(msg.sender));
require(dispute.finished == false, "dispute already finished");
require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner");
int256 stake = dioneStaking.minerStake(msg.sender);
if (voteStatus) {
dispute.sum.sub(stake);
@ -63,16 +66,21 @@ contract DioneDispute {
}
function finishDispute(bytes32 dhash) public {
require(disputes[dhash]);
require(disputes[dhash], "dispute doesn't exist");
Dispute storage dispute = disputes[dhash];
require(dispute.finished == false);
require(dispute.disputeInitiator == msg.sender);
require((now - dispute[dhash].timestamp) >= 2 hours, "vote window must be two hours");
require(dispute.finished == false, "dispute already finished");
require(dispute.disputeInitiator == msg.sender, "only dispute initiator can call this function");
if (dispute.sum < 0) {
dispute.disputeResult = false;
} else {
dispute.disputeResult = true;
}
dioneStaking.slashMiner(dispute.miner);
dispute.finished = true;
emit DisputeFinished(dhash, dispute.disputeResult);
}
}

View File

@ -24,6 +24,8 @@ contract DioneStaking is Ownable, ReentrancyGuard {
DioneToken public dione;
// Aggregator contract address.
address public aggregatorAddr;
// Dispute contract address.
address public disputeContractAddr;
// Miner rewards in DIONE tokens.
uint256 public minerReward;
// The block number when DIONE mining starts.
@ -45,6 +47,11 @@ contract DioneStaking is Ownable, ReentrancyGuard {
_;
}
modifier onlyDispute(address addr) {
require(addr == disputeContractAddr, "Exception: caller is not the dispute contract");
_;
}
constructor(
DioneToken _dione,
uint256 _minerReward,
@ -132,4 +139,13 @@ contract DioneStaking is Ownable, ReentrancyGuard {
function setAggregator(address _aggregatorAddr) public onlyOwner {
aggregatorAddr = _aggregatorAddr;
}
function slashMiner(address miner, address[] receipentMiners) public onlyDispute {
uint256 share = minerInfo[miner].amount.div(receipentMiners.length);
for (var i = 0; i < receipentMiners.length; i++) {
minerInfo[miner].amount.sub(share);
minerInfo[receipentMiners[i]].amount += share;
}
}
}