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

Commit

Permalink
feat: use session for dispute deadline and appeal created at
Browse files Browse the repository at this point in the history
  • Loading branch information
satello committed Jun 12, 2018
1 parent d97fc97 commit 2920255
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"dependencies": {
"babel-runtime": "^6.26.0",
"eth-sig-util": "^1.4.2",
"kleros": "^0.0.5",
"kleros": "^0.0.6",
"kleros-interaction": "^0.0.8",
"lodash": "^4.17.4",
"truffle-contract": "^2.0.5",
Expand Down
22 changes: 13 additions & 9 deletions src/contracts/abstractions/Arbitrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ class Arbitrator extends AbstractContract {
this._contractImplementation.getSession()
])

const _getDisputesForUserFromStore = async account =>
Promise.all(
(await this._StoreProvider.getDisputes(account)).map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId)
)
)

// new jurors have not been chosen yet. don't update
if (period !== arbitratorConstants.PERIOD.VOTE) {
return _getDisputesForUserFromStore(account)
return this.getDisputesForUserFromStore(account)
}

let profile = await this._StoreProvider.newUserProfile(account)
Expand Down Expand Up @@ -69,7 +62,18 @@ class Arbitrator extends AbstractContract {
// })
}

return _getDisputesForUserFromStore(account)
return this.getDisputesForUserFromStore(account)
}

getDisputesForUserFromStore = async account => {
const aribtratorAddress = this._contractImplementation.getContractAddress()
return Promise.all(
(await this._StoreProvider.getDisputes(account)).filter(dispute =>
dispute.arbitratorAddress === aribtratorAddress
).map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId)
)
)
}

/**
Expand Down
63 changes: 41 additions & 22 deletions src/contracts/implementations/arbitrator/KlerosPOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,29 +605,24 @@ class KlerosPOC extends ContractImplementation {
* @param {number} appeal - the number of appeals we need to fetch events for.
* @returns {number[]} an array of timestamps
*/
getDisputeDeadlineTimestamps = async (blockNumber, appeal = 0) => {
const eventLogs = await this._getNewPeriodEventLogs(
blockNumber,
getDisputeDeadlineTimestamp = async session => {
const eventLog = await this._getNewPeriodEventLogForSession(
session,
arbitratorConstants.PERIOD.VOTE,
appeal
)
// May not have happened yet
if (!eventLog) return null

// Fetch length of Vote period
const periodLength = await this.getTimeForPeriod(
arbitratorConstants.PERIOD.VOTE
)
const eventLogTimestamps = []

for (let i = 0; i < eventLogs.length; i++) {
const eventLog = eventLogs[i]

const periodStartTimestamp = await this._getTimestampForBlock(
eventLog.blockNumber
)

eventLogTimestamps.push((periodLength + periodStartTimestamp) * 1000)
}
// Get the time that the period started
const periodStartTimestamp = await this._getTimestampForBlock(
eventLog.blockNumber
)

return eventLogTimestamps
return (periodLength + periodStartTimestamp) * 1000
}

/**
Expand All @@ -636,13 +631,15 @@ class KlerosPOC extends ContractImplementation {
* @param {number} numberOfAppeals - the number of appeals we need to fetch events for.
* @returns {number[]} an array of timestamps
*/
getAppealCreationTimestamps = async (blockNumber, numberOfAppeals) => {
const eventLogs = await this._getNewPeriodEventLogs(
blockNumber,
arbitratorConstants.PERIOD.VOTE,
numberOfAppeals + 1
getAppealCreationTimestamps = async session => {
const eventLog = await this._getNewPeriodEventLogForSession(
session,
arbitratorConstants.PERIOD.EXECUTE
)

// May not have happened yet
if (!eventLog) return null

const creationTimestamp = await this._getTimestampForBlock(blockNumber)
const eventLogTimestamps = [creationTimestamp * 1000]

Expand Down Expand Up @@ -725,7 +722,7 @@ class KlerosPOC extends ContractImplementation {
const logs = await EventListener.getEventLogs(
this,
'NewPeriod',
blockNumber
0
)
// List off all appeal logs for period
const eventLogs = []
Expand Down Expand Up @@ -764,6 +761,28 @@ class KlerosPOC extends ContractImplementation {
return eventLogs
}

/**
* Get event NewPeriod event logs a period in a session.
* @param {number} session - The session number.
* @param {number} periodNumber - The period number we want logs for.
* @returns {object} event log object.
*/
_getNewPeriodEventLogForSession = async (session, periodNumber) => {
const logs = await EventListener.getEventLogs(
this,
'NewPeriod',
0,
'latest',
{ _session: session }
)
for (let i=0; i<logs.length; i++) {
const eventLog = logs[i]
if (eventLog.args._period.toNumber() === periodNumber) return eventLog
}
// We have hit the latest event log and did not find data. Return all that we have.
return null
}

/**
* Get data from Kleros contract.
* TODO split these into their own methods for more flexability and speed
Expand Down
25 changes: 12 additions & 13 deletions src/resources/Disputes.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,30 +192,29 @@ class Disputes {
* @returns {number} timestamp of the appeal
*/
getDisputeDeadline = async (disputeId, account, appeal = 0) => {
const cachedDispute = this.disputeCache[disputeId]
const cachedDispute = this.disputeCache[disputeId] || {}
if (
cachedDispute &&
cachedDispute.appealDeadlines &&
cachedDispute.appealDeadlines[appeal]
)
return cachedDispute.appealDeadlines[appeal]

const startBlock = await this._getDisputeStartBlock(disputeId, account)
// if there is no start block that means that dispute has not been created yet.
if (!startBlock) return []
const dispute = await this._ArbitratorInstance.getDispute(disputeId)

const deadlineTimestamps = await this._ArbitratorInstance.getDisputeDeadlineTimestamps(
startBlock,
appeal
const deadlineTimestamp = await this._ArbitratorInstance.getDisputeDeadlineTimestamp(
dispute.firstSession + appeal
)

// cache the deadline for the appeal
if (deadlineTimestamps.length > 0)
if (deadlineTimestamp) {
const currentDeadlines = cachedDispute.appealDeadlines || []
currentDeadlines[appeal] = deadlineTimestamp
// cache the deadline for the appeal
this._updateDisputeCache(disputeId, {
appealDeadlines: deadlineTimestamps
appealDeadlines: currentDeadlines
})
}

return deadlineTimestamps[appeal]
return deadlineTimestamp
}

/**
Expand Down Expand Up @@ -273,7 +272,7 @@ class Disputes {
// if there is no start block that means that dispute has not been created yet.
if (!startBlock) return []

const appealCreatedAtTimestamps = await this._ArbitratorInstance.getDisputeDeadlineTimestamps(
const appealCreatedAtTimestamps = await this._ArbitratorInstance.getAppealCreationTimestamps(
startBlock,
appeal
)
Expand Down
10 changes: 7 additions & 3 deletions src/resources/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,13 @@ class Notifications {
*/
_newPeriodHandler = async (event, account, callback) => {
const newPeriod = event.args._period.toNumber()

// send appeal possible notifications
if (newPeriod === arbitratorConstants.PERIOD.APPEAL) {
const eventSession = event.args._session.toNumber()
const currentSession = await this._ArbitratorInstance.getSession()
// send appeal possible notifications if in current session
if (
newPeriod === arbitratorConstants.PERIOD.APPEAL &&
eventSession === currentSession
) {
const disputes = await this._getDisputes(account) // get users disputes
const openDisputes = await this._ArbitratorInstance.getOpenDisputesForSession() // get all disputes for session
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()
Expand Down
1 change: 0 additions & 1 deletion src/utils/EventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class EventListener {
filters = {}
) => {
await contractImplementationInstance.loadContract()

return new Promise((resolve, reject) => {
contractImplementationInstance.contractInstance[eventName](filters, {
fromBlock: firstBlock,
Expand Down

0 comments on commit 2920255

Please sign in to comment.