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 #6822 from brave/0.13.1-ledger-startup
Browse files Browse the repository at this point in the history
Speed up ledger startup
  • Loading branch information
bsclifton authored Jan 24, 2017
2 parents 492be44 + 56c5870 commit 134ad76
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ const ipc = electron.ipcMain
const session = electron.session

const acorn = require('acorn')
const ledgerBalance = require('ledger-balance')
const ledgerClient = require('ledger-client')
const ledgerGeoIP = require('ledger-geoip')
const ledgerPublisher = require('ledger-publisher')
const qr = require('qr-image')
const querystring = require('querystring')
const random = require('random-lib')
Expand All @@ -60,6 +56,12 @@ const ledgerUtil = require('./common/lib/ledgerUtil')
const Tabs = require('./browser/tabs')
const {fileUrl} = require('../js/lib/appUrlUtil')

// "only-when-needed" loading...
let ledgerBalance = null
let ledgerClient = null
let ledgerGeoIP = null
let ledgerPublisher = null

// TBD: remove these post beta [MTR]
const logPath = 'ledger-log.json'
const publisherPath = 'ledger-publisher.json'
Expand Down Expand Up @@ -204,10 +206,6 @@ const doAction = (action) => {
*/
var init = () => {
try {
ledgerInfo._internal.debugP = ledgerClient.prototype.boolion(process.env.LEDGER_CLIENT_DEBUG)
publisherInfo._internal.debugP = ledgerClient.prototype.boolion(process.env.LEDGER_PUBLISHER_DEBUG)
publisherInfo._internal.verboseP = ledgerClient.prototype.boolion(process.env.LEDGER_PUBLISHER_VERBOSE)

appDispatcher.register(doAction)
initialize(getSetting(settings.PAYMENTS_ENABLED))

Expand All @@ -233,6 +231,7 @@ var boot = () => {
ledgerInfo.creating = true
appActions.updateLedgerInfo({ creating: true })
try {
clientprep()
client = ledgerClient(null, underscore.extend({ roundtrip: roundtrip }, clientOptions), null)
} catch (ex) {
appActions.updateLedgerInfo({})
Expand Down Expand Up @@ -539,6 +538,7 @@ var initialize = (paymentsEnabled) => {
}
if (client) return

if (!ledgerPublisher) ledgerPublisher = require('ledger-publisher')
cacheRuleSet(ledgerPublisher.rules)

fs.access(pathName(statePath), fs.FF_OK, (err) => {
Expand All @@ -560,6 +560,7 @@ var initialize = (paymentsEnabled) => {
getStateInfo(state)

try {
clientprep()
client = ledgerClient(state.personaId,
underscore.extend(state.options, { roundtrip: roundtrip }, clientOptions),
state)
Expand All @@ -584,8 +585,12 @@ var initialize = (paymentsEnabled) => {
} catch (ex) {
return console.log('ledger client creation error: ' + ex.toString() + '\n' + ex.stack)
}
if (client.sync(callback) === true) run(random.randomInt({ min: msecs.minute, max: 10 * msecs.minute }))
cacheRuleSet(state.ruleset)

// speed-up browser start-up by delaying the first synchronization action
setTimeout(() => {
if (client.sync(callback) === true) run(random.randomInt({ min: msecs.minute, max: 10 * msecs.minute }))
cacheRuleSet(state.ruleset)
}, 3 * msecs.second)

// Make sure bravery props are up-to-date with user settings
setPaymentInfo(getSetting(settings.PAYMENTS_CONTRIBUTION_AMOUNT))
Expand All @@ -599,6 +604,13 @@ var initialize = (paymentsEnabled) => {
})
}

var clientprep = () => {
if (!ledgerClient) ledgerClient = require('ledger-client')
ledgerInfo._internal.debugP = ledgerClient.prototype.boolion(process.env.LEDGER_CLIENT_DEBUG)
publisherInfo._internal.debugP = ledgerClient.prototype.boolion(process.env.LEDGER_PUBLISHER_DEBUG)
publisherInfo._internal.verboseP = ledgerClient.prototype.boolion(process.env.LEDGER_PUBLISHER_VERBOSE)
}

var enable = (paymentsEnabled) => {
if (paymentsEnabled && !getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
Expand All @@ -607,6 +619,7 @@ var enable = (paymentsEnabled) => {
publisherInfo._internal.enabled = paymentsEnabled
if (synopsis) return updatePublisherInfo()

if (!ledgerPublisher) ledgerPublisher = require('ledger-publisher')
synopsis = new (ledgerPublisher.Synopsis)()
fs.readFile(pathName(synopsisPath), (err, data) => {
var initSynopsis = () => {
Expand Down Expand Up @@ -1048,6 +1061,8 @@ var updateLedgerInfo = () => {

if ((client) && (now > ledgerInfo._internal.geoipExpiry)) {
ledgerInfo._internal.geoipExpiry = now + (5 * msecs.minute)

if (!ledgerGeoIP) ledgerGeoIP = require('ledger-geoip')
return ledgerGeoIP.getGeoIP(client.options, (err, provider, result) => {
if (err) console.log('ledger geoip warning: ' + JSON.stringify(err, null, 2))
if (result) ledgerInfo.countryCode = result
Expand Down Expand Up @@ -1330,6 +1345,7 @@ var getBalance = () => {
balanceTimeoutId = setTimeout(getBalance, 1 * msecs.minute)
if (!ledgerInfo.address) return

if (!ledgerBalance) ledgerBalance = require('ledger-balance')
ledgerBalance.getBalance(ledgerInfo.address, underscore.extend({ balancesP: true }, client.options),
(err, provider, result) => {
var unconfirmed
Expand Down

0 comments on commit 134ad76

Please sign in to comment.