Skip to content
Permalink
Browse files

feat: get all period based timestamps by session

  • Loading branch information...
satello committed Jun 11, 2018
1 parent 2920255 commit a6d19bb8d7e8da6b428147775ae9fe2c616a8f91
Showing with 50 additions and 109 deletions.
  1. +17 −81 src/contracts/implementations/arbitrator/KlerosPOC.js
  2. +33 −28 src/resources/Disputes.js
@@ -579,24 +579,19 @@ class KlerosPOC extends ContractImplementation {
* @param {number} appeal - the number of appeals we need to fetch events for.
* @returns {number[]} an array of timestamps
*/
getAppealRuledAtTimestamps = async (blockNumber, appeal = 0) => {
const eventLogs = await this._getNewPeriodEventLogs(
blockNumber,
arbitratorConstants.PERIOD.APPEAL,
appeal
getAppealRuledAtTimestamp = async (session) => {
const eventLog = await this._getNewPeriodEventLogForSession(
session,
arbitratorConstants.PERIOD.APPEAL
)
// May not have happened yet
if (!eventLog) return null

const eventLogTimestamps = []

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

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

eventLogTimestamps.push(timestamp * 1000)
}
const ruledAtTimestamp = await this._getTimestampForBlock(
eventLog.blockNumber
)

return eventLogTimestamps
return ruledAtTimestamp * 1000
}

/**
@@ -608,7 +603,7 @@ class KlerosPOC extends ContractImplementation {
getDisputeDeadlineTimestamp = async session => {
const eventLog = await this._getNewPeriodEventLogForSession(
session,
arbitratorConstants.PERIOD.VOTE,
arbitratorConstants.PERIOD.VOTE
)
// May not have happened yet
if (!eventLog) return null
@@ -631,28 +626,19 @@ 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 session => {
getAppealCreationTimestamp = 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]

// skip first execute phase as this is the original ruling
for (let i = 1; i < eventLogs.length; i++) {
const eventLog = eventLogs[i]

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

eventLogTimestamps.push(timestamp * 1000)
}
const createdAtTimestamp = await this._getTimestampForBlock(
eventLog.blockNumber
)

return eventLogTimestamps
return createdAtTimestamp * 1000
}

/**
@@ -666,7 +652,7 @@ class KlerosPOC extends ContractImplementation {
'DisputeCreation',
0,
'latest',
{ _disputeId: disputeId }
{ _disputeID: disputeId }
)

for (let i = 0; i < eventLogs.length; i++) {
@@ -711,56 +697,6 @@ class KlerosPOC extends ContractImplementation {
_getTimestampForBlock = async blockNumber =>
(await this.getBlock(blockNumber)).timestamp

/**
* Get event NewPeriod event logs for a certain number of appeals.
* @param {number} blockNumber - The block number the dispute was created.
* @param {number} periodNumber - The period number we want logs for.
* @param {number} appeals - The number of appeals.
* @returns {object[]} an array of event logs.
*/
_getNewPeriodEventLogs = async (blockNumber, periodNumber, appeals = 0) => {
const logs = await EventListener.getEventLogs(
this,
'NewPeriod',
0
)
// List off all appeal logs for period
const eventLogs = []
let skip = false

let appealCount = appeals

const firstLogPeriod = logs[0].args._period.toNumber()
// if dispute was created for next session and there is still an event in this session for that period
if (
firstLogPeriod >= arbitratorConstants.PERIOD.VOTE &&
firstLogPeriod <= periodNumber
)
skip = true

for (let i = 0; i < logs.length; i++) {
const eventLog = logs[i]
const period = eventLog.args._period.toNumber()
if (period === periodNumber) {
if (skip) {
skip = false
} else {
// this is if dispute was created in the previous session.
eventLogs[appeals - appealCount] = eventLog
appealCount -= 1

// we have exhausted all appeals. Return events
if (appealCount < 0) {
return eventLogs
}
}
}
}

// We have hit the latest event log and did not find data. Return all that we have.
return eventLogs
}

/**
* Get event NewPeriod event logs a period in a session.
* @param {number} session - The session number.
@@ -187,11 +187,10 @@ class Disputes {
/**
* Get the dispute deadline for the appeal.
* @param {number} disputeId - The index of the dispute.
* @param {string} account - The users address.
* @param {number} appeal - The appeal number. 0 if there have been no appeals.
* @returns {number} timestamp of the appeal
*/
getDisputeDeadline = async (disputeId, account, appeal = 0) => {
getDisputeDeadline = async (disputeId, appeal = 0) => {
const cachedDispute = this.disputeCache[disputeId] || {}
if (
cachedDispute.appealDeadlines &&
@@ -233,23 +232,21 @@ class Disputes {
)
return cachedDispute.appealRuledAt[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 appealRuledAtTimestamps = await this._ArbitratorInstance.getAppealRuledAtTimestamps(
startBlock,
appeal
const dispute = await this._ArbitratorInstance.getDispute(disputeId)
const appealRuledAtTimestamp = await this._ArbitratorInstance.getAppealRuledAtTimestamp(
dispute.firstSession + appeal
)

// cache the deadline for the appeal
if (appealRuledAtTimestamps.length > 0) {
if (appealRuledAtTimestamp) {
const currentRuledAt = cachedDispute.appealRuledAt || []
currentRuledAt[appeal] = appealRuledAtTimestamp
this._updateDisputeCache(disputeId, {
appealRuledAt: appealRuledAtTimestamps
appealRuledAt: currentRuledAt
})
}

return appealRuledAtTimestamps[appeal]
return appealRuledAtTimestamp
}

/**
@@ -268,23 +265,34 @@ class Disputes {
)
return cachedDispute.appealCreatedAt[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 appealCreatedAtTimestamps = await this._ArbitratorInstance.getAppealCreationTimestamps(
startBlock,
appeal
)
let appealCreatedAtTimestamp = null
if (appeal === 0) {
const creationBlock = await this._getDisputeStartBlock(disputeId, account)
if (creationBlock) {
const timestampSeconds = await this._ArbitratorInstance._getTimestampForBlock(
creationBlock
)

// cache the deadline for the appeal
if (appealCreatedAtTimestamps) {
this._updateDisputeCache(disputeId, {
appealCreatedAt: appealCreatedAtTimestamps
})
appealCreatedAtTimestamp = timestampSeconds * 1000
}
} else {
appealCreatedAtTimestamp = await this._ArbitratorInstance.getAppealCreationTimestamp(
dispute.firstSession + (appeal - 1) // appeal was created during previous session
)

// cache the deadline for the appeal
if (appealCreatedAtTimestamp) {
const currentCreatedAt = cachedDispute.appealCreatedAt || []
currentCreatedAt[appeal] = appealCreatedAtTimestamp
this._updateDisputeCache(disputeId, {
appealCreatedAt: currentCreatedAt
})
}
}

return appealCreatedAtTimestamps[appeal]
return appealCreatedAtTimestamp
}

/**
@@ -378,17 +386,14 @@ class Disputes {

const appealCreatedAt = await this.getAppealCreatedAt(
dispute.disputeId,
account,
appeal
)
const appealDeadline = await this.getDisputeDeadline(
dispute.disputeId,
account,
appeal
)
const appealRuledAt = await this.getAppealRuledAt(
dispute.disputeId,
account,
appeal
)

0 comments on commit a6d19bb

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