Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #12287 from NejcZdovc/hotfix/#12272-caching
Browse files Browse the repository at this point in the history
Improves verified-caching
  • Loading branch information
bsclifton authored Jan 8, 2018
2 parents a24496c + b96a73f commit 9f70216
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 68 deletions.
132 changes: 88 additions & 44 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ let balanceTimeoutId = false
let runTimeoutId
let promotionTimeoutId
let togglePromotionTimeoutId
let verifiedTimeoutId = false

// Database
let v2RulesetDB
Expand Down Expand Up @@ -149,12 +150,24 @@ const paymentPresent = (state, tabId, present) => {
if (!balanceTimeoutId) {
module.exports.getBalance(state)
}

getPublisherTimestamp(true)
} else if (balanceTimeoutId) {
clearTimeout(balanceTimeoutId)
balanceTimeoutId = false
}
}

const getPublisherTimestamp = (updateList) => {
client.publisherTimestamp((err, result) => {
if (err) {
console.error('Error while retrieving publisher timestamp', err.toString())
return
}
appActions.onPublisherTimestamp(result.timestamp, updateList)
})
}

const addFoundClosed = (state) => {
if (balanceTimeoutId) {
clearTimeout(balanceTimeoutId)
Expand Down Expand Up @@ -185,13 +198,8 @@ const onBootStateFile = (state) => {
try {
clientprep()
client = ledgerClient(null, underscore.extend({roundtrip: roundtrip}, clientOptions), null)
client.publisherTimestamp((err, result) => {
if (err) {
console.error('Error while retrieving publisher timestamp', err.toString())
return
}
appActions.onPublisherTimestamp(result.timestamp)
})

getPublisherTimestamp()
} catch (ex) {
state = ledgerState.resetInfo(state)
bootP = false
Expand Down Expand Up @@ -540,21 +548,26 @@ const inspectP = (db, path, publisher, property, key, callback) => {

// TODO rename function name
const verifiedP = (state, publisherKey, callback) => {
client.publisherInfo(publisherKey, (err, result) => {
const clientCallback = (err, result) => {
if (err) {
console.error(`Error verifying publisher ${publisherKey}: `, err.toString())
return
}

if (callback) {
let data = false
if (result && result.properties && result.properties.verified) {
data = result.properties.verified
if (result) {
callback(null, result)
} else {
callback(err, {})
}

callback(null, data)
}
})
}

if (Array.isArray(publisherKey)) {
client.publishersInfo(publisherKey, clientCallback)
} else {
client.publisherInfo(publisherKey, clientCallback)
}

if (process.env.NODE_ENV === 'test') {
['brianbondy.com', 'clifton.io'].forEach((key) => {
Expand Down Expand Up @@ -689,30 +702,50 @@ const saveVisit = (state, publisherKey, options) => {
})
state = ledgerState.setPublisher(state, publisherKey, synopsis.publishers[publisherKey])
state = updatePublisherInfo(state)
state = checkVerifiedStatus(state, publisherKey)
state = module.exports.checkVerifiedStatus(state, publisherKey)

return state
}

const checkVerifiedStatus = (state, publisherKey) => {
if (publisherKey == null) {
const checkVerifiedStatus = (state, publisherKeys, publisherTimestamp) => {
if (publisherKeys == null) {
return state
}

const lastUpdate = parseInt(ledgerState.getLedgerValue(state, 'publisherTimestamp'))
const lastPublisherUpdate = parseInt(ledgerState.getPublisherOption(state, publisherKey, 'verifiedTimestamp') || 0)
if (!Array.isArray(publisherKeys)) {
publisherKeys = [publisherKeys]
}

const checkKeys = []
const lastUpdate = parseInt(publisherTimestamp || ledgerState.getLedgerValue(state, 'publisherTimestamp'))

if (lastUpdate > lastPublisherUpdate) {
state = module.exports.verifiedP(state, publisherKey, (error, result) => {
if (!error) {
appActions.onPublisherOptionUpdate(publisherKey, 'verified', result)
appActions.onPublisherOptionUpdate(publisherKey, 'verifiedTimestamp', lastUpdate)
savePublisherOption(publisherKey, 'verified', result)
savePublisherOption(publisherKey, 'verifiedTimestamp', lastUpdate)
}
})
publisherKeys.forEach(key => {
const lastPublisherUpdate = parseInt(ledgerState.getPublisherOption(state, key, 'verifiedTimestamp') || 0)

if (lastUpdate > lastPublisherUpdate) {
checkKeys.push(key)
}
})

if (checkKeys.length === 0) {
return state
}

state = module.exports.verifiedP(state, checkKeys, (error, result) => {
if (!error) {
const publisherKey = result.publisher

if (result && result.properties && result.properties) {
const verified = !!result.properties.verified
appActions.onPublisherOptionUpdate(publisherKey, 'verified', verified)
savePublisherOption(publisherKey, 'verified', verified)
}

appActions.onPublisherOptionUpdate(publisherKey, 'verifiedTimestamp', lastUpdate)
savePublisherOption(publisherKey, 'verifiedTimestamp', lastUpdate)
}
})

return state
}

Expand Down Expand Up @@ -1748,12 +1781,18 @@ const initialize = (state, paymentsEnabled) => {

ledgerNotifications.init()

if (verifiedTimeoutId) {
clearInterval(verifiedTimeoutId)
}

if (!paymentsEnabled) {
client = null
newClient = false
return ledgerState.resetInfo(state, true)
}

verifiedTimeoutId = setInterval(getPublisherTimestamp, 1 * ledgerUtil.milliseconds.hour)

if (client) {
return state
}
Expand Down Expand Up @@ -1838,13 +1877,8 @@ const onInitRead = (state, parsedData) => {
client = ledgerClient(parsedData.personaId,
underscore.extend(parsedData.options, {roundtrip: roundtrip}, options),
parsedData)
client.publisherTimestamp((err, result) => {
if (err) {
console.error('Error while retrieving publisher timestamp', err.toString())
return
}
appActions.onPublisherTimestamp(result.timestamp)
})

getPublisherTimestamp(true)

// Scenario: User enables Payments, disables it, waits 30+ days, then
// enables it again -> reconcileStamp is in the past.
Expand Down Expand Up @@ -2272,13 +2306,7 @@ const transitionWalletToBat = () => {
}))
appActions.onBitcoinToBatTransitioned()
ledgerNotifications.showBraveWalletUpdated()
client.publisherTimestamp((err, result) => {
if (err) {
console.error('Error while retrieving publisher timestamp', err.toString())
return
}
appActions.onPublisherTimestamp(result.timestamp)
})
getPublisherTimestamp()
}
})
} catch (ex) {
Expand Down Expand Up @@ -2508,6 +2536,21 @@ const onPromotionResponse = (state, status) => {
return state
}

const onPublisherTimestamp = (state, oldTimestamp, newTimestamp) => {
if (oldTimestamp === newTimestamp) {
return
}

const publishers = ledgerState.getPublishers(state)
if (publishers.isEmpty()) {
return
}

publishers.forEach((publisher, key) => {
module.exports.checkVerifiedStatus(state, key, newTimestamp)
})
}

const getMethods = () => {
const publicMethods = {
backupKeys,
Expand Down Expand Up @@ -2550,7 +2593,9 @@ const getMethods = () => {
claimPromotion,
onPromotionResponse,
getBalance,
getPromotion
getPromotion,
onPublisherTimestamp,
checkVerifiedStatus
}

let privateMethods = {}
Expand Down Expand Up @@ -2584,7 +2629,6 @@ const getMethods = () => {
},
getCurrentMediaKey: (key) => currentMediaKey,
synopsisNormalizer,
checkVerifiedStatus,
roundtrip,
observeTransactions,
onWalletRecovery,
Expand Down
4 changes: 4 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_ON_PUBLISHER_TIMESTAMP:
{
const oldValue = ledgerState.getLedgerValue(state, 'publisherTimestamp')
state = ledgerState.setLedgerValue(state, 'publisherTimestamp', action.get('timestamp'))
if (action.get('updateList')) {
ledgerApi.onPublisherTimestamp(state, oldValue, action.get('timestamp'))
}
break
}
case appConstants.APP_SAVE_LEDGER_PROMOTION:
Expand Down
26 changes: 13 additions & 13 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,19 @@ AppStore
},
info: {
addresses: {
BAT: string,
BTC: string,
CARD_ID: string,
ETH: string,
BAT: string,
BTC: string,
CARD_ID: string,
ETH: string,
LTC: string
},
balance: number, // confirmed balance in BAT.toFixed(2)
bravery: {
days: number,
days: number,
fee: {
amount: number,
currency: string
},
},
setting: string
},
converted: string,
Expand All @@ -210,9 +210,9 @@ AppStore
paymentId: string,
probi: number,
rates:{
BTC: string,
ETH: number,
EUR: number,
BTC: string,
ETH: number,
EUR: number,
USD: number
},
reconcileFrequency: number // duration between each reconciliation in days
Expand Down Expand Up @@ -265,7 +265,7 @@ AppStore
options: {
persist: boolean,
style: string
}
}
},
panel: {
optInMarkup: {
Expand All @@ -288,7 +288,7 @@ AppStore
options: {
persist: boolean,
style: string
}
}
},
panel: {
disclaimer: string,
Expand All @@ -313,7 +313,7 @@ AppStore
options: {
persist: boolean,
style: string
}
}
},
panel: {
disclaimer: string,
Expand Down Expand Up @@ -348,7 +348,7 @@ AppStore
options: {
exclude: boolean,
verified: boolean,
verifiedTimestamp: number, // timestamp of the last change
verifiedTimestamp: number, // timestamp of the last change
stickyP: boolean
},
pinPercentage: number,
Expand Down
5 changes: 3 additions & 2 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1826,10 +1826,11 @@ const appActions = {
})
},

onPublisherTimestamp: function (timestamp) {
onPublisherTimestamp: function (timestamp, updateList) {
dispatch({
actionType: appConstants.APP_ON_PUBLISHER_TIMESTAMP,
timestamp
timestamp,
updateList
})
},

Expand Down
Loading

0 comments on commit 9f70216

Please sign in to comment.