Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
feat(disputes): implement canRepartition and canExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras authored and satello committed Mar 15, 2018
1 parent 8962fea commit 7e7f68f
Showing 1 changed file with 97 additions and 69 deletions.
166 changes: 97 additions & 69 deletions src/abstractWrappers/Disputes.js
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash'


import * as ethConstants from '../constants/eth' import * as ethConstants from '../constants/eth'
import * as arbitratorConstants from '../constants/arbitrator' import * as arbitratorConstants from '../constants/arbitrator'
import * as disputeConstants from '../constants/dispute'
import * as notificationConstants from '../constants/notification' import * as notificationConstants from '../constants/notification'
import * as errorConstants from '../constants/error' import * as errorConstants from '../constants/error'


Expand Down Expand Up @@ -331,7 +332,8 @@ class Disputes extends AbstractWrapper {
this.getDataForDispute( this.getDataForDispute(
dispute.arbitratorAddress, dispute.arbitratorAddress,
dispute.disputeId, dispute.disputeId,
account account,
arbitratorData
) )
) )
) )
Expand Down Expand Up @@ -649,100 +651,124 @@ class Disputes extends AbstractWrapper {


/** /**
* Get data for a dispute. * Get data for a dispute.
* @param {string} arbitratorAddress - Address for arbitrator contract. * @param {string} arbitratorAddress - The arbitrator contract's address.
* @param {number} disputeId - Index of dispute. * @param {number} disputeID - The dispute's ID.
* @param {string} account - Juror account address. * @param {string} account - The juror's address.
* @returns {object} - Data object for dispute that uses data from the contract and store. * @param {object} [arbitratorData={}] - Optional arbitrator data for computing `canRepartition`.
* @returns {object} - Data object for the dispute that uses data from the contract and the store.
* TODO: Should we return what we have in the store even if dispute is not in the contract?
*/ */
getDataForDispute = async (arbitratorAddress, disputeId, account) => { getDataForDispute = async (
arbitratorAddress,
disputeID,
account,
{ session, period } = {}
) => {
this._checkArbitratorWrappersSet() this._checkArbitratorWrappersSet()
this._checkArbitrableWrappersSet() this._checkArbitrableWrappersSet()


// FIXME should we just return what we have in the store? // Get dispute data from contract, and throw if not found. Also get current session and period
const dispute = await this._Arbitrator.getDispute( const dispute = await this._Arbitrator.getDispute(
arbitratorAddress, arbitratorAddress,
disputeId disputeID
) )
if (!dispute) { if (!dispute) {
throw new Error( throw new Error(
`Dispute with arbitrator: ${arbitratorAddress} and disputeId: ${disputeId} does not exist` `Dispute with arbitrator: ${arbitratorAddress} and disputeId: ${disputeID} does not exist`
) )
} }
const arbitrableContractAddress = dispute.arbitratedContract


const arbitrableContractData = await this._ArbitrableContract.getData( // Get arbitrable contract data and evidence
arbitrableContractAddress const arbitrableContractAddress = dispute.arbitratedContract
) const [arbitrableContractData, evidence] = await Promise.all([
const constractStoreData = await this._StoreProvider.getContractByAddress( this._ArbitrableContract.getData(arbitrableContractAddress),
this.getEvidenceForArbitrableContract(arbitrableContractAddress)
])
const contractStoreData = await this._StoreProvider.getContractByAddress(
arbitrableContractData.partyA, arbitrableContractData.partyA,
arbitrableContractAddress arbitrableContractAddress
) )


// Get dispute data from the store
let appealDraws = [] let appealDraws = []
let netPNK = 0
let appealCreatedAt = [] let appealCreatedAt = []
let appealRuledAt = []
let appealDeadlines = [] let appealDeadlines = []
if (account) { let appealRuledAt = []
try { let netPNK = 0
const userData = await this._StoreProvider.getDisputeData( try {
arbitratorAddress, const userData = await this._StoreProvider.getDisputeData(
disputeId, arbitratorAddress,
account disputeID,
) account
appealDraws = userData.appealDraws || [] )
netPNK = userData.netPNK || 0 if (userData.appealDraws) appealDraws = userData.appealDraws
appealCreatedAt = userData.appealCreatedAt || [] if (userData.appealCreatedAt) appealCreatedAt = userData.appealCreatedAt
appealRuledAt = userData.appealRuledAt || [] if (userData.appealDeadlines) appealDeadlines = userData.appealDeadlines
appealDeadlines = userData.appealDeadlines || [] if (userData.appealRuledAt) appealRuledAt = userData.appealRuledAt
// eslint-disable-next-line no-unused-vars if (userData.netPNK) netPNK = userData.netPNK
} catch (err) { // eslint-disable-next-line no-unused-vars
// fetching dispute will fail if it hasn't been added to the store yet. this is ok we can just not return store data } catch (err) {
} // Fetching a dispute will fail if it hasn't been added to the store yet. This is ok, we can just not return store data
} }


// get evidence // Build juror info and ruling arrays, indexed by appeal number
const evidence = await this.getEvidenceForArbitrableContract(
arbitrableContractAddress
)

const firstSession = dispute.firstSession
const lastSession = dispute.firstSession + dispute.numberOfAppeals const lastSession = dispute.firstSession + dispute.numberOfAppeals
// NOTE arrays indexed by appeal number
const appealRulings = []
const appealJuror = [] const appealJuror = []
for (let appeal = 0; appeal <= lastSession - firstSession; appeal++) { const appealRulings = []
// get ruling for appeal. Note appeal 0 is first session for (let appeal = 0; appeal <= dispute.numberOfAppeals; appeal++) {
const ruling = await this._Arbitrator.currentRulingForDispute( const isLastAppeal = dispute.firstSession + appeal === lastSession
arbitratorAddress,
disputeId,
appeal
)

appealRulings[appeal] = {
ruling,
voteCounter: dispute.voteCounters[appeal],
ruledAt: appealRuledAt[appeal],
deadline: appealDeadlines[appeal]
}


const baseFee = dispute.arbitrationFeePerJuror // Get appeal data
const draws = appealDraws[appeal] || [] const draws = appealDraws[appeal] || []
let canRule = false let canRule = false
if (firstSession + appeal === lastSession && draws.length > 0) { let canRepartition = false
canRule = await this._Arbitrator.canRuleDispute( let canExecute = false
let ruling
const promises = [
this._Arbitrator.currentRulingForDispute(
arbitratorAddress, arbitratorAddress,
disputeId, disputeID,
draws, appeal
account
) )
]

// Extra info for the last appeal
if (isLastAppeal) {
if (draws.length > 0)
promises.push(
this._Arbitrator.canRuleDispute(
arbitratorAddress,
disputeID,
draws,
account
)
)

if (session && period)
canRepartition =
lastSession <= session && // Not appealed to the next session
period === arbitratorConstants.PERIOD.EXECUTE && // Executable period
dispute.state === disputeConstants.STATE.OPEN // Open dispute
canExecute = dispute.state === disputeConstants.STATE.EXECUTABLE // Executable state
} }


// Wait for parallel requests to complete
;[ruling, canRule] = await Promise.all(promises)

appealJuror[appeal] = { appealJuror[appeal] = {
fee: baseFee * draws.length, createdAt: appealCreatedAt[appeal],
fee: dispute.arbitrationFeePerJuror * draws.length,
draws, draws,
canRule canRule
} }
appealRulings[appeal] = {
voteCounter: dispute.voteCounters[appeal],
deadline: appealDeadlines[appeal],
ruledAt: appealRuledAt[appeal],
ruling,
canRepartition,
canExecute
}
} }


return { return {
Expand All @@ -754,25 +780,27 @@ class Disputes extends AbstractWrapper {
partyB: arbitrableContractData.partyB, partyB: arbitrableContractData.partyB,


// Dispute Data // Dispute Data
disputeId, disputeID,
firstSession, firstSession: dispute.firstSession,
lastSession, lastSession,
numberOfAppeals: dispute.numberOfAppeals, numberOfAppeals: dispute.numberOfAppeals,
disputeState: dispute.state, disputeState: dispute.state,
disputeStatus: dispute.status, disputeStatus: dispute.status,
appealRulings,
appealJuror, appealJuror,
appealRulings,


// Store Data // Store Data
description: constractStoreData description: contractStoreData
? constractStoreData.description ? contractStoreData.description
: undefined, : undefined,
email: constractStoreData ? constractStoreData.email : undefined, email: contractStoreData ? contractStoreData.email : undefined,
evidence, evidence,
netPNK, netPNK,

// Deprecated Data // TODO: Remove
appealCreatedAt, appealCreatedAt,
appealRuledAt, appealDeadlines,
appealDeadlines appealRuledAt
} }
} }


Expand Down

0 comments on commit 7e7f68f

Please sign in to comment.