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

Fix persistence edge-case issue with ledger client #11495

Merged
merged 1 commit into from
Oct 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to const newClientPath = pathName('ledger-newstate.json'), so that we call pathName only once. Same can be done with statePath

Copy link
Member Author

@bsclifton bsclifton Oct 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at doing this, but since it uses electron.app.getPath() (which may or may not be initialized at the time module is loaded), I think changing that may be better to do in a different PR. Thanks for the suggestion though 😄


// 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