Skip to content

Commit f6f8347

Browse files
committed
fix(sampledEvaluateReductionTraceNodes): allow empty scripts to be debugged properly
1 parent dda926a commit f6f8347

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// tslint:disable:no-expression-statement no-magic-numbers
2+
import test from 'ava';
3+
4+
import {
5+
AuthenticationInstruction,
6+
createAuthenticationProgramExternalStateCommonEmpty,
7+
createAuthenticationProgramStateCommon
8+
} from '../../auth';
9+
import {
10+
AuthenticationProgramStateBCH,
11+
instantiateVirtualMachineBCH,
12+
OpcodesBCH
13+
} from '../../instruction-sets/bch/bch';
14+
15+
import { sampledEvaluateReductionTraceNodes } from './reduce';
16+
17+
const vmPromise = instantiateVirtualMachineBCH();
18+
const createCreateStateWithStack = <Opcodes, Errors>(stack: Uint8Array[]) => (
19+
instructions: ReadonlyArray<AuthenticationInstruction<Opcodes>>
20+
) =>
21+
createAuthenticationProgramStateCommon<Opcodes, Errors>(
22+
instructions,
23+
stack,
24+
createAuthenticationProgramExternalStateCommonEmpty()
25+
);
26+
const state = createCreateStateWithStack<
27+
OpcodesBCH,
28+
AuthenticationProgramStateBCH
29+
>([])([]) as AuthenticationProgramStateBCH;
30+
const getState = () => state;
31+
32+
test('sampledEvaluateReductionTraceNodes: empty evaluation', async t => {
33+
const vm = await vmPromise;
34+
t.deepEqual(
35+
sampledEvaluateReductionTraceNodes<
36+
OpcodesBCH,
37+
AuthenticationProgramStateBCH
38+
>(
39+
[
40+
{
41+
bytecode: Uint8Array.of(),
42+
range: {
43+
endColumn: 1,
44+
endLineNumber: 1,
45+
startColumn: 1,
46+
startLineNumber: 1
47+
}
48+
}
49+
],
50+
vm,
51+
getState
52+
),
53+
{
54+
bytecode: Uint8Array.of(),
55+
samples: [
56+
{
57+
range: {
58+
endColumn: 1,
59+
endLineNumber: 1,
60+
startColumn: 1,
61+
startLineNumber: 1
62+
},
63+
state
64+
}
65+
],
66+
success: true
67+
}
68+
);
69+
});

src/lib/auth/templates/language/reduce.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,20 @@ export const sampledEvaluateReductionTraceNodes = <
271271
getState
272272
);
273273
// tslint:disable-next-line: no-if-statement
274-
if (parsed.success && evaluated.success && evaluated.samples.length > 0) {
275-
const lastSample = evaluated.samples[evaluated.samples.length - 1];
274+
if (parsed.success && evaluated.success) {
275+
const samples =
276+
evaluated.samples.length > 0
277+
? evaluated.samples
278+
: [{ range: parsed.aggregations[0].range, state: getState([]) }];
279+
const lastSample = samples[samples.length - 1];
276280
const lastStackItem = lastSample.state.stack[
277281
lastSample.state.stack.length - 1
278282
] as Uint8Array | undefined;
279283
const evaluationResult =
280284
lastStackItem !== undefined ? lastStackItem.slice() : Uint8Array.of();
281285
return {
282286
bytecode: evaluationResult,
283-
samples: evaluated.samples,
287+
samples,
284288
success: true
285289
};
286290
}

0 commit comments

Comments
 (0)