Commit 34bc1cd6 authored by aniket-engg's avatar aniket-engg Committed by Aniket

providers, init, util, txRunner

parent 142d6478
......@@ -9,6 +9,7 @@ const Web3VMProvider = require('../web3Provider/web3VmProvider')
const LogsManager = require('./logsManager.js')
declare let ethereum: any;
let web3
if (typeof window !== 'undefined' && typeof window['ethereum'] !== 'undefined') {
var injectedProvider = window['ethereum']
......
'use strict'
import { Transaction } from 'ethereumjs-tx'
import { Block } from 'ethereumjs-block'
import { BN } from 'ethereumjs-util'
import { BN, bufferToHex } from 'ethereumjs-util'
import { ExecutionContext } from './execution-context'
const EventManager = require('../eventManager')
......
'use strict'
const Web3 = require('web3')
import Web3 from 'web3'
module.exports = {
loadWeb3: function (url) {
if (!url) url = 'http://localhost:8545'
export function loadWeb3 (url = 'http://localhost:8545') {
const web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider(url))
web3.setProvider(new Web3.providers.HttpProvider(url))
this.extend(web3)
return web3
},
}
extendWeb3: function (web3) {
export function extendWeb3 (web3) {
this.extend(web3)
},
}
setProvider: function (web3, url) {
export function setProvider (web3, url) {
web3.setProvider(new web3.providers.HttpProvider(url))
},
}
web3DebugNode: function (network) {
export function web3DebugNode (network) {
const web3DebugNodes = {
'Main': 'https://gethmainnet.komputing.org',
'Rinkeby': 'https://remix-rinkeby.ethdevops.io',
'Ropsten': 'https://remix-ropsten.ethdevops.io',
'Goerli': 'https://remix-goerli.ethdevops.io',
'Kovan': 'https://remix-kovan.ethdevops.io'
}
if (web3DebugNodes[network]) {
return this.loadWeb3(web3DebugNodes[network])
}
return null
},
}
extend: function (web3) {
export function extend (web3) {
if (!web3.extend) {
return
}
......@@ -65,12 +70,3 @@ module.exports = {
})
}
}
}
const web3DebugNodes = {
'Main': 'https://gethmainnet.komputing.org',
'Rinkeby': 'https://remix-rinkeby.ethdevops.io',
'Ropsten': 'https://remix-ropsten.ethdevops.io',
'Goerli': 'https://remix-goerli.ethdevops.io',
'Kovan': 'https://remix-kovan.ethdevops.io'
}
......@@ -10,11 +10,10 @@ import { BN, bufferToHex, keccak, setLengthLeft } from 'ethereumjs-util'
- swarm hash extraction
- bytecode comparison
*/
module.exports = {
/*
ints: IntArray
*/
hexConvert: function (ints) {
export function hexConvert (ints) {
let ret = '0x'
for (let i = 0; i < ints.length; i++) {
const h = ints[i]
......@@ -25,12 +24,12 @@ module.exports = {
}
}
return ret
},
}
/**
* Converts a hex string to an array of integers.
*/
hexToIntArray: function (hexString) {
export function hexToIntArray (hexString) {
if (hexString.slice(0, 2) === '0x') {
hexString = hexString.slice(2)
}
......@@ -39,12 +38,12 @@ module.exports = {
integers.push(parseInt(hexString.slice(i, i + 2), 16))
}
return integers
},
}
/*
ints: list of BNs
*/
hexListFromBNs: function (bnList) {
export function hexListFromBNs (bnList) {
const ret = []
for (let k in bnList) {
const v = bnList[k]
......@@ -55,23 +54,23 @@ module.exports = {
}
}
return ret
},
}
/*
/*
ints: list of IntArrays
*/
hexListConvert: function (intsList) {
*/
export function hexListConvert (intsList) {
const ret = []
for (let k in intsList) {
ret.push(this.hexConvert(intsList[k]))
}
return ret
},
}
/*
/*
ints: ints: IntArray
*/
formatMemory: function (mem) {
*/
export function formatMemory (mem) {
const hexMem = this.hexConvert(mem).substr(2)
const ret = []
for (let k = 0; k < hexMem.length; k += 32) {
......@@ -79,14 +78,14 @@ module.exports = {
ret.push(row)
}
return ret
},
}
/*
/*
Binary Search:
Assumes that @arg array is sorted increasingly
return largest i such that array[i] <= target; return -1 if array[0] > target || array is empty
*/
findLowerBound: function (target, array) {
*/
export function findLowerBound (target, array) {
let start = 0
let length = array.length
while (length > 0) {
......@@ -100,25 +99,25 @@ module.exports = {
}
}
return start - 1
},
}
/*
/*
Binary Search:
Assumes that @arg array is sorted increasingly
return largest array[i] such that array[i] <= target; return null if array[0] > target || array is empty
*/
findLowerBoundValue: function (target, array) {
*/
export function findLowerBoundValue (target, array) {
const index = this.findLowerBound(target, array)
return index >= 0 ? array[index] : null
},
}
/*
/*
Binary Search:
Assumes that @arg array is sorted increasingly
return Return i such that |array[i] - target| is smallest among all i and -1 for an empty array.
Returns the smallest i for multiple candidates.
*/
findClosestIndex: function (target, array) {
*/
export function findClosestIndex (target, array): number {
if (array.length === 0) {
return -1
}
......@@ -131,16 +130,19 @@ module.exports = {
const middle = (array[index] + array[index + 1]) / 2
return target <= middle ? index : index + 1
}
},
}
/**
/**
* Find the call from @args rootCall which contains @args index (recursive)
*
* @param {Int} index - index of the vmtrace
* @param {Object} rootCall - call tree, built by the trace analyser
* @return {Object} - return the call which include the @args index
*/
findCall: findCall,
export function findCall (index, rootCall) {
const ret = buildCallPath(index, rootCall)
return ret[ret.length - 1]
}
/**
* Find calls path from @args rootCall which leads to @args index (recursive)
......@@ -149,7 +151,11 @@ module.exports = {
* @param {Object} rootCall - call tree, built by the trace analyser
* @return {Array} - return the calls path to @args index
*/
buildCallPath: buildCallPath,
export function buildCallPath (index, rootCall) {
const ret = []
findCallInternal(index, rootCall, ret)
return ret
}
/**
* sha3 the given @arg value (left pad to 32 bytes)
......@@ -157,63 +163,63 @@ module.exports = {
* @param {String} value - value to sha3
* @return {Object} - return sha3ied value
*/
sha3_256: function (value) {
export function sha3_256 (value) {
if (typeof value === 'string' && value.indexOf('0x') !== 0) {
value = '0x' + value
}
let ret: any = bufferToHex(setLengthLeft(value, 32))
ret = keccak(ret)
return bufferToHex(ret)
},
}
/**
/**
* return a regex which extract the swarmhash from the bytecode.
*
* @return {RegEx}
*/
swarmHashExtraction: function () {
export function swarmHashExtraction () {
return /a165627a7a72305820([0-9a-f]{64})0029$/
},
}
/**
/**
* return a regex which extract the swarmhash from the bytecode, from POC 0.3
*
* @return {RegEx}
*/
swarmHashExtractionPOC31: function () {
export function swarmHashExtractionPOC31 () {
return /a265627a7a72315820([0-9a-f]{64})64736f6c6343([0-9a-f]{6})0032$/
},
}
/**
/**
* return a regex which extract the swarmhash from the bytecode, from POC 0.3
*
* @return {RegEx}
*/
swarmHashExtractionPOC32: function () {
export function swarmHashExtractionPOC32 () {
return /a265627a7a72305820([0-9a-f]{64})64736f6c6343([0-9a-f]{6})0032$/
},
}
/**
/**
* return a regex which extract the cbor encoded metadata : {"ipfs": <IPFS hash>, "solc": <compiler version>} from the bytecode.
* ref https://solidity.readthedocs.io/en/v0.6.6/metadata.html?highlight=ipfs#encoding-of-the-metadata-hash-in-the-bytecode
* @return {RegEx}
*/
cborEncodedValueExtraction: function () {
export function cborEncodedValueExtraction () {
return /64697066735822([0-9a-f]{68})64736f6c6343([0-9a-f]{6})0033$/
},
}
extractcborMetadata: function (value) {
export function extractcborMetadata (value) {
return value.replace(this.cborEncodedValueExtraction(), '')
},
}
extractSwarmHash: function (value) {
export function extractSwarmHash (value) {
value = value.replace(this.swarmHashExtraction(), '')
value = value.replace(this.swarmHashExtractionPOC31(), '')
value = value.replace(this.swarmHashExtractionPOC32(), '')
return value
},
}
/**
/**
* Compare bytecode. return true if the code is equal (handle swarm hash and library references)
* @param {String} code1 - the bytecode that is actually deployed (contains resolved library reference and a potentially different swarmhash)
* @param {String} code2 - the bytecode generated by the compiler (contains unresolved library reference and a potentially different swarmhash)
......@@ -221,7 +227,7 @@ module.exports = {
*
* @return {bool}
*/
compareByteCode: function (code1, code2) {
export function compareByteCode (code1, code2) {
if (code1 === code2) return true
if (code2 === '0x') return false // abstract contract. see comment
......@@ -245,25 +251,29 @@ module.exports = {
return true
}
return false
},
groupBy: groupBy,
concatWithSeperator: concatWithSeperator,
escapeRegExp: escapeRegExp
}
/* util extracted out from remix-ide. @TODO split this file, cause it mix real util fn with solidity related stuff ... */
export function groupBy (arr, key) {
return arr.reduce((sum, item) => {
const groupByVal = item[key]
const groupedItems = sum[groupByVal] || []
groupedItems.push(item)
sum[groupByVal] = groupedItems
return sum
}, {})
}
function replaceLibReference (code, pos) {
return code.substring(0, pos) + '0000000000000000000000000000000000000000' + code.substring(pos + 40)
export function concatWithSeperator (list, seperator) {
return list.reduce((sum, item) => sum + item + seperator, '').slice(0, -seperator.length)
}
function buildCallPath (index, rootCall) {
const ret = []
findCallInternal(index, rootCall, ret)
return ret
export function escapeRegExp (str) {
return str.replace(/[-[\]/{}()+?.\\^$|]/g, '\\$&')
}
function findCall (index, rootCall) {
const ret = buildCallPath(index, rootCall)
return ret[ret.length - 1]
function replaceLibReference (code, pos) {
return code.substring(0, pos) + '0000000000000000000000000000000000000000' + code.substring(pos + 40)
}
function findCallInternal (index, rootCall, callsPath) {
......@@ -279,22 +289,3 @@ function findCallInternal (index, rootCall, callsPath) {
}
return ret
}
/* util extracted out from remix-ide. @TODO split this file, cause it mix real util fn with solidity related stuff ... */
function groupBy (arr, key) {
return arr.reduce((sum, item) => {
const groupByVal = item[key]
const groupedItems = sum[groupByVal] || []
groupedItems.push(item)
sum[groupByVal] = groupedItems
return sum
}, {})
}
function concatWithSeperator (list, seperator) {
return list.reduce((sum, item) => sum + item + seperator, '').slice(0, -seperator.length)
}
function escapeRegExp (str) {
return str.replace(/[-[\]/{}()+?.\\^$|]/g, '\\$&')
}
function dummyProvider () {
export class dummyProvider {
eth
debug
providers
currentProvider
constructor() {
this.eth = {}
this.debug = {}
this.eth.getCode = (address, cb) => { return this.getCode(address, cb) }
......@@ -9,42 +15,41 @@ function dummyProvider () {
this.debug.storageRangeAt = (blockNumber, txIndex, address, start, end, maxLength, cb) => { return this.storageRangeAt(blockNumber, txIndex, address, start, end, maxLength, cb) }
this.providers = { 'HttpProvider': function (url) {} }
this.currentProvider = {'host': ''}
}
}
dummyProvider.prototype.getCode = function (address, cb) {
getCode (address, cb) {
cb(null, '')
}
}
dummyProvider.prototype.setProvider = function (provider) {}
setProvider (provider) {}
dummyProvider.prototype.traceTransaction = function (txHash, options, cb) {
traceTransaction (txHash, options, cb) {
if (cb) {
cb(null, {})
}
return {}
}
}
dummyProvider.prototype.storageRangeAt = function (blockNumber, txIndex, address, start, end, maxLength, cb) {
storageRangeAt (blockNumber, txIndex, address, start, end, maxLength, cb) {
if (cb) {
cb(null, {})
}
return {}
}
}
dummyProvider.prototype.getBlockNumber = function (cb) { cb(null, '') }
getBlockNumber (cb) { cb(null, '') }
dummyProvider.prototype.getTransaction = function (txHash, cb) {
getTransaction (txHash, cb) {
if (cb) {
cb(null, {})
}
return {}
}
}
dummyProvider.prototype.getTransactionFromBlock = function (blockNumber, txIndex, cb) {
getTransactionFromBlock (blockNumber, txIndex, cb) {
if (cb) {
cb(null, {})
}
return {}
}
}
module.exports = dummyProvider
const Web3VMProvider = require('./web3VmProvider')
const init = require('../init')
import { Web3VmProvider } from './web3VmProvider'
import { loadWeb3, extendWeb3 } from '../init'
function Web3Providers () {
export class Web3Providers {
modes
constructor() {
this.modes = {}
}
}
Web3Providers.prototype.addProvider = function (type, obj) {
addProvider (type, obj) {
if (type === 'INTERNAL') {
const web3 = init.loadWeb3()
const web3 = loadWeb3()
this.addWeb3(type, web3)
} else if (type === 'vm') {
this.addVM(type, obj)
} else {
init.extendWeb3(obj)
extendWeb3(obj)
this.addWeb3(type, obj)
}
}
}
Web3Providers.prototype.get = function (type, cb) {
get (type, cb) {
if (this.modes[type]) {
return cb(null, this.modes[type])
}
cb('error: this provider has not been setup (' + type + ')', null)
}
}
Web3Providers.prototype.addWeb3 = function (type, web3) {
addWeb3 (type, web3) {
this.modes[type] = web3
}
}
Web3Providers.prototype.addVM = function (type, vm) {
const vmProvider = new Web3VMProvider()
addVM (type, vm) {
const vmProvider = new Web3VmProvider()
vmProvider.setVM(vm)
this.modes[type] = vmProvider
}
}
module.exports = Web3Providers
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