Commit a1f50eb1 authored by aniket-engg's avatar aniket-engg

lint, build & test working for remix-simulator

parent 6c1c5677
{
"extends": "../../.eslintrc",
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-unused-vars": "off"
},
"env": {
"browser": true,
"amd": true,
"node": true,
"es6": true
},
"ignorePatterns": ["!**/*"]
}
\ No newline at end of file
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
"express": "^4.16.3", "express": "^4.16.3",
"express-ws": "^4.0.0", "express-ws": "^4.0.0",
"merge": "^1.2.0", "merge": "^1.2.0",
"remix-lib": "0.4.29", "@remix-project/remix-lib": "0.4.29",
"standard": "^10.0.3", "standard": "^10.0.3",
"time-stamp": "^2.0.0", "time-stamp": "^2.0.0",
"web3": "^1.2.4" "web3": "^1.2.4"
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
}, },
"scripts": { "scripts": {
"lint": "standard", "lint": "standard",
"test": "standard && mocha test/" "test": "./../../node_modules/.bin/mocha --require tsconfig-paths/register test/"
}, },
"bin": { "bin": {
"ethsim": "./bin/ethsim", "ethsim": "./bin/ethsim",
......
const Blocks = function (executionContext, _options) { class Blocks {
this.executionContext = executionContext constructor (executionContext, _options) {
const options = _options || {} this.executionContext = executionContext
this.coinbase = options.coinbase || '0x0000000000000000000000000000000000000000' const options = _options || {}
this.blockNumber = 0 this.coinbase = options.coinbase || '0x0000000000000000000000000000000000000000'
} this.blockNumber = 0
Blocks.prototype.methods = function () {
return {
eth_getBlockByNumber: this.eth_getBlockByNumber.bind(this),
eth_gasPrice: this.eth_gasPrice.bind(this),
eth_coinbase: this.eth_coinbase.bind(this),
eth_blockNumber: this.eth_blockNumber.bind(this),
eth_getBlockByHash: this.eth_getBlockByHash.bind(this),
eth_getBlockTransactionCountByHash: this.eth_getBlockTransactionCountByHash.bind(this),
eth_getBlockTransactionCountByNumber: this.eth_getBlockTransactionCountByNumber.bind(this),
eth_getUncleCountByBlockHash: this.eth_getUncleCountByBlockHash.bind(this),
eth_getUncleCountByBlockNumber: this.eth_getUncleCountByBlockNumber.bind(this),
eth_getStorageAt: this.eth_getStorageAt.bind(this)
} }
}
Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { methods () {
let blockIndex = payload.params[0] return {
if (blockIndex === 'latest') { eth_getBlockByNumber: this.eth_getBlockByNumber.bind(this),
blockIndex = this.executionContext.latestBlockNumber eth_gasPrice: this.eth_gasPrice.bind(this),
eth_coinbase: this.eth_coinbase.bind(this),
eth_blockNumber: this.eth_blockNumber.bind(this),
eth_getBlockByHash: this.eth_getBlockByHash.bind(this),
eth_getBlockTransactionCountByHash: this.eth_getBlockTransactionCountByHash.bind(this),
eth_getBlockTransactionCountByNumber: this.eth_getBlockTransactionCountByNumber.bind(this),
eth_getUncleCountByBlockHash: this.eth_getUncleCountByBlockHash.bind(this),
eth_getUncleCountByBlockNumber: this.eth_getUncleCountByBlockNumber.bind(this),
eth_getStorageAt: this.eth_getStorageAt.bind(this)
}
} }
const block = this.executionContext.blocks[blockIndex] eth_getBlockByNumber (payload, cb) {
let blockIndex = payload.params[0]
if (blockIndex === 'latest') {
blockIndex = this.executionContext.latestBlockNumber
}
if (!block) { const block = this.executionContext.blocks[blockIndex]
return cb(new Error('block not found'))
}
let b = { if (!block) {
'number': toHex(block.header.number), return cb(new Error('block not found'))
'hash': toHex(block.hash()), }
'parentHash': toHex(block.header.parentHash),
'nonce': toHex(block.header.nonce),
'sha3Uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
'logsBloom': '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
'transactionsRoot': '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'stateRoot': toHex(block.header.stateRoot),
'miner': this.coinbase,
'difficulty': toHex(block.header.difficulty),
'totalDifficulty': toHex(block.header.totalDifficulty),
'extraData': toHex(block.header.extraData),
'size': '0x027f07', // 163591
'gasLimit': toHex(block.header.gasLimit),
'gasUsed': toHex(block.header.gasUsed),
'timestamp': toHex(block.header.timestamp),
'transactions': block.transactions.map((t) => '0x' + t.hash().toString('hex')),
'uncles': []
}
cb(null, b) let b = {
'number': this.toHex(block.header.number),
'hash': this.toHex(block.hash()),
'parentHash': this.toHex(block.header.parentHash),
'nonce': this.toHex(block.header.nonce),
'sha3Uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
'logsBloom': '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
'transactionsRoot': '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'stateRoot': this.toHex(block.header.stateRoot),
'miner': this.coinbase,
'difficulty': this.toHex(block.header.difficulty),
'totalDifficulty': this.toHex(block.header.totalDifficulty),
'extraData': this.toHex(block.header.extraData),
'size': '0x027f07', // 163591
'gasLimit': this.toHex(block.header.gasLimit),
'gasUsed': this.toHex(block.header.gasUsed),
'timestamp': this.toHex(block.header.timestamp),
'transactions': block.transactions.map((t) => '0x' + t.hash().toString('hex')),
'uncles': []
}
cb(null, b)
} }
function toHex (value) { toHex (value) {
if (!value) return '0x0' if (!value) return '0x0'
let v = value.toString('hex') let v = value.toString('hex')
return ((v === '0x' || v === '') ? '0x0' : ('0x' + v)) return ((v === '0x' || v === '') ? '0x0' : ('0x' + v))
}
Blocks.prototype.eth_getBlockByHash = function (payload, cb) {
var block = this.executionContext.blocks[payload.params[0]]
let b = {
'number': toHex(block.header.number),
'hash': toHex(block.hash()),
'parentHash': toHex(block.header.parentHash),
'nonce': toHex(block.header.nonce),
'sha3Uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
'logsBloom': '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
'transactionsRoot': '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'stateRoot': toHex(block.header.stateRoot),
'miner': this.coinbase,
'difficulty': toHex(block.header.difficulty),
'totalDifficulty': toHex(block.header.totalDifficulty),
'extraData': toHex(block.header.extraData),
'size': '0x027f07', // 163591
'gasLimit': toHex(block.header.gasLimit),
'gasUsed': toHex(block.header.gasUsed),
'timestamp': toHex(block.header.timestamp),
'transactions': block.transactions.map((t) => '0x' + t.hash().toString('hex')),
'uncles': []
} }
cb(null, b) eth_getBlockByHash (payload, cb) {
} var block = this.executionContext.blocks[payload.params[0]]
let b = {
'number': this.toHex(block.header.number),
'hash': this.toHex(block.hash()),
'parentHash': this.toHex(block.header.parentHash),
'nonce': this.toHex(block.header.nonce),
'sha3Uncles': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
'logsBloom': '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331',
'transactionsRoot': '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
'stateRoot': this.toHex(block.header.stateRoot),
'miner': this.coinbase,
'difficulty': this.toHex(block.header.difficulty),
'totalDifficulty': this.toHex(block.header.totalDifficulty),
'extraData': this.toHex(block.header.extraData),
'size': '0x027f07', // 163591
'gasLimit': this.toHex(block.header.gasLimit),
'gasUsed': this.toHex(block.header.gasUsed),
'timestamp': this.toHex(block.header.timestamp),
'transactions': block.transactions.map((t) => '0x' + t.hash().toString('hex')),
'uncles': []
}
Blocks.prototype.eth_gasPrice = function (payload, cb) { cb(null, b)
cb(null, 1) }
}
Blocks.prototype.eth_coinbase = function (payload, cb) { eth_gasPrice (payload, cb) {
cb(null, this.coinbase) cb(null, 1)
} }
Blocks.prototype.eth_blockNumber = function (payload, cb) { eth_coinbase (payload, cb) {
cb(null, this.blockNumber) cb(null, this.coinbase)
} }
Blocks.prototype.eth_getBlockTransactionCountByHash = function (payload, cb) { eth_blockNumber (payload, cb) {
var block = this.executionContext.blocks[payload.params[0]] cb(null, this.blockNumber)
}
cb(null, block.transactions.length) eth_getBlockTransactionCountByHash (payload, cb) {
} var block = this.executionContext.blocks[payload.params[0]]
Blocks.prototype.eth_getBlockTransactionCountByNumber = function (payload, cb) { cb(null, block.transactions.length)
var block = this.executionContext.blocks[payload.params[0]] }
cb(null, block.transactions.length) eth_getBlockTransactionCountByNumber (payload, cb) {
} var block = this.executionContext.blocks[payload.params[0]]
Blocks.prototype.eth_getUncleCountByBlockHash = function (payload, cb) { cb(null, block.transactions.length)
cb(null, 0) }
}
Blocks.prototype.eth_getUncleCountByBlockNumber = function (payload, cb) { eth_getUncleCountByBlockHash (payload, cb) {
cb(null, 0) cb(null, 0)
} }
Blocks.prototype.eth_getStorageAt = function (payload, cb) { eth_getUncleCountByBlockNumber (payload, cb) {
const [address, position, blockNumber] = payload.params cb(null, 0)
}
this.executionContext.web3().debug.storageRangeAt(blockNumber, 'latest', address.toLowerCase(), position, 1, (err, result) => { eth_getStorageAt (payload, cb) {
if (err || (result.storage && Object.values(result.storage).length === 0)) { const [address, position, blockNumber] = payload.params
return cb(err, '')
}
let value = Object.values(result.storage)[0].value this.executionContext.web3().debug.storageRangeAt(blockNumber, 'latest', address.toLowerCase(), position, 1, (err, result) => {
cb(err, value) if (err || (result.storage && Object.values(result.storage).length === 0)) {
}) return cb(err, '')
}
let value = Object.values(result.storage)[0].value
cb(err, value)
})
}
} }
module.exports = Blocks module.exports = Blocks
const Filters = function (executionContext) { class Filters {
this.executionContext = executionContext constructor(executionContext) {
} this.executionContext = executionContext
}
Filters.prototype.methods = function () { methods () {
return { return {
eth_getLogs: this.eth_getLogs.bind(this), eth_getLogs: this.eth_getLogs.bind(this),
eth_subscribe: this.eth_subscribe.bind(this), eth_subscribe: this.eth_subscribe.bind(this),
eth_unsubscribe: this.eth_unsubscribe.bind(this) eth_unsubscribe: this.eth_unsubscribe.bind(this)
}
} }
}
Filters.prototype.eth_getLogs = function (payload, cb) { eth_getLogs (payload, cb) {
let results = this.executionContext.logsManager.getLogsFor(payload.params[0]) let results = this.executionContext.logsManager.getLogsFor(payload.params[0])
cb(null, results) cb(null, results)
} }
Filters.prototype.eth_subscribe = function (payload, cb) { eth_subscribe (payload, cb) {
let subscriptionId = this.executionContext.logsManager.subscribe(payload.params) let subscriptionId = this.executionContext.logsManager.subscribe(payload.params)
cb(null, subscriptionId) cb(null, subscriptionId)
} }
Filters.prototype.eth_unsubscribe = function (payload, cb) { eth_unsubscribe (payload, cb) {
this.executionContext.logsManager.unsubscribe(payload.params[0]) this.executionContext.logsManager.unsubscribe(payload.params[0])
cb(null, true) cb(null, true)
} }
Filters.prototype.eth_newFilter = function (payload, cb) { eth_newFilter (payload, cb) {
const filterId = this.executionContext.logsManager.newFilter('filter', payload.params[0]) const filterId = this.executionContext.logsManager.newFilter('filter', payload.params[0])
cb(null, filterId) cb(null, filterId)
} }
Filters.prototype.eth_newBlockFilter = function (payload, cb) { eth_newBlockFilter (payload, cb) {
const filterId = this.executionContext.logsManager.newFilter('block') const filterId = this.executionContext.logsManager.newFilter('block')
cb(null, filterId) cb(null, filterId)
} }
Filters.prototype.eth_newPendingTransactionFilter = function (payload, cb) { eth_newPendingTransactionFilter (payload, cb) {
const filterId = this.executionContext.logsManager.newFilter('pendingTransactions') const filterId = this.executionContext.logsManager.newFilter('pendingTransactions')
cb(null, filterId) cb(null, filterId)
} }
Filters.prototype.eth_uninstallfilter = function (payload, cb) { eth_uninstallfilter (payload, cb) {
const result = this.executionContext.logsManager.uninstallFilter(payload.params[0]) const result = this.executionContext.logsManager.uninstallFilter(payload.params[0])
cb(null, result) cb(null, result)
} }
Filters.prototype.eth_getFilterChanges = function (payload, cb) { eth_getFilterChanges (payload, cb) {
const filterId = payload.params[0] const filterId = payload.params[0]
let results = this.executionContext.logsManager.getLogsForFilter(filterId) let results = this.executionContext.logsManager.getLogsForFilter(filterId)
cb(null, results) cb(null, results)
} }
Filters.prototype.eth_getFilterLogs = function (payload, cb) { eth_getFilterLogs (payload, cb) {
const filterId = payload.params[0] const filterId = payload.params[0]
let results = this.executionContext.logsManager.getLogsForFilter(filterId, true) let results = this.executionContext.logsManager.getLogsForFilter(filterId, true)
cb(null, results) cb(null, results)
}
} }
module.exports = Filters module.exports = Filters
const RemixLib = require('remix-lib') const RemixLib = require('@remix-project/remix-lib')
const TxExecution = RemixLib.execution.txExecution const TxExecution = RemixLib.execution.txExecution
const TxRunner = RemixLib.execution.txRunner const TxRunner = RemixLib.execution.txRunner
......
const RemixLib = require('remix-lib') const RemixLib = require('@remix-project/remix-lib')
const executionContext = RemixLib.execution.executionContext const executionContext = RemixLib.execution.executionContext
const log = require('./utils/logs.js') const log = require('./utils/logs.js')
...@@ -13,63 +13,65 @@ const Transactions = require('./methods/transactions.js') ...@@ -13,63 +13,65 @@ const Transactions = require('./methods/transactions.js')
const generateBlock = require('./genesis.js') const generateBlock = require('./genesis.js')
var Provider = function (options) { class Provider {
this.options = options || {} constructor(options) {
// TODO: init executionContext here this.options = options || {}
this.executionContext = executionContext // TODO: init executionContext here
this.Accounts = new Accounts(this.executionContext) this.executionContext = executionContext
this.Transactions = new Transactions(this.executionContext) this.Accounts = new Accounts(this.executionContext)
this.Transactions = new Transactions(this.executionContext)
this.methods = {} this.methods = {}
this.methods = merge(this.methods, this.Accounts.methods()) this.methods = merge(this.methods, this.Accounts.methods())
this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods()) this.methods = merge(this.methods, (new Blocks(this.executionContext, options)).methods())
this.methods = merge(this.methods, (new Misc()).methods()) this.methods = merge(this.methods, (new Misc()).methods())
this.methods = merge(this.methods, (new Filters(this.executionContext)).methods()) this.methods = merge(this.methods, (new Filters(this.executionContext)).methods())
this.methods = merge(this.methods, (new Net()).methods()) this.methods = merge(this.methods, (new Net()).methods())
this.methods = merge(this.methods, this.Transactions.methods()) this.methods = merge(this.methods, this.Transactions.methods())
generateBlock(this.executionContext) generateBlock(this.executionContext)
this.init() this.init()
} }
Provider.prototype.init = async function () { async init () {
await this.Accounts.init() await this.Accounts.init()
this.Transactions.init(this.Accounts.accounts) this.Transactions.init(this.Accounts.accounts)
} }
Provider.prototype.sendAsync = function (payload, callback) { sendAsync (payload, callback) {
log.info('payload method is ', payload.method) log.info('payload method is ', payload.method)
const method = this.methods[payload.method] const method = this.methods[payload.method]
if (this.options.logDetails) { if (this.options.logDetails) {
log.info(payload) log.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.logDetails) {
log.info(err) log.info(err)
log.info(result) log.info(result)
} }
if (err) { if (err) {
return callback(err) return callback(err)
} }
const response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result} const response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result}
callback(null, response) callback(null, response)
}) })
}
callback(new Error('unknown method ' + payload.method))
} }
callback(new Error('unknown method ' + payload.method))
}
Provider.prototype.send = function (payload, callback) { send (payload, callback) {
this.sendAsync(payload, callback || function () {}) this.sendAsync(payload, callback || function () {})
} }
Provider.prototype.isConnected = function () { isConnected () {
return true return true
} }
Provider.prototype.on = function (type, cb) { on (type, cb) {
this.executionContext.logsManager.addListener(type, cb) this.executionContext.logsManager.addListener(type, cb)
}
} }
module.exports = Provider module.exports = Provider
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"allowJs": true,
"declaration": true,
"rootDir": "./",
"types": ["node"]
},
"exclude": ["**/*.spec.js"],
"include": [
"src/**/*.js",
"./index.js",
"bin/"
]
}
\ No newline at end of file
This diff is collapsed.
...@@ -132,11 +132,14 @@ ...@@ -132,11 +132,14 @@
"dependencies": { "dependencies": {
"@remixproject/engine": "^0.2.3", "@remixproject/engine": "^0.2.3",
"@types/tape": "^4.2.33", "@types/tape": "^4.2.33",
"ansi-gray": "^0.1.1",
"color-support": "^1.1.3",
"ethereumjs-block": "^2.2.2", "ethereumjs-block": "^2.2.2",
"ethereumjs-tx": "^2.1.2", "ethereumjs-tx": "^2.1.2",
"ethereumjs-vm": "4.1.3", "ethereumjs-vm": "4.1.3",
"http-server": "^0.11.1", "http-server": "^0.11.1",
"npm-install-version": "^6.0.2" "npm-install-version": "^6.0.2",
"time-stamp": "^2.2.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.4.5", "@babel/core": "^7.4.5",
...@@ -200,6 +203,7 @@ ...@@ -200,6 +203,7 @@
"js-beautify": "1.6.14", "js-beautify": "1.6.14",
"minixhr": "^3.2.2", "minixhr": "^3.2.2",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"mocha": "^8.0.1",
"nanohtml": "^1.6.3", "nanohtml": "^1.6.3",
"nightwatch": "^1.3.5", "nightwatch": "^1.3.5",
"notify-error": "^1.2.0", "notify-error": "^1.2.0",
......
...@@ -234,14 +234,14 @@ ...@@ -234,14 +234,14 @@
"schematics": {}, "schematics": {},
"architect": { "architect": {
"lint": { "lint": {
"builder": "@nrwl/workspace:run-commands", "builder": "@nrwl/linter:lint",
"options": { "options": {
"commands": [ "linter": "eslint",
{ "config": "libs/remix-simulator/.eslintrc",
"command": "./../../node_modules/.bin/npm-run-all lint" "files": [
} "libs/remix-simulator/**/*.js"
], ],
"cwd": "libs/remix-simulator" "exclude": ["**/node_modules/**", "libs/remix-simulator/test/**/*"]
} }
}, },
"test": { "test": {
...@@ -249,14 +249,21 @@ ...@@ -249,14 +249,21 @@
"options": { "options": {
"commands": [ "commands": [
{ {
"command": "rm -rf ../../dist"
},
{
"command": "./../../node_modules/.bin/npm-run-all test" "command": "./../../node_modules/.bin/npm-run-all test"
} }
], ],
"cwd": "libs/remix-simulator" "cwd": "libs/remix-simulator"
} }
},
"build": {
"builder": "@nrwl/node:package",
"options": {
"outputPath": "dist/libs/remix-simulator",
"tsConfig": "libs/remix-simulator/tsconfig.lib.json",
"packageJson": "libs/remix-simulator/package.json",
"main": "libs/remix-simulator/index.js",
"assets": ["libs/remix-simulator/*.md"]
}
} }
} }
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment