add subscription & logs methods; create logmanager to direct subscriptiosn

pull/7/head
Iuri Matias 5 years ago
parent 45e865d542
commit 02fbc04375
  1. 6
      remix-lib/src/execution/execution-context.js
  2. 74
      remix-lib/src/execution/logsManager.js
  3. 48
      remix-simulator/src/methods/filters.js
  4. 18
      remix-simulator/src/provider.js
  5. 6
      remix-simulator/src/server.js

@ -7,6 +7,8 @@ var ethUtil = require('ethereumjs-util')
var StateManager = require('ethereumjs-vm/dist/stateManager') var StateManager = require('ethereumjs-vm/dist/stateManager')
var Web3VMProvider = require('../web3Provider/web3VmProvider') var Web3VMProvider = require('../web3Provider/web3VmProvider')
var LogsManager = require('./logsManager.js');
var rlp = ethUtil.rlp var rlp = ethUtil.rlp
var injectedProvider var injectedProvider
@ -105,6 +107,8 @@ function ExecutionContext () {
var self = this var self = this
this.event = new EventManager() this.event = new EventManager()
this.logsManager = new LogsManager()
var executionContext = null var executionContext = null
this.blockGasLimitDefault = 4300000 this.blockGasLimitDefault = 4300000
@ -305,6 +309,8 @@ function ExecutionContext () {
self.blocks['0x' + block.hash().toString('hex')] = block self.blocks['0x' + block.hash().toString('hex')] = block
self.blocks[blockNumber] = block self.blocks[blockNumber] = block
this.logsManager.checkBlock(blockNumber, block)
} }
this.trackTx = function (tx, block) { this.trackTx = function (tx, block) {

@ -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;

@ -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

@ -1,8 +1,12 @@
var RemixLib = require('remix-lib')
var executionContext = RemixLib.execution.executionContext
const log = require('./utils/logs.js') const log = require('./utils/logs.js')
const merge = require('merge') const merge = require('merge')
const Accounts = require('./methods/accounts.js') const Accounts = require('./methods/accounts.js')
const Blocks = require('./methods/blocks.js') const Blocks = require('./methods/blocks.js')
const Filters = require('./methods/filters.js')
const Misc = require('./methods/misc.js') const Misc = require('./methods/misc.js')
const Net = require('./methods/net.js') const Net = require('./methods/net.js')
const Transactions = require('./methods/transactions.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, this.Accounts.methods())
this.methods = merge(this.methods, (new Blocks(options)).methods()) this.methods = merge(this.methods, (new Blocks(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()).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()))
this.methods = merge(this.methods, (new Whisper()).methods()) this.methods = merge(this.methods, (new Whisper()).methods())
generateBlock() generateBlock()
setTimeout(() => {
console.dir("hello!")
}, 10 * 1000)
} }
Provider.prototype.init = async function () { Provider.prototype.init = async function () {
@ -32,14 +41,19 @@ Provider.prototype.init = async function () {
Provider.prototype.sendAsync = function (payload, callback) { Provider.prototype.sendAsync = function (payload, callback) {
log.info('payload method is ', payload.method) log.info('payload method is ', payload.method)
console.dir(payload)
let method = this.methods[payload.method] let method = this.methods[payload.method]
if (method) { if (method) {
return method.call(method, payload, (err, result) => { return method.call(method, payload, (err, result) => {
if (err) { if (err) {
console.dir("====== error")
console.dir(err)
return callback(err) return callback(err)
} }
let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result} let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result}
console.dir("response")
console.dir(response)
callback(null, response) callback(null, response)
}) })
} }
@ -54,4 +68,8 @@ Provider.prototype.isConnected = function () {
return true return true
} }
Provider.prototype.on = function (type, cb) {
executionContext.logsManager.addListener(type, cb)
}
module.exports = Provider module.exports = Provider

@ -48,6 +48,12 @@ class Server {
ws.send(JSON.stringify(jsonResponse)) ws.send(JSON.stringify(jsonResponse))
}) })
}) })
this.provider.on('data', (result) => {
console.dir("-----> sending")
console.dir(JSON.stringify(result))
ws.send(JSON.stringify(result));
})
}) })
} }

Loading…
Cancel
Save