How to find which Assert in PyTEAL code failed?

Hello,
When developing an app using PyTEAL Assert we very often have errors like :

algosdk.error.AlgodHTTPError: TransactionPool.Remember: transaction WVSGUZXHIOZ4XLI6ZBLN2URJHKEKJB6EQHY2Y5GASWEMPLZ335VA: logic eval error: assert failed pc=303. Details: pc=303, opcodes===

Is there an easy way to know which Assert in my PyTEAL code failed ?

Would it be possible to improve debugging by allowing to pass an integer id when we call Assert so that the algod error displays the id and we directly know which assert failed ?

Or to have a utility witch lists the pc, the compiled bytes and the teal source together? Then pc=303 could be located quite easily…

I mean a list like this:

-PC- -BYTE CODE-
0000 06							        #pragma version 6
0001 20 02 00 01					        intcblock 0 1
0005 26 01 0f 5f
0009 5f 61 70 70
0013 5f 63 72 65
0017 61 74 6f 72
0021 5f 5f						        bytecblock 0x5f5f6170705f63726561746f725f5f		// "__app_creator__"
0023 31 18							txn ApplicationID
0025 22								intc_0 // 0
0026 12								==
0027 40 00 F5    						bnz label1
0030 31 19							txn OnCompletion
0032 22								intc_0 // 0
0033 12								==
0034 40 00 30   						bnz label2
0037 31 19							txn OnCompletion
0039 81 05							pushint 5
0041 12								==
...

It could be easily done using the goal clerk -D option for .teal.tok, and using the bytes of the teal.tok file.

And then how do I find the line responsible in my PyTeal code ?

Well, a good question. You can approximately map the compiled code to the pyteal. Or, store values (“pseudo line numbers”) in a pyteal scratch var. Or push and pop an Int before the assert.

For reference, I found :

@Maugli or @fabrice
I saw it is possible to get the source map by passing the -m flag to goal clerk compile (here) but is it possible to get it with the Python SDK i.e. by passing a parameter to algod_client.compile(source_code) ?

It is supported in the REST endpoint but looks like the SDK do not support it yet:

1 Like

I tried using Postman to test the retrieval of the source map and I have the same response (200 OK) with those 3 URLs :

The response is a json with only 2 keys : hash and result

Algod version of the purestake node is 3.8.1

So I don’t know how to get the source map of my smart contract (useful to debug assert errors).

Can you try with this API service API or that one https://algoexplorer.io/api-dev/v2 ?

1 Like

Thanks a lot @fabrice !! It works with both algoexplorer and algonode.

Strange that Purestake is not compatible yet despite having up to date node.

This has been updated for PureStake in BetaNet and TestNet, MainNet will follow tomorrow.