summaryrefslogtreecommitdiff
path: root/data/transactions/logic/debugger_eval_test.go
blob: d99452f103a51848532772d25cff6d0e77c5a59b (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package logic_test

import (
	"os"
	"strings"
	"testing"

	"github.com/algorand/go-algorand/data/basics"
	. "github.com/algorand/go-algorand/data/transactions/logic"
	"github.com/algorand/go-algorand/data/transactions/logic/mocktracer"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

const debuggerTestProgramApprove string = `intcblock 0 1 1 1 1 5 100
bytecblock 0x414c474f 0x1337 0x2001 0xdeadbeef 0x70077007
bytec 0
sha256
keccak256
sha512_256
len
intc_0
+
intc_1
-
intc_2
/
intc_3
*
intc 4
<
intc_1
>
intc_1
<=
intc_1
>=
intc_1
&&
intc_1
||
bytec_1
bytec_2
!=
bytec_3
bytec 4
!=
&&
&&
`
const debuggerTestProgramReject string = debuggerTestProgramApprove + "!"
const debuggerTestProgramError string = debuggerTestProgramApprove + "err"
const debuggerTestProgramPanic string = debuggerTestProgramApprove + "panic"

func TestWebDebuggerManual(t *testing.T) { //nolint:paralleltest // Manual test
	partitiontest.PartitionTest(t)

	debugURL := os.Getenv("TEAL_DEBUGGER_URL")
	if len(debugURL) == 0 {
		t.Skip("this must be run manually")
	}

	ep, tx, _ := MakeSampleEnv()
	ep.TxnGroup[0].Lsig.Args = [][]byte{
		tx.Sender[:],
		tx.Receiver[:],
		tx.CloseRemainderTo[:],
		tx.VotePK[:],
		tx.SelectionPK[:],
		tx.Note,
	}
	ep.Tracer = MakeEvalTracerDebuggerAdaptor(&WebDebugger{URL: debugURL})
	TestLogic(t, debuggerTestProgramApprove, AssemblerMaxVersion, ep)
}

type testDebugger struct {
	register int
	update   int
	complete int
	state    *DebugState
}

func (d *testDebugger) Register(state *DebugState) {
	d.register++
	d.state = state
}

func (d *testDebugger) Update(state *DebugState) {
	d.update++
	d.state = state
}

func (d *testDebugger) Complete(state *DebugState) {
	d.complete++
	d.state = state
}

var debuggerTestCases = []struct {
	name             string
	program          string
	evalProblems     []string
	expectedRegister int
	expectedUpdate   int
	expectedComplete int
	expectedStack    []basics.TealValue
}{
	{
		name:             "approve",
		program:          debuggerTestProgramApprove,
		expectedRegister: 1,
		expectedUpdate:   35,
		expectedComplete: 1,
		expectedStack: []basics.TealValue{
			{
				Type: basics.TealUintType,
				Uint: 1,
			},
		},
	},
	{
		name:             "reject",
		program:          debuggerTestProgramReject,
		evalProblems:     []string{"REJECT"},
		expectedRegister: 1,
		expectedUpdate:   36,
		expectedComplete: 1,
		expectedStack: []basics.TealValue{
			{
				Type: basics.TealUintType,
				Uint: 0,
			},
		},
	},
	{
		name:             "error",
		program:          debuggerTestProgramError,
		evalProblems:     []string{"err opcode executed"},
		expectedRegister: 1,
		expectedUpdate:   36,
		expectedComplete: 1,
		expectedStack: []basics.TealValue{
			{
				Type: basics.TealUintType,
				Uint: 1,
			},
		},
	},
}

func TestDebuggerLogicSigEval(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	for _, testCase := range debuggerTestCases {
		testCase := testCase
		t.Run(testCase.name, func(t *testing.T) {
			t.Parallel()
			testDbg := testDebugger{}
			ep := DefaultSigParams()
			ep.Tracer = MakeEvalTracerDebuggerAdaptor(&testDbg)
			TestLogic(t, testCase.program, AssemblerMaxVersion, ep, testCase.evalProblems...)

			require.Equal(t, testCase.expectedRegister, testDbg.register)
			require.Equal(t, testCase.expectedComplete, testDbg.complete)
			require.Equal(t, testCase.expectedUpdate, testDbg.update)
			require.Equal(t, testCase.expectedStack, testDbg.state.Stack)
		})
	}
}

func TestDebuggerTopLeveLAppEval(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	for _, testCase := range debuggerTestCases {
		testCase := testCase
		t.Run(testCase.name, func(t *testing.T) {
			t.Parallel()
			testDbg := testDebugger{}
			ep := DefaultAppParams()
			ep.Tracer = MakeEvalTracerDebuggerAdaptor(&testDbg)
			TestApp(t, testCase.program, ep, testCase.evalProblems...)

			require.Equal(t, testCase.expectedRegister, testDbg.register)
			require.Equal(t, testCase.expectedComplete, testDbg.complete)
			require.Equal(t, testCase.expectedUpdate, testDbg.update)
			require.Equal(t, testCase.expectedStack, testDbg.state.Stack)
		})
	}
}

func TestDebuggerInnerAppEval(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	scenarios := mocktracer.GetTestScenarios()
	for scenarioName, makeScenario := range scenarios {
		scenarioName := scenarioName
		makeScenario := makeScenario
		t.Run(scenarioName, func(t *testing.T) {
			t.Parallel()
			testDbg := testDebugger{}
			ep, tx, ledger := MakeSampleEnv()

			// Establish 888 as the app id, and fund it.
			ledger.NewApp(tx.Receiver, 888, basics.AppParams{})
			ledger.NewAccount(basics.AppIndex(888).Address(), 200_000)

			scenario := makeScenario(mocktracer.TestScenarioInfo{
				CallingTxn:   *tx,
				CreatedAppID: basics.AppIndex(888),
			})

			var evalProblems []string
			switch scenario.Outcome {
			case mocktracer.RejectionOutcome:
				evalProblems = []string{"REJECT"}
			case mocktracer.ErrorOutcome:
				if scenario.ExpectedError == "overspend" {
					// the logic test ledger uses this error instead
					evalProblems = []string{"insufficient balance"}
				} else {
					evalProblems = []string{scenario.ExpectedError}
				}
			}

			ep.Tracer = MakeEvalTracerDebuggerAdaptor(&testDbg)
			ops := TestProg(t, scenario.Program, AssemblerNoVersion)
			TestAppBytes(t, ops.Program, ep, evalProblems...)

			require.Equal(t, 1, testDbg.register)
			require.Equal(t, 1, testDbg.complete)

			var expectedUpdateCount int
			expectedStack := []basics.TealValue{}
			switch {
			case scenarioName == "none":
				expectedUpdateCount = 32
				expectedStack = []basics.TealValue{{Type: basics.TealUintType, Uint: 1}}
			case strings.HasPrefix(scenarioName, "before inners"):
				expectedUpdateCount = 4
				expectedStack = []basics.TealValue{{Type: basics.TealUintType}}
			case strings.HasPrefix(scenarioName, "first inner"):
				expectedUpdateCount = 12
			case strings.HasPrefix(scenarioName, "between inners"):
				expectedUpdateCount = 16
				expectedStack = []basics.TealValue{{Type: basics.TealUintType}}
			case scenarioName == "second inner":
				expectedUpdateCount = 29
			case scenarioName == "third inner":
				expectedUpdateCount = 29
			case strings.HasPrefix(scenarioName, "after inners"):
				expectedUpdateCount = 32
				if scenario.Outcome == mocktracer.RejectionOutcome {
					expectedStack = []basics.TealValue{{Type: basics.TealUintType}}
				}
			}

			require.Equal(t, expectedUpdateCount, testDbg.update)
			require.Equal(t, expectedStack, testDbg.state.Stack)
		})
	}
}

func TestCallStackUpdate(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	expectedCallFrames := []CallFrame{
		{
			FrameLine: 2,
			LabelName: "label1",
		},
		{
			FrameLine: 5,
			LabelName: "label2",
		},
	}

	testDbg := testDebugger{}
	ep := DefaultSigParams()
	ep.Tracer = MakeEvalTracerDebuggerAdaptor(&testDbg)
	TestLogic(t, TestCallStackProgram, AssemblerMaxVersion, ep)

	require.Equal(t, 1, testDbg.register)
	require.Equal(t, 1, testDbg.complete)
	require.Greater(t, testDbg.update, 1)
	require.Len(t, testDbg.state.Stack, 1)
	require.Equal(t, testDbg.state.CallStack, expectedCallFrames)
}