Commit 79e564b5 authored by aniket-engg's avatar aniket-engg Committed by Aniket

execution src updated

parent a4a16863
'use strict' 'use strict'
import { ethers } from 'ethers' import { ethers } from 'ethers'
const txHelper = require('./txHelper') import { visitContracts } from './txHelper'
/** /**
* Register to txListener and extract events * Register to txListener and extract events
...@@ -50,7 +50,7 @@ export class EventsDecoder { ...@@ -50,7 +50,7 @@ export class EventsDecoder {
_eventsABI (compiledContracts) { _eventsABI (compiledContracts) {
const eventsABI = {} const eventsABI = {}
txHelper.visitContracts(compiledContracts, (contract) => { visitContracts(compiledContracts, (contract) => {
eventsABI[contract.name] = this._eventABI(contract.object) eventsABI[contract.name] = this._eventABI(contract.object)
}) })
return eventsABI return eventsABI
......
...@@ -3,14 +3,12 @@ ...@@ -3,14 +3,12 @@
import Web3 from 'web3' import Web3 from 'web3'
const EventManager = require('../eventManager') const EventManager = require('../eventManager')
const EthJSVM = require('ethereumjs-vm').default const EthJSVM = require('ethereumjs-vm').default
const ethUtil = require('ethereumjs-util') import { rlp, keccak, bufferToHex } from 'ethereumjs-util'
const StateManager = require('ethereumjs-vm/dist/state/stateManager').default const StateManager = require('ethereumjs-vm/dist/state/stateManager').default
const Web3VMProvider = require('../web3Provider/web3VmProvider') const Web3VMProvider = require('../web3Provider/web3VmProvider')
const LogsManager = require('./logsManager.js') const LogsManager = require('./logsManager.js')
const rlp = ethUtil.rlp
let web3 let web3
if (typeof window !== 'undefined' && typeof window['ethereum'] !== 'undefined') { if (typeof window !== 'undefined' && typeof window['ethereum'] !== 'undefined') {
var injectedProvider = window['ethereum'] var injectedProvider = window['ethereum']
...@@ -32,7 +30,7 @@ class StateManagerCommonStorageDump extends StateManager { ...@@ -32,7 +30,7 @@ class StateManagerCommonStorageDump extends StateManager {
} }
putContractStorage (address, key, value, cb) { putContractStorage (address, key, value, cb) {
this.keyHashes[ethUtil.keccak(key).toString('hex')] = ethUtil.bufferToHex(key) this.keyHashes[keccak(key).toString('hex')] = bufferToHex(key)
super.putContractStorage(address, key, value, cb) super.putContractStorage(address, key, value, cb)
} }
......
'use strict' 'use strict'
import async from 'async' import async from 'async'
import { ethers } from 'ethers' import { ethers } from 'ethers'
const ethJSUtil = require('ethereumjs-util') import { toBuffer } from 'ethereumjs-util'
const EventManager = require('../eventManager') const EventManager = require('../eventManager')
const codeUtil = require('../util') const codeUtil = require('../util')
const defaultExecutionContext = require('./execution-context') import { ExecutionContext } from './execution-context'
const txFormat = require('./txFormat') import { decodeResponse } from './txFormat'
const txHelper = require('./txHelper') import { getFunction, getReceiveInterface, getConstructorInterface, visitContracts, makeFullTypeDefinition } from './txHelper'
function addExecutionCosts(txResult, tx) { function addExecutionCosts(txResult, tx) {
if (txResult && txResult.result) { if (txResult && txResult.result) {
...@@ -26,7 +26,7 @@ function addExecutionCosts(txResult, tx) { ...@@ -26,7 +26,7 @@ function addExecutionCosts(txResult, tx) {
* trigger 'newBlock' * trigger 'newBlock'
* *
*/ */
class TxListener { export class TxListener {
event event
executionContext executionContext
...@@ -42,7 +42,7 @@ class TxListener { ...@@ -42,7 +42,7 @@ class TxListener {
constructor (opt, executionContext) { constructor (opt, executionContext) {
this.event = new EventManager() this.event = new EventManager()
// has a default for now for backwards compatability // has a default for now for backwards compatability
this.executionContext = executionContext || defaultExecutionContext this.executionContext = executionContext || new ExecutionContext()
this._api = opt.api this._api = opt.api
this._resolvedTransactions = {} this._resolvedTransactions = {}
this._resolvedContracts = {} this._resolvedContracts = {}
...@@ -71,7 +71,7 @@ class TxListener { ...@@ -71,7 +71,7 @@ class TxListener {
input: data, input: data,
hash: txResult.transactionHash ? txResult.transactionHash : 'call' + (from || '') + to + data, hash: txResult.transactionHash ? txResult.transactionHash : 'call' + (from || '') + to + data,
isCall: true, isCall: true,
returnValue: this.executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result), returnValue: this.executionContext.isVM() ? txResult.result.execResult.returnValue : toBuffer(txResult.result),
envMode: this.executionContext.getProvider() envMode: this.executionContext.getProvider()
} }
...@@ -289,7 +289,7 @@ class TxListener { ...@@ -289,7 +289,7 @@ class TxListener {
const methodIdentifiers = contract.object.evm.methodIdentifiers const methodIdentifiers = contract.object.evm.methodIdentifiers
for (let fn in methodIdentifiers) { for (let fn in methodIdentifiers) {
if (methodIdentifiers[fn] === inputData.substring(0, 8)) { if (methodIdentifiers[fn] === inputData.substring(0, 8)) {
const fnabi = txHelper.getFunction(abi, fn) const fnabi = getFunction(abi, fn)
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contract.name, contractName: contract.name,
to: tx.to, to: tx.to,
...@@ -297,13 +297,13 @@ class TxListener { ...@@ -297,13 +297,13 @@ class TxListener {
params: this._decodeInputParams(inputData.substring(8), fnabi) params: this._decodeInputParams(inputData.substring(8), fnabi)
} }
if (tx.returnValue) { if (tx.returnValue) {
this._resolvedTransactions[tx.hash].decodedReturnValue = txFormat.decodeResponse(tx.returnValue, fnabi) this._resolvedTransactions[tx.hash].decodedReturnValue = decodeResponse(tx.returnValue, fnabi)
} }
return this._resolvedTransactions[tx.hash] return this._resolvedTransactions[tx.hash]
} }
} }
// receive function // receive function
if (!inputData && txHelper.getReceiveInterface(abi)) { if (!inputData && getReceiveInterface(abi)) {
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contract.name, contractName: contract.name,
to: tx.to, to: tx.to,
...@@ -323,7 +323,7 @@ class TxListener { ...@@ -323,7 +323,7 @@ class TxListener {
const bytecode = contract.object.evm.bytecode.object const bytecode = contract.object.evm.bytecode.object
let params = null let params = null
if (bytecode && bytecode.length) { if (bytecode && bytecode.length) {
params = this._decodeInputParams(inputData.substring(bytecode.length), txHelper.getConstructorInterface(abi)) params = this._decodeInputParams(inputData.substring(bytecode.length), getConstructorInterface(abi))
} }
this._resolvedTransactions[tx.hash] = { this._resolvedTransactions[tx.hash] = {
contractName: contract.name, contractName: contract.name,
...@@ -337,7 +337,7 @@ class TxListener { ...@@ -337,7 +337,7 @@ class TxListener {
_tryResolveContract (codeToResolve, compiledContracts, isCreation) { _tryResolveContract (codeToResolve, compiledContracts, isCreation) {
let found = null let found = null
txHelper.visitContracts(compiledContracts, (contract) => { visitContracts(compiledContracts, (contract) => {
const bytes = isCreation ? contract.object.evm.bytecode.object : contract.object.evm.deployedBytecode.object const bytes = isCreation ? contract.object.evm.bytecode.object : contract.object.evm.deployedBytecode.object
if (codeUtil.compareByteCode(codeToResolve, '0x' + bytes)) { if (codeUtil.compareByteCode(codeToResolve, '0x' + bytes)) {
found = contract found = contract
...@@ -348,13 +348,13 @@ class TxListener { ...@@ -348,13 +348,13 @@ class TxListener {
} }
_decodeInputParams (data, abi) { _decodeInputParams (data, abi) {
data = ethJSUtil.toBuffer('0x' + data) data = toBuffer('0x' + data)
if (!data.length) data = new Uint8Array(32 * abi.inputs.length) // ensuring the data is at least filled by 0 cause `AbiCoder` throws if there's not engouh data if (!data.length) data = new Uint8Array(32 * abi.inputs.length) // ensuring the data is at least filled by 0 cause `AbiCoder` throws if there's not engouh data
const inputTypes = [] const inputTypes = []
for (let i = 0; i < abi.inputs.length; i++) { for (let i = 0; i < abi.inputs.length; i++) {
const type = abi.inputs[i].type const type = abi.inputs[i].type
inputTypes.push(type.indexOf('tuple') === 0 ? txHelper.makeFullTypeDefinition(abi.inputs[i]) : type) inputTypes.push(type.indexOf('tuple') === 0 ? makeFullTypeDefinition(abi.inputs[i]) : type)
} }
const abiCoder = new ethers.utils.AbiCoder() const abiCoder = new ethers.utils.AbiCoder()
const decoded = abiCoder.decode(inputTypes, data) const decoded = abiCoder.decode(inputTypes, data)
...@@ -365,5 +365,3 @@ class TxListener { ...@@ -365,5 +365,3 @@ class TxListener {
return ret return ret
} }
} }
module.exports = TxListener
'use strict' 'use strict'
const EthJSTX = require('ethereumjs-tx').Transaction import { Transaction } from 'ethereumjs-tx'
const EthJSBlock = require('ethereumjs-block') import { Block } from 'ethereumjs-block'
import { BN } from 'ethereumjs-util' import { BN } from 'ethereumjs-util'
const defaultExecutionContext = require('./execution-context') import { ExecutionContext } from './execution-context'
const EventManager = require('../eventManager') const EventManager = require('../eventManager')
class TxRunner { class TxRunner {
...@@ -20,7 +20,7 @@ class TxRunner { ...@@ -20,7 +20,7 @@ class TxRunner {
constructor (vmaccounts, api, executionContext) { constructor (vmaccounts, api, executionContext) {
this.event = new EventManager() this.event = new EventManager()
// has a default for now for backwards compatability // has a default for now for backwards compatability
this.executionContext = executionContext || defaultExecutionContext this.executionContext = executionContext || new ExecutionContext()
this._api = api this._api = api
this.blockNumber = 0 this.blockNumber = 0
this.runAsync = true this.runAsync = true
...@@ -117,7 +117,7 @@ class TxRunner { ...@@ -117,7 +117,7 @@ class TxRunner {
// See https://github.com/ethereumjs/ethereumjs-tx/blob/master/docs/classes/transaction.md#constructor // See https://github.com/ethereumjs/ethereumjs-tx/blob/master/docs/classes/transaction.md#constructor
// for initialization fields and their types // for initialization fields and their types
value = value ? parseInt(value) : 0 value = value ? parseInt(value) : 0
const tx = new EthJSTX({ const tx = new Transaction({
nonce: new BN(res.nonce), nonce: new BN(res.nonce),
gasPrice: '0x1', gasPrice: '0x1',
gasLimit: gasLimit, gasLimit: gasLimit,
...@@ -128,7 +128,7 @@ class TxRunner { ...@@ -128,7 +128,7 @@ class TxRunner {
tx.sign(account.privateKey) tx.sign(account.privateKey)
const coinbases = ['0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', '0x8945a1288dc78a6d8952a92c77aee6730b414778', '0x94d76e24f818426ae84aa404140e8d5f60e10e7e'] const coinbases = ['0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', '0x8945a1288dc78a6d8952a92c77aee6730b414778', '0x94d76e24f818426ae84aa404140e8d5f60e10e7e']
const difficulties = [new BN('69762765929000', 10), new BN('70762765929000', 10), new BN('71762765929000', 10)] const difficulties = [new BN('69762765929000', 10), new BN('70762765929000', 10), new BN('71762765929000', 10)]
const block = new EthJSBlock({ const block = new Block({
header: { header: {
timestamp: timestamp || (new Date().getTime() / 1000 | 0), timestamp: timestamp || (new Date().getTime() / 1000 | 0),
number: self.blockNumber, number: self.blockNumber,
...@@ -166,7 +166,7 @@ class TxRunner { ...@@ -166,7 +166,7 @@ class TxRunner {
this.executionContext.trackTx('0x' + tx.hash().toString('hex'), block) this.executionContext.trackTx('0x' + tx.hash().toString('hex'), block)
callback(null, { callback(null, {
result: result, result: result,
transactionHash: ethJSUtil.bufferToHex(Buffer.from(tx.hash())) transactionHash: bufferToHex(Buffer.from(tx.hash()))
}) })
}).catch(function (err) { }).catch(function (err) {
callback(err) callback(err)
......
'use strict' 'use strict'
const ethJSUtil = require('ethereumjs-util') import { BN, bufferToHex } from 'ethereumjs-util'
const BN = ethJSUtil.BN
module.exports = { export function toInt (h) {
toInt: (h) => { if (h.indexOf && h.indexOf('0x') === 0) {
if (h.indexOf && h.indexOf('0x') === 0) { return (new BN(h.replace('0x', ''), 16)).toString(10)
return (new BN(h.replace('0x', ''), 16)).toString(10) } else if (h.constructor && h.constructor.name === 'BigNumber' || BN.isBN(h)) {
} else if (h.constructor && h.constructor.name === 'BigNumber' || BN.isBN(h)) { return h.toString(10)
return h.toString(10) }
} return h
return h
},
stringify: stringify
} }
export var stringify = convertToString
function stringify (v) { function convertToString (v) {
try { try {
if (v instanceof Array) { if (v instanceof Array) {
const ret = [] const ret = []
for (var k in v) { for (var k in v) {
ret.push(stringify(v[k])) ret.push(convertToString(v[k]))
} }
return ret return ret
} else if (BN.isBN(v) || (v.constructor && v.constructor.name === 'BigNumber')) { } else if (BN.isBN(v) || (v.constructor && v.constructor.name === 'BigNumber')) {
return v.toString(10) return v.toString(10)
} else if (v._isBuffer) { } else if (v._isBuffer) {
return ethJSUtil.bufferToHex(v) return bufferToHex(v)
} else if (typeof v === 'object') { } else if (typeof v === 'object') {
const retObject = {} const retObject = {}
for (let i in v) { for (let i in v) {
retObject[i] = stringify(v[i]) retObject[i] = convertToString(v[i])
} }
return retObject return retObject
} else { } else {
......
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