Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement EIP150 [WIP] #95

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/hooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports.fromWeb3Provider = fromWeb3Provider
```js
var vm = createHookedVm({
enableHomestead: true,
enableHomesteadReprice: true
}, {
fetchAccountBalance: function(addressHex, cb){ ... },
fetchAccountNonce: function(addressHex, cb){ ... },
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ VM.deps = {
* @param {Blockchain} [opts.blockchain] A blockchain object for storing/retrieving blocks
* @param {Boolean} [opts.activatePrecompiles] Create entries in the state tree for the precompiled contracts
* @param {Boolean} [opts.enableHomestead] Force enable Homestead irrelevant to block number
* @param {Boolean} [opts.enableHomesteadReprice] Force enable Homestead Reprice (EIP150) irrevelant to block number
*/
function VM (opts = {}) {
this.stateManager = new StateManager({
Expand Down
19 changes: 19 additions & 0 deletions lib/opFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ module.exports = {

checkCallMemCost(runState, options, localOpts)

gasLimit = calcCallLimit(runState, gasLimit)

stateManager.exists(toAddress, function (err, exists) {
if (err) {
done(err)
Expand Down Expand Up @@ -679,6 +681,12 @@ module.exports = {
cb(err)
return
}

if (runState.enableHomsteadReprice && !toAccount.exists) {
// can't wrap because we are in a callback
runState.gasLeft.isub(new BN(fees.callNewAccountGas.v))
}

var newBalance = new Buffer(new BN(contract.balance).add(new BN(toAccount.balance)).toArray())
async.series([
stateManager.putAccountBalance.bind(stateManager, suicideToAddress, newBalance),
Expand Down Expand Up @@ -877,3 +885,14 @@ function makeCall (runState, callOptions, localOpts, cb) {
}
}
}

function calcCallLimit (runState, gas) {
if (!runState.enableHomesteadReprice) {
return gas
}

// div should round down
const max = runState.gasLimit.sub(runState.gasLimit.div(64))

return BN.min(gas, max)
}
1 change: 1 addition & 0 deletions lib/runCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = function (opts, cb) {
var depth = opts.depth
var suicides = opts.suicides
var enableHomestead = this.opts.enableHomestead === undefined ? block.isHomestead() : this.opts.enableHomestead
var enableHomesteadReprice = this.opts.enableHomesteadReprice === undefined ? block.isHomesteadReprice() : this.opts.enableHomesteadReprice

txValue = new BN(txValue)

Expand Down
1 change: 1 addition & 0 deletions lib/runCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = function (opts, cb) {
code: opts.code,
populateCache: opts.populateCache === undefined ? true : opts.populateCache,
enableHomestead: this.opts.enableHomestead === undefined ? block.isHomestead() : this.opts.enableHomestead // this == vm
enableHomesteadReprice: this.opts.enableHomesteadReprice === undefined ? block.isHomesteadReprice() : this.opts.enableHomesteadReprice // this == vm
}

// temporary - to be factored out
Expand Down
4 changes: 0 additions & 4 deletions lib/runTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ module.exports = function (opts, cb) {
block = new Block()
}

if (this.opts.enableHomestead) {
tx._homestead = true
}

if (new BN(block.header.gasLimit).cmp(new BN(tx.gasLimit)) === -1) {
cb(new Error('tx has a higher gas limit than the block'))
return
Expand Down