async def sub(**kwargs):
    a = kwargs.get("a", None)
    b = kwargs.get("b", None)

    log.debug(f"sub({a},{b})")

    if a is None:
        raise InvalidParams("\"a\" is required")

    if b is None:
        raise InvalidParams("\"b\" is required")

    if type(a) is str:
        if a.isdigit():
            a = int(a)
        else:
            raise InvalidParams("\"a\" must be a number")

    if type(b) is str:
        if b.isdigit():
            b = int(b)
        else:
            raise InvalidParams("\"b\" must be a number")

    result = a - b
    return {"result": result}
async def find_face(**kwargs):
    image = kwargs.get("image", None)
    algorithm = kwargs.get("algorithm", "dlib_cnn")

    if image is None:
        raise InvalidParams("image is required")

    face_detector = get_detector(algorithm)
    if face_detector is None:
        raise InvalidParams("unknown algorithm")

    binary_image = base64.b64decode(image)
    img_data = io.BytesIO(binary_image)
    img = ioimg.imread(img_data)

    # Drop alpha channel if it exists
    if img.shape[-1] == 4:
        img = img[:, :, :3]
        log.debug("Dropping alpha channel from image")

    dets = face_detect(img, face_detector, algorithm)

    faces = []
    for d in dets:
        faces.append(dict(x=d.left(), y=d.top(), w=d.right() - d.left(), h=d.bottom() - d.top()))
    return {'faces': faces}
Beispiel #3
0
        async def blockchain_passthrough(job_address=None,
                                         job_signature=None,
                                         **kwargs):
            job_address = self.chain.to_checksum_address(job_address)

            if job_address is None:
                logger.error("invalid request: job_address is required")
                raise InvalidParams("invalid request: job_address is required")

            if job_signature is None:
                logger.error("invalid request: job_signature is required")
                raise InvalidParams(
                    "invalid request: job_signature is required")

            if await self.chain.validate_job_invocation(
                    job_address, job_signature):
                logger.debug("dispatching request to service; job_address: %s",
                             job_address)
                response = client.request(config.PASSTHROUGH_ENDPOINT, method,
                                          **kwargs)
                db = self.app["db"]
                job_entry = db.get(job_address, {})
                job_entry["job_signature"] = job_signature
                job_entry["completed"] = True
                logger.debug("saving job to db; job_address: %s; db entry: %s",
                             job_address, job_entry)
                db[job_address] = job_entry
                self.app.loop.create_task(
                    self.chain.complete_job(job_address, job_signature))
                logger.debug("returning response to client; job_address: %s",
                             job_address)
                return response
            else:
                logger.error("job invocation failed to validate")
                raise ServerError("job invocation failed to validate")
Beispiel #4
0
    async def sendTransaction(self, **data):
        def get_data_default(key, decoder, default=None):
            if key in data:
                return decoder(data[key])
            return default

        to = get_data_default("to", recipient_decoder, b"")
        startgas = get_data_default("gas", quantity_decoder, DEFAULT_STARTGAS)
        gasprice = get_data_default("gasPrice", quantity_decoder,
                                    DEFAULT_GASPRICE)
        gas_token_id = get_data_default("gasTokenId", quantity_decoder, 0)
        value = get_data_default("value", quantity_decoder, 0)
        transfer_token_id = get_data_default("transfer_token_id",
                                             quantity_decoder, 0)
        data_ = get_data_default("data", data_decoder, b"")
        v = get_data_default("v", quantity_decoder, 0)
        r = get_data_default("r", quantity_decoder, 0)
        s = get_data_default("s", quantity_decoder, 0)
        nonce = get_data_default("nonce", quantity_decoder, None)

        to_full_shard_id = get_data_default("toFullShardId",
                                            full_shard_id_decoder, None)
        from_full_shard_id = get_data_default("fromFullShardId",
                                              full_shard_id_decoder, None)
        network_id = get_data_default(
            "networkId", quantity_decoder,
            self.master.env.quark_chain_config.NETWORK_ID)

        if nonce is None:
            raise InvalidParams("Missing nonce")
        if not (v and r and s):
            raise InvalidParams("Missing v, r, s")
        if from_full_shard_id is None:
            raise InvalidParams("Missing fromFullShardId")

        if to_full_shard_id is None:
            to_full_shard_id = from_full_shard_id

        evm_tx = EvmTransaction(
            nonce,
            gasprice,
            startgas,
            gas_token_id,
            to,
            value,
            transfer_token_id,
            data_,
            v,
            r,
            s,
            from_full_shard_id=from_full_shard_id,
            to_full_shard_id=to_full_shard_id,
            network_id=network_id,
        )
        tx = Transaction(code=Code.create_evm_code(evm_tx))
        success = await self.master.add_transaction(tx)
        if not success:
            return None

        return id_encoder(tx.get_hash(), from_full_shard_id)
Beispiel #5
0
def data_decoder(hex_str):
    """Decode `hexStr` representing unformatted hex_str."""
    if not hex_str.startswith("0x"):
        raise InvalidParams("Invalid hex_str encoding")
    try:
        return bytes.fromhex(hex_str[2:])
    except Exception:
        raise InvalidParams("Invalid hex_str hex encoding")
Beispiel #6
0
    async def _call_or_estimate_gas(self, is_call: bool, **data):
        """ Returns the result of the transaction application without putting in block chain """
        if not isinstance(data, dict):
            raise InvalidParams("Transaction must be an object")

        def get_data_default(key, decoder, default=None):
            if key in data:
                return decoder(data[key])
            return default

        to = get_data_default("to", address_decoder, None)
        if to is None:
            raise InvalidParams("Missing to")

        to_full_shard_key = int.from_bytes(to[20:], "big")

        gas = get_data_default("gas", quantity_decoder, 0)
        gas_price = get_data_default("gasPrice", quantity_decoder, 0)
        value = get_data_default("value", quantity_decoder, 0)
        data_ = get_data_default("data", data_decoder, b"")
        sender = get_data_default("from", address_decoder,
                                  b"\x00" * 20 + to[20:])
        sender_address = Address.create_from(sender)
        gas_token_id = get_data_default(
            "gas_token_id", quantity_decoder,
            self.env.quark_chain_config.genesis_token)
        transfer_token_id = get_data_default(
            "transfer_token_id",
            quantity_decoder,
            self.env.quark_chain_config.genesis_token,
        )

        network_id = self.master.env.quark_chain_config.NETWORK_ID

        nonce = 0  # slave will fill in the real nonce
        evm_tx = EvmTransaction(
            nonce,
            gas_price,
            gas,
            to[:20],
            value,
            data_,
            from_full_shard_key=sender_address.full_shard_key,
            to_full_shard_key=to_full_shard_key,
            network_id=network_id,
            gas_token_id=gas_token_id,
            transfer_token_id=transfer_token_id,
        )

        tx = TypedTransaction(SerializedEvmTransaction.from_evm_tx(evm_tx))
        if is_call:
            res = await self.master.execute_transaction(
                tx, sender_address, data["block_height"])
            return data_encoder(res) if res is not None else None
        else:  # estimate gas
            res = await self.master.estimate_gas(tx, sender_address)
            return quantity_encoder(res) if res is not None else None
Beispiel #7
0
    async def sendUnsigedTransaction(self, **data):
        """ Returns the unsigned hash of the evm transaction """
        if not isinstance(data, dict):
            raise InvalidParams("Transaction must be an object")

        def get_data_default(key, decoder, default=None):
            if key in data:
                return decoder(data[key])
            return default

        nonce = get_data_default("nonce", quantity_decoder, None)
        to = get_data_default("to", recipient_decoder, b"")
        startgas = get_data_default("gas", quantity_decoder, DEFAULT_STARTGAS)
        gasprice = get_data_default("gasPrice", quantity_decoder,
                                    DEFAULT_GASPRICE)
        value = get_data_default("value", quantity_decoder, 0)
        data_ = get_data_default("data", data_decoder, b"")

        from_full_shard_id = get_data_default("fromFullShardId",
                                              full_shard_id_decoder, None)
        to_full_shard_id = get_data_default("toFullShardId",
                                            full_shard_id_decoder, None)

        if nonce is None:
            raise InvalidParams("nonce is missing")
        if from_full_shard_id is None:
            raise InvalidParams("fromFullShardId is missing")

        if to_full_shard_id is None:
            to_full_shard_id = from_full_shard_id

        evm_tx = EvmTransaction(
            nonce,
            gasprice,
            startgas,
            to,
            value,
            data_,
            from_full_shard_id=from_full_shard_id,
            to_full_shard_id=to_full_shard_id,
            network_id=self.master.env.quark_chain_config.NETWORK_ID,
        )

        return {
            "txHashUnsigned": data_encoder(evm_tx.hash_unsigned),
            "nonce": quantity_encoder(evm_tx.nonce),
            "to": data_encoder(evm_tx.to),
            "fromFullShardId":
            full_shard_id_encoder(evm_tx.from_full_shard_id),
            "toFullShardId": full_shard_id_encoder(evm_tx.to_full_shard_id),
            "value": quantity_encoder(evm_tx.value),
            "gasPrice": quantity_encoder(evm_tx.gasprice),
            "gas": quantity_encoder(evm_tx.startgas),
            "data": data_encoder(evm_tx.data),
            "networkId": quantity_encoder(evm_tx.network_id),
        }
Beispiel #8
0
def quantity_decoder(hex_str):
    """Decode `hexStr` representing a quantity."""
    # must start with "0x"
    if not hex_str.startswith("0x") or len(hex_str) < 3:
        raise InvalidParams("Invalid quantity encoding")

    try:
        return int(hex_str, 16)
    except ValueError:
        raise InvalidParams("Invalid quantity encoding")
Beispiel #9
0
async def classify(**kwargs):
    image = kwargs.get("image", None)
    image_type = kwargs.get("image_type", None)

    if image is None:
        raise InvalidParams("image is required")

    if image_type is None:
        raise InvalidParams("image_type is required")

    binary_image = base64.b64decode(image)
    if image_type == 'jpeg' or image_type == 'jpg':
        decoder_key = 'DecodeJpeg/contents:0'
    elif image_type == 'png':
        decoder_key = 'DecodeJpeg/contents:0'
    elif image_type == 'gif':
        raise RuntimeError("TensorflowImageNet - cannot decode gif images")
    elif image_type == 'bmp':
        raise RuntimeError("TensorflowImageNet - cannot decode bmp images")
    else:
        decoder_key = 'DecodeJpeg/contents:0'
        logger.warning("Missing image type {0}".format(image_type))

    logger.debug("classifying '{0}' image".format(image_type))

    raw_predictions = session.run(softmax_tensor, {decoder_key: binary_image})

    # Pull the predicted scorces out of the raw predictions.
    predicted_scores = raw_predictions[0]

    # Sort and strip off the top 5 predictions.
    top_predictions = predicted_scores.argsort()[-5:][::-1]
    image_predictions = []
    image_scores = []
    for predicted_node_id in top_predictions:
        # Get a text description for the top predicted node.
        description = node_lookup.id_to_string(predicted_node_id)

        # Cast to a float so JSON can serialize it. Normal Tensorflow float32 are not serializable.
        score = float(predicted_scores[predicted_node_id])

        logger.debug("        prediction = '{0}', score = {1}".format(
            description, score))

        # Add only those that exceed our minimum score to the predictions and scores lists.
        if score > config.MINIMUM_SCORE:
            image_predictions.append(description)
            image_scores.append(score)

    return {"predictions": image_predictions, "confidences": image_scores}
Beispiel #10
0
async def recognise_face(**kwargs):
    image = kwargs.get("image", None)
    face_bboxes = kwargs.get("faces", [])

    if image is None:
        raise InvalidParams("image is required")

    binary_image = base64.b64decode(image)
    img_data = io.BytesIO(binary_image)
    img = ioimg.imread(img_data)

    # Drop alpha channel if it exists
    if img.shape[-1] == 4:
        img = img[:, :, :3]
        log.debug("Dropping alpha channel from image")

    face_identities = []
    for b in face_bboxes:
        bbox = BoundingBox(**b)
        dlib_bbox = dlib.rectangle(bbox.x, bbox.y, bbox.x + bbox.w,
                                   bbox.y + bbox.h)

        detection_object = landmark_predictor(img, dlib_bbox)
        face_descriptor = facerec.compute_face_descriptor(
            img, detection_object, 10)

        face_identities.append([x for x in face_descriptor])

    return {'face_identities': face_identities}
async def get_landmarks(**kwargs):
    image = kwargs.get("image", None)
    lm = kwargs.get("landmark_model", "5")
    bboxes = kwargs.get("face_bboxes", [])

    if image is None:
        raise InvalidParams("image is required")

    binary_image = base64.b64decode(image)
    img_data = io.BytesIO(binary_image)
    img = ioimg.imread(img_data)

    # Drop alpha channel if it exists
    if img.shape[-1] == 4:
        img = img[:, :, :3]
        log.debug("Dropping alpha channel from image")

    face_landmarks = []

    for bbox in bboxes:
        bbox_pb = BoundingBox(**bbox)
        points = landmark_finder(img, bbox_pb, lm)
        face_landmarks.append({
            'landmark_model': lm,
            'points': [{
                'x': p.x,
                'y': p.y
            } for p in points]
        })

    return {'landmarks': face_landmarks}
Beispiel #12
0
def _get_arguments(request):
    """Takes the 'params' part of a JSON-RPC request and converts it to either
    positional or keyword arguments usable in Python. The value can be a JSON
    array (python list), object (python dict), or omitted. There are no other
    acceptable options. Note that a JSON-RPC request can have positional or
    keyword arguments, but not both! See
    http://www.jsonrpc.org/specification#parameter_structures

    :param request: JSON-RPC request in dict form.
    :raises InvalidParams: If 'params' was present but was not a list or dict.
    :returns: A tuple containing the positionals (in a list, or None) and
    keywords (in a dict, or None) extracted from the 'params' part of the
    request.
    """
    positionals = keywords = None
    params = request.get('params')
    # Params was omitted from the request. Taken as no arguments.
    if 'params' not in request:
        pass
    # Params is a list. Taken as positional arguments.
    elif isinstance(params, list):
        positionals = params
    # Params is a dict. Taken as keyword arguments.
    elif isinstance(params, dict):
        keywords = params
    # Anything else is invalid. (This should never happen if the request has
    # passed the schema validation.)
    else:
        raise InvalidParams('Params of type %s is not allowed' % \
            type(params).__name__)
    return (positionals, keywords)
Beispiel #13
0
 def add_subscriber(self, sub_type, sub_id, conn, extra=None):
     if sub_type not in self.subscribers:
         raise InvalidParams("Invalid subscription")
     self.subscribers[sub_type][sub_id] = conn
     if sub_type == SUB_LOGS:
         assert extra and isinstance(extra, Callable)
         self.log_filter_gen[sub_id] = extra
Beispiel #14
0
def _call(methods, method_name, args=None, kwargs=None):
    """Find a method from a list, then validate the arguments before calling it.

    :param methods: The list of methods - either a python list, or Methods obj.
    :param args: Positional arguments (list)
    :param kwargs: Keyword arguments (dict)
    :raises MethodNotFound: If the method is not in the list.
    :raises InvalidParams: If the arguments don't match the method signature.
    :returns: The return value from the method called.
    """
    # Get the method object from a list of rpc methods
    method = _get_method(methods, method_name)
    # Ensure the arguments match the method's signature
    _validate_arguments_against_signature(method, args, kwargs)
    # Call the method
    if args and kwargs:
        # Cannot have both positional and keyword arguments in JSON-RPC.
        raise InvalidParams()
    # No arguments
    elif not args and not kwargs:
        return method()
    # Positional arguments
    elif args:
        return method(*args)
    # Keyword arguments
    elif kwargs:
        return method(**kwargs)
Beispiel #15
0
def signature_decoder(hex_str):
    """Decode a block signature."""
    if not hex_str:
        return None
    decoded = data_decoder(hex_str)
    if len(decoded) != 65:
        raise InvalidParams("Signature must be 65 bytes long")
    return decoded
Beispiel #16
0
 def remove_subscriber(self, sub_id):
     for sub_type, subscriber_dict in self.subscribers.items():
         if sub_id in subscriber_dict:
             del subscriber_dict[sub_id]
             if sub_type == SUB_LOGS:
                 del self.log_filter_gen[sub_id]
             return
     raise InvalidParams("subscription not found")
Beispiel #17
0
async def classify(**kwargs):
    image = kwargs.get("image", None)
    image_type = kwargs.get("image_type", None)
    if image is None:
        raise InvalidParams("image is required")
    if image_type is None:
        raise InvalidParams("image type is required")
    binary_image = base64.b64decode(image)
    tmp_file_name = tempfile.NamedTemporaryFile()
    tmp_file_name.write(binary_image)
    model.args.path = tmp_file_name.name
    bounding_boxes, emotions = model.predict()

    return {
        "bounding boxes": str(bounding_boxes),
        "predictions": str(emotions)
    }
Beispiel #18
0
def eth_address_to_quarkchain_address_decoder(hex_str):
    eth_hex = hex_str[2:]
    if len(eth_hex) != 40:
        raise InvalidParams("Addresses must be 40 or 0 bytes long")
    full_shard_id_hex = ""
    for i in range(4):
        index = i * 10
        full_shard_id_hex += eth_hex[index:index + 2]
    return address_decoder("0x" + eth_hex + full_shard_id_hex)
Beispiel #19
0
async def detect(**kwargs):
    model = kwargs.get("model", "yolov3")
    confidence = kwargs.get("confidence", "0.7")
    img_path = kwargs.get("img_path", None)
    if img_path is None:
        raise InvalidParams("\"img_path\" is required")
    log.debug("detect({},{},{})".format(model, confidence, len(img_path)))
    objd = ObjectDetector(model, confidence, map_names, img_path)

    return {"result": objd.detect()}
Beispiel #20
0
 def test_with_id(self):
     response = ExceptionResponse(InvalidParams(), 1)
     self.assertEqual(
         {
             'jsonrpc': '2.0',
             'error': {
                 'code': -32602,
                 'message': 'Invalid params'
             },
             'id': 1
         }, response)
async def dogs(**kwargs):
    log.debug("dogs({})".format(kwargs))
    model = kwargs.get("model", "ResNet18")
    map_names = dogs_map_names
    img_path = kwargs.get("img_path", None)
    if img_path is None:
        raise InvalidParams("\"img_path\" is required")
    image_dims = (3, 224, 224)

    result = img_recon.image_recognition("dogs", model, map_names, img_path, image_dims)
    return {"result": result}
Beispiel #22
0
 def test_jsonrpcservererror(self):
     response = ExceptionResponse(InvalidParams(), None)
     self.assertEqual(
         {
             'jsonrpc': '2.0',
             'error': {
                 'code': -32602,
                 'message': 'Invalid params'
             },
             'id': None
         }, response)
Beispiel #23
0
async def summarise(**kwargs):
    text = kwargs.get("text", None)

    if text is None:
        raise InvalidParams("text is required")

    from multiprocessing import Pool
    global config
    with Pool(1) as p:
        result = p.apply(summarise_text, (text, ))

    return {'summary': result}
Beispiel #24
0
async def translate(**kwargs):
    text = kwargs.get("text", None)
    source = kwargs.get("source", None)
    target = kwargs.get("target", None)

    if text is None:
        raise InvalidParams("text param is required")

    if source not in translations:
        raise InvalidParams("source param must be one of", translations.keys())

    if target not in translations[source]:
        raise InvalidParams("target param must be one of",
                            translations[source].keys())

    from multiprocessing import Pool
    global config
    with Pool(1) as p:
        result = p.apply(translate_text, (text, source, target))

    return {'summary': result}
Beispiel #25
0
 def test_jsonrpcservererror(self):
     response = ExceptionResponse(InvalidParams(), None)
     self.assertEqual(
         {
             "jsonrpc": "2.0",
             "error": {
                 "code": -32602,
                 "message": "Invalid params"
             },
             "id": None,
         },
         response,
     )
Beispiel #26
0
 def test_with_id(self):
     response = ExceptionResponse(InvalidParams(), 1)
     self.assertEqual(
         {
             "jsonrpc": "2.0",
             "error": {
                 "code": -32602,
                 "message": "Invalid params"
             },
             "id": 1,
         },
         response,
     )
Beispiel #27
0
 def test_with_data(self):
     config.debug = True
     response = ExceptionResponse(InvalidParams('Password missing'), 1)
     self.assertEqual(
         {
             'jsonrpc': '2.0',
             'error': {
                 'code': -32602,
                 'message': 'Invalid params',
                 'data': 'Password missing'
             },
             'id': 1
         }, response)
Beispiel #28
0
async def classify(**kwargs):
    image = kwargs.get("image", None)
    image_type = kwargs.get("image_type", None)
    if image is None:
        raise InvalidParams("image is required")
    if image_type is None:
        raise InvalidParams("image type is required")
    binary_image = base64.b64decode(image)
    # this requires that we save the file.
    current_files = os.listdir('tmp')
    tmp_file_name = 'tmp/tmp_' + str(random.randint(
        0, 100000000000)) + '_.' + str(image_type)
    while tmp_file_name in current_files:
        tmp_file_name = 'tmp/tmp_' + str(random.randint(
            0, 100000000000)) + '_.' + str(image_type)
    with open(tmp_file_name, 'wb') as f:
        f.write(binary_image)
    model.args.path = tmp_file_name
    bounding_boxes, emotions = model.predict()
    return {
        "bounding boxes": str(bounding_boxes),
        "predictions": str(emotions)
    }
async def classify(**kwargs):
    image = kwargs.get("image", None)
    image_type = kwargs.get("image_type", None)
    if image is None:
        raise InvalidParams("image is required")
    if image_type is None:
        raise InvalidParams("image type is required")

    binary_image = base64.b64decode(image)

    with Pool(1) as p:
        bounding_boxes, emotions = p.apply(_classify, (binary_image, ))

    return {
        "bounding boxes": [
            dict(x=d.left(),
                 y=d.top(),
                 w=d.right() - d.left(),
                 h=d.bottom() - d.top()) for d in bounding_boxes
        ],
        "predictions":
        emotions
    }
Beispiel #30
0
 async def getMinorBlockByHeight(
     self, shard: int, height=None, include_transactions=False
 ):
     shard_size = self.master.get_shard_size()
     if height is not None:
         height = quantity_decoder(height)
     if shard >= shard_size:
         raise InvalidParams(
             "shard is larger than shard size {} > {}".format(shard, shard_size)
         )
     branch = Branch.create(shard_size, shard)
     block = await self.master.get_minor_block_by_height(height, branch)
     if not block:
         return None
     return minor_block_encoder(block, include_transactions)