From f8576cffeae4137ad4260e65c6eafe73400f3bb1 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 27 Apr 2021 21:10:40 +0300 Subject: [PATCH] [DioneDispute] Change type of sum property in Dispute struct, add missing checks and add minStake check --- eth-contracts/common/deployment.ts | 3 ++- eth-contracts/contracts/DioneDispute.sol | 27 +++++++++++++++--------- eth-contracts/scripts/deploy.ts | 3 ++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/eth-contracts/common/deployment.ts b/eth-contracts/common/deployment.ts index 3b88479..3da8d23 100644 --- a/eth-contracts/common/deployment.ts +++ b/eth-contracts/common/deployment.ts @@ -18,6 +18,7 @@ interface DeploymentOptions { actualStake: number; // of each node nodeCount: number; logging: boolean; + minStakeForDisputeVotes: number; } async function deploy(opts: DeploymentOptions): Promise { @@ -39,7 +40,7 @@ async function deploy(opts: DeploymentOptions): Promise { await dioneStaking.deployed(); logger.log("staking_contract_address = \"" + dioneStaking.address+ "\""); - const dioneDispute = await DioneDispute.deploy(dioneStaking.address, opts.voteWindowTime); + const dioneDispute = await DioneDispute.deploy(dioneStaking.address, opts.voteWindowTime, ethers.constants.WeiPerEther.mul(opts.minStakeForDisputeVotes)); await dioneDispute.deployed(); logger.log("dispute_contract_address = \"" + dioneDispute.address+ "\""); diff --git a/eth-contracts/contracts/DioneDispute.sol b/eth-contracts/contracts/DioneDispute.sol index cf1baf1..d8aa7f1 100644 --- a/eth-contracts/contracts/DioneDispute.sol +++ b/eth-contracts/contracts/DioneDispute.sol @@ -4,14 +4,15 @@ import "@openzeppelin/contracts/utils/math/SafeMath.sol"; import "./interfaces/IDioneStaking.sol"; contract DioneDispute { - using SafeMath for uint256; - IDioneStaking public dioneStaking; uint256 public voteWindowTime; + // Minimum amount of DIONE tokens required to vote to dispute + uint256 public minStake; + struct Dispute { bytes32 dhash; // id of dispute - keccak256(_miner,_requestId,_timestamp) - uint256 sum; // vote measure (for/against this dispute) + int256 sum; // vote measure (for/against this dispute) 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 @@ -20,15 +21,21 @@ contract DioneDispute { address[] voted; // map of miners who vote for/against } + modifier onlyExistingDispute(bytes32 _dhash) { + require(disputes[_dhash].disputeInitiator != address(0), "dispute doesn't exist"); + _; + } + mapping(bytes32 => Dispute) disputes; event NewDispute(bytes32 dhash, uint256 requestID, address indexed miner, address indexed disputeInitiator); event NewVote(bytes32 dhash, address indexed votedMiner); event DisputeFinished(bytes32 dhash, bool status); - constructor(IDioneStaking _dioneStaking, uint256 _voteWindowTime) { + constructor(IDioneStaking _dioneStaking, uint256 _voteWindowTime, uint256 _minStake) { dioneStaking = _dioneStaking; voteWindowTime = _voteWindowTime; + minStake = _minStake; } function beginDispute(address miner, uint256 requestID) public { @@ -48,24 +55,24 @@ contract DioneDispute { emit NewDispute(dhash, requestID, miner, msg.sender); } - function vote(bytes32 dhash, bool voteStatus) public { + function vote(bytes32 dhash, bool voteStatus) public onlyExistingDispute(dhash) { Dispute memory dispute = disputes[dhash]; - require(dispute.dhash.length != 0, "dispute doesn't exist"); require(dispute.finished == false, "dispute already finished"); + require(msg.sender != dispute.disputeInitiator, "dispute initiator isn't allowed to vote"); require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner"); + require(dioneStaking.minerStake(msg.sender) >= minStake, "miner doesn't have minimum stake to vote"); uint256 stake = dioneStaking.minerStake(msg.sender); if (voteStatus) { - disputes[dhash].sum = disputes[dhash].sum.add(stake); + disputes[dhash].sum = disputes[dhash].sum + int256(stake); } else { - disputes[dhash].sum = disputes[dhash].sum.sub(stake); + disputes[dhash].sum = disputes[dhash].sum - int256(stake); } disputes[dhash].voted.push(msg.sender); emit NewVote(dhash, msg.sender); } - function finishDispute(bytes32 dhash) public { - require(disputes[dhash].dhash.length != 0, "dispute doesn't exist"); + function finishDispute(bytes32 dhash) public onlyExistingDispute(dhash) { Dispute memory dispute = disputes[dhash]; require((block.timestamp - dispute.timestamp) >= voteWindowTime, "vote window hasn't passed yet"); require(dispute.finished == false, "dispute already finished"); diff --git a/eth-contracts/scripts/deploy.ts b/eth-contracts/scripts/deploy.ts index 1be96a1..65412ad 100644 --- a/eth-contracts/scripts/deploy.ts +++ b/eth-contracts/scripts/deploy.ts @@ -13,7 +13,8 @@ async function main() { maxStake: 0, // don't use this deployment feature actualStake: 5000, nodeCount: 4, - logging: true + logging: true, + minStakeForDisputeVotes: 100 }); }