Merge pull request #4827 from ethereum/refactor_simulator

export server
pull/5289/head
yann300 6 months ago committed by GitHub
commit bc9e9fd266
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 39
      libs/remix-simulator/bin/ethsim
  2. 1
      libs/remix-simulator/src/index.ts
  3. 6
      libs/remix-simulator/src/provider.ts
  4. 56
      libs/remix-simulator/src/server.ts

@ -23,22 +23,37 @@ program
.command('start') .command('start')
.option('-p, --port [port]', 'specify port', 8545) .option('-p, --port [port]', 'specify port', 8545)
.option('-b, --ip [host]', 'specify host', '127.0.0.1') .option('-b, --ip [host]', 'specify host', '127.0.0.1')
.option('-c, --coinbase [coinbase]', 'specify coinbase', '0x0000000000000000000000000000000000000000')
.option('--rpc', 'run rpc server only', true) .option('--rpc', 'run rpc server only', true)
.option('--details', 'display payloads for every requests and their responses', false) .option('--details', 'display payloads for every requests and their responses', false)
.action((option) => { .option('-c, --coinbase [coinbase]', 'specify coinbase', '0x0000000000000000000000000000000000000000')
console.log('coinbase: ', option.coinbase) .option('-f, --fork [fork]', 'specify fork name')
.option('-n, --nodeUrl [nodeUrl]', 'specify node url')
.option('-bn, --blockNumber [blockNumber]', 'specify block Number')
.option('-s, --stateDb [stateDb]', 'specify state database')
.option('-bs, --blocks [blocks]', 'specify blocks')
.action((option, env) => {
env.outputHelp()
console.log('\n')
console.log('Usage:')
console.log('remix-simulator start')
console.log('remix-simulator start -n <node url> -f cancun -bn latest')
console.log('\n')
console.log('Command line options:')
console.log('port: ', option.port)
console.log('host: ', option.ip)
console.log('rpc: ', option.rpc) console.log('rpc: ', option.rpc)
console.log('details: ', option.details) console.log('details: ', option.details)
console.log('host: ', option.ip) console.log('\n')
console.log('port: ', option.port) console.log('Provider options:')
const Server = require('../src/server') console.log('coinbase: ', option.coinbase)
const server = new Server({ console.log('fork: ', option.fork)
coinbase: option.coinbase, console.log('nodeUrl: ', option.nodeUrl)
rpc: option.rpc, console.log('blockNumber: ', option.blockNumber)
logDetails: option.details console.log('stateDb: ', option.stateDb)
}) console.log('blocks: ', option.blocks)
server.start(option.ip, option.port) const { Server } = require('../src/server')
const server = new Server(option)
server.start(option)
}) })
program.parse(process.argv) program.parse(process.argv)

@ -1 +1,2 @@
export { Provider, extend, JSONRPCRequestPayload, JSONRPCResponsePayload, JSONRPCResponseCallback } from './provider' export { Provider, extend, JSONRPCRequestPayload, JSONRPCResponsePayload, JSONRPCResponseCallback } from './provider'
export { Server } from './server'

@ -35,7 +35,7 @@ export type ProviderOptions = {
nodeUrl?: string, nodeUrl?: string,
blockNumber?: number | 'latest', blockNumber?: number | 'latest',
stateDb?: State, stateDb?: State,
logDetails?: boolean details?: boolean
blocks?: string[], blocks?: string[],
coinbase?: string coinbase?: string
} }
@ -90,12 +90,12 @@ export class Provider {
return return
} }
const method = this.methods[payload.method] const method = this.methods[payload.method]
if (this.options.logDetails) { if (this.options.details) {
info(payload) info(payload)
} }
if (method) { if (method) {
return method.call(method, payload, (err, result) => { return method.call(method, payload, (err, result) => {
if (this.options.logDetails) { if (this.options.details) {
info(err) info(err)
info(result) info(result)
} }

@ -1,26 +1,32 @@
import express from 'express'
import cors from 'cors' import cors from 'cors'
import bodyParser from 'body-parser' import bodyParser from 'body-parser'
import expressWs from 'express-ws' import { Provider, ProviderOptions } from './provider'
import { Provider } from './provider' import { log, error } from './utils/logs'
import { log } from './utils/logs'
const app = express() export type CliOptions = {
rpc?: boolean,
port: number
ip: string
}
class Server { export class Server {
provider provider
rpcOnly
constructor (options) { constructor (options?: ProviderOptions) {
this.provider = new Provider(options) this.provider = new Provider(options)
this.provider.init().then(() => { this.provider.init().then(() => {
log('Provider initiated') log('Provider initiated')
log('Test accounts:')
log(Object.keys(this.provider.Accounts.accounts))
}).catch((error) => { }).catch((error) => {
log(error) log(error)
}) })
this.rpcOnly = options.rpc
} }
start (host, port) { async start (cliOptions: CliOptions) {
const expressWs = (await import('express-ws')).default
const express = (await import('express')).default
const app = express()
const wsApp = expressWs(app) const wsApp = expressWs(app)
app.use(cors()) app.use(cors())
@ -31,22 +37,39 @@ class Server {
res.send('Welcome to remix-simulator') res.send('Welcome to remix-simulator')
}) })
if (this.rpcOnly) { if (cliOptions.rpc) {
app.use((req, res) => { app.use((req, res) => {
if (req && req.body && (req.body.method === 'eth_sendTransaction' || req.body.method === 'eth_call')) {
log('Receiving call/transaction:')
log(req.body.params)
}
this.provider.sendAsync(req.body, (err, jsonResponse) => { this.provider.sendAsync(req.body, (err, jsonResponse) => {
if (err) { if (err) {
error(err)
return res.send(JSON.stringify({ error: err })) return res.send(JSON.stringify({ error: err }))
} }
if (req && req.body && (req.body.method === 'eth_sendTransaction' || req.body.method === 'eth_call')) {
log(jsonResponse)
}
res.send(jsonResponse) res.send(jsonResponse)
}) })
}) })
} else { } else {
wsApp.app.ws('/', (ws, req) => { wsApp.app.ws('/', (ws, req) => {
ws.on('message', (msg) => { ws.on('message', (msg) => {
this.provider.sendAsync(JSON.parse(msg.toString()), (err, jsonResponse) => { const body = JSON.parse(msg.toString())
if (body && (body.method === 'eth_sendTransaction' || body.method === 'eth_call')) {
log('Receiving call/transaction:')
log(body.params)
}
this.provider.sendAsync(body, (err, jsonResponse) => {
if (err) { if (err) {
error(err)
return ws.send(JSON.stringify({ error: err })) return ws.send(JSON.stringify({ error: err }))
} }
if (body && (body.method === 'eth_sendTransaction' || body.method === 'eth_call')) {
log(jsonResponse)
}
ws.send(JSON.stringify(jsonResponse)) ws.send(JSON.stringify(jsonResponse))
}) })
}) })
@ -57,13 +80,14 @@ class Server {
}) })
} }
app.listen(port, host, () => { app.listen(cliOptions.port, cliOptions.ip, () => {
log('Remix Simulator listening on ws://' + host + ':' + port) if (!cliOptions.rpc) {
if (!this.rpcOnly) { log('Remix Simulator listening on ws://' + cliOptions.ip + ':' + cliOptions.port)
log('http json-rpc is deprecated and disabled by default. To enable it use --rpc') log('http json-rpc is deprecated and disabled by default. To enable it use --rpc')
} else {
log('Remix Simulator listening on http://' + cliOptions.ip + ':' + cliOptions.port)
} }
}) })
} }
} }
module.exports = Server

Loading…
Cancel
Save