diff --git a/remix-lib/src/execution/execution-context.js b/remix-lib/src/execution/execution-context.js index 85a15608b3..fd785d4e35 100644 --- a/remix-lib/src/execution/execution-context.js +++ b/remix-lib/src/execution/execution-context.js @@ -7,6 +7,8 @@ var ethUtil = require('ethereumjs-util') var StateManager = require('ethereumjs-vm/dist/stateManager') var Web3VMProvider = require('../web3Provider/web3VmProvider') +var LogsManager = require('./logsManager.js'); + var rlp = ethUtil.rlp var injectedProvider @@ -105,6 +107,8 @@ function ExecutionContext () { var self = this this.event = new EventManager() + this.logsManager = new LogsManager() + var executionContext = null this.blockGasLimitDefault = 4300000 @@ -305,6 +309,8 @@ function ExecutionContext () { self.blocks['0x' + block.hash().toString('hex')] = block self.blocks[blockNumber] = block + + this.logsManager.checkBlock(blockNumber, block) } this.trackTx = function (tx, block) { diff --git a/remix-lib/src/execution/logsManager.js b/remix-lib/src/execution/logsManager.js new file mode 100644 index 0000000000..a6f0e44eb9 --- /dev/null +++ b/remix-lib/src/execution/logsManager.js @@ -0,0 +1,74 @@ +var crypto = require('crypto') + +class LogsManager { + + constructor() { + this.notificationCallbacks = [] + this.subscriptions = {} + } + + checkBlock(blockNumber, block) { + let subscriptionId = Object.keys(this.subscriptions)[0]; + + let result = { + "logIndex": "0x1", // 1 + "blockNumber": "0x1b4", // 436 + "blockHash": "0x8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcfdf829c5a142f1fccd7d", + "transactionHash": "0xdf829c5a142f1fccd7d8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcf", + "transactionIndex": "0x0", // 0 + "address": "0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d", + "data": "0x0000000000000000000000000000000000000000000000000000000000000000", + "topics": ["0x59ebeb90bc63057b6515673c3ecf9438e5058bca0f92585014eced636878c9a5"] + } + + let response = { 'jsonrpc': '2.0', "method": "eth_subscription", params: { 'result': result, 'subscription': subscriptionId } }; + this.transmit(response); + } + + transmit(result) { + console.dir("-----------------") + console.dir("---- transmit") + console.dir(this.notificationCallbacks) + console.dir(result) + + // TODO: manage subscriptions + + this.notificationCallbacks.forEach((callback) => { + callback(result) + }); + } + + addListener(type, cb) { + this.notificationCallbacks.push(cb) + console.dir("--------------------------------------------------------->") + console.dir("==========================") + console.dir("==========================") + console.dir(this.notificationCallbacks) + console.dir("==========================") + console.dir("==========================") + } + + subscribe(params) { + let subscriptionId = "0x" + crypto.randomBytes(16).toString('hex') + this.subscriptions[subscriptionId] = params + return subscriptionId + } + + getLogsFor(params) { + let results = [{ + "logIndex": "0x1", // 1 + "blockNumber": "0x1b4", // 436 + "blockHash": "0x8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcfdf829c5a142f1fccd7d", + "transactionHash": "0xdf829c5a142f1fccd7d8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcf", + "transactionIndex": "0x0", // 0 + "address": "0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d", + "data": "0x0000000000000000000000000000000000000000000000000000000000000000", + "topics": ["0x59ebeb90bc63057b6515673c3ecf9438e5058bca0f92585014eced636878c9a5"] + }] + + return results + } + +} + +module.exports = LogsManager; \ No newline at end of file diff --git a/remix-simulator/src/methods/filters.js b/remix-simulator/src/methods/filters.js new file mode 100644 index 0000000000..eb73c5a3e6 --- /dev/null +++ b/remix-simulator/src/methods/filters.js @@ -0,0 +1,48 @@ +var RemixLib = require('remix-lib') +var executionContext = RemixLib.execution.executionContext + +var Filters = function (_options) { + const options = _options || {} +} + +Filters.prototype.methods = function () { + return { + eth_getLogs: this.eth_getLogs.bind(this), + eth_subscribe: this.eth_subscribe.bind(this) + } +} + +// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs +Filters.prototype.eth_getLogs = function (payload, cb) { + console.dir("===============================") + console.dir("===============================") + console.dir("=== eth_getLogs") + console.dir(payload.params) + // [ { fromBlock: '0x0', + // address: '0xdb2eb1480cb3ac3a5c0ee957045d1ad9dcd34f01', + // topics: [] } ] + + // console.dir(executionContext.vm().stateManager) + // console.dir(executionContext.vm().blockchain) + + // var block = executionContext.blocks[payload.params[0]] + + // executionContext.vm().stateManager.getLogs(address, (err, account) => { + + let results = executionContext.logsManager.getLogsFor(payload.params); + + cb(null, results) +} + +Filters.prototype.eth_subscribe = function (payload, cb) { + console.dir("===============================") + console.dir("===============================") + console.dir("=== eth_subscribe") + console.dir(payload.params) + + let subscriptionId = executionContext.logsManager.subscribe(payload.params); + + cb(null, subscriptionId) +} + +module.exports = Filters diff --git a/remix-simulator/src/provider.js b/remix-simulator/src/provider.js index 4c1f9e706a..1d89d473d6 100644 --- a/remix-simulator/src/provider.js +++ b/remix-simulator/src/provider.js @@ -1,8 +1,12 @@ +var RemixLib = require('remix-lib') +var executionContext = RemixLib.execution.executionContext + const log = require('./utils/logs.js') const merge = require('merge') const Accounts = require('./methods/accounts.js') const Blocks = require('./methods/blocks.js') +const Filters = require('./methods/filters.js') const Misc = require('./methods/misc.js') const Net = require('./methods/net.js') const Transactions = require('./methods/transactions.js') @@ -18,11 +22,16 @@ var Provider = function (options) { this.methods = merge(this.methods, this.Accounts.methods()) this.methods = merge(this.methods, (new Blocks(options)).methods()) this.methods = merge(this.methods, (new Misc()).methods()) + this.methods = merge(this.methods, (new Filters()).methods()) this.methods = merge(this.methods, (new Net()).methods()) this.methods = merge(this.methods, (this.Transactions.methods())) this.methods = merge(this.methods, (new Whisper()).methods()) generateBlock() + + setTimeout(() => { + console.dir("hello!") + }, 10 * 1000) } Provider.prototype.init = async function () { @@ -32,14 +41,19 @@ Provider.prototype.init = async function () { Provider.prototype.sendAsync = function (payload, callback) { log.info('payload method is ', payload.method) + console.dir(payload) let method = this.methods[payload.method] if (method) { return method.call(method, payload, (err, result) => { if (err) { + console.dir("====== error") + console.dir(err) return callback(err) } let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result} + console.dir("response") + console.dir(response) callback(null, response) }) } @@ -54,4 +68,8 @@ Provider.prototype.isConnected = function () { return true } +Provider.prototype.on = function (type, cb) { + executionContext.logsManager.addListener(type, cb) +} + module.exports = Provider diff --git a/remix-simulator/src/server.js b/remix-simulator/src/server.js index 7059562afd..d30c4e5b82 100644 --- a/remix-simulator/src/server.js +++ b/remix-simulator/src/server.js @@ -48,6 +48,12 @@ class Server { ws.send(JSON.stringify(jsonResponse)) }) }) + + this.provider.on('data', (result) => { + console.dir("-----> sending") + console.dir(JSON.stringify(result)) + ws.send(JSON.stringify(result)); + }) }) }