Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Reorder things
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Sep 24, 2019
1 parent b2d808d commit 266ecac
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lib/utils/gasEstimation.js
@@ -1,8 +1,12 @@
const utils = require("ethereumjs-util");
const BN = utils.BN;
const { BN } = require("ethereumjs-util");
const bn = (val = 0) => new BN(val);
const STIPEND = bn(2300);
const callOrCallcode = new Set(["CALL", "CALLCODE"]);

const check = (set) => (opname) => set.has(opname);
const isCall = check(new Set(["CALL", "DELEGATECALL", "STATICCALL", "CALLCODE"]));
const isCallOrCallcode = check(new Set(["CALL", "CALLCODE"]));
const isCreate = check(new Set(["CREATE", "CREATE2"]));
const isTerminator = check(new Set(["STOP", "RETURN", "REVERT", "INVALID", "SELFDESTRUCT"]));

module.exports = async(vm, runArgs, callback) => {
const steps = stepTracker();
Expand All @@ -17,7 +21,7 @@ module.exports = async(vm, runArgs, callback) => {
const op = steps.ops[index];
const next = steps.ops[index + 1];
const intermediateCost = op.gasLeft.sub(next.gasLeft);
let callingFee = fee || bn();
const callingFee = fee || bn();
let compositeContext = false;

function addGas(val) {
Expand Down Expand Up @@ -68,7 +72,7 @@ module.exports = async(vm, runArgs, callback) => {
}
range.isub(callingFee);
addGas(range);
if (callOrCallcode.has(op.opcode.name) && !op.stack[op.stack.length - 3].isZero()) {
if (isCallOrCallcode(op.opcode.name) && !op.stack[op.stack.length - 3].isZero()) {
cost.iadd(sixtyFloorths);
const innerCost = next.gasLeft.sub(steps.ops[stop - 1].gasLeft);
if (innerCost.gt(STIPEND)) {
Expand Down Expand Up @@ -124,7 +128,7 @@ module.exports = async(vm, runArgs, callback) => {
}
cursor++;
}
let gas = context.getCost();
const gas = context.getCost();
return gas.cost.add(gas.sixtyFloorths);
};

Expand All @@ -135,7 +139,7 @@ module.exports = async(vm, runArgs, callback) => {
} else if (result.execResult.exceptionError) {
return callback(new Error(`execution error: ${result.execResult.exceptionError.error}`));
} else if (steps.done()) {
let estimate = result.gasUsed;
const estimate = result.gasUsed;
result.gasEstimate = estimate;
} else {
const actualUsed = steps.ops[0].gasLeft.sub(steps.ops[steps.ops.length - 1].gasLeft);
Expand All @@ -145,12 +149,6 @@ module.exports = async(vm, runArgs, callback) => {
callback(vmerr, result);
};

const check = (set) => (opname) => set.has(opname);
const isCall = check(new Set(["CALL", "DELEGATECALL", "STATICCALL", "CALLCODE"]));
const isStipend = check(new Set(["CALL", "CALLCODE"]));
const isCreate = check(new Set(["CREATE", "CREATE2"]));
const isTerminator = check(new Set(["STOP", "RETURN", "REVERT", "INVALID", "SELFDESTRUCT"]));

const stepTracker = () => {
const sysOps = [];
const allOps = [];
Expand Down Expand Up @@ -182,7 +180,6 @@ const stepTracker = () => {
isPrecompile: (index) => preCompile.has(index),
done: () => !allOps.length || sysOps.length < 2 || !isTerminator(allOps[allOps.length - 1].opcode.name),
ops: allOps,
systemOps: sysOps,
isStipend
systemOps: sysOps
};
};

0 comments on commit 266ecac

Please sign in to comment.