Exemplo n.º 1
0
    def execute(self, code: str) -> InterpreterResult:
        """Execute some code preserving current context and stack

        :param code: Michelson code
        """
        result = InterpreterResult(stdout=[])
        stack_backup = deepcopy(self.stack)
        context_backup = deepcopy(self.context)

        try:
            code_section = CodeSection.match(michelson_to_micheline(code))
            instructions = code_section.args[0].execute(
                self.stack, result.stdout, self.context)
            result.instructions = MichelineSequence([instructions])
            result.stack = self.stack
        except (MichelsonParserError, MichelsonRuntimeError) as e:
            if self.context.debug:
                raise

            self.stack = stack_backup
            self.context = context_backup
            result.stdout.append(e.format_stdout())
            result.error = e

        return result
Exemplo n.º 2
0
 def _find_stack_items(self, instructions: MichelineSequence, stack: MichelsonStack) -> Optional[List[MichelsonInstruction]]:
     for operation in instructions.items[::-1]:
         items = getattr(operation, 'items', None)
         if isinstance(items, list):
             stack_items = self._find_stack_items(MichelineSequence(items), stack)
             if stack_items:
                 return stack_items
         if not isinstance(operation, MichelsonInstruction):
             continue
         if operation.stack_items_added:
             return cast(List[MichelsonInstruction], stack.items[-operation.stack_items_added :])
     return None