Skip to content
Permalink
Browse files

feat: use session for dispute deadline and appeal created at

  • Loading branch information...
satello committed Jun 11, 2018
1 parent d97fc97 commit 2920255785a3e8400e0109f9e1910813940e7d64
@@ -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",
@@ -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)
@@ -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)
)
)
}

/**
@@ -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
}

/**
@@ -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]

@@ -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 = []
@@ -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
@@ -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
}

/**
@@ -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
)
@@ -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()
@@ -75,7 +75,6 @@ class EventListener {
filters = {}
) => {
await contractImplementationInstance.loadContract()

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

0 comments on commit 2920255

Please sign in to comment.
You can’t perform that action at this time.