From ef994a799d3b32e72a74a796d8cbdd9bd9bfa777 Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Tue, 8 May 2018 09:45:52 +0200 Subject: [PATCH] Merge pull request #14006 from NejcZdovc/fix/#14000-notify Notify users when contribution date is pushed back --- app/browser/api/ledger.js | 14 ++-- app/browser/reducers/ledgerReducer.js | 9 +++ app/common/constants/ledgerStatuses.js | 3 +- app/common/lib/ledgerUtil.js | 10 +++ .../locales/en-US/preferences.properties | 1 + js/actions/appActions.js | 7 ++ js/constants/appConstants.js | 1 + package-lock.json | 14 ++-- package.json | 2 +- .../app/browser/reducers/ledgerReducerTest.js | 66 ++++++++++++++++++- test/unit/app/common/lib/ledgerUtilTest.js | 12 +++- 11 files changed, 123 insertions(+), 16 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 0891e0633fd..3748e8ce070 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -2352,7 +2352,7 @@ const onInitRead = (state, parsedData) => { // enables it again -> reconcileStamp is in the past. // In this case reset reconcileStamp to the future. try { - timeUntilReconcile = client.timeUntilReconcile(synopsis) + timeUntilReconcile = client.timeUntilReconcile(synopsis, onFuzzing) } catch (ex) {} let ledgerWindow = (ledgerState.getSynopsisOption(state, 'numFrames') - 1) * ledgerState.getSynopsisOption(state, 'frameSize') @@ -2386,6 +2386,12 @@ const onInitRead = (state, parsedData) => { return state } +const onFuzzing = () => { + if (client && client.state) { + appActions.onLedgerFuzzing(client.state.reconcileStamp) + } +} + const onTimeUntilReconcile = (state, stateResult) => { state = getStateInfo(state, stateResult.toJS()) // TODO optimize muonWriter(statePath, stateResult) @@ -2462,7 +2468,7 @@ const run = (state, delayTime) => { } const publishers = ledgerState.getAboutProp(state, 'synopsis') || Immutable.List() - if (isList(publishers) && publishers.isEmpty() && client.isReadyToReconcile(synopsis)) { + if (isList(publishers) && publishers.isEmpty() && client.isReadyToReconcile(synopsis, onFuzzing)) { setNewTimeUntilReconcile() } @@ -2523,7 +2529,7 @@ const run = (state, delayTime) => { if (delayTime === 0) { try { - delayTime = client.timeUntilReconcile(synopsis) + delayTime = client.timeUntilReconcile(synopsis, onFuzzing) } catch (ex) { delayTime = false } @@ -2557,7 +2563,7 @@ const run = (state, delayTime) => { return } - if (client.isReadyToReconcile(synopsis)) { + if (client.isReadyToReconcile(synopsis, onFuzzing)) { client.reconcile(uuid.v4().toLowerCase(), callback) } } diff --git a/app/browser/reducers/ledgerReducer.js b/app/browser/reducers/ledgerReducer.js index f63faee1401..0d03878afc2 100644 --- a/app/browser/reducers/ledgerReducer.js +++ b/app/browser/reducers/ledgerReducer.js @@ -503,6 +503,15 @@ const ledgerReducer = (state, action, immutableAction) => { state = ledgerApi.onFetchReferralHeaders(state, action.get('error'), action.get('response'), action.get('body')) break } + case appConstants.APP_ON_LEDGER_FUZZING: + { + state = ledgerState.setAboutProp(state, 'status', ledgerStatuses.FUZZING) + const newStamp = parseInt(action.get('newStamp')) + if (!isNaN(newStamp) && newStamp > 0) { + state = ledgerState.setInfoProp(state, 'reconcileStamp', newStamp) + } + break + } case appConstants.APP_ON_REFERRAL_ACTIVITY: { state = updateState.setUpdateProp(state, 'referralTimestamp', new Date().getTime()) diff --git a/app/common/constants/ledgerStatuses.js b/app/common/constants/ledgerStatuses.js index a6fbf1b6bc6..216ca1f966d 100644 --- a/app/common/constants/ledgerStatuses.js +++ b/app/common/constants/ledgerStatuses.js @@ -5,7 +5,8 @@ const statuses = { CORRUPTED_SEED: 'corruptedSeed', IN_PROGRESS: 'contributionInProgress', - SERVER_PROBLEM: 'serverProblem' + SERVER_PROBLEM: 'serverProblem', + FUZZING: 'fuzzing' } module.exports = statuses diff --git a/app/common/lib/ledgerUtil.js b/app/common/lib/ledgerUtil.js index c0355978a9e..87d4ebc7616 100644 --- a/app/common/lib/ledgerUtil.js +++ b/app/common/lib/ledgerUtil.js @@ -20,6 +20,7 @@ const ledgerVideoCache = require('../cache/ledgerVideoCache') const settings = require('../../../js/constants/settings') const ledgerMediaProviders = require('../constants/ledgerMediaProviders') const twitchEvents = require('../constants/twitchEvents') +const ledgerStatuses = require('../constants/ledgerStatuses') // Utils const {responseHasContent} = require('./httpUtil') @@ -98,6 +99,15 @@ const formattedDateFromTimestamp = (timestamp, dateFormat) => { const walletStatus = (ledgerData, settings) => { let status = {} + switch (ledgerData.get('status')) { + case ledgerStatuses.FUZZING: + { + return { + id: 'ledgerFuzzed' + } + } + } + if (ledgerData == null) { return { id: 'createWalletStatus' diff --git a/app/extensions/brave/locales/en-US/preferences.properties b/app/extensions/brave/locales/en-US/preferences.properties index e0190d55589..f2f1350d841 100644 --- a/app/extensions/brave/locales/en-US/preferences.properties +++ b/app/extensions/brave/locales/en-US/preferences.properties @@ -163,6 +163,7 @@ lastPass=LastPass® ledgerBackupText1=Below, you will find the anonymized recovery key that is required if you ever lose access to this computer. ledgerBackupText2=Make sure you keep this key private, or else your wallet will be compromised. ledgerBackupTitle=Backup your Brave wallet +ledgerFuzzed=You haven't accrued 30 minutes of browser activity yet so we pushed back the settlement date to give you more time. ledgerNetworkErrorMessage=The Brave Payments server is not responding. We will fix this as soon as possible. ledgerNetworkErrorTitle=Uh oh. ledgerNetworkErrorText=Note: This error could also be caused by a network connection problem. diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 08c87b7817d..c95872889fa 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -1970,6 +1970,13 @@ const appActions = { }) }, + onLedgerFuzzing: function (newStamp) { + dispatch({ + actionType: appConstants.APP_ON_LEDGER_FUZZING, + newStamp + }) + }, + onLedgerBackupSuccess: function () { dispatch({ actionType: appConstants.APP_ON_LEDGER_BACKUP_SUCCESS diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index 10a839abef0..2a5a13a010f 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -191,6 +191,7 @@ const appConstants = { APP_ON_LEDGER_PIN_PUBLISHER: _, APP_ON_LEDGER_NOTIFICATION_INTERVAL: _, APP_ON_LEDGER_MEDIA_DATA: _, + APP_ON_LEDGER_FUZZING: _, APP_ON_PRUNE_SYNOPSIS: _, APP_ON_HISTORY_LIMIT: _, APP_ON_REFERRAL_CODE_READ: _, diff --git a/package-lock.json b/package-lock.json index ea273cbca53..50c6b381d03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1496,9 +1496,9 @@ } }, "bat-client": { - "version": "2.2.6", - "resolved": "https://registry.npmjs.org/bat-client/-/bat-client-2.2.6.tgz", - "integrity": "sha512-/AbRmbMsipnGEFl1sgvLQfCCj9D2pW5B0sd3iSx+Wama3A0l9evW1UJ/1tglEF4Coj3TD093KOVSUDyHny2exw==", + "version": "2.2.8", + "resolved": "https://registry.npmjs.org/bat-client/-/bat-client-2.2.8.tgz", + "integrity": "sha512-txDR0PP3hiTS4wi+/CvRAOxKpxqqsEt/AKSaDwaPMgqFx4ZoxWaAHBmysTTygOmmCtF2xwzVp6m9+Cf0jpIlqg==", "requires": { "@ambassify/backoff-strategies": "1.0.0", "bat-balance": "1.0.7", @@ -5484,7 +5484,7 @@ "file-entry-cache": "2.0.0", "functional-red-black-tree": "1.0.1", "glob": "7.1.2", - "globals": "11.4.0", + "globals": "11.5.0", "ignore": "3.3.7", "imurmurhash": "0.1.4", "inquirer": "3.3.0", @@ -8238,9 +8238,9 @@ } }, "globals": { - "version": "11.4.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.4.0.tgz", - "integrity": "sha512-Dyzmifil8n/TmSqYDEXbm+C8yitzJQqQIlJQLNRMwa+BOUJpRC19pyVeN12JAjt61xonvXjtff+hJruTRXn5HA==" + "version": "11.5.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.5.0.tgz", + "integrity": "sha512-hYyf+kI8dm3nORsiiXUQigOU62hDLfJ9G01uyGMxhc6BKsircrUhC4uJPQPUSuq2GrTmiiEt7ewxlMdBewfmKQ==" }, "globby": { "version": "5.0.0", diff --git a/package.json b/package.json index 73e836d315b..e65ed8dacab 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "aphrodite": "1.1.0", "async": "^2.0.1", "bat-balance": "^1.0.7", - "bat-client": "^2.2.6", + "bat-client": "^2.2.8", "bat-publisher": "^2.0.15", "bignumber.js": "^4.0.4", "bloodhound-js": "brave/bloodhound", diff --git a/test/unit/app/browser/reducers/ledgerReducerTest.js b/test/unit/app/browser/reducers/ledgerReducerTest.js index a0796a32ea0..28b209525e3 100644 --- a/test/unit/app/browser/reducers/ledgerReducerTest.js +++ b/test/unit/app/browser/reducers/ledgerReducerTest.js @@ -6,6 +6,8 @@ const sinon = require('sinon') const appConstants = require('../../../../../js/constants/appConstants') const settings = require('../../../../../js/constants/settings') require('../../../braveUnit') +const ledgerStatuses = require('../../../../../app/common/constants/ledgerStatuses') +const ledgerState = require('../../../../../app/common/state/ledgerState') describe('ledgerReducer unit tests', function () { let ledgerReducer @@ -66,11 +68,12 @@ describe('ledgerReducer unit tests', function () { resetSynopsis: dummyModifyState, setRecoveryStatus: dummyModifyState, setRecoveryInProgressStatus: dummyModifyState, - setInfoProp: dummyModifyState, + setInfoProp: ledgerState.setInfoProp, saveSynopsis: dummyModifyState, savePromotion: dummyModifyState, remindMeLater: dummyModifyState, - removePromotion: dummyModifyState + removePromotion: dummyModifyState, + setAboutProp: ledgerState.setAboutProp } fakeLedgerNotifications = { onPromotionReceived: dummyModifyState, @@ -907,4 +910,63 @@ describe('ledgerReducer unit tests', function () { assert.equal(true, pageDataChangedSpy.getCall(1).args[2]) }) }) + + describe('APP_ON_LEDGER_FUZZING', function () { + let newState + + before(() => { + newState = appState + .setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING) + }) + + it('null case', function () { + const result = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_FUZZING + })) + + assert.deepEqual(result.toJS(), newState.toJS()) + }) + + it('stamp is string', function () { + const result = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_FUZZING, + newStamp: 'str' + })) + + assert.deepEqual(result.toJS(), newState.toJS()) + }) + + it('stamp is negative', function () { + const result = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_FUZZING, + newStamp: -10 + })) + + assert.deepEqual(result.toJS(), newState.toJS()) + }) + + it('stamp is number (string)', function () { + const result = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_FUZZING, + newStamp: '10' + })) + + const expectedState = newState + .setIn(['ledger', 'info', 'reconcileStamp'], 10) + + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + + it('reconcile stamp is set', function () { + const result = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ON_LEDGER_FUZZING, + newStamp: 10 + })) + + const expectedState = newState + .setIn(['ledger', 'info', 'reconcileStamp'], 10) + + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + }) }) diff --git a/test/unit/app/common/lib/ledgerUtilTest.js b/test/unit/app/common/lib/ledgerUtilTest.js index abf6f0f4146..2f54ef62322 100644 --- a/test/unit/app/common/lib/ledgerUtilTest.js +++ b/test/unit/app/common/lib/ledgerUtilTest.js @@ -7,6 +7,7 @@ const settings = require('../../../../../js/constants/settings') const ledgerMediaProviders = require('../../../../../app/common/constants/ledgerMediaProviders') const twitchEvents = require('../../../../../app/common/constants/twitchEvents') const urlUtil = require('../../../../../js/lib/urlutil') +const ledgerStatuses = require('../../../../../app/common/constants/ledgerStatuses') const defaultState = Immutable.fromJS({ ledger: {} @@ -265,12 +266,21 @@ describe('ledgerUtil unit test', function () { describe('walletStatus', function () { it('null case', function () { - const result = ledgerUtil.walletStatus() + const result = ledgerUtil.walletStatus(Immutable.Map()) assert.deepEqual(result, { id: 'createWalletStatus' }) }) + it('on fuzzing', function () { + const result = ledgerUtil.walletStatus(Immutable.fromJS({ + status: ledgerStatuses.FUZZING + })) + assert.deepEqual(result, { + id: 'ledgerFuzzed' + }) + }) + it('on error', function () { const state = Immutable.fromJS({ error: {