Esempio n. 1
0
    def test_debot4(self):
        keypair = async_custom_client.crypto.generate_random_sign_keys()
        debot_address, target_address = self.__init_debot4(keypair=keypair)

        # Check target address type
        target_boc = self.download_account(address=target_address)
        self.assertIsNotNone(target_boc)
        account = async_custom_client.boc.parse_account(
            params=ParamsOfParse(boc=target_boc))
        self.assertEqual(0, account.parsed['acc_type'])

        # Run debot
        params = ParamsOfInit(address=debot_address)
        terminal_outputs = [
            'Target contract deployed.',
            'Enter 1',
            'getData',
            'setData(128)',
            'Sign external message:',
            'Transaction succeeded',
            'setData2(129)'
        ]
        debot_browser(
            steps=[], params=params, start=True, keypair=keypair,
            terminal_outputs=terminal_outputs)

        # Check address type again
        target_boc = self.download_account(address=target_address)
        self.assertIsNotNone(target_boc)
        account = async_custom_client.boc.parse_account(
            params=ParamsOfParse(boc=target_boc))
        self.assertEqual(1, account.parsed['acc_type'])
Esempio n. 2
0
    def __handle_message_queue():
        while len(state['msg_queue']):
            msg_opt = state['msg_queue'].pop(0)

            parsed = async_custom_client.boc.parse_message(
                params=ParamsOfParse(boc=msg_opt))
            body = parsed.parsed['body']
            dest_address = parsed.parsed['dst']
            src_address = parsed.parsed['src']
            wc, interface_id = dest_address.split(':')

            if wc == str(DEBOT_WC):
                if interface_id == INTERFACES[0]:
                    abi = Abi.Json(value=json.dumps(ECHO_ABI))
                elif interface_id == INTERFACES[1]:
                    abi = Abi.Json(value=json.dumps(TERMINAL_ABI))
                else:
                    raise ValueError('Unsupported interface')

                decoded = async_custom_client.abi.decode_message_body(
                    params=ParamsOfDecodeMessageBody(
                        abi=abi, body=body, is_internal=True))
                logging.info(f'Request: `{decoded.name}` ({decoded.value})')

                if interface_id == INTERFACES[0]:
                    method = state['echo'].methods[decoded.name]
                elif interface_id == INTERFACES[1]:
                    method = state['terminal'].methods[decoded.name]
                else:
                    raise ValueError('Unsupported interface')

                func_id, kwargs = method(decoded.value)
                logging.info(f'Response: `{func_id}` ({kwargs})')
                call_set = CallSet(function_name=hex(func_id), input=kwargs) \
                    if func_id > 0 else None
                internal = async_custom_client.abi.encode_internal_message(
                    params=ParamsOfEncodeInternalMessage(
                        value='1000000000000000',
                        abi=Abi.Json(value=debot.debot_abi),
                        address=src_address, call_set=call_set))
                async_custom_client.debot.send(
                    params=ParamsOfSend(
                        debot_handle=debot.debot_handle,
                        message=internal.message))
            else:
                debot_fetched = state['bots'].get(dest_address)
                if not debot_fetched:
                    _params = ParamsOfInit(address=dest_address)
                    debot_browser(
                        steps=[], start=False, keypair=keypair,
                        params=_params, state=state)

                debot_fetched = state['bots'][dest_address].debot_handle
                async_custom_client.debot.send(
                    params=ParamsOfSend(
                        debot_handle=debot_fetched, message=msg_opt))
Esempio n. 3
0
 def test_interface_calls(self):
     debot_address, _ = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 7, 'inputs': [], 'outputs': ['', 'test1 - call interface']},
         {'choice': 0, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []},
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 4
0
 def test_goto(self):
     debot_address, _ = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 0, 'inputs': [], 'outputs': ['Test Goto Action']},
         {'choice': 0, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []}
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 5
0
 def test_debot_msg_interface(self):
     keypair = async_custom_client.crypto.generate_random_sign_keys()
     debot_address = self.__init_debot2(keypair=keypair)
     params = ParamsOfInit(address=debot_address)
     terminal_outputs = [
         'counter=10',
         'Increment succeeded',
         'counter=15'
     ]
     debot_browser(
         steps=[], params=params, start=True, keypair=keypair,
         terminal_outputs=terminal_outputs)
Esempio n. 6
0
 def test_print(self):
     debot_address, target_address = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 1, 'inputs': [], 'outputs': ['Test Print Action', 'test2: instant print', 'test instant print']},
         {'choice': 0, 'inputs': [], 'outputs': ['test simple print']},
         {'choice': 1, 'inputs': [], 'outputs': [f'integer=1,addr={target_address},string=test_string_1']},
         {'choice': 2, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []}
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 7
0
 def test_send_message(self):
     debot_address, _ = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 4, 'inputs': [], 'outputs': ['Test Send Msg Action']},
         {'choice': 0, 'inputs': [], 'outputs': ['Sending message {}', 'Transaction succeeded.']},
         {'choice': 1, 'inputs': [], 'outputs': []},
         {'choice': 2, 'inputs': [], 'outputs': ['data=100']},
         {'choice': 3, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []}
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 8
0
    def test_debot_invoke_msgs(self):
        keypair = async_custom_client.crypto.generate_random_sign_keys()
        debot_a, _ = self.__init_debot_pair(keypair=keypair)

        # Run debot
        params = ParamsOfInit(address=debot_a)
        terminal_outputs = [
            'Invoking Debot B',
            'DebotB receives question: What is your name?',
            'DebotA receives answer: My name is DebotB'
        ]
        debot_browser(
            steps=[], params=params, start=True, keypair=keypair,
            terminal_outputs=terminal_outputs)
Esempio n. 9
0
 def test_invoke(self):
     debot_address, _ = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 5, 'inputs': [debot_address], 'outputs': ['Test Invoke Debot Action', 'enter debot address:']},
         {'choice': 0, 'inputs': [debot_address], 'outputs': ['Test Invoke Debot Action', 'enter debot address:'], 'invokes': [
             {'choice': 0, 'inputs': [], 'outputs': ['Print test string', 'Debot is invoked']},
             {'choice': 0, 'inputs': [], 'outputs': ['Sending message {}', 'Transaction succeeded.']}
         ]},
         {'choice': 1, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []}
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 10
0
    def test_run_method(self):
        keypair = async_custom_client.crypto.generate_random_sign_keys()
        debot_address, target_address = self.__init_debot(keypair=keypair)

        steps = [
            {'choice': 3, 'inputs': [], 'outputs': ['Test Run Method Action']},
            {'choice': 0, 'inputs': [], 'outputs': []},
            {'choice': 1, 'inputs': [], 'outputs': ['data=64']},
            {'choice': 2, 'inputs': [], 'outputs': ['Debot Tests']},
            {'choice': 8, 'inputs': [], 'outputs': []}
        ]
        params = ParamsOfInit(address=debot_address)
        debot_browser(
            steps=steps, params=params, start=True, keypair=keypair)
Esempio n. 11
0
 def test_run_action(self):
     debot_address, _ = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 2, 'inputs': [], 'outputs': ['Test Run Action']},
         {'choice': 0, 'inputs': ['-1:1111111111111111111111111111111111111111111111111111111111111111'], 'outputs': ['Test Instant Run', 'test1: instant run 1', 'test2: instant run 2']},
         {'choice': 0, 'inputs': [], 'outputs': ['Test Run Action']},
         {'choice': 1, 'inputs': ['hello'], 'outputs': []},
         {'choice': 2, 'inputs': [], 'outputs': ['integer=2,addr=-1:1111111111111111111111111111111111111111111111111111111111111111,string=hello']},
         {'choice': 3, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []}
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 12
0
 def test_engine_calls(self):
     debot_address, _ = self.__init_debot(keypair=self.keypair)
     steps = [
         {'choice': 6, 'inputs': [], 'outputs': ['Test Engine Calls']},
         {'choice': 0, 'inputs': [], 'outputs': []},
         {'choice': 1, 'inputs': [], 'outputs': []},
         # {'choice': 2, 'inputs': [], 'outputs': []},
         {'choice': 3, 'inputs': [], 'outputs': []},
         {'choice': 4, 'inputs': [], 'outputs': []},
         {'choice': 5, 'inputs': [], 'outputs': ['Debot Tests']},
         {'choice': 8, 'inputs': [], 'outputs': []}
     ]
     params = ParamsOfInit(address=debot_address)
     debot_browser(
         steps=steps, params=params, start=True, keypair=self.keypair)
Esempio n. 13
0
    def test_debot_sdk_get_accounts_by_hash(self):
        count = 2
        keypair = async_custom_client.crypto.generate_random_sign_keys()
        addresses, contract_hash = self.__init_debot5(count=count)

        # Get contracts with hash count
        params = ParamsOfAggregateCollection(
            collection='accounts', filter={'code_hash': {'eq': contract_hash}},
            fields=[FieldAggregation(field='', fn=AggregationFn.COUNT)])
        result = async_custom_client.net.aggregate_collection(params=params)
        exists = int(result.values[0])

        # Run debot
        params = ParamsOfInit(address=addresses[0])
        terminal_outputs = [
            f'{exists} contracts.'
        ]
        debot_browser(
            steps=[], params=params, start=True, keypair=keypair,
            terminal_outputs=terminal_outputs)
Esempio n. 14
0
 def test_debot_sdk_interface(self):
     keypair = async_custom_client.crypto.generate_random_sign_keys()
     debot_address = self.__init_debot3(keypair=keypair)
     params = ParamsOfInit(address=debot_address)
     terminal_outputs = [
         'test substring1 passed',
         'test substring2 passed',
         'test mnemonicDeriveSignKeys passed',
         'test genRandom passed',
         'test naclbox passed',
         'test naclKeypairFromSecret passed',
         'test hex encode passed',
         'test base64 encode passed',
         'test mnemonic passed',
         'test naclboxopen passed',
         'test account passed',
         'test hdkeyXprv passed',
         'test hex decode passed',
         'test base64 decode passed'
     ]
     debot_browser(
         steps=[], params=params, start=True, keypair=keypair,
         terminal_outputs=terminal_outputs)
Esempio n. 15
0
    def __callback(response_data, response_type, *args):
        # Process notifications
        if response_type == TCResponseType.AppNotify:
            notify = ParamsOfAppDebotBrowser.from_dict(data=response_data)

            # Process notification types
            if isinstance(notify, ParamsOfAppDebotBrowser.Log):
                state['messages'].append(notify.msg)
            if isinstance(notify, ParamsOfAppDebotBrowser.Switch):
                state['switch_started'] = True
                if notify.context_id == DebotState.EXIT:
                    state['finished'] = True
                state['actions'].clear()
            if isinstance(
                    notify, ParamsOfAppDebotBrowser.SwitchCompleted):
                state['switch_started'] = False
            if isinstance(notify, ParamsOfAppDebotBrowser.ShowAction):
                state['actions'].append(notify.action)
            if isinstance(notify, ParamsOfAppDebotBrowser.Send):
                state['msg_queue'].append(notify.message)

        # Process requests
        if response_type == TCResponseType.AppRequest:
            request = ParamsOfAppRequest(**response_data)
            request_data = ParamsOfAppDebotBrowser.from_dict(
                data=request.request_data)
            result = None

            # Process request types
            if isinstance(request_data, ParamsOfAppDebotBrowser.Input):
                result = ResultOfAppDebotBrowser.Input(
                    value=state['step']['inputs'][0])
            if isinstance(
                    request_data, ParamsOfAppDebotBrowser.GetSigningBox):
                with ThreadPoolExecutor() as executor:
                    future = executor.submit(
                        async_custom_client.crypto.get_signing_box,
                        params=keypair)
                    signing_box = future.result()
                result = ResultOfAppDebotBrowser.GetSigningBox(
                    signing_box=signing_box.handle)
            if isinstance(
                    request_data, ParamsOfAppDebotBrowser.InvokeDebot):
                invoke_steps = state['step']['invokes']
                _params = ParamsOfInit(address=request_data.debot_addr)

                # Here we should call `debot_browser` in `spawn` mode
                # subprocess for compatibility with `Unix` systems.
                # MacOS, Windows use `spawn` by default
                with get_context('spawn').Pool() as pool:
                    pool.apply(debot_browser, kwds={
                        'steps': invoke_steps,
                        'params': _params,
                        'start': False,
                        'actions': [request_data.action],
                        'keypair': keypair
                    })
                result = ResultOfAppDebotBrowser.InvokeDebot()
            if isinstance(request_data, ParamsOfAppDebotBrowser.Approve):
                result = ResultOfAppDebotBrowser.Approve(approved=True)

            # Resolve app request
            result = AppRequestResult.Ok(result=result.dict)
            resolve_params = ParamsOfResolveAppRequest(
                app_request_id=request.app_request_id, result=result)
            with ThreadPoolExecutor() as executor:
                future = executor.submit(
                    async_custom_client.resolve_app_request,
                    params=resolve_params)
                future.result()