Skip to content

Commit

Permalink
feat(spectests): update to use two values instead of 3
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfourzerofour committed Sep 21, 2023
1 parent 18c5e22 commit a5aa192
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions crates/sim/tracer/src/validationTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ interface StorageAccess {
}

interface RelevantStepData {
opcode: string
stackEnd: BigInt
opcode: string;
stackEnd: BigInt;
}

type InternalPhase = Omit<
Phase,
| "forbiddenOpcodesUsed"
| "forbiddenPrecompilesUsed"
| "storageAccesses"
| "addressesCallingWithValue"
| "undeployedContractAccesses"
| "extCodeAccessInfo"
> & {
forbiddenOpcodesUsed: StringSet;
forbiddenPrecompilesUsed: StringSet;
storageAccesses: Record<string, StringSet>;
addressesCallingWithValue: StringSet;
undeployedContractAccesses: StringSet;
extCodeAccessInfo: Record<string, string>;
};

type StringSet = Record<string, boolean | undefined>;
Expand Down Expand Up @@ -87,13 +86,9 @@ type StringSet = Record<string, boolean | undefined>;
]);
// If you add any opcodes to this list, make sure they take the contract
// address as their *first* argument, or modify the handling below.
const EXT_OPCODES = stringSet([
"EXTCODECOPY",
"EXTCODEHASH",
"EXTCODESIZE",
]);
const EXT_OPCODES = stringSet(["EXTCODECOPY", "EXTCODEHASH", "EXTCODESIZE"]);
// Whitelisted precompile addresses.
const PRECOMPILE_WHITELIST= stringSet([
const PRECOMPILE_WHITELIST = stringSet([
"0x0000000000000000000000000000000000000001", // ecRecover
"0x0000000000000000000000000000000000000002", // SHA2-256
"0x0000000000000000000000000000000000000003", // RIPEMD-160
Expand All @@ -115,7 +110,8 @@ type StringSet = Record<string, boolean | undefined>;
let entryPointAddress = "";
let justCalledGas = false;
let pendingKeccakAddress = "";
let lastThreeOpcodes: RelevantStepData[] = [];
let last: RelevantStepData | null = null;
let secondLast: RelevantStepData | null = null;

function newInternalPhase(): InternalPhase {
return {
Expand Down Expand Up @@ -189,7 +185,7 @@ type StringSet = Record<string, boolean | undefined>;

function getContractCombinedKey(log: LogStep, key: string): string {
return [toHex(log.contract.getAddress()), key].join(":");
}
}

return {
result(_ctx, _db): Output {
Expand Down Expand Up @@ -248,17 +244,6 @@ type StringSet = Record<string, boolean | undefined>;
}

const opcode = log.op.toString();
let stackEnd = {} as BigInt;

if (log.stack.length() > 0) {
stackEnd = log.stack.peek(0);
}

lastThreeOpcodes.push({ opcode, stackEnd })

if (lastThreeOpcodes.length > 3) {
lastThreeOpcodes.shift()
}

const entryPointIsExecuting = log.getDepth() === 1;
if (entryPointIsExecuting) {
Expand All @@ -273,32 +258,42 @@ type StringSet = Record<string, boolean | undefined>;
// The entry point is allowed to freely call `GAS`, but otherwise we
// require that a call opcode comes next.
if (justCalledGas && !CALL_OPCODES[opcode]) {
currentPhase.forbiddenOpcodesUsed[getContractCombinedKey(log, "GAS")] = true;
currentPhase.forbiddenOpcodesUsed[
getContractCombinedKey(log, "GAS")
] = true;
}
justCalledGas = opcode === "GAS";
if (FORBIDDEN_OPCODES[opcode]) {
currentPhase.forbiddenOpcodesUsed[getContractCombinedKey(log, opcode)] = true;
currentPhase.forbiddenOpcodesUsed[
getContractCombinedKey(log, opcode)
] = true;
}
}

const thirdLastOp = lastThreeOpcodes[lastThreeOpcodes.length - 2]
// store all addresses touched by EXTCODE* opcodes
if (thirdLastOp?.opcode?.match(/^(EXT.*)$/) != null) {
const addr = toAddress(thirdLastOp.stackEnd.toString(16))
const addrHex = toHex(addr)
const last3opcodesString = lastThreeOpcodes.map(x => x.opcode).join(' ')
// only store the last EXTCODE* opcode per address - could even be a boolean for our current use-case
if (last3opcodesString.match(/^(\w+) EXTCODESIZE ISZERO$/) == null) {
currentPhase.extCodeAccessInfo[addrHex] = opcode
}
if (secondLast?.opcode.includes("EXT")) {
const opString = `${secondLast.opcode} ${last?.opcode}`;
if (opString !== "EXTCODESIZE ISZERO") {
const addr = toAddress(secondLast.stackEnd.toString(16));
const hexAddr = toHex(addr);
currentPhase.extCodeAccessInfo[hexAddr] = opcode;
}
}

secondLast = last;
const stackEnd =
log.stack.length() > 0
? log.stack.peek(0)
: { value: 0, sign: false, isSmall: false };
last = { opcode, stackEnd };

if (opcode === "CREATE2") {
if (phases.length === 0) {
// In factory phase.
factoryCreate2Count++;
} else {
currentPhase.forbiddenOpcodesUsed[getContractCombinedKey(log, opcode)] = true;
currentPhase.forbiddenOpcodesUsed[
getContractCombinedKey(log, opcode)
] = true;
}
} else if (opcode === "KECCAK256") {
//
Expand Down Expand Up @@ -363,7 +358,9 @@ type StringSet = Record<string, boolean | undefined>;
}
accessedContractAddresses[addressHex] = true;
} else if (!PRECOMPILE_WHITELIST[addressHex]) {
currentPhase.forbiddenPrecompilesUsed[getContractCombinedKey(log, addressHex)] = true;
currentPhase.forbiddenPrecompilesUsed[
getContractCombinedKey(log, addressHex)
] = true;
}
}
},
Expand Down Expand Up @@ -399,4 +396,4 @@ type StringSet = Record<string, boolean | undefined>;

exit(_frame) {},
};
})();
})();

0 comments on commit a5aa192

Please sign in to comment.