diff --git a/lib/utils/gasEstimation.js b/lib/utils/gasEstimation.js index 237de14cb6..e39c808ded 100644 --- a/lib/utils/gasEstimation.js +++ b/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(); @@ -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) { @@ -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)) { @@ -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); }; @@ -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); @@ -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 = []; @@ -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 }; };