diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-15 04:58:28 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-15 04:58:54 +0800 |
commit | 7ab921669bf52c1cb2d43350b2cccc8efe91bdbd (patch) | |
tree | 3ed0e1047f6b9bb04ce7084cb0b98bdb5cb5b78a /packages/sol-cov/src/trace.ts | |
parent | 4efd28c092e74b438d0397069c0c55cc90c537f2 (diff) | |
download | dexon-0x-contracts-7ab921669bf52c1cb2d43350b2cccc8efe91bdbd.tar.gz dexon-0x-contracts-7ab921669bf52c1cb2d43350b2cccc8efe91bdbd.tar.zst dexon-0x-contracts-7ab921669bf52c1cb2d43350b2cccc8efe91bdbd.zip |
Introduce subprovider for printing revert stack traces
Diffstat (limited to 'packages/sol-cov/src/trace.ts')
-rw-r--r-- | packages/sol-cov/src/trace.ts | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/packages/sol-cov/src/trace.ts b/packages/sol-cov/src/trace.ts index 45e45e9c5..b43fd19a2 100644 --- a/packages/sol-cov/src/trace.ts +++ b/packages/sol-cov/src/trace.ts @@ -1,17 +1,13 @@ -import { addressUtils, BigNumber, logUtils } from '@0xproject/utils'; +import { logUtils } from '@0xproject/utils'; import { OpCode, StructLog } from 'ethereum-types'; -import { addHexPrefix } from 'ethereumjs-util'; import * as _ from 'lodash'; +import { utils } from './utils'; + export interface TraceByContractAddress { [contractAddress: string]: StructLog[]; } -function getAddressFromStackEntry(stackEntry: string): string { - const hexBase = 16; - return addressUtils.padZeros(new BigNumber(addHexPrefix(stackEntry)).toString(hexBase)); -} - export function getTracesByContractAddress(structLogs: StructLog[], startAddress: string): TraceByContractAddress { const traceByContractAddress: TraceByContractAddress = {}; let currentTraceSegment = []; @@ -19,13 +15,10 @@ export function getTracesByContractAddress(structLogs: StructLog[], startAddress if (_.isEmpty(structLogs)) { return traceByContractAddress; } - if (structLogs[0].depth === 1) { - // Geth uses 1-indexed depth counter whilst ganache starts from 0 - _.forEach(structLogs, structLog => structLog.depth--); - } + const normalizedStructLogs = utils.normalizeStructLogs(structLogs); // tslint:disable-next-line:prefer-for-of - for (let i = 0; i < structLogs.length; i++) { - const structLog = structLogs[i]; + for (let i = 0; i < normalizedStructLogs.length; i++) { + const structLog = normalizedStructLogs[i]; if (structLog.depth !== callStack.length - 1) { throw new Error("Malformed trace. Trace depth doesn't match call stack depth"); } @@ -34,27 +27,19 @@ export function getTracesByContractAddress(structLogs: StructLog[], startAddress // That means that we can always safely pop from it currentTraceSegment.push(structLog); - const isCallLike = _.includes( - [OpCode.CallCode, OpCode.StaticCall, OpCode.Call, OpCode.DelegateCall], - structLog.op, - ); - const isEndOpcode = _.includes( - [OpCode.Return, OpCode.Stop, OpCode.Revert, OpCode.Invalid, OpCode.SelfDestruct], - structLog.op, - ); - if (isCallLike) { + if (utils.isCallLike(structLog.op)) { const currentAddress = _.last(callStack) as string; const jumpAddressOffset = 1; - const newAddress = getAddressFromStackEntry( + const newAddress = utils.getAddressFromStackEntry( structLog.stack[structLog.stack.length - jumpAddressOffset - 1], ); - if (structLog === _.last(structLogs)) { + if (structLog === _.last(normalizedStructLogs)) { throw new Error('Malformed trace. CALL-like opcode can not be the last one'); } // Sometimes calls don't change the execution context (current address). When we do a transfer to an // externally owned account - it does the call and immediately returns because there is no fallback // function. We manually check if the call depth had changed to handle that case. - const nextStructLog = structLogs[i + 1]; + const nextStructLog = normalizedStructLogs[i + 1]; if (nextStructLog.depth !== structLog.depth) { callStack.push(newAddress); traceByContractAddress[currentAddress] = (traceByContractAddress[currentAddress] || []).concat( @@ -62,7 +47,7 @@ export function getTracesByContractAddress(structLogs: StructLog[], startAddress ); currentTraceSegment = []; } - } else if (isEndOpcode) { + } else if (utils.isEndOpcode(structLog.op)) { const currentAddress = callStack.pop() as string; traceByContractAddress[currentAddress] = (traceByContractAddress[currentAddress] || []).concat( currentTraceSegment, @@ -85,8 +70,8 @@ export function getTracesByContractAddress(structLogs: StructLog[], startAddress ); return traceByContractAddress; } else { - if (structLog !== _.last(structLogs)) { - const nextStructLog = structLogs[i + 1]; + if (structLog !== _.last(normalizedStructLogs)) { + const nextStructLog = normalizedStructLogs[i + 1]; if (nextStructLog.depth === structLog.depth) { continue; } else if (nextStructLog.depth === structLog.depth - 1) { |