Commit e1fa7df4 authored by Iuri Matias's avatar Iuri Matias

implement/fix getTransactionReceipt, eth_getCode, eth_getTransactionByHash

parent 324c50f1
...@@ -67,6 +67,7 @@ function createVm (hardfork) { ...@@ -67,6 +67,7 @@ function createVm (hardfork) {
stateManager: stateManager, stateManager: stateManager,
hardfork: hardfork hardfork: hardfork
}) })
vm.blockchain.validate = false
var web3vm = new Web3VMProvider() var web3vm = new Web3VMProvider()
web3vm.setVM(vm) web3vm.setVM(vm)
return { vm, web3vm, stateManager } return { vm, web3vm, stateManager }
...@@ -91,6 +92,8 @@ function ExecutionContext () { ...@@ -91,6 +92,8 @@ function ExecutionContext () {
this.blockGasLimitDefault = 4300000 this.blockGasLimitDefault = 4300000
this.blockGasLimit = this.blockGasLimitDefault this.blockGasLimit = this.blockGasLimitDefault
this.customNetWorks = {} this.customNetWorks = {}
this.blocks = {}
this.txs = {}
this.init = function (config) { this.init = function (config) {
if (config.get('settings/always-use-vm')) { if (config.get('settings/always-use-vm')) {
...@@ -115,6 +118,8 @@ function ExecutionContext () { ...@@ -115,6 +118,8 @@ function ExecutionContext () {
} }
this.web3 = function () { this.web3 = function () {
console.dir("isVM")
console.dir(this.isVM())
return this.isVM() ? vms.constantinople.web3vm : web3 return this.isVM() ? vms.constantinople.web3vm : web3
} }
...@@ -183,9 +188,9 @@ function ExecutionContext () { ...@@ -183,9 +188,9 @@ function ExecutionContext () {
if (context === 'vm') { if (context === 'vm') {
executionContext = context executionContext = context
vms.constantinople.stateManager.revert(() => { // vms.constantinople.stateManager.revert(() => {
vms.constantinople.stateManager.checkpoint(() => {}) // vms.constantinople.stateManager.checkpoint(() => {})
}) // })
self.event.trigger('contextChanged', ['vm']) self.event.trigger('contextChanged', ['vm'])
return cb() return cb()
} }
...@@ -274,6 +279,14 @@ function ExecutionContext () { ...@@ -274,6 +279,14 @@ function ExecutionContext () {
return transactionDetailsLinks[network] + hash 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 = { var transactionDetailsLinks = {
......
...@@ -13,12 +13,14 @@ class TxRunner { ...@@ -13,12 +13,14 @@ class TxRunner {
this.blockNumber = 0 this.blockNumber = 0
this.runAsync = true this.runAsync = true
if (executionContext.isVM()) { 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.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.pendingTxs = {}
this.vmaccounts = vmaccounts this.vmaccounts = vmaccounts
this.queusTxs = [] this.queusTxs = []
this.blocks = []
} }
rawRun (args, confirmationCb, gasEstimationForceSend, promptCb, cb) { rawRun (args, confirmationCb, gasEstimationForceSend, promptCb, cb) {
...@@ -115,11 +117,16 @@ class TxRunner { ...@@ -115,11 +117,16 @@ class TxRunner {
header: { header: {
timestamp: timestamp || (new Date().getTime() / 1000 | 0), timestamp: timestamp || (new Date().getTime() / 1000 | 0),
number: self.blockNumber, number: self.blockNumber,
coinbase: coinbases[self.blockNumber % coinbases.length], // coinbase: coinbases[self.blockNumber % coinbases.length],
difficulty: difficulties[self.blockNumber % difficulties.length], // difficulty: difficulties[self.blockNumber % difficulties.length],
gasLimit: new BN(gasLimit, 10).imuln(2) // 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: [] uncleHeaders: []
}) })
if (!useCall) { if (!useCall) {
...@@ -128,7 +135,36 @@ class TxRunner { ...@@ -128,7 +135,36 @@ class TxRunner {
executionContext.vm().stateManager.checkpoint(() => {}) executionContext.vm().stateManager.checkpoint(() => {})
} }
executionContext.vm().runTx({block: block, tx: tx, skipBalance: true, skipNonce: true}, function (err, result) { //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) { if (useCall) {
executionContext.vm().stateManager.revert(function () {}) executionContext.vm().stateManager.revert(function () {})
} }
...@@ -136,10 +172,54 @@ class TxRunner { ...@@ -136,10 +172,54 @@ class TxRunner {
if (result) { if (result) {
result.status = '0x' + result.vm.exception.toString(16) 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, { callback(err, {
result: result, result: result,
transactionHash: ethJSUtil.bufferToHex(Buffer.from(tx.hash())) 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) { ...@@ -193,6 +193,8 @@ web3VmProvider.prototype.pushTrace = function (self, data) {
} }
web3VmProvider.prototype.getCode = function (address, cb) { web3VmProvider.prototype.getCode = function (address, cb) {
console.dir("===> web3VmProvider: ")
const account = ethutil.toBuffer(address) const account = ethutil.toBuffer(address)
this.vm.stateManager.getContractCode(account, function (error, result) { this.vm.stateManager.getContractCode(account, function (error, result) {
cb(error, util.hexConvert(result)) cb(error, util.hexConvert(result))
......
var Web3 = require('web3') var Web3 = require('web3')
var RemixLib = require('remix-lib')
var executionContext = RemixLib.execution.executionContext
var Blocks = function (_options) { var Blocks = function (_options) {
const options = _options || {} const options = _options || {}
...@@ -43,6 +45,12 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) { ...@@ -43,6 +45,12 @@ Blocks.prototype.eth_getBlockByNumber = function (payload, cb) {
} }
Blocks.prototype.eth_getBlockByHash = 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 = { let b = {
'difficulty': '0x0', 'difficulty': '0x0',
'extraData': '0x', 'extraData': '0x',
......
...@@ -38,18 +38,22 @@ Transactions.prototype.eth_sendTransaction = function (payload, cb) { ...@@ -38,18 +38,22 @@ Transactions.prototype.eth_sendTransaction = function (payload, cb) {
} }
Transactions.prototype.eth_getTransactionReceipt = 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) => { executionContext.web3().eth.getTransactionReceipt(payload.params[0], (error, receipt) => {
if (error) { if (error) {
return cb(error) return cb(error)
} }
var txBlock = executionContext.txs[receipt.hash];
var r = { var r = {
'transactionHash': receipt.hash, 'transactionHash': receipt.hash,
'transactionIndex': '0x00', 'transactionIndex': '0x00',
'blockHash': '0x766d18646a06cf74faeabf38597314f84a82c3851859d9da9d94fc8d037269e5', 'blockHash': "0x" + txBlock.hash().toString('hex'),
'blockNumber': '0x06', 'blockNumber': "0x" + txBlock.header.number.toString('hex'),
'gasUsed': '0x06345f', 'gasUsed': web3.utils.toHex(receipt.gas),
'cumulativeGasUsed': '0x06345f', 'cumulativeGasUsed': web3.utils.toHex(receipt.gas),
'contractAddress': receipt.contractAddress, 'contractAddress': receipt.contractAddress,
'logs': receipt.logs, 'logs': receipt.logs,
'status': receipt.status 'status': receipt.status
...@@ -64,12 +68,23 @@ Transactions.prototype.eth_estimateGas = function (payload, cb) { ...@@ -64,12 +68,23 @@ Transactions.prototype.eth_estimateGas = function (payload, cb) {
} }
Transactions.prototype.eth_getCode = function (payload, cb) { Transactions.prototype.eth_getCode = function (payload, cb) {
console.dir("== eth_getCode")
console.dir(payload.params)
let address = payload.params[0] let address = payload.params[0]
console.dir(address);
const account = ethJSUtil.toBuffer(address) // const account = ethJSUtil.toBuffer(address)
// console.dir(account)
executionContext.vm().stateManager.getContractCode(account, (error, result) => { //executionContext.vm().stateManager.getContractCode(account, (error, result) => {
cb(error, hexConvert(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)
}) })
} }
...@@ -90,6 +105,8 @@ Transactions.prototype.eth_getTransactionCount = function (payload, cb) { ...@@ -90,6 +105,8 @@ Transactions.prototype.eth_getTransactionCount = function (payload, cb) {
} }
Transactions.prototype.eth_getTransactionByHash = function (payload, cb) { Transactions.prototype.eth_getTransactionByHash = function (payload, cb) {
console.dir("== eth_getTransactionByHash")
console.dir(payload.params)
const address = payload.params[0] const address = payload.params[0]
executionContext.web3().eth.getTransactionReceipt(address, (error, receipt) => { executionContext.web3().eth.getTransactionReceipt(address, (error, receipt) => {
...@@ -97,19 +114,38 @@ Transactions.prototype.eth_getTransactionByHash = function (payload, cb) { ...@@ -97,19 +114,38 @@ Transactions.prototype.eth_getTransactionByHash = function (payload, cb) {
return cb(error) 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) => { // 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, 'hash': receipt.transactionHash,
// "nonce": 2, 'input': receipt.input,
'blockHash': receipt.hash, // "nonce": 2, // 0x15
// 'blockNumber': block.number,
// "transactionIndex": 0, // "transactionIndex": 0,
'from': receipt.from, "value": receipt.value
'to': receipt.to, // "value":"0xf3dbb76162000" // 4290000000000000
'value': receipt.value, // "v": "0x25", // 37
'gas': receipt.gas, // "r": "0x1b5e176d927f8e9ab405058b2d2457392da3e20f328b16ddabcebc33eaac5fea",
'gasPrice': '2000000000000', // "s": "0x4ba69724e8f69de52f0125ad8b3c5c2cef33019bac3249e2c0a2192766d1721c"
'input': receipt.input }
if (receipt.to) {
r.to = receipt.to
}
if (r.value === "0x") {
r.value = "0x0"
} }
cb(null, r) cb(null, r)
......
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