calculate cost of SSTORE & store empty storage if creation

pull/7/head
yann300 8 years ago
parent d3f25cf53d
commit 035150e4a4
  1. 42
      src/web3Provider/web3VmProvider.js

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

Loading…
Cancel
Save