Exemplo n.º 1
0
def do_run(ctx: Context, prim, args, annots):
    do_begin(ctx, prim, args, annots)

    code = ctx.get('code')
    assert code, f'code is not initialized'

    do_interpret(ctx, code)
    return do_commit(ctx, prim, args=[], annots=[])
Exemplo n.º 2
0
def do_include(ctx: Context, prim, args, annots):
    path = get_string(args[0])

    if isfile(path):
        code = Contract.from_file(path).code
    else:
        parts = path.split(':')
        network = parts[0] if len(parts) > 1 else ctx.get('NETWORK', 'mainnet')
        address = parts[1] if len(parts) > 1 else parts[0]
        assert is_kt(address), f'expected filename or KT address (with network), got {path}'
        script = Interop().using(network).shell.contracts[address].script()
        code = script['code']
        ctx.set('STORAGE', script['storage'])

    do_interpret(ctx, code)
Exemplo n.º 3
0
    def execute(self, code):
        """ Execute Michelson instructions (note that stack is not cleared after execution).

        :param code: Michelson source (any valid Michelson expression or special helpers)
        :returns: {"success": True|False, "stdout": "", "stderr": {}, "result": {"value": "", ...}}
        """
        int_res = {'success': False}

        try:
            code_expr = michelson_to_micheline(code, parser=self.parser)
        except MichelsonParserError as e:
            if self.debug:
                raise e
            int_res['stderr'] = format_stderr(e)
            return int_res

        backup = deepcopy(self.ctx)

        try:
            res = do_interpret(self.ctx, code_expr)
            if res is None and self.ctx.pushed:
                res = {'kind': 'stack', 'stack': self.ctx.dump(count=1)}

            int_res['result'] = format_result(res)
            int_res['stdout'] = format_stdout(self.ctx.stdout)
            int_res['success'] = True
            self.ctx.reset()
        except MichelsonRuntimeError as e:
            int_res['stderr'] = format_stderr(e)
            int_res['stdout'] = format_stdout(self.ctx.stdout)
            self.ctx = backup

            if self.debug:
                if int_res.get('stdout'):
                    print(int_res['stdout'])
                raise e

        if self.debug:
            if int_res.get('stdout'):
                print(int_res['stdout'])
            if int_res.get('result'):
                print('RESULT: ' + pformat(int_res['result']))

        return int_res
Exemplo n.º 4
0
    def execute(self, code):
        int_res = {'success': False}

        try:
            code_expr = michelson_to_micheline(code, parser=self.parser)
        except MichelsonParserError as e:
            if self.debug:
                raise e
            int_res['stderr'] = format_stderr(e)
            return int_res

        backup = deepcopy(self.ctx)

        try:
            res = do_interpret(self.ctx, code_expr)
            if res is None and self.ctx.pushed:
                res = {'kind': 'stack', 'stack': self.ctx.dump(count=1)}

            int_res['result'] = format_result(res)
            int_res['stdout'] = format_stdout(self.ctx.stdout)
            int_res['success'] = True
            self.ctx.reset()
        except MichelsonRuntimeError as e:
            int_res['stderr'] = format_stderr(e)
            int_res['stdout'] = format_stdout(self.ctx.stdout)
            self.ctx = backup

            if self.debug:
                if int_res.get('stdout'):
                    print(int_res['stdout'])
                raise e

        if self.debug:
            if int_res.get('stdout'):
                print(int_res['stdout'])
            if int_res.get('result'):
                print('RESULT: ' + pformat(int_res['result']))

        return int_res