Skip to content

Commit

Permalink
Added mock API server
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Nov 24, 2017
1 parent 5747133 commit 076c3db
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
10 changes: 7 additions & 3 deletions app/src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict'

let { app, BrowserWindow, Menu } = require('electron')
let fs = require('fs-extra')
Expand All @@ -11,6 +11,7 @@ let event = require('event-to-promise')
let toml = require('toml')
let pkg = require('../../../package.json')
let rmdir = require('../helpers/rmdir.js')
let mockServer = require('./mockServer.js')

let shuttingDown = false
let mainWindow
Expand Down Expand Up @@ -404,9 +405,12 @@ async function main () {
await initBaseserver(chainId, baseserverHome, nodeIP)
}

log('starting baseserver')
log('starting gaia server')
baseserverProcess = await startBaseserver(baseserverHome)
log('baseserver ready')
log('gaia server ready')

// start mock API server on port 8999
mockServer(8999)
}
module.exports = Object.assign(
main()
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/mockServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let express = require('express')
let proxy = require('express-http-proxy')
let randomBytes = require('crypto').pseudoRandomBytes

let randomPubkey = () => ({
type: 'ed25519',
data: randomBytes(32).toString('hex')
})

module.exports = function (port = 8999) {
let app = express()

// delegation mock API
let candidates = new Array(205).fill(0).map(randomPubkey)
app.get('/query/stake/candidate', (req, res) => {
res.json({
height: 10000,
data: candidates
})
})
app.get('/query/stake/candidate/:pubkey', (req, res) => {
res.json({
height: 10000,
data: {
pubkey: randomPubkey(),
owner: {
chain: 'gaia-1',
app: 'sig',
address: randomBytes(20).toString('hex')
},
shares: Math.floor(Math.random() * 1e7),
voting_power: Math.floor(Math.random() * 1e5),
description: 'This is a fake candidate description.'
}
})
})

// proxy everything else to light client
app.use(proxy('http://localhost:8998'))

app.listen(port)
}
3 changes: 2 additions & 1 deletion app/src/renderer/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const RpcClient = require('tendermint')
let sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

module.exports = async function (nodeIP) {
let rest = RestClient()
// TODO: once we're done with the mock API server, switch this from port 8999 to 8998
let rest = RestClient('http://localhost:8999')
let rpc = RpcClient(`ws://${nodeIP}`)
// TODO: handle disconnect, try to reconnect
// TODO: eventually, get all data from light-client connection instead of RPC
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"eslint-plugin-html": "^2.0.0",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^2.0.1",
"express": "^4.16.2",
"express-http-proxy": "^1.1.0",
"extract-text-webpack-plugin": "^2.0.0-beta.4",
"fast-future": "^1.0.2",
"file-loader": "^0.9.0",
Expand Down
16 changes: 12 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ de-indent@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/de-indent/-/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d"

debug@*, debug@^3.1.0:
debug@*, debug@^3.0.1, debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies:
Expand Down Expand Up @@ -2585,7 +2585,7 @@ es6-map@^0.1.3:
es6-symbol "~3.1.1"
event-emitter "~0.3.5"

es6-promise@^4.0.5:
es6-promise@^4.0.5, es6-promise@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.1.1.tgz#8811e90915d9a0dba36274f0b242dbda78f9c92a"

Expand Down Expand Up @@ -2874,7 +2874,15 @@ expect@^21.2.1:
jest-message-util "^21.2.1"
jest-regex-util "^21.2.0"

express@^4.13.3:
express-http-proxy@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/express-http-proxy/-/express-http-proxy-1.1.0.tgz#b533842add15b7021d78d248c80b53578a7b6ba0"
dependencies:
debug "^3.0.1"
es6-promise "^4.1.1"
raw-body "^2.3.0"

express@^4.13.3, express@^4.16.2:
version "4.16.2"
resolved "https://registry.yarnpkg.com/express/-/express-4.16.2.tgz#e35c6dfe2d64b7dca0a5cd4f21781be3299e076c"
dependencies:
Expand Down Expand Up @@ -6546,7 +6554,7 @@ range-parser@^1.0.3, range-parser@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.0.tgz#f49be6b487894ddc40dcc94a322f611092e00d5e"

[email protected]:
[email protected], raw-body@^2.3.0:
version "2.3.2"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.3.2.tgz#bcd60c77d3eb93cde0050295c3f379389bc88f89"
dependencies:
Expand Down

0 comments on commit 076c3db

Please sign in to comment.