From 11b7e038bb3086dc5dbbc7261209395f31ea2bc1 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 27 Oct 2022 20:05:18 +0200 Subject: [PATCH] display gas cost at function scope --- .../src/solidity-decoder/internalCallTree.ts | 15 +++++++++++---- .../src/source/sourceLocationTracker.ts | 4 ---- .../src/lib/vm-debugger/vm-debugger-head.tsx | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libs/remix-debug/src/solidity-decoder/internalCallTree.ts b/libs/remix-debug/src/solidity-decoder/internalCallTree.ts index 2d7e6b7329..ca5cadae0e 100644 --- a/libs/remix-debug/src/solidity-decoder/internalCallTree.ts +++ b/libs/remix-debug/src/solidity-decoder/internalCallTree.ts @@ -10,7 +10,7 @@ import { Uint } from './types/Uint' export type StepDetail = { depth: number, - gas: number, + gas: number | string, gasCost: number, memory: number[], op: string, @@ -138,6 +138,7 @@ export class InternalCallTree { const scope = this.findScope(vmtraceIndex) if (!scope) return [] let scopeId = this.scopeStarts[scope.firstStep] + const scopeDetail = this.scopes[scopeId] const functions = [] if (!scopeId) return functions let i = 0 @@ -147,7 +148,7 @@ export class InternalCallTree { if (i > 1000) throw new Error('retrieFunctionStack: recursion too deep') const functionDefinition = this.functionDefinitionsByScope[scopeId] if (functionDefinition !== undefined) { - functions.push(functionDefinition) + functions.push({ ...functionDefinition, ...scopeDetail }) } const parent = this.parentScope(scopeId) if (!parent) break @@ -185,7 +186,7 @@ export class InternalCallTree { async function buildTree (tree, step, scopeId, isExternalCall, isCreation) { let subScope = 1 tree.scopeStarts[step] = scopeId - tree.scopes[scopeId] = { firstStep: step, locals: {}, isCreation } + tree.scopes[scopeId] = { firstStep: step, locals: {}, isCreation, gasCost: 0 } function callDepthChange (step, trace) { if (step + 1 < trace.length) { @@ -222,8 +223,14 @@ async function buildTree (tree, step, scopeId, isExternalCall, isCreation) { return { outStep: step, error: 'InternalCallTree - No source Location. ' + step } } const stepDetail: StepDetail = tree.traceManager.trace[step] + const nextStepDetail: StepDetail = tree.traceManager.trace[step + 1] + if (stepDetail && nextStepDetail) { + stepDetail.gasCost = parseInt(stepDetail.gas as string) - parseInt(nextStepDetail.gas as string) + } tree.locationAndOpcodePerVMTraceIndex[step] = { sourceLocation, stepDetail } - console.log('locationAndOpcodePerVMTraceIndex', stepDetail) + tree.scopes[scopeId].gasCost += stepDetail.gasCost + console.log(step, stepDetail.op, stepDetail.gas, nextStepDetail.gas) + const isCallInstrn = isCallInstruction(stepDetail) const isCreateInstrn = isCreateInstruction(stepDetail) // we are checking if we are jumping in a new CALL or in an internal function diff --git a/libs/remix-debug/src/source/sourceLocationTracker.ts b/libs/remix-debug/src/source/sourceLocationTracker.ts index 92abb2ea00..94264c5801 100644 --- a/libs/remix-debug/src/source/sourceLocationTracker.ts +++ b/libs/remix-debug/src/source/sourceLocationTracker.ts @@ -103,10 +103,6 @@ export class SourceLocationTracker { this happens when generated sources should not be considered. */ const originStep = cache[vmtraceStepIndex] - const nextStep = cache[vmtraceStepIndex + 1] - if (nextStep && originStep) { - originStep.stepDetail.gasCost = originStep.stepDetail.gas - nextStep.stepDetail.gas - } while (vmtraceStepIndex >= 0 && (map.file === -1 || map.file > amountOfSources - 1)) { map = cache[vmtraceStepIndex].sourceLocation vmtraceStepIndex = vmtraceStepIndex - 1 diff --git a/libs/remix-ui/debugger-ui/src/lib/vm-debugger/vm-debugger-head.tsx b/libs/remix-ui/debugger-ui/src/lib/vm-debugger/vm-debugger-head.tsx index 4402e7a57c..0ffc505833 100644 --- a/libs/remix-ui/debugger-ui/src/lib/vm-debugger/vm-debugger-head.tsx +++ b/libs/remix-ui/debugger-ui/src/lib/vm-debugger/vm-debugger-head.tsx @@ -31,7 +31,7 @@ export const VmDebuggerHead = ({ vmDebugger: { registerEvent, triggerEvent } }) const functions = [] for (const func of stack) { - functions.push(func.functionDefinition.name + '(' + func.inputs.join(', ') + ')') + functions.push(func.functionDefinition.name + '(' + func.inputs.join(', ') + ')' + ' - ' + func.gasCost + ' gas') } setFunctionPanel(() => functions) })