Commit 72619b99 authored by yann300's avatar yann300

get web3VMProvider out of txlistener

parent 7830b342
...@@ -7,7 +7,8 @@ var base64 = require('js-base64').Base64 ...@@ -7,7 +7,8 @@ var base64 = require('js-base64').Base64
var swarmgw = require('swarmgw') var swarmgw = require('swarmgw')
var csjs = require('csjs-inject') var csjs = require('csjs-inject')
var yo = require('yo-yo') var yo = require('yo-yo')
var EventManager = require('ethereum-remix').lib.EventManager var remix = require('ethereum-remix')
var EventManager = remix.lib.EventManager
var UniversalDApp = require('./universal-dapp.js') var UniversalDApp = require('./universal-dapp.js')
var Remixd = require('./lib/remixd') var Remixd = require('./lib/remixd')
...@@ -30,10 +31,9 @@ var EditorPanel = require('./app/editor-panel') ...@@ -30,10 +31,9 @@ var EditorPanel = require('./app/editor-panel')
var RighthandPanel = require('./app/righthand-panel') var RighthandPanel = require('./app/righthand-panel')
var examples = require('./app/example-contracts') var examples = require('./app/example-contracts')
var modalDialogCustom = require('./app/modal-dialog-custom') var modalDialogCustom = require('./app/modal-dialog-custom')
/*
var Txlistener = require('./app/txListener') var Txlistener = require('./app/txListener')
var EventsDecoder = require('./app/eventsDecoder') var EventsDecoder = require('./app/eventsDecoder')
*/ var Web3VMProvider = remix.web3.web3VMProvider
var css = csjs` var css = csjs`
html { box-sizing: border-box; } html { box-sizing: border-box; }
...@@ -752,7 +752,32 @@ function run () { ...@@ -752,7 +752,32 @@ function run () {
// ----------------- Tx listener ----------------- // ----------------- Tx listener -----------------
// not used right now // not used right now
/*
// TODO the following should be put in execution context
var web3VM = new Web3VMProvider()
web3VM.setVM(executionContext.vm())
var currentWeb3 = function () {
return executionContext.isVM() ? web3VM : executionContext.web3()
}
var transactionReceiptResolver = {
_transactionReceipts: {},
resolve: function (tx, cb) {
if (this._transactionReceipts[tx.hash]) {
return cb(null, this._transactionReceipts[tx.hash])
}
currentWeb3().eth.getTransactionReceipt(tx.hash, (error, receipt) => {
if (!error) {
this._transactionReceipts[tx.hash] = receipt
cb(null, receipt)
} else {
cb(error)
}
})
}
}
var compiledContracts = function () { var compiledContracts = function () {
if (compiler.lastCompilationResult && compiler.lastCompilationResult.data) { if (compiler.lastCompilationResult && compiler.lastCompilationResult.data) {
return compiler.lastCompilationResult.data.contracts return compiler.lastCompilationResult.data.contracts
...@@ -762,22 +787,29 @@ function run () { ...@@ -762,22 +787,29 @@ function run () {
var txlistener = new Txlistener({ var txlistener = new Txlistener({
api: { api: {
web3: function () { return executionContext.web3() }, web3: function () { return currentWeb3() },
isVM: function () { return executionContext.isVM() }, isVM: function () { return executionContext.isVM() },
vm: function () { return executionContext.vm() },
contracts: compiledContracts, contracts: compiledContracts,
context: function () { context: function () {
return executionContext.getProvider() return executionContext.getProvider()
},
resolveReceipt: function (tx, cb) {
transactionReceiptResolver.resolve(tx, cb)
} }
}, },
event: { event: {
executionContext: executionContext.event, executionContext: executionContext.event,
udapp: udapp.event udapp: udapp.event
}})
var eventsDecoder = new EventsDecoder({
api: {
resolveReceipt: function (tx, cb) {
transactionReceiptResolver.resolve(tx, cb)
}
} }
}) })
var eventsDecoder = new EventsDecoder({ txListener: txlistener })
txlistener.startListening() txlistener.startListening()
txlistener.event.register('newTransaction', (tx) => { txlistener.event.register('newTransaction', (tx) => {
...@@ -788,22 +820,27 @@ function run () { ...@@ -788,22 +820,27 @@ function run () {
address = resolvedTransaction.contractAddress ? resolvedTransaction.contractAddress : tx.to address = resolvedTransaction.contractAddress ? resolvedTransaction.contractAddress : tx.to
resolvedContract = txlistener.resolvedContract(address) resolvedContract = txlistener.resolvedContract(address)
if (resolvedContract) { if (resolvedContract) {
eventsDecoder.parseLogs(tx, resolvedContract, compiledContracts(), () => { eventsDecoder.parseLogs(tx, resolvedContract, compiledContracts(), (error, log) => {
console.log({ console.log({
tx: tx, tx: tx,
resolvedContract: resolvedContract,
resolvedTransaction: resolvedTransaction, resolvedTransaction: resolvedTransaction,
resolvedEvents: eventsDecoder.eventsOf(tx.hash) resolvedContract: resolvedContract,
resolvedEvents: (!error ? log : error)
}) })
}) })
} else {
console.log({
tx: tx,
resolvedTransaction: resolvedTransaction
})
} }
} else { } else {
// contract unknown - just displaying raw tx.
console.log({ console.log({
tx: tx tx: tx
}) })
} }
}) })
*/
// ----------------- autoCompile ----------------- // ----------------- autoCompile -----------------
var autoCompile = document.querySelector('#autoCompile').checked var autoCompile = document.querySelector('#autoCompile').checked
......
...@@ -8,8 +8,7 @@ var ethJSABI = require('ethereumjs-abi') ...@@ -8,8 +8,7 @@ var ethJSABI = require('ethereumjs-abi')
*/ */
class EventsDecoder { class EventsDecoder {
constructor (opt = {}) { constructor (opt = {}) {
this.txListener = opt.txListener this._api = opt.api
this.resolvedEvents = {}
} }
/** /**
...@@ -20,19 +19,18 @@ class EventsDecoder { ...@@ -20,19 +19,18 @@ class EventsDecoder {
* @param {Function} cb - callback * @param {Function} cb - callback
*/ */
parseLogs (tx, contractName, compiledContracts, cb) { parseLogs (tx, contractName, compiledContracts, cb) {
this.txListener.resolveTransactionReceipt(tx, (error, receipt) => { this._api.resolveReceipt(tx, (error, receipt) => {
if (error) cb(error) if (error) cb(error)
this._decodeLogs(tx, receipt, contractName, compiledContracts, cb) this._decodeLogs(tx, receipt, contractName, compiledContracts, cb)
}) })
} }
eventsOf (hash) {
return this.resolvedEvents[hash]
}
_decodeLogs (tx, receipt, contract, contracts, cb) { _decodeLogs (tx, receipt, contract, contracts, cb) {
if (!contract || !receipt.logs) { if (!contract || !receipt) {
return cb() return cb('cannot decode logs - contract or receipt not resolved ')
}
if (!receipt.logs) {
return cb(null, [])
} }
this._decodeEvents(tx, receipt.logs, contract, contracts, cb) this._decodeEvents(tx, receipt.logs, contract, contracts, cb)
} }
...@@ -53,6 +51,7 @@ class EventsDecoder { ...@@ -53,6 +51,7 @@ class EventsDecoder {
_decodeEvents (tx, logs, contractName, compiledContracts, cb) { _decodeEvents (tx, logs, contractName, compiledContracts, cb) {
var eventABI = this._eventABI(contractName, compiledContracts) var eventABI = this._eventABI(contractName, compiledContracts)
// FIXME: support indexed events // FIXME: support indexed events
var events = []
for (var i in logs) { for (var i in logs) {
// [address, topics, mem] // [address, topics, mem]
var log = logs[i] var log = logs[i]
...@@ -70,12 +69,9 @@ class EventsDecoder { ...@@ -70,12 +69,9 @@ class EventsDecoder {
} catch (e) { } catch (e) {
decoded = log.data decoded = log.data
} }
if (!this.resolvedEvents[tx.hash]) { events.push({ event: event, args: decoded })
this.resolvedEvents[tx.hash] = []
}
this.resolvedEvents[tx.hash].push({ event: event, args: decoded })
} }
cb() cb(null, events)
} }
} }
......
...@@ -5,7 +5,6 @@ var ethJSUtil = require('ethereumjs-util') ...@@ -5,7 +5,6 @@ var ethJSUtil = require('ethereumjs-util')
var EventManager = require('ethereum-remix').lib.EventManager var EventManager = require('ethereum-remix').lib.EventManager
var remix = require('ethereum-remix') var remix = require('ethereum-remix')
var codeUtil = remix.util.code var codeUtil = remix.util.code
var Web3VMProvider = remix.web3.web3VMProvider
/** /**
* poll web3 each 2s if web3 * poll web3 each 2s if web3
...@@ -18,11 +17,8 @@ class TxListener { ...@@ -18,11 +17,8 @@ class TxListener {
constructor (opt) { constructor (opt) {
this.event = new EventManager() this.event = new EventManager()
this._api = opt.api this._api = opt.api
this._web3VMProvider = new Web3VMProvider() // TODO this should maybe be put in app.js
this._web3VMProvider.setVM(opt.api.vm())
this._resolvedTransactions = {} this._resolvedTransactions = {}
this._resolvedContracts = {} this._resolvedContracts = {}
this._transactionReceipts = {}
this.init() this.init()
opt.event.executionContext.register('contextChanged', (context) => { opt.event.executionContext.register('contextChanged', (context) => {
if (this.loopId) { if (this.loopId) {
...@@ -31,7 +27,7 @@ class TxListener { ...@@ -31,7 +27,7 @@ class TxListener {
}) })
opt.event.udapp.register('transactionExecuted', (to, data, lookupOnly, txResult) => { opt.event.udapp.register('transactionExecuted', (to, data, lookupOnly, txResult) => {
if (this.loopId && this._api.isVM()) { if (this.loopId && this._api.isVM()) {
this._web3VMProvider.getTransaction(txResult.transactionHash, (error, tx) => { this._api.web3().eth.getTransaction(txResult.transactionHash, (error, tx) => {
if (error) return console.log(error) if (error) return console.log(error)
this._newBlock({ this._newBlock({
type: 'VM', type: 'VM',
...@@ -65,6 +61,7 @@ class TxListener { ...@@ -65,6 +61,7 @@ class TxListener {
} else { } else {
this.loopId = setInterval(() => { this.loopId = setInterval(() => {
this._api.web3().eth.getBlockNumber((error, blockNumber) => { this._api.web3().eth.getBlockNumber((error, blockNumber) => {
if (this.loopId === null || this.loopId === 'vm-listener') return
if (error) return console.log(error) if (error) return console.log(error)
if (!this.lastBlock || blockNumber > this.lastBlock) { if (!this.lastBlock || blockNumber > this.lastBlock) {
this.lastBlock = blockNumber this.lastBlock = blockNumber
...@@ -79,10 +76,6 @@ class TxListener { ...@@ -79,10 +76,6 @@ class TxListener {
} }
} }
currentWeb3 () { // TODO this should maybe be put in app.js
return this._api.isVM() ? this._web3VMProvider : this._api.web3()
}
/** /**
* stop listening for incoming transactions. do not reset the recorded pool. * stop listening for incoming transactions. do not reset the recorded pool.
* *
...@@ -147,7 +140,7 @@ class TxListener { ...@@ -147,7 +140,7 @@ class TxListener {
var code = tx.input var code = tx.input
contractName = this._tryResolveContract(code, contracts, 'bytecode') contractName = this._tryResolveContract(code, contracts, 'bytecode')
if (contractName) { if (contractName) {
this.resolveTransactionReceipt(tx, (error, receipt) => { this._api.resolveReceipt(tx, (error, receipt) => {
if (error) return cb(error) if (error) return cb(error)
var address = receipt.contractAddress var address = receipt.contractAddress
this._resolvedContracts[address] = contractName this._resolvedContracts[address] = contractName
...@@ -164,7 +157,7 @@ class TxListener { ...@@ -164,7 +157,7 @@ class TxListener {
// first check known contract, resolve against the `runtimeBytecode` if not known // first check known contract, resolve against the `runtimeBytecode` if not known
contractName = this._resolvedContracts[tx.to] contractName = this._resolvedContracts[tx.to]
if (!contractName) { if (!contractName) {
this.currentWeb3().eth.getCode(tx.to, (error, code) => { this._api.web3().eth.getCode(tx.to, (error, code) => {
if (error) return cb(error) if (error) return cb(error)
if (code) { if (code) {
var contractName = this._tryResolveContract(code, contracts, 'runtimeBytecode') var contractName = this._tryResolveContract(code, contracts, 'runtimeBytecode')
...@@ -186,20 +179,6 @@ class TxListener { ...@@ -186,20 +179,6 @@ class TxListener {
} }
} }
resolveTransactionReceipt (tx, cb) {
if (this._transactionReceipts[tx.hash]) {
return cb(null, this._transactionReceipts[tx.hash])
}
this.currentWeb3().eth.getTransactionReceipt(tx.hash, (error, receipt) => {
if (!error) {
this._transactionReceipts[tx.hash] = receipt
cb(null, receipt)
} else {
cb(error)
}
})
}
_resolveFunction (contractName, compiledContracts, tx, isCtor) { _resolveFunction (contractName, compiledContracts, tx, isCtor) {
var abi = JSON.parse(compiledContracts[contractName].interface) var abi = JSON.parse(compiledContracts[contractName].interface)
var inputData = tx.input.replace('0x', '') var inputData = tx.input.replace('0x', '')
......
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