Merge pull request #9 from ethereum/startnode

Options to run geth, mist, frontend
pull/454/head
yann300 7 years ago committed by GitHub
commit 8719ce8667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      package.json
  2. 57
      src/main.js
  3. 8
      src/router.js
  4. 37
      src/services/autoMine.js
  5. 13
      src/services/startFrontend.js
  6. 99
      src/services/startMistGeth.js
  7. 17
      src/websocket.js

@ -32,5 +32,10 @@
},
"bin": {
"remixd": "./src/main.js"
},
"devDependencies": {
"serve": "^6.3.1",
"stdout": "0.0.3",
"web3": "^1.0.0-beta.24"
}
}

@ -1,19 +1,62 @@
#!/usr/bin/env node
var Router = require('./router')
var program = require('commander')
var startmistGeth = require('./services/startMistGeth')
var startFrontend = require('./services/startFrontend')
var fs = require('fs-extra')
program
.usage('-S <shared folder>')
.description('Provide a two ways connection between the local computer and Remix IDE')
.option('-S, --shared-folder <path>', 'Folder to share with Remix IDE')
.option('-s, --shared-folder <path>', 'Folder to share with Remix IDE')
.option('-m, --mist', 'start mist')
.option('-g, --geth', 'start geth')
.option('-p, --dev-path <dev-path>', 'Folder used by mist/geth to start the development instance')
.option('-f, --frontend <front-end>', 'Folder that should be served by remixd')
.option('-p, --frontend-port <front-end-port>', 'Http port used by the frontend (default 8082)')
.option('-a, --auto-mine', 'mine pending transactions')
.option('-r, --rpc <cors-domains>', 'start rpc server. Values are CORS domain')
.option('-rp, --rpc-port', 'rpc server port (default 8545)')
.parse(process.argv)
console.log('example: --dev-path /home/devchains/chain1 --mist --geth --frontend /home/frontend --frontend-port 8084 --auto-mine')
program.outputHelp()
var killCallBack = []
if (program.devPath) {
if (fs.existsSync(program.devPath)) {
killCallBack.push(startmistGeth(program.devPath, program.mist, program.geth, program.autoMine, program.rpc, program.rpcPort))
} else {
console.log('\x1b[31m%s\x1b[0m', '[ERR] can\'t start mist/geth. ' + program.devPath + ' does not exist')
}
}
if (!program.sharedFolder) {
program.outputHelp()
process.exit(1)
} else {
if (program.frontend) {
if (!program.frontendPort) program.frontendPort = 8082
if (fs.existsSync(program.frontend)) {
killCallBack.push(startFrontend(program.frontend, program.frontendPort))
} else {
console.log('\x1b[31m%s\x1b[0m', '[ERR] can\'t start frontend. ' + program.frontend + ' does not exist')
}
}
if (program.sharedFolder) {
console.log('\x1b[33m%s\x1b[0m', '[WARN] Any application that runs on your computer can potentially read from and write to all files in the directory.')
console.log('\x1b[33m%s\x1b[0m', '[WARN] Symbolinc links are not forwarded to Remix IDE\n')
var router = new Router()
killCallBack.push(router.start(program.sharedFolder))
}
var router = new Router()
router.start(program.sharedFolder)
// kill
function kill () {
for (var k in killCallBack) {
try {
killCallBack[k]()
} catch (e) {
console.log(e)
}
}
}
process.on('SIGINT', kill) // catch ctrl-c
process.on('SIGTERM', kill) // catch kill
process.on('exit', kill)

@ -3,13 +3,19 @@ var Websocket = require('./websocket')
class Router {
start (sharedFolder) {
this.websocket = new Websocket()
var websocket = new Websocket()
this.websocket = websocket
this.websocket.start((message) => {
this.call(message.id, message.service, message.fn, message.args)
})
servicesList['sharedfolder'].setupNotifications(this.websocket, sharedFolder)
servicesList['sharedfolder'].sharedFolder(sharedFolder)
console.log('Shared folder : ' + sharedFolder)
return function () {
if (websocket) {
websocket.close()
}
}
}
call (callid, name, fn, args) {

@ -0,0 +1,37 @@
module.exports = function (web3) {
console.log('auto mine transactions')
var methods = []
methods.push({
name: 'start',
call: 'miner_start',
inputFormatter: [null],
params: 1
})
methods.push({
name: 'stop',
call: 'miner_stop',
inputFormatter: [],
params: 0
})
web3.extend({
property: 'miner',
methods: methods,
properties: []
})
var timeOutId
web3.eth.subscribe('pendingTransactions', (error, result) => {
if (error) {
console.log(error)
} else {
console.log('start or continue mining')
web3.miner.start()
if (timeOutId) clearTimeout(timeOutId)
timeOutId = setTimeout(() => {
console.log('stop mining')
web3.miner.stop()
}, 30000)
}
})
}

@ -0,0 +1,13 @@
const serve = require('serve')
module.exports = function (path, port) {
var server = serve(path, {
port: port
})
function kill () {
console.log('stopping frontend')
server.stop()
}
return kill
}

@ -0,0 +1,99 @@
var spawn = require('child_process').spawn
var stdout = require('stdout')
var autoMine = require('./autoMine')
var Web3 = require('web3')
var net = require('net')
var connectTimeout
module.exports = function (dataDir, mist, geth, mine, rpc, rpcPort) {
console.log('opening dev env at ' + dataDir)
// geth --vmdebug --dev --ipcpath /home/yann/Ethereum/testchains/test2/geth.ipc --datadir /home/yann/Ethereum/testchains/test2
var gethprocess
if (geth) {
var ipcPath = dataDir + '/geth.ipc'
var gethArgs = [
'--vmdebug',
'--dev',
'--ipcpath', ipcPath,
'--datadir', dataDir
]
if (rpc) {
gethArgs.push('--rpc')
gethArgs.push('--rpccorsdomain')
gethArgs.push(rpc)
gethArgs.push('--rpcapi')
gethArgs.push('web3,eth,debug,net')
if (!rpcPort) {
rpcPort = 8545
}
gethArgs.push('--rpcport')
gethArgs.push(rpcPort)
}
console.log(gethArgs)
console.log('starting geth ... ')
gethprocess = run('geth', gethArgs)
connectTimeout = setInterval(() => {
connectWeb3(ipcPath, (web3) => {
clearInterval(connectTimeout)
if (mine) {
autoMine(web3)
}
})
}, 1000)
}
// mist --rpc /home/yann/Ethereum/testchains/test2/geth.ipc
var mistprocess
if (mist) {
const mistArgs = [
'--rpc', ipcPath
]
console.log('starting mist ...')
mistprocess = run('mist', mistArgs)
}
function kill () {
if (connectTimeout) {
clearInterval(connectTimeout)
}
if (mistprocess) {
console.log('stopping mist')
mistprocess.kill()
}
if (gethprocess) {
console.log('stopping geth')
gethprocess.kill()
}
}
return kill
}
function connectWeb3 (ipcpath, cb) {
try {
console.log('connect to ' + ipcpath)
var web3 = new Web3(new Web3.providers.IpcProvider(ipcpath, net))
web3.eth.getBlockNumber(function (error) {
if (error) {
console.log('still trying to connect to node... ' + error)
} else {
cb(web3)
}
})
} catch (e) {}
}
function run (app, args) {
var proc
try {
proc = spawn(app, args)
proc.on('error', (err) => {
console.log('\x1b[31m%s\x1b[0m', '[ERR] can\'t start ' + app + '. seems not installed')
console.log(err)
})
proc.stdout.pipe(stdout())
} catch (e) {
}
return proc
}

@ -8,18 +8,18 @@ class WebSocket {
}
start (callback) {
var server = http.createServer(function (request, response) {
this.server = http.createServer(function (request, response) {
console.log((new Date()) + ' Received request for ' + request.url)
response.writeHead(404)
response.end()
})
var loopback = '127.0.0.1'
server.listen(65520, loopback, function () {
this.server.listen(65520, loopback, function () {
console.log((new Date()) + ' Remixd is listening on ' + loopback + ':65520')
})
this.wsServer = new WebSocketServer({
httpServer: server,
httpServer: this.server,
autoAcceptConnections: false
})
@ -53,6 +53,17 @@ class WebSocket {
send (data) {
this.connection.sendUTF(data)
}
close () {
if (this.connection) {
this.connection.close()
this.connection = null
}
if (this.server) {
this.server.close()
this.server = null
}
}
}
function originIsAllowed (origin) {

Loading…
Cancel
Save