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

Commit

Permalink
fix: bugs in fetching timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
satello committed Jun 7, 2018
1 parent 5da5bc2 commit 1d5af49
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 32 deletions.
20 changes: 16 additions & 4 deletions src/contracts/abstractions/Arbitrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash'

import * as arbitratorConstants from '../../constants/arbitrator'
import AbstractContract from '../AbstractContract'
import EventListener from '../../utils/EventListener'

/**
* Arbitrator Abstract Contarct API. This wraps an arbitrator contract. It provides
Expand Down Expand Up @@ -44,21 +45,32 @@ class Arbitrator extends AbstractContract {
// update user profile for each dispute
await Promise.all(
myDisputes.map(async dispute => {
const disputeCreationLog = (await EventListener.getEventLogs(
this._contractImplementation,
'DisputeCreation',
0,
'latest',
{ "_disputeID": dispute.disputeId}
))[0]
if (!disputeCreationLog)
throw new Error('Could not fetch dispute creation event log')
// update profile for account
await this._StoreProvider.updateDisputeProfile(
account,
dispute.arbitratorAddress,
dispute.disputeId,
{
appealDraws: dispute.appealDraws
appealDraws: dispute.appealDraws,
blockNumber: disputeCreationLog.blockNumber
}
)
})
)

this._StoreProvider.updateUserProfile(account, {
session: currentSession
})
// FIXME do we want to store session?
// this._StoreProvider.updateUserProfile(account, {
// session: currentSession
// })
}

return _getDisputesForUserFromStore(account)
Expand Down
15 changes: 9 additions & 6 deletions src/contracts/implementations/arbitrator/KlerosPOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,10 @@ class KlerosPOC extends ContractImplementation {
numberOfAppeals + 1
)

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

// skip first execute phase as this is the original ruling
for (let i = 1; i < eventLogs.length; i++) {
Expand All @@ -662,7 +665,7 @@ class KlerosPOC extends ContractImplementation {
* @returns {object} dispute creation event log.
*/
getDisputeCreationEvent = async disputeId => {
const eventLogs = await EventListener.getNextEventLogs(
const eventLogs = await EventListener.getEventLogs(
this,
'DisputeCreation',
0
Expand All @@ -671,7 +674,7 @@ class KlerosPOC extends ContractImplementation {
for (let i = 0; i < eventLogs.length; i++) {
const log = eventLogs[i]

if (log.args.disputeID.toNumber() === disputeId) return log
if (log.args._disputeID.toNumber() === disputeId) return log
}

return null
Expand All @@ -684,7 +687,7 @@ class KlerosPOC extends ContractImplementation {
* @returns {number} The net total PNK
*/
getNetTokensForDispute = async (disputeId, account) => {
const eventLogs = await EventListener.getNextEventLogs(
const eventLogs = await EventListener.getEventLogs(
this,
'TokenShift',
0,
Expand All @@ -695,7 +698,7 @@ class KlerosPOC extends ContractImplementation {
let netPNK = 0
for (let i = 0; i < eventLogs.length; i++) {
const event = eventLogs[i]
if (event.args.disputeID.toNumber() === disputeId)
if (event.args._disputeID.toNumber() === disputeId)
netPNK += event.args._amount.toNumber()
}

Expand All @@ -718,7 +721,7 @@ class KlerosPOC extends ContractImplementation {
* @returns {object[]} an array of event logs.
*/
_getNewPeriodEventLogs = async (blockNumber, periodNumber, appeals = 0) => {
const logs = await EventListener.getNextEventLogs(
const logs = await EventListener.getEventLogs(
this,
'NewPeriod',
blockNumber
Expand Down
75 changes: 55 additions & 20 deletions src/resources/Disputes.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ class Disputes {
}
}

_getDisputeStartBlock = async (disputeId, account) => {
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()

let blockNumber

try {
const userData = await this._StoreProviderInstance.getDispute(
account,
arbitratorAddress,
disputeId
)
blockNumber = userData.blockNumber
// eslint-disable-next-line no-unused-vars
} catch (err) {}
// if block number is not stored we can look it up
if (!blockNumber) {
// 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
// see if we can get dispute start block from events
const disputeCreationEvent = await this._ArbitratorInstance.getDisputeCreationEvent(
disputeId
)
if (disputeCreationEvent) {
blockNumber = disputeCreationEvent.blockNumber
}
}
return blockNumber
}

// **************************** //
// * Public * //
// **************************** //
Expand All @@ -118,7 +146,7 @@ class Disputes {
* @param {string} disputeId - The index of the dispute.
* @returns {Promise} The dispute data in the store.
*/
getDisputeFromStore = (account, disputeId) => {
getDisputeFromStore = async (account, disputeId) => {
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()
return this._StoreProviderInstance.getDispute(
account,
Expand All @@ -127,6 +155,17 @@ class Disputes {
)
}

getDisputeDeadline = async (disputeId, account, appeal=0) => {
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 []

return this._ArbitratorInstance.getDisputeDeadlineTimestamps(
startBlock,
appeal
)
}

/**
* Get data for a dispute. This method provides data from the store as well as both
* arbitrator and arbitrable contracts. Used to get all relevant data on a dispute.
Expand Down Expand Up @@ -164,27 +203,23 @@ class Disputes {
let appealCreatedAt = []
let appealDeadlines = []
let appealRuledAt = []
let startBlock
try {
const userData = await this._StoreProviderInstance.getDispute(
account,
arbitratorAddress,
disputeId
)
if (userData.appealDraws) appealDraws = userData.appealDraws || []
startBlock = userData.blockNumber
// eslint-disable-next-line no-unused-vars
} 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
// see if we can get dispute start block from events
const disputeCreationEvent = this._ArbitratorInstance.getDisputeCreationEvent(
disputeId
)
if (disputeCreationEvent) startBlock = disputeCreationEvent.blockNumber
}
const startBlock = await this._getDisputeStartBlock(disputeId, account)

// dispute exists
if (startBlock) {
// get timestamps
try {
const userData = await this._StoreProviderInstance.getDispute(
account,
arbitratorAddress,
disputeId
)
if (userData.appealDraws) appealDraws = userData.appealDraws || []
// eslint-disable-next-line no-unused-vars
} catch (err) {
// Dispute exists on chain but not in store. We have lost draws for past disputes.
console.error("Dispute does not exist in store.")
}

appealDeadlines = await this._ArbitratorInstance.getDisputeDeadlineTimestamps(
startBlock,
dispute.numberOfAppeals
Expand Down
2 changes: 1 addition & 1 deletion src/utils/EventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class EventListener {
* @param {object} filters - Extra filters
* @returns {Promise} All events in block range.
*/
static getNextEventLogs = async (
static getEventLogs = async (
contractImplementationInstance = isRequired(
'contractImplementationInstance'
),
Expand Down
2 changes: 1 addition & 1 deletion src/utils/StoreProviderWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class StoreProviderWrapper {
getContractByAddress = async (userAddress, addressContract) => {
const userProfile = await this.getUserProfile(userAddress)
if (!userProfile)
throw new Error(errorConstants.PROFILE_NOT_FOUND(userAddress))
return {}

let contract = _.filter(
userProfile.contracts,
Expand Down

0 comments on commit 1d5af49

Please sign in to comment.