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

Commit

Permalink
Fix persistence issues with ledger client
Browse files Browse the repository at this point in the history
- client is now persisted after sync
- file is now cleaned up after transition is complete (when new ledger file is being written)

Fixes #11494

Auditors: @evq
  • Loading branch information
bsclifton committed Oct 12, 2017
1 parent a314b4e commit 20c46b6
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 @@ -1885,7 +1886,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 @@ -2246,9 +2262,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 @@ -2291,6 +2307,8 @@ const transitionWalletToBat = () => {

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

muonWriter(newClientPath, newClient)

setTimeout(() => transitionWalletToBat(), delayTime)
})
return
Expand All @@ -2311,13 +2329,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 20c46b6

Please sign in to comment.