예제 #1
0
    async def batch_broadcast_xshard_tx_list(
        self,
        block_hash_to_xshard_list_and_prev_root_height: Dict[bytes, Tuple[List, int]],
        source_branch: Branch,
    ):
        branch_to_add_xshard_tx_list_request_list = dict()
        for (
            block_hash,
            x_shard_list_and_prev_root_height,
        ) in block_hash_to_xshard_list_and_prev_root_height.items():
            xshard_tx_list = x_shard_list_and_prev_root_height[0]
            prev_root_height = x_shard_list_and_prev_root_height[1]
            branch_to_add_xshard_tx_list_request = self.__get_branch_to_add_xshard_tx_list_request(
                block_hash, xshard_tx_list, prev_root_height
            )
            for branch, request in branch_to_add_xshard_tx_list_request.items():
                if branch == source_branch or not is_neighbor(
                    branch,
                    source_branch,
                    len(
                        self.env.quark_chain_config.get_initialized_full_shard_ids_before_root_height(
                            prev_root_height
                        )
                    ),
                ):
                    check(
                        len(request.tx_list.tx_list) == 0,
                        "there shouldn't be xshard list for non-neighbor shard ({} -> {})".format(
                            source_branch.value, branch.value
                        ),
                    )
                    continue

                branch_to_add_xshard_tx_list_request_list.setdefault(branch, []).append(
                    request
                )

        rpc_futures = []
        for branch, request_list in branch_to_add_xshard_tx_list_request_list.items():
            if branch in self.shards:
                for request in request_list:
                    self.shards[
                        branch
                    ].state.add_cross_shard_tx_list_by_minor_block_hash(
                        request.minor_block_hash, request.tx_list
                    )

            batch_request = BatchAddXshardTxListRequest(request_list)
            for (
                slave_conn
            ) in self.slave_connection_manager.get_connections_by_full_shard_id(
                branch.get_full_shard_id()
            ):
                future = slave_conn.write_rpc_request(
                    ClusterOp.BATCH_ADD_XSHARD_TX_LIST_REQUEST, batch_request
                )
                rpc_futures.append(future)
        responses = await asyncio.gather(*rpc_futures)
        check(all([response.error_code == 0 for _, response, _ in responses]))
예제 #2
0
    async def batch_broadcast_xshard_tx_list(
        self,
        block_hash_to_xshard_list_and_prev_root_height: Dict[bytes, Tuple[List, int]],
        source_branch: Branch,
    ):
        branch_to_add_xshard_tx_list_request_list = dict()
        for (
            block_hash,
            x_shard_list_and_prev_root_height,
        ) in block_hash_to_xshard_list_and_prev_root_height.items():
            xshard_tx_list = x_shard_list_and_prev_root_height[0]
            prev_root_height = x_shard_list_and_prev_root_height[1]
            branch_to_add_xshard_tx_list_request = self.__get_branch_to_add_xshard_tx_list_request(
                block_hash, xshard_tx_list, prev_root_height
            )
            for branch, request in branch_to_add_xshard_tx_list_request.items():
                if branch == source_branch or not is_neighbor(branch, source_branch):
                    continue

                branch_to_add_xshard_tx_list_request_list.setdefault(branch, []).append(
                    request
                )

        rpc_futures = []
        for branch, request_list in branch_to_add_xshard_tx_list_request_list.items():
            check(is_neighbor(branch, source_branch))

            if branch in self.shards:
                for request in request_list:
                    self.shards[
                        branch
                    ].state.add_cross_shard_tx_list_by_minor_block_hash(
                        request.minor_block_hash, request.tx_list
                    )

            batch_request = BatchAddXshardTxListRequest(request_list)
            for slave_conn in self.slave_connection_manager.get_connections_by_shard(
                branch.get_shard_id()
            ):
                future = slave_conn.write_rpc_request(
                    ClusterOp.BATCH_ADD_XSHARD_TX_LIST_REQUEST, batch_request
                )
                rpc_futures.append(future)
        responses = await asyncio.gather(*rpc_futures)
        check(all([response.error_code == 0 for _, response, _ in responses]))