OpShin/opshin

Unexpected output of compiling/linting a smart contract w/o a validator function

Closed this issue · 3 comments

Describe the bug
Compiling a smart contract without a validator function outputs an unwanted output:

Traceback (most recent call last):
  File "/Users/chrissi/Library/Caches/pypoetry/virtualenvs/opshin-_DC4-49p-py3.11/bin/opshin", line 6, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/chrissi/Desktop/repos/cardano/OpShin Toolchain/public repo/opshin/opshin/__main__.py", line 193, in main
    annotations = list(sc.validator.__annotations__.values())
                       ^^^^^^^^^^^^
AttributeError: module '__tmp_opshin' has no attribute 'validator'

The problem is that in line 119 of opshin/__main__.py it is already expected that the smart contract has a function called validator.

Whereas this would normally be checked at a later step when the source_code gets parsed/compiled.
Then the error would be normally get caught by an assertion in the method visit_Module in opshin/compiler.py.

This impacts also the linting functionality since this is an uncaught exception and then it doesn't get printed out correctly.

To Reproduce
Steps to reproduce the behavior:

  1. create a python file without an validator() function
  2. run something like opshin build any broken.py
  3. See unwanted output

Expected behavior
expected should be something like:

Traceback (most recent call last):
  File "/Users/chrissi/Library/Caches/pypoetry/virtualenvs/opshin-_DC4-49p-py3.11/bin/opshin", line 6, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/chrissi/Desktop/repos/cardano/OpShin Toolchain/public repo/opshin/opshin/__main__.py", line 161, in main
    raise SyntaxError(
  File "examples/broken.py", line 1
    from opshin.prelude import *
AssertionError: Could not find function named validator
Note that opshin errors may be overly restrictive as they aim to prevent code with unintended consequences.

current version of the main branch with commit 9e3ad8a8c6c3d7d55262a43eb5b5b35939d33695

to fix this issue we should add a test that looks something like:

    def test_no_validator_function(self):
        source_code = ""
        ast = compiler.parse(source_code)
        with self.assertRaises(CompilerError):
            compiler.compile(ast) 

this for example would be a valid test for an older commit like 12ac3122242d0c7695c9091d00241f855fb4e499 (opshin 0.13.0)

Please use the newest version of OpShin (dev branch) and opshin lint lib to compile functions without validator class

To make linter message valid we should definitely catch more error messages in the main function :)