Fix unexpected reverts and implement customizable vote window time in DioneDispute contract

This commit is contained in:
ChronosX88 2021-04-21 00:39:47 +03:00
parent b1cfc81cf7
commit 4b3aa6df57
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 22 additions and 20 deletions

View File

@ -7,6 +7,7 @@ contract DioneDispute {
using SafeMath for uint256; using SafeMath for uint256;
IDioneStaking public dioneStaking; IDioneStaking public dioneStaking;
uint256 public voteWindowTime;
struct Dispute { struct Dispute {
bytes32 dhash; // id of dispute - keccak256(_miner,_requestId,_timestamp) bytes32 dhash; // id of dispute - keccak256(_miner,_requestId,_timestamp)
@ -25,13 +26,14 @@ contract DioneDispute {
event NewVote(bytes32 dhash, address indexed votedMiner); event NewVote(bytes32 dhash, address indexed votedMiner);
event DisputeFinished(bytes32 dhash, bool status); event DisputeFinished(bytes32 dhash, bool status);
constructor(IDioneStaking _dioneStaking) { constructor(IDioneStaking _dioneStaking, uint256 _voteWindowTime) {
dioneStaking = _dioneStaking; dioneStaking = _dioneStaking;
voteWindowTime = _voteWindowTime;
} }
function beginDispute(address miner, uint256 requestID) public { function beginDispute(address miner, uint256 requestID) public {
bytes32 dhash = keccak256(abi.encodePacked(miner, requestID, block.timestamp)); bytes32 dhash = keccak256(abi.encodePacked(miner, requestID));
require(disputes[dhash].dhash.length != 0, "dispute already exists"); require(disputes[dhash].miner == address(0), "dispute already exists");
Dispute storage dispute = disputes[dhash]; Dispute storage dispute = disputes[dhash];
dispute.dhash = dhash; dispute.dhash = dhash;
dispute.sum = 0; dispute.sum = 0;
@ -47,37 +49,37 @@ contract DioneDispute {
} }
function vote(bytes32 dhash, bool voteStatus) public { function vote(bytes32 dhash, bool voteStatus) public {
require(disputes[dhash].dhash.length == 0, "dispute doesn't exist"); Dispute memory dispute = disputes[dhash];
Dispute storage dispute = disputes[dhash]; require(dispute.dhash.length != 0, "dispute doesn't exist");
require(dispute.finished == false, "dispute already finished"); require(dispute.finished == false, "dispute already finished");
require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner"); require(dioneStaking.isMiner(msg.sender), "caller isn't dione miner");
uint256 stake = dioneStaking.minerStake(msg.sender); uint256 stake = dioneStaking.minerStake(msg.sender);
if (voteStatus) { if (voteStatus) {
dispute.sum.sub(stake); disputes[dhash].sum = disputes[dhash].sum.add(stake);
} else { } 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); emit NewVote(dhash, msg.sender);
} }
function finishDispute(bytes32 dhash) public { function finishDispute(bytes32 dhash) public {
require(disputes[dhash].dhash.length == 0, "dispute doesn't exist"); require(disputes[dhash].dhash.length != 0, "dispute doesn't exist");
Dispute storage dispute = disputes[dhash]; Dispute memory dispute = disputes[dhash];
require((block.timestamp - dispute.timestamp) >= 2 hours, "vote window must be two hours"); require((block.timestamp - dispute.timestamp) >= voteWindowTime, "vote window hasn't passed yet");
require(dispute.finished == false, "dispute already finished"); require(dispute.finished == false, "dispute already finished");
require(dispute.disputeInitiator == msg.sender, "only dispute initiator can call this function"); require(dispute.disputeInitiator == msg.sender, "only dispute initiator can call this function");
if (dispute.sum < 0) { if (dispute.sum <= 0) {
dispute.disputeResult = false; disputes[dhash].disputeResult = false;
} else { } else {
dispute.disputeResult = true; disputes[dhash].disputeResult = true;
dispute.voted.push(msg.sender); disputes[dhash].voted.push(msg.sender);
dioneStaking.slashMiner(dispute.miner, dispute.voted); 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);
} }
} }

View File

@ -18,7 +18,7 @@ async function main() {
await dioneStaking.deployed(); await dioneStaking.deployed();
console.log("staking_contract_address = \"" + dioneStaking.address+ "\""); 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(); await dioneDispute.deployed();
console.log("dispute_contract_address = \"" + dioneDispute.address+ "\""); console.log("dispute_contract_address = \"" + dioneDispute.address+ "\"");
@ -31,7 +31,7 @@ async function main() {
console.log("mediator_contract_address = \"" + mediator.address +"\"") console.log("mediator_contract_address = \"" + mediator.address +"\"")
await dioneStaking.setOracleContractAddress(dioneOracle.address); await dioneStaking.setOracleContractAddress(dioneOracle.address);
await dioneStaking.setDisputeContractAddress(dioneOracle.address); await dioneStaking.setDisputeContractAddress(dioneDispute.address);
const addresses = ["0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", "0x90F79bf6EB2c4f870365E785982E1f101E93b906"] const addresses = ["0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", "0x90F79bf6EB2c4f870365E785982E1f101E93b906"]
await dioneToken.mint(addresses[0], ethers.constants.WeiPerEther.mul(50000)); await dioneToken.mint(addresses[0], ethers.constants.WeiPerEther.mul(50000));