Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Gaia version at runtime #411

Merged
merged 8 commits into from
Jan 31, 2018
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added a changelog @jolesbi.

## [0.3.1] - 2018-01-30
###
* Check to ensure gaia version is correct for the current network @mappum

### Changed
* Resolved notifications error on NiSessionLoading.vue @nylira.
* Resolved old saved prevAccountKey being used in NiSessionSignIn.vue @nylira.
Expand Down
33 changes: 27 additions & 6 deletions app/src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ async function startBaseserver (home, nodeIP) {
return child
}

async function getGaiaVersion () {
let child = startProcess(SERVER_BINARY, ['version'])
let data = await new Promise((resolve) => {
child.stdout.on('data', resolve)
})
return data.toString('utf8').trim()
}

function exists (path) {
try {
fs.accessSync(path)
Expand Down Expand Up @@ -323,8 +331,11 @@ if (!TEST) {
})
}

function consistentConfigDir (versionPath, genesisPath, configPath) {
return exists(genesisPath) && exists(versionPath) && exists(configPath)
function consistentConfigDir (appVersionPath, genesisPath, configPath, gaiaVersionPath) {
return exists(genesisPath) &&
exists(appVersionPath) &&
exists(configPath) &&
exists(gaiaVersionPath)
}

function pickNode (seeds) {
Expand Down Expand Up @@ -374,9 +385,10 @@ async function main () {
return
}

let versionPath = join(root, 'app_version')
let appVersionPath = join(root, 'app_version')
let genesisPath = join(root, 'genesis.json')
let configPath = join(root, 'config.toml')
let gaiaVersionPath = join(root, 'gaiaversion.txt')

let rootExists = exists(root)
await fs.ensureDir(root)
Expand All @@ -389,8 +401,8 @@ async function main () {

// check if the existing data came from a compatible app version
// if not, backup the data and re-initialize
if (consistentConfigDir(versionPath, genesisPath, configPath)) {
let existingVersion = fs.readFileSync(versionPath, 'utf8')
if (consistentConfigDir(appVersionPath, genesisPath, configPath, gaiaVersionPath)) {
let existingVersion = fs.readFileSync(appVersionPath, 'utf8')
let compatible = semver.diff(existingVersion, pkg.version) !== 'major'
if (compatible) {
log('configs are compatible with current app version')
Expand Down Expand Up @@ -427,13 +439,22 @@ async function main () {
fs.accessSync(networkPath) // crash if invalid path
fs.copySync(networkPath, root)

fs.writeFileSync(versionPath, pkg.version)
fs.writeFileSync(appVersionPath, pkg.version)
}

log('starting app')
log(`dev mode: ${DEV}`)
log(`winURL: ${winURL}`)

let gaiaVersion = await getGaiaVersion()
let expectedGaiaVersion = fs.readFileSync(gaiaVersionPath, 'utf8').trim()
log(`gaia version: "${gaiaVersion}", expected: "${expectedGaiaVersion}"`)
// TODO: semver check, or exact match?
if (gaiaVersion !== expectedGaiaVersion) {
throw Error(`Requires gaia ${expectedGaiaVersion}, but got ${gaiaVersion}.
Please update your gaia installation or build with a newer binary.`)
}

// read chainId from genesis.json
let genesisText = fs.readFileSync(genesisPath, 'utf8')
let genesis = JSON.parse(genesisText)
Expand Down
5 changes: 5 additions & 0 deletions tasks/testnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ async function main () {
.catch(e => {
throw new Error(`Can't load config.toml: ${e.message}`)
})
let gaiaVersionTxt = await get(`https://github.com/tendermint/testnets/raw/master/${network}/gaia/gaiaversion.txt`)
.catch(e => {
throw new Error(`Can't load config.toml: ${e.message}`)
})
let path = join(tmpdir(), Math.random().toString(36).slice(2))
mkdirp(path)
write(join(path, 'genesis.json'), genesisJson)
write(join(path, 'config.toml'), configToml)
write(join(path, 'gaiaversion.txt'), gaiaVersionTxt)
runDev(path)
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/specs/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock('fs-extra', () => {
let mockFs = mockFsExtra()
mockFs.writeFile('./app/networks/gaia-2/config.toml', fs.readFileSync('./app/networks/gaia-2/config.toml', 'utf8'))
mockFs.writeFile('./app/networks/gaia-2/genesis.json', fs.readFileSync('./app/networks/gaia-2/genesis.json', 'utf8'))
mockFs.writeFile('./app/networks/gaia-2/gaiaversion.txt', fs.readFileSync('./app/networks/gaia-2/gaiaversion.txt', 'utf8'))
return mockFs
})
let fs = require('fs-extra')
Expand All @@ -27,6 +28,13 @@ childProcessMock((path, args) => ({
if (type === 'exit' && args[1] === 'init') {
cb(0)
}
},
stdout: {
on: (type, cb) => {
if (args[0] === 'version' && type === 'data') {
cb({toString: () => 'v0.5.0'})
}
}
}
}))

Expand Down Expand Up @@ -429,6 +437,13 @@ function failingChildProcess (mockName, mockCmd) {
cb(0)
}
}
},
stdout: {
on: (type, cb) => {
if (args[0] === 'version' && type === 'data') {
cb({toString: () => 'v0.5.0'})
}
}
}
}))
}
Expand Down