Skip to content

Commit

Permalink
Merge pull request #483 from cosmos/fabo/334-gaia-init-bug
Browse files Browse the repository at this point in the history
Fix app not recovering from failed Gaia initialization
  • Loading branch information
jbibla authored Feb 16, 2018
2 parents 8c9cf2c + af18e80 commit 6f9f6d6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
28 changes: 26 additions & 2 deletions app/src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,28 @@ function consistentConfigDir (appVersionPath, genesisPath, configPath, gaiaVersi
exists(gaiaVersionPath)
}

// check if baseserver is initialized as the configs could be corrupted
// we need to parse the error on initialization as there is no way to just get this status programmatically
function baseserverInitialized (home) {
return new Promise((resolve, reject) => {
let child = startProcess(SERVER_BINARY, [
'client',
'init',
'--home', home
// '--trust-node'
])
child.stderr.on('data', data => {
if (data.toString().includes('already is initialized')) {
return resolve(true)
}
if (data.toString().includes('"--chain-id" required')) {
return resolve(false)
}
reject('Unknown state for Gaia initialization: ' + data.toString())
})
})
}

function pickNode (seeds) {
let nodeIP = NODE || seeds[Math.floor(Math.random() * seeds.length)]
// let nodeRegex = /([http[s]:\/\/]())/g
Expand Down Expand Up @@ -482,8 +504,10 @@ async function main () {
}
nodeIP = pickNode(seeds)

if (init) {
log(`Initializing baseserver with remote node ${nodeIP}`)
let _baseserverInitialized = await baseserverInitialized(join(root, 'baseserver'))
console.log('Baseserver is', _baseserverInitialized ? '' : 'not', 'initialized')
if (init || !_baseserverInitialized) {
log(`Trying to initialize baseserver with remote node ${nodeIP}`)
await initBaseserver(chainId, baseserverHome, nodeIP)
}

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
"build:linux": "cross-env PLATFORM_TARGET=linux node tasks/release.js",
"build:mas": "cross-env PLATFORM_TARGET=mas node tasks/release.js",
"build:win32": "cross-env PLATFORM_TARGET=win32 node tasks/release.js",
"local": "npm run testnet local",
"local": "yarn run testnet local",
"testnet": "node tasks/testnet.js",
"lint": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter app test",
"lint:fix": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter --fix app test",
"pack": "npm run pack:main && npm run pack:renderer",
"pack": "yarn run pack:main && yarn run pack:renderer",
"pack:main": "cross-env NODE_ENV=production webpack --colors --config webpack.main.config.js",
"pack:renderer": "cross-env NODE_ENV=production webpack --colors --config webpack.renderer.config.js",
"test": "npm run lint && npm run test:unit",
"test": "yarn run lint && yarn run test:unit",
"test:unit": "cross-env LOGGING=false MOCK=false jest --maxWorkers=2",
"test:e2e": "tape \"test/e2e/*.js\"",
"test:e2e": "yarn run pack && tape \"test/e2e/*.js\"",
"test:exe": "node tasks/test-build.js",
"test:coverage": "http-server test/unit/coverage/lcov-report",
"vue:route": "node tasks/vue/route.js",
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
while (await client.isExisting(`.ni-notification`)) {
await client.$(`.ni-notification`).click()
}
await client.$('#app-menu-button').click()
await client.waitForExist('.material-icons=menu', 1000)
await client.$('.material-icons=menu').click()
await client.waitForExist('.app-menu', 1000)
},
async navigate (client, linkText, titleText = linkText) {
Expand Down
15 changes: 13 additions & 2 deletions test/e2e/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ let test = require('tape-promise/tape')
let electron = require('electron')
let { join } = require('path')
let { spawn } = require('child_process')
let fs = require('fs-extra')
let { newTempDir, login } = require('./common.js')

let app, home, cliHome, started
let binary = process.env.BINARY_PATH

module.exports = function launch (t) {
if (!started) {
// tape doesn't exit properly on uncaught promise rejections
process.on('unhandledRejection', error => {
console.error('unhandledRejection', error)
process.exit(1)
});

started = new Promise(async (resolve, reject) => {
console.log('using binary', binary)

Expand Down Expand Up @@ -47,13 +54,17 @@ module.exports = function launch (t) {
await startApp(app)
t.ok(app.isRunning(), 'app is running')

console.log('stopping app to test consecutive run')
// test if app restores from unitialized gaia folder
await app.stop()
fs.removeSync(join(home, 'baseserver'))
fs.mkdirpSync(join(home, 'baseserver'))
await startApp(app)
t.ok(app.isRunning(), 'app recovers from uninitialized gaia')

await app.stop()
await createAccount('testkey', 'chair govern physical divorce tape movie slam field gloom process pen universe allow pyramid private ability')
await createAccount('testreceiver', 'crash ten rug mosquito cart south allow pluck shine island broom deputy hungry photo drift absorb')
console.log('restored test accounts')

await startApp(app)
t.ok(app.isRunning(), 'app is running')

Expand Down
3 changes: 1 addition & 2 deletions test/e2e/signin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let test = require('tape-promise/tape')
let launchApp = require('./launch.js')
let { navigate, logout, openMenu } = require('./common.js')
let { logout, openMenu } = require('./common.js')

test('sign in', async function (t) {
let {app, home} = await launchApp(t)
Expand All @@ -21,7 +21,6 @@ test('sign in', async function (t) {
let warning = () => el('#sign-up-warning')
let backedup = () => el('#sign-up-backup')

navigate(t, client, 'Sign In', 'Welcome to Cosmos Voyager')
t.test('did check warning', async function (t) {
await continueButton().click()
t.ok(await warning().$('..').$('..').$('..').isExisting('.ni-form-msg--error'), 'shows error')
Expand Down
24 changes: 23 additions & 1 deletion test/unit/specs/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jest.mock('electron', () => {
childProcessMock((path, args) => ({
on: (type, cb) => {
// init processes always should return with 0
if (type === 'exit' && args[1] === 'init') {
if (type === 'exit' && args[1] === 'init' && args.length > 4) {
cb(0)
}
},
Expand All @@ -35,6 +35,14 @@ childProcessMock((path, args) => ({
cb({toString: () => 'v0.5.0'})
}
}
},
stderr: {
on: (type, cb) => {
// test for init of gaia
if (type === 'data' && args[1] === 'init' && args.length === 4) {
cb({ toString: () => 'already is initialized' })
}
}
}
}))

Expand Down Expand Up @@ -373,6 +381,12 @@ describe('Startup Process', () => {
it('should survive the baseserver folder being removed', async () => {
fs.removeSync(join(testRoot, 'baseserver'))
await initMain()
expect(childProcess.spawn.mock.calls
.find(([path, args]) =>
path.includes('gaia') &&
args.includes('init')
).length
).toBe(3) // one to check in first round, one to check + one to init in the second round
})
})
})
Expand Down Expand Up @@ -456,6 +470,14 @@ function failingChildProcess (mockName, mockCmd) {
cb({toString: () => 'v0.5.0'})
}
}
},
stderr: {
on: (type, cb) => {
// test for init of gaia
if (type === 'data' && args[1] === 'init' && args.length === 4) {
cb({ toString: () => 'already is initialized' })
}
}
}
}))
}
Expand Down

0 comments on commit 6f9f6d6

Please sign in to comment.