Пример #1
0
    async def publish_new_block(ws, channel_name, height, peer_id):
        call_method = WSDispatcher.PUBLISH_NEW_BLOCK
        channel_stub = get_channel_stub_by_channel_name(channel_name)
        try:
            while True:
                new_block_dumped, confirm_info_bytes = await channel_stub.async_task().announce_new_block(
                    subscriber_block_height=height,
                    subscriber_id=peer_id
                )
                new_block: dict = json.loads(new_block_dumped)

                if "error" in new_block:
                    Logger.error(f"announce_new_block error: {new_block}, to citizen({peer_id})")
                    break

                confirm_info = confirm_info_bytes.decode('utf-8')
                request = Request(call_method, block=new_block, confirm_info=confirm_info)
                Logger.debug(f"{call_method}: {request}")

                await ws.send(json.dumps(request))
                height += 1
        except exceptions.ConnectionClosed:
            Logger.debug("Connection Closed by child.")  # TODO: Useful message needed.
        except Exception as e:
            traceback.print_exc()  # TODO: Keep this tb?
            await WSDispatcher.send_exception(
                ws, call_method,
                exception=e,
                error_code=message_code.Response.fail_announce_block
            )
Пример #2
0
    async def icx_getReceiptProof(context: Dict[str, str], **kwargs):
        channel = context.get('channel')
        channel_stub = get_channel_stub_by_channel_name(channel)

        tx_hash = kwargs['txHash']
        response = await channel_stub.async_task().get_receipt_proof(tx_hash)
        return response_to_json_query(response)
Пример #3
0
        async def _publish_heartbeat():
            channel_stub = get_channel_stub_by_channel_name(channel_name)
            exception = None
            while ws.open:
                try:
                    is_registered = await channel_stub.async_task(
                    ).is_registered_subscriber(peer_id=peer_id)
                    if is_registered:
                        request = Request("node_ws_PublishHeartbeat")
                        await ws.send(json.dumps(request))
                        heartbeat_time = StubCollection().conf[
                            ConfigKey.WS_HEARTBEAT_TIME]
                        await asyncio.sleep(heartbeat_time)
                        continue

                    raise RuntimeError("Unregistered")

                except Exception as e:
                    exception = e
                    traceback.print_exc()
                    break

            if not exception:
                exception = ConnectionError("Connection closed.")

            request = Request("node_ws_PublishHeartbeat", error=str(exception))
            await ws.send(json.dumps(request))
            raise exception
Пример #4
0
    async def icx_getReceiptProof(**kwargs):
        channel = kwargs['context']['channel']
        channel_stub = get_channel_stub_by_channel_name(channel)

        tx_hash = kwargs['txHash']
        response = await channel_stub.async_task().get_receipt_proof(tx_hash)
        return response_to_json_query(response)
Пример #5
0
    async def publish_new_block(ws, channel_name, height, peer_id):
        exception = None
        error_code = None
        channel_stub = get_channel_stub_by_channel_name(channel_name)
        try:
            while ws.open:
                new_block_dumped, confirm_info_bytes = await \
                    channel_stub.async_task().announce_new_block(subscriber_block_height=height, subscriber_id=peer_id)
                new_block: dict = json.loads(new_block_dumped)
                confirm_info = confirm_info_bytes.decode('utf-8')
                request = Request("node_ws_PublishNewBlock",
                                  block=new_block,
                                  confirm_info=confirm_info)
                Logger.debug(f"node_ws_PublishNewBlock: {request}")

                await ws.send(json.dumps(request))
                height += 1
        except exceptions.ConnectionClosed as e:
            exception = e
            error_code = message_code.Response.fail_connection_closed
        except Exception as e:
            exception = e
            error_code = message_code.Response.fail_announce_block
            traceback.print_exc()

        if not exception:
            exception = ConnectionError("Connection closed.")

        await WSDispatcher.send_and_raise_exception(ws,
                                                    "node_ws_PublishNewBlock",
                                                    exception, error_code)
Пример #6
0
    async def icx_proveTransaction(**kwargs):
        channel = kwargs['context']['channel']
        channel_stub = get_channel_stub_by_channel_name(channel)

        tx_hash = kwargs['txHash']
        proof = kwargs['proof']
        response = await channel_stub.async_task().prove_tx(tx_hash, proof)
        return response_to_json_query(response)
Пример #7
0
    async def channel_register(ws, channel_name: str, peer_id: str):
        channel_stub = get_channel_stub_by_channel_name(channel_name)
        approved = await channel_stub.async_task().register_subscriber(peer_id=peer_id)

        if not approved:
            raise RuntimeError("This peer can no longer take more subscribe requests.")

        Logger.debug(f"register subscriber: {peer_id}")
Пример #8
0
    async def rep_getListByHash(**kwargs):
        channel = kwargs['context']['channel']
        channel_stub = get_channel_stub_by_channel_name(channel)
        request = convert_params(kwargs, RequestParamType.get_reps_by_hash)
        reps_hash = request.get('repsHash')
        reps = await channel_stub.async_task().get_reps_by_hash(reps_hash=reps_hash)
        Logger.debug(f"reps_hash: {reps_hash}, reps: {reps}")
        response = [{'address': rep['id'], 'p2pEndpoint': rep['p2pEndpoint']} for rep in reps]

        return response_to_json_query(response)
Пример #9
0
 async def node_AnnounceConfirmedBlock(**kwargs):
     try:
         channel = kwargs['context']['channel']
         del kwargs['context']
         channel = channel if channel is not None else kwargs['channel']
     except KeyError:
         channel = kwargs['channel']
     block, commit_state = kwargs['block'], kwargs.get('commit_state', "{}")
     channel_stub = get_channel_stub_by_channel_name(channel)
     response_code = await channel_stub.async_task().announce_confirmed_block(block.encode('utf-8'), commit_state)
     return {"response_code": response_code,
             "message": message_code.get_response_msg(response_code)}
Пример #10
0
 async def publish_unregister(ws, channel_name, peer_id, force: bool = False):
     call_method = WSDispatcher.PUBLISH_HEARTBEAT
     if force:  # unregister due to subscribe limit
         signal = True
         error_code = message_code.Response.fail_subscribe_limit
     else:
         channel_stub = get_channel_stub_by_channel_name(channel_name)
         signal = await channel_stub.async_task().wait_for_unregister_signal(peer_id)
         error_code = message_code.Response.fail_connect_to_leader
     if signal:
         await WSDispatcher.send_exception(
             ws, call_method,
             exception=ConnectionError("Unregistered"),
             error_code=error_code)
Пример #11
0
        async def _publish_new_block():
            nonlocal height

            channel_stub = get_channel_stub_by_channel_name(channel_name)
            try:
                while ws.open:
                    new_block: dict = json.loads(
                        await channel_stub.async_task().announce_new_block(subscriber_block_height=height)
                    )
                    Logger.debug(f"publish_new_block: {new_block}")
                    request = Request("node_ws_PublishNewBlock", block=new_block)

                    await ws.send(json.dumps(request))
                    height += 1
            except Exception as e:
                traceback.print_exc()
                raise e
Пример #12
0
 def __init__(self, channel_name: str, peer_id: str, remote_target: str):
     self._peer_id = peer_id
     self._remote_target = remote_target
     self._channel_stub = get_channel_stub_by_channel_name(channel_name)
Пример #13
0
    async def channel_unregister(ws, channel_name: str, peer_id: str):
        channel_stub = get_channel_stub_by_channel_name(channel_name)
        await channel_stub.async_task().unregister_subscriber(peer_id=peer_id)

        Logger.debug(f"unregister subscriber: {peer_id}")
Пример #14
0
 async def node_getCitizens(**kwargs):
     channel = kwargs['context']['channel']
     channel_stub = get_channel_stub_by_channel_name(channel)
     citizens: List[Dict[
         str, str]] = await channel_stub.async_task().get_citizens()
     return {"citizens": citizens}
Пример #15
0
 async def node_getCitizens(context: Dict[str, str], **kwargs):
     channel = context.get('channel')
     channel_stub = get_channel_stub_by_channel_name(channel)
     citizens: List[Dict[
         str, str]] = await channel_stub.async_task().get_citizens()
     return {"citizens": citizens}