-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
161 lines (127 loc) · 4.53 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* eslint-disable no-console */
import fs from 'fs';
import algosdk from 'algosdk';
import response from './response.json';
type FullTrace = {
teal: string
pc: number
scratchDelta?: {[slot: number]: string | number}
stack: (string | number)[]
}[]
type PytealMapping = {
[pc: number]: {
pyteal: string
line: number
file: string
}
}
async function getFullTrace(
simTrace: any[],
teal: string,
algod: algosdk.Algodv2,
): Promise<FullTrace> {
const result = await algod.compile(teal).sourcemap(true).do();
const srcMap = new algosdk.SourceMap(result.sourcemap);
let stack: (string | number)[] = [];
const fullTrace: FullTrace = [];
simTrace.forEach((t) => {
let newStack: (string | number)[] = [...stack];
if (t['stack-pop-count']) {
newStack = newStack.slice(0, -t['stack-pop-count']);
}
t['stack-additions']?.forEach((s: any) => {
if (s.bytes) {
newStack.push(`0x${Buffer.from(s.bytes, 'base64').toString('hex')}`);
} else newStack.push(s.uint || 0);
});
const scratchDelta = t['scratch-changes']?.map((s) => {
const delta = {};
const value = s['new-value'];
if (s.bytes) {
delta[s.slot] = `0x${Buffer.from(value.bytes, 'base64').toString('hex')}`;
} else delta[s.slot] = value.uint || 0;
return delta;
});
fullTrace.push({
teal: teal.split('\n')[srcMap.pcToLine[t.pc as number]],
pc: t.pc,
stack: newStack,
scratchDelta,
});
stack = newStack;
});
return fullTrace;
}
function adjustWidth(line: string, width: number) {
if (line.length > width) {
return `${line.slice(0, width - 3)}...`;
} if (line.length < width) {
return line.padEnd(width);
} return line;
}
function printFullTrace(fullTrace: FullTrace, width: number = 30) {
console.log(`${'TEAL'.padEnd(width)} | PC | STACK`);
console.log(`${'-'.repeat(width)}-|------|${'-'.repeat(7)}`);
fullTrace.forEach((t) => {
const teal = adjustWidth(t.teal.trim(), width);
const pc = t.pc.toString().padEnd(4);
console.log(`${teal} | ${pc} | [${t.stack}]`);
});
}
function printFullPyTealTrace(
fullTrace: FullTrace,
pytealMapping: PytealMapping,
tealWidth: number = 15,
pytealWidth: number = 40,
) {
console.log(`${'TEAL'.padEnd(tealWidth)} | PC | ${'FILE'.padEnd(10)} | LINE | ${'PyTeal'.padEnd(pytealWidth)} | STACK`);
console.log(`${'-'.repeat(tealWidth)}-|------|-${'-'.repeat(10)}-|------|-${'-'.repeat(pytealWidth)}-|${'-'.repeat(7)}`);
fullTrace.forEach((t) => {
const teal = adjustWidth(t.teal.trim(), tealWidth);
const pyteal = adjustWidth(pytealMapping[t.pc]?.pyteal || '', pytealWidth);
const pc = t.pc.toString().padEnd(4);
const file = adjustWidth(pytealMapping[t.pc]?.file || '', 10);
const line = (pytealMapping[t.pc]?.line || '').toString().padEnd(4);
console.log(`${teal} | ${pc} | ${file} | ${line} | ${pyteal} | [${t.stack}]`);
});
}
function getPcToPyteal(annotatedTeal: string) {
const annotatedTealLines = annotatedTeal.toString().split('\n');
const annotationStart = annotatedTealLines[0].indexOf('// PC');
const pytealPathStart = annotatedTealLines[0].indexOf('PYTEAL PATH');
const pytealLineStart = annotatedTealLines[0].indexOf('LINE');
const pytealStart = annotatedTealLines[0].match(/PYTEAL$/)!.index;
const mapping: PytealMapping = {};
let lastFile = '';
annotatedTealLines.slice(1).forEach((line) => {
const annotation = line.slice(annotationStart);
if (annotation.slice(2) === '') return;
const pcStr = line.slice(annotationStart + 2, pytealPathStart).trim();
if (pcStr === '') return;
const pc = Number(pcStr.slice(1, -1));
let file = line.slice(pytealPathStart, pytealLineStart).trim();
if (file === '') {
file = lastFile;
} else {
lastFile = file;
}
const lineNumber = Number(line.slice(pytealLineStart, pytealStart).trim());
const pyteal = line.slice(pytealStart).trim();
mapping[pc] = {
pyteal,
file,
line: lineNumber,
};
});
return mapping;
}
async function main() {
const trace = response['txn-groups'][0]['txn-results'][0]['exec-trace']['approval-program-trace'];
const teal = fs.readFileSync('approval.teal', 'utf8');
const algodClient = new algosdk.Algodv2('a'.repeat(64), 'http://localhost', 4001);
const fullTrace = await getFullTrace(trace, teal, algodClient);
printFullTrace(fullTrace);
const pytealMapping = getPcToPyteal(fs.readFileSync('./approval.teal').toString());
printFullPyTealTrace(fullTrace, pytealMapping);
}
main();