def test_generate_nefdbgnfo_file(self):
        path = self.get_contract_path('GenerationWithDecorator.py')

        expected_nef_output = path.replace('.py', '.nefdbgnfo')
        compiler = Compiler()
        compiler.compile_and_save(path, path.replace('.py', '.nef'))
        methods: Dict[str, Method] = {
            name: method
            for name, method in self.get_compiler_analyser(
                compiler).symbol_table.items() if isinstance(method, Method)
        }

        self.assertTrue(os.path.exists(expected_nef_output))
        debug_info = self.get_debug_info(path)
        self.assertNotIn('entrypoint', debug_info)
        self.assertIn('methods', debug_info)
        self.assertGreater(len(debug_info['methods']), 0)

        for debug_method in debug_info['methods']:
            self.assertIn('name', debug_method)
            parsed_name = debug_method['name'].split(',')
            self.assertEqual(2, len(parsed_name))
            self.assertIn(parsed_name[-1], methods)
            actual_method = methods[parsed_name[-1]]

            # validate id
            self.assertIn('id', debug_method)
            self.assertEqual(str(id(actual_method)), debug_method['id'])

            # validate parameters
            self.assertIn('params', debug_method)
            self.assertEqual(len(actual_method.args),
                             len(debug_method['params']))
            for var in debug_method['params']:
                self.assertEqual(2, len(var.split(',')))
                param_id, param_type = var.split(',')
                self.assertIn(param_id, actual_method.args)
                self.assertEqual(param_type,
                                 actual_method.args[param_id].type.abi_type)

            # validate local variables
            self.assertIn('variables', debug_method)
            self.assertEqual(len(actual_method.locals),
                             len(debug_method['variables']))
            for var in debug_method['variables']:
                self.assertEqual(2, len(var.split(',')))
                var_id, var_type = var.split(',')
                self.assertIn(var_id, actual_method.locals)
                self.assertEqual(actual_method.locals[var_id].type.abi_type,
                                 var_type)
Beispiel #2
0
    def test_generate_debug_info_with_multiple_flows(self):
        path = '%s/boa3_test/test_sc/generation_test/GenerationWithMultipleFlows.py' % self.dirname

        compiler = Compiler()
        compiler.compile_and_save(path, path.replace('.py', '.nef'))
        methods: Dict[str, Method] = {
            name: method
            for name, method in self.get_compiler_analyser(
                compiler).symbol_table.items() if isinstance(method, Method)
        }
        self.assertGreater(len(methods), 0)

        debug_info = self.get_debug_info(path)
        self.assertIn('methods', debug_info)
        self.assertGreater(len(debug_info['methods']), 0)

        for debug_method in debug_info['methods']:
            self.assertIn('name', debug_method)
            parsed_name = debug_method['name'].split(',')
            self.assertEqual(2, len(parsed_name))
            self.assertIn(parsed_name[-1], methods)
            actual_method = methods[parsed_name[-1]]

            # validate id
            self.assertIn('id', debug_method)
            self.assertEqual(str(id(actual_method)), debug_method['id'])

            # validate parameters
            self.assertIn('params', debug_method)
            self.assertEqual(len(actual_method.args),
                             len(debug_method['params']))
            for var in debug_method['params']:
                self.assertEqual(2, len(var.split(',')))
                param_id, param_type = var.split(',')
                self.assertIn(param_id, actual_method.args)
                self.assertEqual(param_type,
                                 actual_method.args[param_id].type.abi_type)

            # validate local variables
            self.assertIn('variables', debug_method)
            self.assertEqual(len(actual_method.locals),
                             len(debug_method['variables']))
            for var in debug_method['variables']:
                self.assertEqual(2, len(var.split(',')))
                var_id, var_type = var.split(',')
                self.assertIn(var_id, actual_method.locals)
                self.assertEqual(actual_method.locals[var_id].type.abi_type,
                                 var_type)
Beispiel #3
0
    def test_generate_nefdbgnfo_file_with_event(self):
        path = self.get_contract_path('test_sc/event_test',
                                      'EventWithArgument.py')

        expected_nef_output = path.replace('.py', '.nefdbgnfo')
        compiler = Compiler()
        compiler.compile_and_save(path,
                                  path.replace('.py', '.nef'),
                                  debug=True)
        events: Dict[str, Event] = {
            name: method
            for name, method in self.get_compiler_analyser(
                compiler).symbol_table.items() if isinstance(method, Event)
        }

        self.assertTrue(os.path.exists(expected_nef_output))
        debug_info = self.get_debug_info(path)
        self.assertNotIn('entrypoint', debug_info)
        self.assertIn('events', debug_info)
        self.assertGreater(len(debug_info['events']), 0)

        for debug_event in debug_info['events']:
            self.assertIn('name', debug_event)
            parsed_name = debug_event['name'].split(
                constants.VARIABLE_NAME_SEPARATOR)
            self.assertEqual(2, len(parsed_name))
            self.assertIn(parsed_name[-1], events)
            actual_event = events[parsed_name[-1]]

            # validate id
            self.assertIn('id', debug_event)
            self.assertEqual(str(id(actual_event)), debug_event['id'])

            # validate parameters
            self.assertIn('params', debug_event)
            self.assertEqual(len(actual_event.args),
                             len(debug_event['params']))
            for var in debug_event['params']:
                self.assertEqual(
                    2, len(var.split(constants.VARIABLE_NAME_SEPARATOR)))
                param_id, param_type = var.split(
                    constants.VARIABLE_NAME_SEPARATOR)
                self.assertIn(param_id, actual_event.args)
                self.assertEqual(param_type,
                                 actual_event.args[param_id].type.abi_type)
Beispiel #4
0
    def test_generate_nefdbgnfo_file_with_static_variables(self):
        path = self.get_contract_path('GenerationWithStaticVariables.py')

        expected_nef_output = path.replace('.py', '.nefdbgnfo')
        compiler = Compiler()
        compiler.compile_and_save(path,
                                  path.replace('.py', '.nef'),
                                  debug=True)
        variables: Dict[str, Method] = {
            name: method
            for name, method in self.get_compiler_analyser(
                compiler).symbol_table.items() if isinstance(method, Variable)
        }

        self.assertTrue(os.path.exists(expected_nef_output))
        debug_info = self.get_debug_info(path)

        self.assertNotIn('entrypoint', debug_info)
        self.assertIn('static-variables', debug_info)
        self.assertGreater(len(debug_info['static-variables']), 0)

        for static_variable in debug_info['static-variables']:
            # validate parameters
            self.assertEqual(
                3,
                len(static_variable.split(constants.VARIABLE_NAME_SEPARATOR)))
            var_id, var_type, var_slot = static_variable.split(
                constants.VARIABLE_NAME_SEPARATOR)
            if var_id not in variables:
                self.assertIn(var_id, [
                    var_full_id.split(constants.VARIABLE_NAME_SEPARATOR)[-1]
                    for var_full_id in variables
                ])
                var_inner_id = next((var for var in variables if var.split(
                    constants.VARIABLE_NAME_SEPARATOR)[-1] == var_id), var_id)
            else:
                var_inner_id = var_id

            self.assertIn(var_inner_id, variables)
            self.assertEqual(var_type, variables[var_inner_id].type.abi_type)
Beispiel #5
0
    def test_generate_nefdbgnfo_file_with_user_module_import(self):
        from boa3.model.type.itype import IType
        path = self.get_contract_path(
            'GenerationWithUserModuleImportsDupNames.py')

        expected_nef_output = path.replace('.py', '.nefdbgnfo')
        compiler = Compiler()
        compiler.compile_and_save(path,
                                  path.replace('.py', '.nef'),
                                  debug=True)
        methods: Dict[str, Method] = {
            name: method
            for name, method in self.get_all_imported_methods(
                compiler).items() if isinstance(method, Method)
        }

        self.assertTrue(os.path.exists(expected_nef_output))
        debug_info = self.get_debug_info(path)
        self.assertNotIn('entrypoint', debug_info)
        self.assertIn('methods', debug_info)
        self.assertGreater(len(debug_info['methods']), 0)

        for debug_method in debug_info['methods']:
            self.assertIn('name', debug_method)
            name_without_parsing = debug_method['name']
            parsed_name = name_without_parsing.split(
                constants.VARIABLE_NAME_SEPARATOR)
            self.assertEqual(2, len(parsed_name))
            self.assertIn(name_without_parsing, methods)
            actual_method = methods[name_without_parsing]

            # validate id
            self.assertIn('id', debug_method)
            self.assertEqual(str(id(actual_method)), debug_method['id'])

            # validate parameters
            self.assertIn('params', debug_method)
            self.assertEqual(len(actual_method.args),
                             len(debug_method['params']))
            for var in debug_method['params']:
                self.assertEqual(
                    2, len(var.split(constants.VARIABLE_NAME_SEPARATOR)))
                param_id, param_type = var.split(
                    constants.VARIABLE_NAME_SEPARATOR)
                self.assertIn(param_id, actual_method.args)
                self.assertEqual(param_type,
                                 actual_method.args[param_id].type.abi_type)

            # validate local variables
            self.assertIn('variables', debug_method)
            self.assertEqual(len(actual_method.locals),
                             len(debug_method['variables']))
            for var in debug_method['variables']:
                self.assertEqual(
                    2, len(var.split(constants.VARIABLE_NAME_SEPARATOR)))
                var_id, var_type = var.split(constants.VARIABLE_NAME_SEPARATOR)
                self.assertIn(var_id, actual_method.locals)
                local_type = actual_method.locals[var_id].type
                self.assertEqual(
                    local_type.abi_type if isinstance(local_type, IType) else
                    AbiType.Any, var_type)