Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #493 from hermeznetwork/localstorage-migrations
Browse files Browse the repository at this point in the history
Add localStorage v2 migration
  • Loading branch information
elias-garcia authored May 21, 2021
2 parents 8945369 + 57afd89 commit 0236f5b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const RETRY_POOL_TXS_RATE = 60000 // 10min

export const STORAGE_VERSION_KEY = 'hermezWalletStorageVersion'

export const STORAGE_VERSION = 1
export const STORAGE_VERSION = 2

export const REPORT_ISSUE_FORM_URL = 'https://docs.google.com/forms/d/e/1FAIpQLScvCK2OaRYSXcFYEiWAkbtWzHxWh8fGO4uOn0sIRdPP9Gigeg/viewform'

Expand Down
3 changes: 3 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import 'normalize.css/normalize.css'

import * as serviceWorker from './serviceWorker'
import configureStore from './store'
import * as storage from './utils/storage'
import theme from './styles/theme'
import App from './views/app.view'

storage.checkVersion()

const history = createBrowserHistory()
const store = configureStore(history)

Expand Down
74 changes: 38 additions & 36 deletions src/store/global/global.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,47 @@ export const LOAD_ETHEREUM_NETWORK_ERROR = {
CHAIN_ID_NOT_SUPPORTED: 'chain-id-not-supported'
}

const initialGlobalState = {
hermezStatusTask: {
status: 'pending'
},
ethereumNetworkTask: {
status: 'pending'
},
wallet: undefined,
signer: undefined,
header: {
type: undefined
},
redirectRoute: '/',
fiatExchangeRatesTask: {
status: 'pending'
},
snackbar: {
status: 'closed'
},
networkStatus: 'online',
pendingWithdraws: storage.getStorage(constants.PENDING_WITHDRAWS_KEY),
pendingDelayedWithdraws: storage.getStorage(constants.PENDING_DELAYED_WITHDRAWS_KEY),
pendingDelayedWithdrawCheckTask: {
status: 'pending'
},
pendingWithdrawalsCheckTask: {
status: 'pending'
},
pendingDeposits: storage.getStorage(constants.PENDING_DEPOSITS_KEY),
pendingDepositsCheckTask: {
status: 'pending'
},
nextForgers: [],
coordinatorStateTask: {
status: 'pending'
function getInitialGlobalState () {
return {
hermezStatusTask: {
status: 'pending'
},
ethereumNetworkTask: {
status: 'pending'
},
wallet: undefined,
signer: undefined,
header: {
type: undefined
},
redirectRoute: '/',
fiatExchangeRatesTask: {
status: 'pending'
},
snackbar: {
status: 'closed'
},
networkStatus: 'online',
pendingWithdraws: storage.getStorage(constants.PENDING_WITHDRAWS_KEY),
pendingDelayedWithdraws: storage.getStorage(constants.PENDING_DELAYED_WITHDRAWS_KEY),
pendingDelayedWithdrawCheckTask: {
status: 'pending'
},
pendingWithdrawalsCheckTask: {
status: 'pending'
},
pendingDeposits: storage.getStorage(constants.PENDING_DEPOSITS_KEY),
pendingDepositsCheckTask: {
status: 'pending'
},
nextForgers: [],
coordinatorStateTask: {
status: 'pending'
}
}
}

function globalReducer (state = initialGlobalState, action) {
function globalReducer (state = getInitialGlobalState(), action) {
switch (action.type) {
case globalActionTypes.LOAD_HERMEZ_STATUS: {
return {
Expand Down
59 changes: 33 additions & 26 deletions src/store/login/login.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,42 @@ export const STEP_NAME = {
ERROR: 'error'
}

const initialLoginState = {
currentStep: STEP_NAME.WALLET_SELECTOR,
networkNameTask: {
status: 'pending'
},
steps: {
[STEP_NAME.ACCOUNT_SELECTOR]: {
walletName: undefined
function getInitialLoginState () {
return {
currentStep: STEP_NAME.WALLET_SELECTOR,
networkNameTask: {
status: 'pending'
},
[STEP_NAME.WALLET_LOADER]: {
walletName: undefined,
accountData: undefined,
walletTask: {
status: 'pending'
steps: {
[STEP_NAME.ACCOUNT_SELECTOR]: {
walletName: undefined
},
[STEP_NAME.WALLET_LOADER]: {
walletName: undefined,
accountData: undefined,
walletTask: {
status: 'pending'
}
},
[STEP_NAME.CREATE_ACCOUNT_AUTH]: {
wallet: undefined
},
[STEP_NAME.ERROR]: {
error: undefined
}
},
[STEP_NAME.CREATE_ACCOUNT_AUTH]: {
wallet: undefined
addAccountAuthTask: {
status: 'pending'
},
[STEP_NAME.ERROR]: {
error: undefined
}
},
addAccountAuthTask: {
status: 'pending'
},
accountAuthSignatures: getStorage(ACCOUNT_AUTH_SIGNATURES_KEY)
accountAuthSignatures: getStorage(ACCOUNT_AUTH_SIGNATURES_KEY)
}
}

function loginReducer (state = initialLoginState, action) {
function loginReducer (state = getInitialLoginState(), action) {
switch (action.type) {
case loginActionTypes.GO_TO_WALLET_SELECTOR_STEP: {
const initialLoginState = getInitialLoginState()

return {
...state,
currentStep: initialLoginState.currentStep,
Expand Down Expand Up @@ -100,6 +104,8 @@ function loginReducer (state = initialLoginState, action) {
}
}
case loginActionTypes.GO_TO_PREVIOUS_STEP: {
const initialLoginState = getInitialLoginState()

switch (state.currentStep) {
case STEP_NAME.ACCOUNT_SELECTOR: {
return {
Expand All @@ -119,8 +125,7 @@ function loginReducer (state = initialLoginState, action) {
: STEP_NAME.ACCOUNT_SELECTOR,
steps: {
...state.steps,
[STEP_NAME.WALLET_LOADER]:
initialLoginState.steps[STEP_NAME.WALLET_LOADER]
[STEP_NAME.WALLET_LOADER]: initialLoginState.steps[STEP_NAME.WALLET_LOADER]
}
}
}
Expand Down Expand Up @@ -224,6 +229,8 @@ function loginReducer (state = initialLoginState, action) {
}
}
case loginActionTypes.RESET_STATE: {
const initialLoginState = getInitialLoginState()

return {
...state,
currentStep: initialLoginState.currentStep,
Expand Down
85 changes: 74 additions & 11 deletions src/utils/storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
import { STORAGE_VERSION, STORAGE_VERSION_KEY } from '../constants'
import { PENDING_DELAYED_WITHDRAWS_KEY, PENDING_WITHDRAWS_KEY, STORAGE_VERSION, STORAGE_VERSION_KEY } from '../constants'

// MIGRATIONS
function runV2Migration () {
const pendingWithdraws = getStorage(PENDING_WITHDRAWS_KEY)
const newPendingWithdraws = Object.keys(pendingWithdraws).reduce((currentChainPendingWithdraws, chainId) => {
const chainPendingWithdraws = pendingWithdraws[chainId]
const newChainPendingWithdraws = Object.keys(chainPendingWithdraws).reduce((currentAccountPendingWithdraws, hezEthereumAddress) => {
const accountPendingWithdraws = chainPendingWithdraws[hezEthereumAddress]
const newAccountPendingWithdraws = accountPendingWithdraws.filter((pendingWithdraw) => pendingWithdraw.hash !== undefined)

return {
...currentAccountPendingWithdraws,
[hezEthereumAddress]: newAccountPendingWithdraws
}
}, {})

return {
...currentChainPendingWithdraws,
[chainId]: newChainPendingWithdraws
}
}, {})

setStorage(PENDING_WITHDRAWS_KEY, newPendingWithdraws)

const pendingDelayedWithdraws = getStorage(PENDING_DELAYED_WITHDRAWS_KEY)
const newPendingDelayedWithdraws = Object.keys(pendingDelayedWithdraws).reduce((currentChainPendingDelayedWithdraws, chainId) => {
const chainPendingDelayedWithdraws = pendingDelayedWithdraws[chainId]
const newChainPendingDelayedWithdraws = Object.keys(chainPendingDelayedWithdraws).reduce((currentAccountPendingDelayedWithdraws, hezEthereumAddress) => {
const accountPendingDelayedWithdraws = chainPendingDelayedWithdraws[hezEthereumAddress]
const newAccountPendingDelayedWithdraws = accountPendingDelayedWithdraws.filter((pendingDelayedWithdraw) => pendingDelayedWithdraw.hash !== undefined)

return {
...currentAccountPendingDelayedWithdraws,
[hezEthereumAddress]: newAccountPendingDelayedWithdraws
}
}, {})

return {
...currentChainPendingDelayedWithdraws,
[chainId]: newChainPendingDelayedWithdraws
}
}, {})

setStorage(PENDING_DELAYED_WITHDRAWS_KEY, newPendingDelayedWithdraws)
}

function checkVersion () {
const currentStorageVersion = JSON.parse(localStorage.getItem(STORAGE_VERSION_KEY))

if (!currentStorageVersion) {
localStorage.setItem(STORAGE_VERSION_KEY, STORAGE_VERSION)
}

// LocalStorage migrations
if (currentStorageVersion && STORAGE_VERSION > currentStorageVersion) {
// Added L1 withdraws tracking
if (STORAGE_VERSION >= 2) {
runV2Migration()
}

localStorage.setItem(STORAGE_VERSION_KEY, STORAGE_VERSION)
}
}

function initStorage (key) {
const initialStorage = {}
Expand All @@ -10,19 +73,18 @@ function initStorage (key) {

function getStorage (key) {
const storage = JSON.parse(localStorage.getItem(key))
const storageVersion = JSON.parse(localStorage.getItem(STORAGE_VERSION_KEY))

if (!storageVersion) {
localStorage.setItem(STORAGE_VERSION_KEY, STORAGE_VERSION)
}

if (!storage || storageVersion !== STORAGE_VERSION) {
if (!storage) {
return initStorage(key)
}

return storage
}

function setStorage (key, storage) {
localStorage.setItem(key, JSON.stringify(storage))
}

function addItem (key, chainId, hermezEthereumAddress, item) {
const storage = getStorage(key)
const chainIdStorage = storage[chainId] || {}
Expand All @@ -35,7 +97,7 @@ function addItem (key, chainId, hermezEthereumAddress, item) {
}
}

localStorage.setItem(key, JSON.stringify(newStorage))
setStorage(key, newStorage)

return newStorage
}
Expand All @@ -52,7 +114,7 @@ function removeItem (key, chainId, hermezEthereumAddress, id) {
}
}

localStorage.setItem(key, JSON.stringify(newStorage))
setStorage(key, newStorage)

return newStorage
}
Expand All @@ -69,7 +131,7 @@ function removeItemByCustomProp (key, chainId, hermezEthereumAddress, prop) {
}
}

localStorage.setItem(key, JSON.stringify(newStorage))
setStorage(key, newStorage)

return newStorage
}
Expand All @@ -91,7 +153,7 @@ function updatePartialItemByCustomProp (key, chainId, hermezEthereumAddress, pro
}
}

localStorage.setItem(key, JSON.stringify(newStorage))
setStorage(key, newStorage)

return newStorage
}
Expand All @@ -104,6 +166,7 @@ function getItemsByHermezAddress (storage, chainId, hermezEthereumAddress) {
}

export {
checkVersion,
getStorage,
addItem,
removeItem,
Expand Down

0 comments on commit 0236f5b

Please sign in to comment.