From 30595d0681760adc2c6374767d7afdf79a946ceb Mon Sep 17 00:00:00 2001 From: turinglabsorg Date: Tue, 12 Jul 2022 13:30:57 +0200 Subject: [PATCH] adding function to start appeal and bootstrap fn --- referee-cli/src/libs/fn.js | 8 +++- .../contracts/DataRetrievability.sol | 45 +++++++++++++++---- smart-contract/scripts/appeal:start.js | 33 ++++++++++++++ .../scripts/tests/appeal:resolve.sh | 1 + smart-contract/scripts/tests/appeal:slash.sh | 6 +-- 5 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 smart-contract/scripts/appeal:start.js diff --git a/referee-cli/src/libs/fn.js b/referee-cli/src/libs/fn.js index 67a2b51..41f9d56 100644 --- a/referee-cli/src/libs/fn.js +++ b/referee-cli/src/libs/fn.js @@ -324,7 +324,13 @@ const daemon = async (node) => { for (let k in appealsEvents) { const appealEvent = appealsEvents[k] const appealIndex = appealEvent.args.index - processappeal(node, appealIndex) + // TODO: Be sure deal is started, if not started startappeal first + const appeal = returnappeal(appealIndex) + if (appeal.origin_timestamp > 0) { + processappeal(node, appealIndex) + } else { + startappeal(appealIndex) + } } // Listen for appeals in contract contract.on("AppealCreated", (index, provider, ipfs_hash) => { diff --git a/smart-contract/contracts/DataRetrievability.sol b/smart-contract/contracts/DataRetrievability.sol index 1533e91..8220944 100644 --- a/smart-contract/contracts/DataRetrievability.sol +++ b/smart-contract/contracts/DataRetrievability.sol @@ -61,6 +61,8 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { mapping(uint256 => bool) processed; // Counter for slashes uint128 slashes; + // Block timestamp of deal creation + uint256 request_timestamp; // Adding block timestamp to calculate timeout uint256 origin_timestamp; } @@ -114,13 +116,18 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { event DealRedeemed(uint256 index); // Event emitted when new appeal is created event AppealCreated(uint256 index, address provider, string deal_uri); + // Event emitted when new appeal started + event AppealStarted(uint256 index); // Event emitted when a slash message is recorded event RoundSlashed(uint256 index); // Event emitted when a deal is invalidated by an appeal event DealInvalidated(uint256 index); constructor(address _protocol_address) ERC721("Retriev", "RTV") { - require(_protocol_address != address(0), "Can't init protocol with black-hole"); + require( + _protocol_address != address(0), + "Can't init protocol with black-hole" + ); protocol_address = _protocol_address; } @@ -345,7 +352,9 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { ); uint256 maximum_collateral = slashing_multiplier * msg.value; require( - msg.value >= min_deal_value && collateral >= msg.value && collateral <= maximum_collateral, + msg.value >= min_deal_value && + collateral >= msg.value && + collateral <= maximum_collateral, "Collateral or value out of range" ); // Creating next id @@ -456,10 +465,13 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { } /* - This method will allow referees to create an appeal + This method will allow client to create an appeal */ function createAppeal(uint256 deal_index) external payable nonReentrant { - require(tot_appeals[deal_index] < max_appeals, "Can't create more appeals on deal"); + require( + tot_appeals[deal_index] < max_appeals, + "Can't create more appeals on deal" + ); require(deals[deal_index].timestamp_start > 0, "Deal is not active"); require( block.timestamp < @@ -497,7 +509,7 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { // Creating appeal appeals[index].deal_index = deal_index; appeals[index].active = true; - appeals[index].origin_timestamp = block.timestamp; + appeals[index].request_timestamp = block.timestamp; // Emit appeal created event emit AppealCreated( index, @@ -506,6 +518,23 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { ); } + /* + This method will allow referees to start an appeal + */ + function startAppeal(uint256 appeal_index) external { + require( + appeals[appeal_index].origin_timestamp == 0, + "Appeal started yet" + ); + require( + referees[msg.sender].active, + "Only referees can start appeals" + ); + appeals[appeal_index].origin_timestamp = block.timestamp; + // Emit appeal created event + emit AppealStarted(appeal_index); + } + /* This method will allow referees to process an appeal */ @@ -604,9 +633,9 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { uint8 value8, uint32 value32 ) external onlyOwner { - if(kind == 0) { + if (kind == 0) { committee_divider = value8; - }else if (kind == 1) { + } else if (kind == 1) { slashing_multiplier = value256; } else if (kind == 2) { proposal_timeout = value32; @@ -620,7 +649,7 @@ contract DataRetrievability is ERC721, Ownable, ReentrancyGuard { slashes_threshold = value8; } else if (kind == 7) { rounds_limit = value8; - } else if(kind == 8){ + } else if (kind == 8) { max_appeals = value8; } } diff --git a/smart-contract/scripts/appeal:start.js b/smart-contract/scripts/appeal:start.js new file mode 100644 index 0000000..eb47cb4 --- /dev/null +++ b/smart-contract/scripts/appeal:start.js @@ -0,0 +1,33 @@ +const { ethers, utils } = require("ethers"); +const fs = require('fs'); +const { generate, derive } = require('../libs/address_generator') + +async function main() { + const configs = JSON.parse(fs.readFileSync(process.env.CONFIG).toString()) + const ABI = JSON.parse(fs.readFileSync('./artifacts/contracts/' + configs.contract_name + '.sol/' + configs.contract_name + '.json').toString()) + const provider = new ethers.providers.JsonRpcProvider(configs.provider); + const wallet = new ethers.Wallet(configs.referees[0].key).connect(provider) + const contract = new ethers.Contract(configs.contract_address, ABI.abi, wallet) + + // Working always with last deal + const deal_index = await contract.totalDeals() + + try { + const tx = await contract.startAppeal(deal_index) + console.log('Pending transaction at: ' + tx.hash) + await tx.wait() + console.log('Appeal successfully started at ' + tx.hash + '!') + const appeal = await contract.appeals(deal_index) + console.log("Appeal is:", appeal) + } catch (e) { + console.log(e.message) + console.log('Can\'t create appeal, check transaction.') + } +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); diff --git a/smart-contract/scripts/tests/appeal:resolve.sh b/smart-contract/scripts/tests/appeal:resolve.sh index 0e790d2..7c11e6a 100644 --- a/smart-contract/scripts/tests/appeal:resolve.sh +++ b/smart-contract/scripts/tests/appeal:resolve.sh @@ -11,6 +11,7 @@ yarn task deal:create $1 yarn task deposit $1 yarn task deal:accept $1 yarn task appeal:create $1 +yarn task appeal:start $1 yarn task appeal:round $1 echo "Waiting 60s before redeem deal" sleep 60 diff --git a/smart-contract/scripts/tests/appeal:slash.sh b/smart-contract/scripts/tests/appeal:slash.sh index 4019da3..f72cf7e 100644 --- a/smart-contract/scripts/tests/appeal:slash.sh +++ b/smart-contract/scripts/tests/appeal:slash.sh @@ -8,13 +8,9 @@ yarn task render $1 fi yarn task deal:create $1 -sleep 5 yarn task deposit $1 -sleep 5 yarn task deal:accept $1 -sleep 5 yarn task appeal:create $1 -sleep 5 +yarn task appeal:start $1 yarn task appeal:slash $1 -sleep 5 yarn task vault $1 \ No newline at end of file