Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/circuit_gas_meter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function trackScriptExecutionGas(opCount: number, gasLimit = 100000): boolean {
if (opCount > gasLimit) throw new Error('Gas limit exceeded');
return true;
}
4 changes: 4 additions & 0 deletions lib/gas_meter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function calculateCircuitEvalGas(opCount: number, memoryBytes: number): number {
const baseGas = 100;
return baseGas + opCount * 2 + Math.floor(memoryBytes / 1024);
}
11 changes: 11 additions & 0 deletions lib/regexExecutionGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* tscircuit/eval - Safe Regex Execution Guard
*/
export function safeRegexTest(pattern: RegExp, input: string, timeoutMs: number = 50): boolean {
const start = Date.now();
const res = pattern.test(input);
if (Date.now() - start > timeoutMs) {
throw new Error('Regex execution exceeded execution time budget');
}
return res;
}
5 changes: 5 additions & 0 deletions lib/stack_guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function checkCallStackDepth(currentDepth: number, maxDepth: number = 250): void {
if (currentDepth > maxDepth) {
throw new Error(`Maximum schematic expansion depth of ${maxDepth} exceeded`);
}
}
7 changes: 7 additions & 0 deletions lib/timeout_guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function enforceEvalTimeout<T>(promise: Promise<T>, timeoutMs: number = 5000): Promise<T> {
let timer: NodeJS.Timeout;
const timeoutPromise = new Promise<T>((_, reject) => {
timer = setTimeout(() => reject(new Error('Circuit eval timeout exceeded')), timeoutMs);
});
return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timer));
}
Loading