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

Reconnect #283

Merged
merged 15 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"casual": "^1.5.17",
"clipboard": "^1.6.0",
"cosmos-delegation-game": "^0.1.0",
"cosmos-sdk": "^1.5.0",
"cosmos-sdk": "^1.5.1",
"event-to-promise": "^0.8.0",
"express": "^4.16.2",
"express-http-proxy": "^1.1.0",
Expand Down
89 changes: 62 additions & 27 deletions app/src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ let semver = require('semver')
// this dependency is wrapped in a file as it was not possible to mock the import with jest any other way
let event = require('event-to-promise')
let toml = require('toml')
let axios = require('axios')
let pkg = require('../../../package.json')
let mockServer = require('./mockServer.js')
let relayServer = require('./relayServer.js')

let started = false
let shuttingDown = false
let mainWindow
let baseserverProcess
let streams = []
let nodeIP
let connecting = false

const root = require('../root.js')
const baseserverHome = join(root, 'baseserver')
const WIN = /^win/.test(process.platform)
const DEV = process.env.NODE_ENV === 'development'
const TEST = JSON.parse(process.env.COSMOS_TEST || 'false') !== false
Expand All @@ -28,6 +32,7 @@ const MOCK = JSON.parse(process.env.MOCK || !TEST && DEV) !== false
const winURL = DEV
? `http://localhost:${require('../../../config').port}`
: `file://${__dirname}/index.html`
const NODE = process.env.COSMOS_NODE

// this network gets used if none is specified via the
// COSMOS_NETWORK env var
Expand Down Expand Up @@ -179,23 +184,16 @@ app.on('activate', () => {
app.on('ready', () => createWindow())

// start baseserver REST API
async function startBaseserver (home) {
async function startBaseserver (home, nodeIP) {
log('startBaseserver', home)
let child = startProcess(SERVER_BINARY, [
'rest-server',
'--home', home // ,
'--home', home,
'--node', nodeIP
// '--trust-node'
])
logProcess(child, join(home, 'baseserver.log'))

// restore baseserver if it crashes
child.on('exit', async () => {
if (shuttingDown) return
log('baseserver crashed, restarting')
await sleep(1000)
await startBaseserver(home)
})

while (true) {
if (shuttingDown) break

Expand Down Expand Up @@ -311,6 +309,48 @@ function consistentConfigDir (versionPath, genesisPath, configPath) {
return exists(genesisPath) && exists(versionPath) && exists(configPath)
}

function pickNode (seeds) {
let nodeIP = NODE || seeds[Math.floor(Math.random() * seeds.length)]
// let nodeRegex = /([http[s]:\/\/]())/g
log('Picked seed:', nodeIP, 'of', seeds)
// replace port with default RPC port
nodeIP = `${nodeIP.split(':')[0]}:46657`

return nodeIP
}

async function connect (seeds, nodeIP) {
log(`starting gaia server with nodeIP ${nodeIP}`)
baseserverProcess = await startBaseserver(baseserverHome, nodeIP)
log('gaia server ready')

return nodeIP
}

async function reconnect (seeds) {
if (connecting) return
connecting = true

let nodeAlive = false
while (!nodeAlive) {
let nodeIP = pickNode(seeds)
nodeAlive = await axios('http://' + nodeIP)
.then(() => true, () => false)
log(`${new Date().toLocaleTimeString()} ${nodeIP} is ${nodeAlive ? 'alive' : 'down'}`)

if (!nodeAlive) await sleep(2000)
}

log('quitting running baseserver')
baseserverProcess.kill('SIGKILL')

await connect(seeds, nodeIP)

connecting = false

return nodeIP
}

async function main () {
// the windows installer opens the app once when installing
// the package recommends, that we exit if this happens
Expand All @@ -323,7 +363,6 @@ async function main () {
return
}

let root = require('../root.js')
let versionPath = join(root, 'app_version')
let genesisPath = join(root, 'genesis.json')
let configPath = join(root, 'config.toml')
Expand Down Expand Up @@ -398,29 +437,25 @@ async function main () {
throw new Error(`Can't open config.toml: ${e.message}`)
}
let config = toml.parse(configText)
let seeds = config.p2p.seeds.split(',')
if (config.p2p.seeds === '' || seeds.length === 0) {
let seeds = config.p2p.seeds.split(',').filter(x => x !== '')
if (seeds.length === 0) {
throw new Error('No seeds specified in config.toml')
}
nodeIP = seeds[Math.floor(Math.random() * seeds.length)]
log('Picked seed:', nodeIP, 'of', seeds)
// replace port with default RPC port
nodeIP = `${nodeIP.split(':')[0]}:46657`
log(`Initializing baseserver with remote node ${nodeIP}`)
nodeIP = pickNode(seeds)

let baseserverHome = join(root, 'baseserver')
if (init) {
log(`Initializing baseserver with remote node ${nodeIP}`)
await initBaseserver(chainId, baseserverHome, nodeIP)
}

log('starting gaia server')
baseserverProcess = await startBaseserver(baseserverHome)
log('gaia server ready')
await connect(seeds, nodeIP)

if (MOCK) {
// start mock API server on port 8999
mockServer(8999)
}
// the view can communicate with the main process by sending requests to the relay server
// the relay server also proxies to the LCD
relayServer({
mock: MOCK,
onReconnectReq: reconnect.bind(this, seeds)
})

started = true
if (mainWindow) {
Expand Down
119 changes: 0 additions & 119 deletions app/src/main/mockServer.js

This file was deleted.

Loading