implement/fix getTransactionReceipt, eth_getCode, eth_getTransactionByHash

pull/5370/head
Iuri Matias 6 years ago
parent f9d50e4d6e
commit b94b7b3e3a
  1. 19
      remix-lib/src/execution/execution-context.js
  2. 112
      remix-lib/src/execution/txRunner.js
  3. 2
      remix-lib/src/web3Provider/web3VmProvider.js
  4. 8
      remix-simulator/src/methods/blocks.js
  5. 76
      remix-simulator/src/methods/transactions.js

@ -67,6 +67,7 @@ function createVm (hardfork) {
stateManager: stateManager,
hardfork: hardfork
})
vm.blockchain.validate = false
var web3vm = new Web3VMProvider()
web3vm.setVM(vm)
return { vm, web3vm, stateManager }
@ -91,6 +92,8 @@ function ExecutionContext () {
this.blockGasLimitDefault = 4300000
this.blockGasLimit = this.blockGasLimitDefault
this.customNetWorks = {}
this.blocks = {}
this.txs = {}
this.init = function (config) {
if (config.get('settings/always-use-vm')) {
@ -115,6 +118,8 @@ function ExecutionContext () {
}
this.web3 = function () {
console.dir("isVM")
console.dir(this.isVM())
return this.isVM() ? vms.constantinople.web3vm : web3
}
@ -183,9 +188,9 @@ function ExecutionContext () {
if (context === 'vm') {
executionContext = context
vms.constantinople.stateManager.revert(() => {
vms.constantinople.stateManager.checkpoint(() => {})
})
// vms.constantinople.stateManager.revert(() => {
// vms.constantinople.stateManager.checkpoint(() => {})
// })
self.event.trigger('contextChanged', ['vm'])
return cb()
}
@ -274,6 +279,14 @@ function ExecutionContext () {
return transactionDetailsLinks[network] + hash
}
}
this.addBlock = function (block) {
self.blocks["0x" + block.hash().toString('hex')] = block
}
this.trackTx = function(tx, block) {
self.txs[tx] = block
}
}
var transactionDetailsLinks = {

@ -13,12 +13,14 @@ class TxRunner {
this.blockNumber = 0
this.runAsync = true
if (executionContext.isVM()) {
this.blockNumber = 1150000 // The VM is running in Homestead mode, which started at this block.
//this.blockNumber = 1150000 // The VM is running in Homestead mode, which started at this block.
this.blockNumber = 1 // The VM is running in Homestead mode, which started at this block.
this.runAsync = false // We have to run like this cause the VM Event Manager does not support running multiple txs at the same time.
}
this.pendingTxs = {}
this.vmaccounts = vmaccounts
this.queusTxs = []
this.blocks = []
}
rawRun (args, confirmationCb, gasEstimationForceSend, promptCb, cb) {
@ -115,11 +117,16 @@ class TxRunner {
header: {
timestamp: timestamp || (new Date().getTime() / 1000 | 0),
number: self.blockNumber,
coinbase: coinbases[self.blockNumber % coinbases.length],
difficulty: difficulties[self.blockNumber % difficulties.length],
gasLimit: new BN(gasLimit, 10).imuln(2)
// coinbase: coinbases[self.blockNumber % coinbases.length],
// difficulty: difficulties[self.blockNumber % difficulties.length],
// coinbase: coinbases[0],
//difficulty: difficulties[0],
coinbase: coinbases[0],
// gasLimit: new BN(gasLimit, 10).imuln(200),
gasLimit: new BN("5000000").imuln(1)
},
transactions: [],
transactions: [tx],
//transactions: [],
uncleHeaders: []
})
if (!useCall) {
@ -128,17 +135,90 @@ class TxRunner {
executionContext.vm().stateManager.checkpoint(() => {})
}
executionContext.vm().runTx({block: block, tx: tx, skipBalance: true, skipNonce: true}, function (err, result) {
if (useCall) {
executionContext.vm().stateManager.revert(function () {})
}
err = err ? err.message : err
if (result) {
result.status = '0x' + result.vm.exception.toString(16)
}
callback(err, {
result: result,
transactionHash: ethJSUtil.bufferToHex(Buffer.from(tx.hash()))
//block.transactions.push(tx);
this.checkpointAndCommit(() => {
//executionContext.vm().blockchain.getLatestBlock((a, b) => {
// console.dir("b.hash()")
// console.dir(b.hash())
// console.dir(b.hash().length)
// console.dir(b.hash().toString('hex'))
// console.dir(b.hash().toString('hex').length)
// block.header.parentHash = b.hash()
// block.header.parentHash = b.hash().toString('hex')
//block.header.parentHash = Buffer.from(b.hash(), 'hex')
//block.header.parentHash = "4599f6765f1d5a50Bf1E3DBFa14A72dF"
// block.header.parentHash = b.hash()
// block.header.difficulty = block.header.canonicalDifficulty(b)
//executionContext.vm().runTx({block: block, tx: tx, skipBalance: true, skipNonce: true}, function (err, result) {
executionContext.vm().runBlock({block: block, generate: true, skipBlockValidation: true, skipBalance: false}, function (err, results) {
console.dir("-- runBlock result")
console.dir(err)
//console.dir(results)
let result = results.results[0]
console.dir(result)
if (useCall) {
executionContext.vm().stateManager.revert(function () {})
}
err = err ? err.message : err
if (result) {
result.status = '0x' + result.vm.exception.toString(16)
}
//executionContext.vm().blockchain.putBlock(block, (err, savedBlock) => {
executionContext.addBlock(block)
executionContext.trackTx("0x" + tx.hash().toString('hex'), block)
// result.blockHash = "0x" + block.hash().toString('hex')
// result.blockNumber = "0x" + block.header.number.toString('hex')
callback(err, {
result: result,
transactionHash: ethJSUtil.bufferToHex(Buffer.from(tx.hash()))
})
//})
})
//})
})
//})
}
checkpointAndCommit (cb) {
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir(executionContext.vm().stateManager._checkpointCount)
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
console.dir("------------------------")
if (executionContext.vm().stateManager._checkpointCount > 0) {
return executionContext.vm().stateManager.commit(() => {
cb()
})
}
executionContext.vm().stateManager.checkpoint(() => {
executionContext.vm().stateManager.commit(() => {
cb()
})
})
}

@ -193,6 +193,8 @@ web3VmProvider.prototype.pushTrace = function (self, data) {
}
web3VmProvider.prototype.getCode = function (address, cb) {
console.dir("===> web3VmProvider: ")
const account = ethutil.toBuffer(address)
this.vm.stateManager.getContractCode(account, function (error, result) {
cb(error, util.hexConvert(result))

@ -1,4 +1,6 @@
var Web3 = require('web3')
var RemixLib = require('remix-lib')
var executionContext = RemixLib.execution.executionContext
var Blocks = function (_options) {
const options = _options || {}
@ -43,6 +45,12 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) {
}
Blocks.prototype.eth_getBlockByHash = function (payload, cb) {
console.dir("eth_getBlockByHash")
console.dir(payload)
console.dir(Object.keys(executionContext.blocks))
console.dir("== toJSON")
console.dir(executionContext.blocks[payload.params[0]].toJSON())
let b = {
'difficulty': '0x0',
'extraData': '0x',

@ -38,18 +38,22 @@ Transactions.prototype.eth_sendTransaction = function (payload, cb) {
}
Transactions.prototype.eth_getTransactionReceipt = function (payload, cb) {
console.dir("== eth_getTransactionReceipt")
console.dir(payload.params)
executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => {
if (error) {
return cb(error)
}
var txBlock = executionContext.txs[receipt.hash];
var r = {
'transactionHash': receipt.hash,
'transactionIndex': '0x00',
'blockHash': '0x766d18646a06cf74faeabf38597314f84a82c3851859d9da9d94fc8d037269e5',
'blockNumber': '0x06',
'gasUsed': '0x06345f',
'cumulativeGasUsed': '0x06345f',
'blockHash': "0x" + txBlock.hash().toString('hex'),
'blockNumber': "0x" + txBlock.header.number.toString('hex'),
'gasUsed': web3.utils.toHex(receipt.gas),
'cumulativeGasUsed': web3.utils.toHex(receipt.gas),
'contractAddress': receipt.contractAddress,
'logs': receipt.logs,
'status': receipt.status
@ -64,13 +68,24 @@ Transactions.prototype.eth_estimateGas = function (payload, cb) {
}
Transactions.prototype.eth_getCode = function (payload, cb) {
console.dir("== eth_getCode")
console.dir(payload.params)
let address = payload.params[0]
const account = ethJSUtil.toBuffer(address)
executionContext.vm().stateManager.getContractCode(account, (error, result) => {
cb(error, hexConvert(result))
})
console.dir(address);
// const account = ethJSUtil.toBuffer(address)
// console.dir(account)
//executionContext.vm().stateManager.getContractCode(account, (error, result) => {
//executionContext.web3().eth.getContractCode(address, (error, result) => {
executionContext.web3().eth.getCode(address, (error, result) => {
if (error) {
console.dir("error getting code");
console.dir(error);
}
//cb(error, hexConvert(result))
cb(error, result)
})
}
Transactions.prototype.eth_call = function (payload, cb) {
@ -90,6 +105,8 @@ Transactions.prototype.eth_getTransactionCount = function (payload, cb) {
}
Transactions.prototype.eth_getTransactionByHash = function (payload, cb) {
console.dir("== eth_getTransactionByHash")
console.dir(payload.params)
const address = payload.params[0]
executionContext.web3().eth.getTransactionReceipt(address, (error, receipt) => {
@ -97,19 +114,38 @@ Transactions.prototype.eth_getTransactionByHash = function (payload, cb) {
return cb(error)
}
console.dir("== receipt")
console.dir(receipt)
var test = executionContext.web3();
var txBlock = executionContext.txs[receipt.transactionHash];
// executionContext.web3().eth.getBlock(receipt.hash).then((block) => {
const r = {
let r = {
'blockHash': "0x" + txBlock.hash().toString('hex'),
'blockNumber': "0x" + txBlock.header.number.toString('hex'),
'from': receipt.from,
'gas': web3.utils.toHex(receipt.gas),
// 'gasPrice': '2000000000000', // 0x123
"gasPrice":"0x4a817c800", // 20000000000
'hash': receipt.transactionHash,
// "nonce": 2,
'blockHash': receipt.hash,
// 'blockNumber': block.number,
'input': receipt.input,
// "nonce": 2, // 0x15
// "transactionIndex": 0,
'from': receipt.from,
'to': receipt.to,
'value': receipt.value,
'gas': receipt.gas,
'gasPrice': '2000000000000',
'input': receipt.input
"value": receipt.value
// "value":"0xf3dbb76162000" // 4290000000000000
// "v": "0x25", // 37
// "r": "0x1b5e176d927f8e9ab405058b2d2457392da3e20f328b16ddabcebc33eaac5fea",
// "s": "0x4ba69724e8f69de52f0125ad8b3c5c2cef33019bac3249e2c0a2192766d1721c"
}
if (receipt.to) {
r.to = receipt.to
}
if (r.value === "0x") {
r.value = "0x0"
}
cb(null, r)

Loading…
Cancel
Save