Commit 035150e4 authored by yann300's avatar yann300

calculate cost of SSTORE & store empty storage if creation

parent d3f25cf5
var util = require('../helpers/util') var util = require('../helpers/util')
var uiutil = require('../helpers/ui')
var traceHelper = require('../helpers/traceHelper') var traceHelper = require('../helpers/traceHelper')
var Web3 = require('web3') var Web3 = require('web3')
var fees = require('ethereum-common')
var ethutil = require('ethereumjs-util')
function web3VmProvider () { function web3VmProvider () {
var self = this var self = this
...@@ -9,6 +12,8 @@ function web3VmProvider () { ...@@ -9,6 +12,8 @@ function web3VmProvider () {
this.vmTraces = {} this.vmTraces = {}
this.txs = {} this.txs = {}
this.processingHash this.processingHash
this.processingAddress
this.processingIndex
this.incr = 0 this.incr = 0
this.eth = {} this.eth = {}
this.debug = {} this.debug = {}
...@@ -43,7 +48,7 @@ web3VmProvider.prototype.releaseCurrentHash = function () { ...@@ -43,7 +48,7 @@ web3VmProvider.prototype.releaseCurrentHash = function () {
return ret return ret
} }
web3VmProvider.prototype.txWillProcess = function (self, data) { web3VmProvider.prototype.txWillProcess = function (self, data) {
self.incr++ self.incr++
self.processingHash = util.hexConvert(data.hash()) self.processingHash = util.hexConvert(data.hash())
self.vmTraces[self.processingHash] = { self.vmTraces[self.processingHash] = {
...@@ -57,6 +62,7 @@ web3VmProvider.prototype.txWillProcess = function (self, data) { ...@@ -57,6 +62,7 @@ web3VmProvider.prototype.txWillProcess = function (self, data) {
if (data.to && data.to.length) { if (data.to && data.to.length) {
tx.to = util.hexConvert(data.to) tx.to = util.hexConvert(data.to)
} }
this.processingAddress = tx.to
tx.data = util.hexConvert(data.data) tx.data = util.hexConvert(data.data)
tx.input = util.hexConvert(data.input) tx.input = util.hexConvert(data.input)
tx.gas = util.hexConvert(data.gas) tx.gas = util.hexConvert(data.gas)
...@@ -70,6 +76,7 @@ web3VmProvider.prototype.txWillProcess = function (self, data) { ...@@ -70,6 +76,7 @@ web3VmProvider.prototype.txWillProcess = function (self, data) {
self.storageCache[self.processingHash][tx.to] = storage self.storageCache[self.processingHash][tx.to] = storage
}) })
} }
this.processingIndex = 0
} }
web3VmProvider.prototype.txProcessed = function (self, data) { web3VmProvider.prototype.txProcessed = function (self, data) {
...@@ -79,6 +86,8 @@ web3VmProvider.prototype.txProcessed = function (self, data) { ...@@ -79,6 +86,8 @@ web3VmProvider.prototype.txProcessed = function (self, data) {
} else { } else {
self.vmTraces[self.processingHash].return = util.hexConvert(data.vm.return) self.vmTraces[self.processingHash].return = util.hexConvert(data.vm.return)
} }
this.processingIndex = null
this.processingAddress = null
} }
web3VmProvider.prototype.pushTrace = function (self, data) { web3VmProvider.prototype.pushTrace = function (self, data) {
...@@ -95,15 +104,36 @@ web3VmProvider.prototype.pushTrace = function (self, data) { ...@@ -95,15 +104,36 @@ web3VmProvider.prototype.pushTrace = function (self, data) {
gasCost: data.opcode.fee.toString(), gasCost: data.opcode.fee.toString(),
gas: data.gasLeft.toString() gas: data.gasLeft.toString()
} }
if (data.opcode.name === 'SSTORE') {
var currentStorage = this.storageCache[this.processingHash][this.processingAddress]
var key = step.stack[step.stack.length - 1]
var value = step.stack[step.stack.length - 2]
value = util.hexToIntArray(value)
value = ethutil.unpad(value)
if (value.length === 0) {
data.opcode.fee = fees.sstoreResetGas.v
} else if (currentStorage[key] === undefined) {
data.opcode.fee = fees.sstoreSetGas.v
} else {
data.opcode.fee = fees.sstoreResetGas.v
}
step.gasCost = data.opcode.fee
}
self.vmTraces[self.processingHash].structLogs.push(step) self.vmTraces[self.processingHash].structLogs.push(step)
if (traceHelper.newContextStorage(step)) { if (traceHelper.newContextStorage(step)) {
if (!self.storageCache[self.processingHash][address]) { if (step.op === 'CREATE') {
var address = step.stack[step.stack.length - 2] this.processingAddress = traceHelper.contractCreationToken(this.processingIndex)
self.vm.stateManager.dumpStorage(address, function (storage) { this.storageCache[this.processingHash][this.processingAddress] = {}
self.storageCache[self.processingHash][address] = storage } else {
}) this.processingAddress = uiutil.normalizeHex(step.stack[step.stack.length - 2])
if (!self.storageCache[self.processingHash][this.processingAddress]) {
self.vm.stateManager.dumpStorage(this.processingAddress, function (storage) {
self.storageCache[self.processingHash][this.processingAddress] = storage
})
}
} }
} }
this.processingIndex++
} }
web3VmProvider.prototype.getCode = function (address, cb) { web3VmProvider.prototype.getCode = function (address, cb) {
......
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