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 #11495 from brave/fix-persist-edge-case
Browse files Browse the repository at this point in the history
Fix persistence edge-case issue with ledger client
  • Loading branch information
bsclifton committed Oct 16, 2017
1 parent b9f354a commit b8f887c
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const v2RulesetPath = 'ledger-rulesV2.leveldb'
let v2PublishersDB
const v2PublishersPath = 'ledger-publishersV2.leveldb'
const statePath = 'ledger-state.json'
const newClientPath = 'ledger-newstate.json'

// Definitions
const miliseconds = {
Expand Down Expand Up @@ -1880,7 +1881,22 @@ const onCallback = (state, result, delayTime) => {
})
}

// persist the new ledger state
muonWriter(statePath, regularResults)

// delete the temp file used during transition (if it still exists)
if (client && client.options && client.options.version === 'v2') {
const fs = require('fs')
fs.access(pathName(newClientPath), fs.FF_OK, (err) => {
if (err) {
return
}
fs.unlink(pathName(newClientPath), (err) => {
if (err) console.error('unlink error: ' + err.toString())
})
})
}

run(state, delayTime)

return state
Expand Down Expand Up @@ -2241,9 +2257,9 @@ const deleteSynopsis = () => {
let newClient = null
const transitionWalletToBat = () => {
let newPaymentId, result
const newClientPath = 'ledger-newstate.json'

if (newClient === true) return

// Restore newClient from the file
if (!newClient) {
const fs = require('fs')
Expand Down Expand Up @@ -2286,6 +2302,8 @@ const transitionWalletToBat = () => {

if (typeof delayTime === 'undefined') delayTime = random.randomInt({ min: 1, max: 500 })

muonWriter(newClientPath, newClient)

setTimeout(() => transitionWalletToBat(), delayTime)
})
return
Expand All @@ -2306,13 +2324,10 @@ const transitionWalletToBat = () => {
result = newClient.transitioned(properties)
client = newClient
newClient = true
// NOTE: onLedgerCallback will save latest client to disk as ledger-state.json
appActions.onLedgerCallback(result, random.randomInt({ min: miliseconds.minute, max: 10 * miliseconds.minute }))
appActions.onBitcoinToBatTransitioned()
notifications.showBraveWalletUpdated()
const fs = require('fs')
fs.unlink(pathName(newClientPath), (err) => {
if (err) console.error('unlink error: ' + err.toString())
})
}
})
} catch (ex) {
Expand Down

0 comments on commit b8f887c

Please sign in to comment.