summaryrefslogtreecommitdiff
path: root/ledger/simulation/tracer.go
blob: b867cf6aac11c0761cdc46729bfabe872981e87f (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (C) 2019-2024 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 simulation

import (
	"fmt"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/data/transactions/logic"
	"github.com/algorand/go-algorand/ledger/ledgercore"
	"github.com/algorand/go-algorand/protocol"
)

// cursorEvalTracer is responsible for maintaining a TxnPath that points to the currently executing
// transaction. The absolutePath() function is used to get this path.
type cursorEvalTracer struct {
	logic.NullEvalTracer

	relativeCursor    []int
	previousInnerTxns []int
}

func (tracer *cursorEvalTracer) BeforeTxnGroup(ep *logic.EvalParams) {
	tracer.relativeCursor = append(tracer.relativeCursor, -1) // will go to 0 in BeforeTxn
}

func (tracer *cursorEvalTracer) BeforeTxn(ep *logic.EvalParams, groupIndex int) {
	top := len(tracer.relativeCursor) - 1
	tracer.relativeCursor[top]++
	tracer.previousInnerTxns = append(tracer.previousInnerTxns, 0)
}

func (tracer *cursorEvalTracer) AfterTxn(ep *logic.EvalParams, groupIndex int, ad transactions.ApplyData, evalError error) {
	tracer.previousInnerTxns = tracer.previousInnerTxns[:len(tracer.previousInnerTxns)-1]
}

func (tracer *cursorEvalTracer) AfterTxnGroup(ep *logic.EvalParams, deltas *ledgercore.StateDelta, evalError error) {
	top := len(tracer.relativeCursor) - 1
	if len(tracer.previousInnerTxns) != 0 {
		tracer.previousInnerTxns[len(tracer.previousInnerTxns)-1] += tracer.relativeCursor[top] + 1
	}
	tracer.relativeCursor = tracer.relativeCursor[:top]
}

func (tracer *cursorEvalTracer) absolutePath() TxnPath {
	path := make(TxnPath, len(tracer.relativeCursor))
	for i, relativeGroupIndex := range tracer.relativeCursor {
		absoluteIndex := uint64(relativeGroupIndex)
		if i > 0 {
			absoluteIndex += uint64(tracer.previousInnerTxns[i-1])
		}
		path[i] = absoluteIndex
	}
	return path
}

// evalTracer is responsible for populating a Result during a simulation evaluation. It saves
// EvalDelta & inner transaction changes as they happen, so if an error occurs during evaluation, we
// can return a partially-built ApplyData with as much information as possible at the time of the
// error.
type evalTracer struct {
	cursorEvalTracer

	result   *Result
	failedAt TxnPath

	unnamedResourcePolicy *resourcePolicy

	// execTraceStack keeps track of the call stack:
	// from top level transaction to the current inner txn that contains latest TransactionTrace.
	// NOTE: execTraceStack is used only for PC/Stack/Storage exposure.
	execTraceStack []*TransactionTrace

	// addCount and popCount keep track of the latest opcode change explanation from opcode.
	addCount int
	popCount int

	// stackHeightAfterDeletion is calculated by stack height before opcode - stack element deletion number.
	// NOTE: both stackChangeExplanation and stackHeightAfterDeletion are used only for Stack exposure.
	stackHeightAfterDeletion int

	// scratchSlots are the scratch slots changed on current opcode (currently either `store` or `stores`).
	// NOTE: this field scratchSlots is used only for scratch change exposure.
	scratchSlots []uint64
}

func makeEvalTracer(lastRound basics.Round, request Request, developerAPI bool) (*evalTracer, error) {
	result, err := makeSimulationResult(lastRound, request, developerAPI)
	if err != nil {
		return nil, err
	}
	return &evalTracer{result: &result}, nil
}

// handleError is responsible for setting the failedAt field properly.
func (tracer *evalTracer) handleError(evalError error) {
	if evalError != nil && tracer.failedAt == nil {
		tracer.failedAt = tracer.absolutePath()
	}
}

func (tracer *evalTracer) getApplyDataAtPath(path TxnPath) (*transactions.ApplyData, error) {
	if len(path) == 0 {
		return nil, fmt.Errorf("simulator debugger error: path is empty")
	}

	applyDataCursor := &tracer.result.TxnGroups[0].Txns[path[0]].Txn.ApplyData

	for _, index := range path[1:] {
		innerTxns := applyDataCursor.EvalDelta.InnerTxns
		if index >= uint64(len(innerTxns)) {
			return nil, fmt.Errorf("simulator debugger error: index %d out of range with length %d. Full path: %v", index, len(innerTxns), path)
		}
		applyDataCursor = &innerTxns[index].ApplyData
	}

	return applyDataCursor, nil
}

func (tracer *evalTracer) mustGetApplyDataAtPath(path TxnPath) *transactions.ApplyData {
	ad, err := tracer.getApplyDataAtPath(path)
	if err != nil {
		panic(err)
	}
	return ad
}

// Copy the inner transaction group to the ApplyData.EvalDelta.InnerTxns of the calling transaction
func (tracer *evalTracer) populateInnerTransactions(txgroup []transactions.SignedTxnWithAD) {
	applyDataOfCallingTxn := tracer.mustGetApplyDataAtPath(tracer.absolutePath()) // this works because the cursor has not been updated yet by `BeforeTxn`
	applyDataOfCallingTxn.EvalDelta.InnerTxns = append(applyDataOfCallingTxn.EvalDelta.InnerTxns, txgroup...)
}

func (tracer *evalTracer) BeforeTxnGroup(ep *logic.EvalParams) {
	if ep.GetCaller() != nil {
		// If this is an inner txn group, save the txns
		tracer.populateInnerTransactions(ep.TxnGroup)
		tracer.result.TxnGroups[0].AppBudgetAdded += uint64(ep.Proto.MaxAppProgramCost)
	}
	tracer.cursorEvalTracer.BeforeTxnGroup(ep)

	// Currently only supports one (first) txn group
	if ep.PooledApplicationBudget != nil && tracer.result.TxnGroups[0].AppBudgetAdded == 0 {
		tracer.result.TxnGroups[0].AppBudgetAdded = uint64(*ep.PooledApplicationBudget)
	}

	// Override transaction group budget if specified in request, retrieve from tracer.result
	if ep.PooledApplicationBudget != nil {
		tracer.result.TxnGroups[0].AppBudgetAdded += tracer.result.EvalOverrides.ExtraOpcodeBudget
		*ep.PooledApplicationBudget += int(tracer.result.EvalOverrides.ExtraOpcodeBudget)
	}

	if ep.GetCaller() == nil {
		// Override runtime related constraints against ep, before entering txn group
		ep.EvalConstants = tracer.result.EvalOverrides.LogicEvalConstants()
		if tracer.result.EvalOverrides.AllowUnnamedResources {
			tracer.unnamedResourcePolicy = newResourcePolicy(ep, &tracer.result.TxnGroups[0])
			ep.EvalConstants.UnnamedResources = tracer.unnamedResourcePolicy
		}
	}
}

func (tracer *evalTracer) AfterTxnGroup(ep *logic.EvalParams, deltas *ledgercore.StateDelta, evalError error) {
	tracer.handleError(evalError)
	tracer.cursorEvalTracer.AfterTxnGroup(ep, deltas, evalError)

	if ep.GetCaller() == nil && tracer.unnamedResourcePolicy != nil {
		tracer.unnamedResourcePolicy = nil
	}
}

func (tracer *evalTracer) saveApplyData(applyData transactions.ApplyData, omitEvalDelta bool) {
	applyDataOfCurrentTxn := tracer.mustGetApplyDataAtPath(tracer.absolutePath())
	evalDelta := applyDataOfCurrentTxn.EvalDelta
	*applyDataOfCurrentTxn = applyData
	if omitEvalDelta {
		// If omitEvalDelta is true, restore the EvalDelta from applyDataOfCurrentTxn
		applyDataOfCurrentTxn.EvalDelta = evalDelta
	}
}

func (tracer *evalTracer) BeforeTxn(ep *logic.EvalParams, groupIndex int) {
	if tracer.result.ReturnTrace() {
		var txnTraceStackElem *TransactionTrace

		// Where should the current transaction trace attach to:
		// - if it is a top level transaction, then attach to TxnResult level
		// - if it is an inner transaction, then refer to the stack for latest exec trace,
		//   and attach to inner array
		if len(tracer.execTraceStack) == 0 {
			// to adapt to logic sig trace here, we separate into 2 cases:
			// - if we already executed `Before/After-Program`,
			//   then there should be a trace containing logic sig.
			//   We should add the transaction type to the pre-existing execution trace.
			// - otherwise, we take the simplest trace with transaction type.
			if tracer.result.TxnGroups[0].Txns[groupIndex].Trace == nil {
				tracer.result.TxnGroups[0].Txns[groupIndex].Trace = &TransactionTrace{}
			}
			txnTraceStackElem = tracer.result.TxnGroups[0].Txns[groupIndex].Trace
		} else {
			// we are reaching inner txns, so we don't have to be concerned about logic sig trace here
			lastExecTrace := tracer.execTraceStack[len(tracer.execTraceStack)-1]
			lastExecTrace.InnerTraces = append(lastExecTrace.InnerTraces, TransactionTrace{})
			txnTraceStackElem = &lastExecTrace.InnerTraces[len(lastExecTrace.InnerTraces)-1]

			innerIndex := len(lastExecTrace.InnerTraces) - 1
			parentOpIndex := len(*lastExecTrace.programTraceRef) - 1

			parentOp := &(*lastExecTrace.programTraceRef)[parentOpIndex]
			parentOp.SpawnedInners = append(parentOp.SpawnedInners, innerIndex)
		}

		currentTxn := ep.TxnGroup[groupIndex]
		if currentTxn.Txn.Type == protocol.ApplicationCallTx {
			switch currentTxn.Txn.ApplicationCallTxnFields.OnCompletion {
			case transactions.ClearStateOC:
				txnTraceStackElem.programTraceRef = &txnTraceStackElem.ClearStateProgramTrace
			default:
				txnTraceStackElem.programTraceRef = &txnTraceStackElem.ApprovalProgramTrace
			}
		}

		// In both case, we need to add to transaction trace to the stack
		tracer.execTraceStack = append(tracer.execTraceStack, txnTraceStackElem)
	}
	if ep.GetCaller() == nil && tracer.unnamedResourcePolicy != nil {
		tracer.unnamedResourcePolicy.txnRootIndex = groupIndex
	}
	tracer.cursorEvalTracer.BeforeTxn(ep, groupIndex)
}

func (tracer *evalTracer) AfterTxn(ep *logic.EvalParams, groupIndex int, ad transactions.ApplyData, evalError error) {
	tracer.handleError(evalError)
	tracer.saveApplyData(ad, evalError != nil)
	// if the current transaction + simulation condition would lead to exec trace making
	// we should clean them up from tracer.execTraceStack.
	if tracer.result.ReturnTrace() {
		lastOne := tracer.execTraceStack[len(tracer.execTraceStack)-1]
		lastOne.programTraceRef = nil
		tracer.execTraceStack = tracer.execTraceStack[:len(tracer.execTraceStack)-1]
	}
	tracer.cursorEvalTracer.AfterTxn(ep, groupIndex, ad, evalError)
}

func (tracer *evalTracer) saveEvalDelta(evalDelta transactions.EvalDelta, appIDToSave basics.AppIndex) {
	applyDataOfCurrentTxn := tracer.mustGetApplyDataAtPath(tracer.absolutePath())
	// Copy everything except the inner transactions, since those have been kept up-to-date when we
	// traced those transactions.
	inners := applyDataOfCurrentTxn.EvalDelta.InnerTxns
	applyDataOfCurrentTxn.EvalDelta = evalDelta
	applyDataOfCurrentTxn.EvalDelta.InnerTxns = inners
}

func (tracer *evalTracer) makeOpcodeTraceUnit(cx *logic.EvalContext) OpcodeTraceUnit {
	return OpcodeTraceUnit{PC: uint64(cx.PC())}
}

func (o *OpcodeTraceUnit) computeStackValueDeletions(cx *logic.EvalContext, tracer *evalTracer) {
	tracer.popCount, tracer.addCount = cx.GetOpSpec().StackExplain(cx)
	o.StackPopCount = uint64(tracer.popCount)

	stackHeight := len(cx.Stack)
	tracer.stackHeightAfterDeletion = stackHeight - int(o.StackPopCount)
}

func (tracer *evalTracer) BeforeOpcode(cx *logic.EvalContext) {
	groupIndex := cx.GroupIndex()

	if cx.RunMode() == logic.ModeApp {
		// Remember app EvalDelta before executing the opcode. We do this
		// because if this opcode fails, the block evaluator resets the EvalDelta.
		var appIDToSave basics.AppIndex
		if cx.TxnGroup[groupIndex].SignedTxn.Txn.ApplicationID == 0 {
			// App creation
			appIDToSave = cx.AppID()
		}
		tracer.saveEvalDelta(cx.TxnGroup[groupIndex].EvalDelta, appIDToSave)
	}

	if tracer.result.ReturnTrace() {
		var txnTrace *TransactionTrace
		if cx.RunMode() == logic.ModeSig {
			txnTrace = tracer.result.TxnGroups[0].Txns[groupIndex].Trace
		} else {
			txnTrace = tracer.execTraceStack[len(tracer.execTraceStack)-1]
		}
		*txnTrace.programTraceRef = append(*txnTrace.programTraceRef, tracer.makeOpcodeTraceUnit(cx))

		latestOpcodeTraceUnit := &(*txnTrace.programTraceRef)[len(*txnTrace.programTraceRef)-1]
		if tracer.result.ReturnStackChange() {
			latestOpcodeTraceUnit.computeStackValueDeletions(cx, tracer)
		}
		if tracer.result.ReturnScratchChange() {
			tracer.recordChangedScratchSlots(cx)
		}
		if tracer.result.ReturnStateChange() {
			latestOpcodeTraceUnit.appendStateOperations(cx)
			tracer.result.InitialStates.increment(cx)
		}
	}
}

func (o *OpcodeTraceUnit) appendAddedStackValue(cx *logic.EvalContext, tracer *evalTracer) {
	for i := tracer.stackHeightAfterDeletion; i < len(cx.Stack); i++ {
		tealValue := cx.Stack[i].ToTealValue()
		o.StackAdded = append(o.StackAdded, basics.TealValue{
			Type:  tealValue.Type,
			Uint:  tealValue.Uint,
			Bytes: tealValue.Bytes,
		})
	}
}

func (o *OpcodeTraceUnit) appendStateOperations(cx *logic.EvalContext) {
	if cx.GetOpSpec().AppStateExplain == nil {
		return
	}
	appState, stateOp, appID, acctAddr, stateKey := cx.GetOpSpec().AppStateExplain(cx)
	// If the operation is not write or delete, return without
	if stateOp == logic.AppStateRead {
		return
	}
	o.StateChanges = append(o.StateChanges, StateOperation{
		AppStateOp: stateOp,
		AppState:   appState,
		AppID:      appID,
		Key:        stateKey,
		Account:    acctAddr,
	})
}

func (tracer *evalTracer) recordChangedScratchSlots(cx *logic.EvalContext) {
	currentOpcodeName := cx.GetOpSpec().Name
	last := len(cx.Stack) - 1
	tracer.scratchSlots = nil

	switch currentOpcodeName {
	case "store":
		slot := uint64(cx.GetProgram()[cx.PC()+1])
		tracer.scratchSlots = append(tracer.scratchSlots, slot)
	case "stores":
		prev := last - 1
		slot := cx.Stack[prev].Uint

		// If something goes wrong for `stores`, we don't have to error here
		// for in runtime already has evalError
		if slot >= uint64(len(cx.Scratch)) {
			return
		}
		tracer.scratchSlots = append(tracer.scratchSlots, slot)
	}
}

func (tracer *evalTracer) recordUpdatedScratchVars(cx *logic.EvalContext) []ScratchChange {
	if len(tracer.scratchSlots) == 0 {
		return nil
	}
	changes := make([]ScratchChange, len(tracer.scratchSlots))
	for i, slot := range tracer.scratchSlots {
		changes[i] = ScratchChange{
			Slot:     slot,
			NewValue: cx.Scratch[slot].ToTealValue(),
		}
	}
	return changes
}

func (o *OpcodeTraceUnit) updateNewStateValues(cx *logic.EvalContext) {
	for i, sc := range o.StateChanges {
		o.StateChanges[i].NewValue = logic.AppStateQuerying(
			cx, sc.AppState, sc.AppStateOp, sc.AppID, sc.Account, sc.Key)
	}
}

func (tracer *evalTracer) AfterOpcode(cx *logic.EvalContext, evalError error) {
	groupIndex := cx.GroupIndex()

	// NOTE: only when we have no evalError on current opcode,
	// we can proceed for recording stack change
	if evalError == nil && tracer.result.ReturnTrace() {
		var txnTrace *TransactionTrace
		if cx.RunMode() == logic.ModeSig {
			txnTrace = tracer.result.TxnGroups[0].Txns[groupIndex].Trace
		} else {
			txnTrace = tracer.execTraceStack[len(tracer.execTraceStack)-1]
		}

		latestOpcodeTraceUnit := &(*txnTrace.programTraceRef)[len(*txnTrace.programTraceRef)-1]
		if tracer.result.ReturnStackChange() {
			latestOpcodeTraceUnit.appendAddedStackValue(cx, tracer)
		}
		if tracer.result.ReturnScratchChange() {
			latestOpcodeTraceUnit.ScratchSlotChanges = tracer.recordUpdatedScratchVars(cx)
		}
		if tracer.result.ReturnStateChange() {
			latestOpcodeTraceUnit.updateNewStateValues(cx)
		}
	}

	if cx.RunMode() == logic.ModeApp {
		if cx.TxnGroup[groupIndex].Txn.ApplicationCallTxnFields.OnCompletion != transactions.ClearStateOC {
			tracer.handleError(evalError)
		}
		if evalError == nil && tracer.unnamedResourcePolicy != nil {
			if err := tracer.unnamedResourcePolicy.tracker.reconcileBoxWriteBudget(cx.BoxDirtyBytes(), cx.Proto.BytesPerBoxReference); err != nil {
				// This should never happen, since we limit the IO budget to tracer.unnamedResourcePolicy.assignment.maxPossibleBoxIOBudget
				// (as shown below), so we should never have to reconcile an unachievable budget.
				panic(err.Error())
			}

			// Update box budget. It will decrease if an additional non-box resource has been accessed.
			cx.SetIOBudget(tracer.unnamedResourcePolicy.tracker.maxPossibleBoxIOBudget(cx.Proto.BytesPerBoxReference))
		}
	}
}

func (tracer *evalTracer) BeforeProgram(cx *logic.EvalContext) {
	groupIndex := cx.GroupIndex()

	switch cx.RunMode() {
	case logic.ModeSig:
		// Before Program, activated for logic sig, happens before txn group execution
		// we should create trace object for this txn result
		if tracer.result.ReturnTrace() {
			tracer.result.TxnGroups[0].Txns[groupIndex].Trace = &TransactionTrace{}
			traceRef := tracer.result.TxnGroups[0].Txns[groupIndex].Trace
			traceRef.programTraceRef = &traceRef.LogicSigTrace
			traceRef.LogicSigHash = crypto.Hash(cx.GetProgram())
		}
	case logic.ModeApp:
		if tracer.result.ReturnTrace() {
			txnTraceStackElem := tracer.execTraceStack[len(tracer.execTraceStack)-1]
			currentTxn := cx.EvalParams.TxnGroup[groupIndex]
			programHash := crypto.Hash(cx.GetProgram())

			switch currentTxn.Txn.ApplicationCallTxnFields.OnCompletion {
			case transactions.ClearStateOC:
				txnTraceStackElem.ClearStateProgramHash = programHash
			default:
				txnTraceStackElem.ApprovalProgramHash = programHash
			}
		}
		if tracer.result.ReturnStateChange() {
			// If we are recording state changes, including initial states,
			// then we should exclude initial states of created app during simulation.
			if cx.TxnGroup[groupIndex].SignedTxn.Txn.ApplicationID == 0 {
				tracer.result.InitialStates.CreatedApp.Add(cx.AppID())
			}
		}

		if tracer.unnamedResourcePolicy != nil {
			globalSharing := false
			for iter := cx; iter != nil; iter = iter.GetCaller() {
				if iter.ProgramVersion() >= 9 {
					// If some caller in the app callstack allows global sharing, global resources can
					// be accessed here. Otherwise the top-level txn must declare all resources locally.
					globalSharing = true
					break
				}
			}
			tracer.unnamedResourcePolicy.globalSharing = globalSharing
			tracer.unnamedResourcePolicy.programVersion = cx.ProgramVersion()
			if tracer.unnamedResourcePolicy.initialBoxSurplusReadBudget == nil {
				s := cx.SurplusReadBudget
				tracer.unnamedResourcePolicy.initialBoxSurplusReadBudget = &s
			}
			cx.SetIOBudget(tracer.unnamedResourcePolicy.tracker.maxPossibleBoxIOBudget(cx.Proto.BytesPerBoxReference))
		}
	}
}

func (tracer *evalTracer) AfterProgram(cx *logic.EvalContext, pass bool, evalError error) {
	groupIndex := cx.GroupIndex()

	if cx.RunMode() == logic.ModeSig {
		// Report cost for LogicSig program and exit
		tracer.result.TxnGroups[0].Txns[groupIndex].LogicSigBudgetConsumed = uint64(cx.Cost())
		if tracer.result.ReturnTrace() {
			tracer.result.TxnGroups[0].Txns[groupIndex].Trace.programTraceRef = nil
		}
		return
	}

	// Report cost of this program.
	// If it is an inner app call, roll up its cost to the top level transaction.
	tracer.result.TxnGroups[0].Txns[tracer.relativeCursor[0]].AppBudgetConsumed += uint64(cx.Cost())

	if cx.TxnGroup[groupIndex].Txn.ApplicationCallTxnFields.OnCompletion == transactions.ClearStateOC {
		if tracer.result.ReturnTrace() && (!pass || evalError != nil) {
			txnTrace := tracer.execTraceStack[len(tracer.execTraceStack)-1]
			txnTrace.ClearStateRollback = true
			if evalError != nil {
				txnTrace.ClearStateRollbackError = evalError.Error()
			}
		}
	} else {
		tracer.handleError(evalError)
	}
}