From 4b3aa6df571135a730d2b9a32dd5a157ccac9ef8 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 21 Apr 2021 00:39:47 +0300 Subject: [PATCH] Fix unexpected reverts and implement customizable vote window time in DioneDispute contract --- eth-contracts/contracts/DioneDispute.sol | 38 +++++++++++++----------- eth-contracts/scripts/deploy.ts | 4 +-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/eth-contracts/contracts/DioneDispute.sol b/eth-contracts/contracts/DioneDispute.sol index 5f967f4..cf1baf1 100644 --- a/eth-contracts/contracts/DioneDispute.sol +++ b/eth-contracts/contracts/DioneDispute.sol @@ -7,6 +7,7 @@ contract DioneDispute { using SafeMath for uint256; IDioneStaking public dioneStaking; + uint256 public voteWindowTime; struct Dispute { bytes32 dhash; // id of dispute - keccak256(_miner,_requestId,_timestamp) @@ -25,13 +26,14 @@ contract DioneDispute { event NewVote(bytes32 dhash, address indexed votedMiner); event DisputeFinished(bytes32 dhash, bool status); - constructor(IDioneStaking _dioneStaking) { + constructor(IDioneStaking _dioneStaking, uint256 _voteWindowTime) { dioneStaking = _dioneStaking; + voteWindowTime = _voteWindowTime; } function beginDispute(address miner, uint256 requestID) public { - bytes32 dhash = keccak256(abi.encodePacked(miner, requestID, block.timestamp)); - require(disputes[dhash].dhash.length != 0, "dispute already exists"); + bytes32 dhash = keccak256(abi.encodePacked(miner, requestID)); + require(disputes[dhash].miner == address(0), "dispute already exists"); Dispute storage dispute = disputes[dhash]; dispute.dhash = dhash; dispute.sum = 0; @@ -47,37 +49,37 @@ contract DioneDispute { } function vote(bytes32 dhash, bool voteStatus) public { - require(disputes[dhash].dhash.length == 0, "dispute doesn't exist"); - Dispute storage dispute = disputes[dhash]; + Dispute memory dispute = disputes[dhash]; + require(dispute.dhash.length != 0, "dispute doesn't exist"); require(dispute.finished == false, "dispute already finished"); require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner"); uint256 stake = dioneStaking.minerStake(msg.sender); if (voteStatus) { - dispute.sum.sub(stake); + disputes[dhash].sum = disputes[dhash].sum.add(stake); } else { - dispute.sum.add(stake); + disputes[dhash].sum = disputes[dhash].sum.sub(stake); } - dispute.voted.push(msg.sender); + 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"); - Dispute storage dispute = disputes[dhash]; - require((block.timestamp - dispute.timestamp) >= 2 hours, "vote window must be two hours"); + require(disputes[dhash].dhash.length != 0, "dispute doesn't exist"); + Dispute memory dispute = disputes[dhash]; + require((block.timestamp - dispute.timestamp) >= voteWindowTime, "vote window hasn't passed yet"); 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; + if (dispute.sum <= 0) { + disputes[dhash].disputeResult = false; } else { - dispute.disputeResult = true; - dispute.voted.push(msg.sender); - dioneStaking.slashMiner(dispute.miner, dispute.voted); + disputes[dhash].disputeResult = true; + disputes[dhash].voted.push(msg.sender); + dioneStaking.slashMiner(dispute.miner, disputes[dhash].voted); } - dispute.finished = true; + disputes[dhash].finished = true; - emit DisputeFinished(dhash, dispute.disputeResult); + emit DisputeFinished(dhash, disputes[dhash].disputeResult); } } diff --git a/eth-contracts/scripts/deploy.ts b/eth-contracts/scripts/deploy.ts index b292e49..6ef8c31 100644 --- a/eth-contracts/scripts/deploy.ts +++ b/eth-contracts/scripts/deploy.ts @@ -18,7 +18,7 @@ async function main() { await dioneStaking.deployed(); console.log("staking_contract_address = \"" + dioneStaking.address+ "\""); - const dioneDispute = await DioneDispute.deploy(dioneStaking.address); + const dioneDispute = await DioneDispute.deploy(dioneStaking.address, 5); await dioneDispute.deployed(); console.log("dispute_contract_address = \"" + dioneDispute.address+ "\""); @@ -31,7 +31,7 @@ async function main() { console.log("mediator_contract_address = \"" + mediator.address +"\"") await dioneStaking.setOracleContractAddress(dioneOracle.address); - await dioneStaking.setDisputeContractAddress(dioneOracle.address); + await dioneStaking.setDisputeContractAddress(dioneDispute.address); const addresses = ["0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", "0x90F79bf6EB2c4f870365E785982E1f101E93b906"] await dioneToken.mint(addresses[0], ethers.constants.WeiPerEther.mul(50000));