Skip to content

Commit

Permalink
adding function to start appeal and bootstrap fn
Browse files Browse the repository at this point in the history
  • Loading branch information
turinglabsorg committed Jul 12, 2022
1 parent 8d25e38 commit 30595d0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
8 changes: 7 additions & 1 deletion referee-cli/src/libs/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
45 changes: 37 additions & 8 deletions smart-contract/contracts/DataRetrievability.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <
Expand Down Expand Up @@ -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,
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down
33 changes: 33 additions & 0 deletions smart-contract/scripts/appeal:start.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions smart-contract/scripts/tests/appeal:resolve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions smart-contract/scripts/tests/appeal:slash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 30595d0

Please sign in to comment.