aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-tracing-utils/src/trace.ts
diff options
context:
space:
mode:
authorHsuan Lee <boczeratul@gmail.com>2019-03-06 17:46:50 +0800
committerHsuan Lee <boczeratul@gmail.com>2019-03-06 17:46:50 +0800
commit35703539d0f2b4ddb3b11d0de8c9634af59ab71f (patch)
treeae3731221dbbb3a6fa40060a8d916cfd3f738289 /packages/sol-tracing-utils/src/trace.ts
parent92a1fde5b1ecd81b07cdb5bf0c9c1cd3544799db (diff)
downloaddexon-0x-contracts-35703539d0f2b4ddb3b11d0de8c9634af59ab71f.tar.gz
dexon-0x-contracts-35703539d0f2b4ddb3b11d0de8c9634af59ab71f.tar.zst
dexon-0x-contracts-35703539d0f2b4ddb3b11d0de8c9634af59ab71f.zip
Deploy @dexon-foundation/0x.jsstable
Diffstat (limited to 'packages/sol-tracing-utils/src/trace.ts')
-rw-r--r--packages/sol-tracing-utils/src/trace.ts104
1 files changed, 0 insertions, 104 deletions
diff --git a/packages/sol-tracing-utils/src/trace.ts b/packages/sol-tracing-utils/src/trace.ts
deleted file mode 100644
index 973452b24..000000000
--- a/packages/sol-tracing-utils/src/trace.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import { logUtils } from '@0x/utils';
-import { OpCode, StructLog } from 'ethereum-types';
-import * as _ from 'lodash';
-
-import { utils } from './utils';
-
-export interface ContractAddressToTraces {
- [contractAddress: string]: StructLog[];
-}
-
-/**
- * Converts linear stack trace to `ContractAddressToTraces`.
- * @param structLogs stack trace
- * @param startAddress initial context address
- */
-export function getContractAddressToTraces(structLogs: StructLog[], startAddress: string): ContractAddressToTraces {
- const contractAddressToTraces: ContractAddressToTraces = {};
- let currentTraceSegment = [];
- const addressStack = [startAddress];
- if (_.isEmpty(structLogs)) {
- return contractAddressToTraces;
- }
- const normalizedStructLogs = utils.normalizeStructLogs(structLogs);
- // tslint:disable-next-line:prefer-for-of
- for (let i = 0; i < normalizedStructLogs.length; i++) {
- const structLog = normalizedStructLogs[i];
- if (structLog.depth !== addressStack.length - 1) {
- throw new Error("Malformed trace. Trace depth doesn't match call stack depth");
- }
- // After that check we have a guarantee that call stack is never empty
- // If it would: callStack.length - 1 === structLog.depth === -1
- // That means that we can always safely pop from it
- currentTraceSegment.push(structLog);
-
- if (utils.isCallLike(structLog.op)) {
- const currentAddress = _.last(addressStack) as string;
- const jumpAddressOffset = 1;
- const newAddress = utils.getAddressFromStackEntry(
- structLog.stack[structLog.stack.length - jumpAddressOffset - 1],
- );
-
- // 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 = normalizedStructLogs[i + 1];
- if (nextStructLog.depth !== structLog.depth) {
- addressStack.push(newAddress);
- contractAddressToTraces[currentAddress] = (contractAddressToTraces[currentAddress] || []).concat(
- currentTraceSegment,
- );
- currentTraceSegment = [];
- }
- } else if (utils.isEndOpcode(structLog.op)) {
- const currentAddress = addressStack.pop() as string;
- contractAddressToTraces[currentAddress] = (contractAddressToTraces[currentAddress] || []).concat(
- currentTraceSegment,
- );
- currentTraceSegment = [];
- if (structLog.op === OpCode.SelfDestruct) {
- // After contract execution, we look at all sub-calls to external contracts, and for each one, fetch
- // the bytecode and compute the coverage for the call. If the contract is destroyed with a call
- // to `selfdestruct`, we are unable to fetch it's bytecode and compute coverage.
- // TODO: Refactor this logic to fetch the sub-called contract bytecode before the selfdestruct is called
- // in order to handle this edge-case.
- logUtils.warn(
- "Detected a selfdestruct. We currently do not support that scenario. We'll just skip the trace part for a destructed contract",
- );
- }
- } else if (structLog.op === OpCode.Create) {
- // TODO: Extract the new contract address from the stack and handle that scenario
- logUtils.warn(
- "Detected a contract created from within another contract. We currently do not support that scenario. We'll just skip that trace",
- );
- return contractAddressToTraces;
- } else {
- if (structLog !== _.last(normalizedStructLogs)) {
- const nextStructLog = normalizedStructLogs[i + 1];
- if (nextStructLog.depth === structLog.depth) {
- continue;
- } else if (nextStructLog.depth === structLog.depth - 1) {
- const currentAddress = addressStack.pop() as string;
- contractAddressToTraces[currentAddress] = (contractAddressToTraces[currentAddress] || []).concat(
- currentTraceSegment,
- );
- currentTraceSegment = [];
- } else {
- throw new Error('Malformed trace. Unexpected call depth change');
- }
- }
- }
- }
- if (addressStack.length !== 0) {
- logUtils.warn('Malformed trace. Call stack non empty at the end');
- }
- if (currentTraceSegment.length !== 0) {
- const currentAddress = addressStack.pop() as string;
- contractAddressToTraces[currentAddress] = (contractAddressToTraces[currentAddress] || []).concat(
- currentTraceSegment,
- );
- currentTraceSegment = [];
- logUtils.warn('Malformed trace. Current trace segment non empty at the end');
- }
- return contractAddressToTraces;
-}