Ejemplo n.º 1
0
def get_tx_reference_address_db(address):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(TxReference_db, alias) as TxReference_db_switch:
                tx_reference_docs = TxReference_db_switch.objects(address=address)
                if len(tx_reference_docs) > 0:
                    return tx_reference_docs
                else:
                    raise DatabaseQueryFailed("Not found! Database query for address: {}".format(address.hex()))
    except (CursorNotFound):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for address: {}".format(address.hex()))
Ejemplo n.º 2
0
def get_tx_reference_address_db(address):
    try:
        tx_reference_docs = TxReference_db.objects(address=address)
        if len(tx_reference_docs) > 0:
            return tx_reference_docs
        else:
            raise DatabaseQueryFailed(
                "Not found! Database query for address: {}".format(
                    address.hex()))
    except (CursorNotFound):
        raise DatabaseQueryFailed(
            "Database query failed for address: {}".format(address.hex()))
Ejemplo n.º 3
0
def get_block_doc_block_height(block_height):
    try:
        block_doc = Block_db.objects(block_height=block_height)[0]
        return block_doc
    except (CursorNotFound, IndexError):
        raise DatabaseQueryFailed(
            "Database query failed for block height: {}".format(block_height))
Ejemplo n.º 4
0
def get_latest_blocks_db():
    try:
        blocks = Block_db.objects.order_by("-block_height")[:10]
        return blocks
    except (CursorNotFound, IndexError):
        raise DatabaseQueryFailed(
            "Database query failed for get latest blocks")
Ejemplo n.º 5
0
def del_tx_reference_bulk(bundle):
    with Mongo_Wrapper() as (alias, db_client):
        with switch_db(TxReference_db, alias) as TxReference_db_switch:
            for b in bundle:
                address, tx_reference, log_handler = b
                tx_hash = bytes(tx_reference[0])
                tx_index = tx_reference[1]

                if log_handler is not None:
                    log_handler("[-] Attempting to delete tx reference: {}:{}".format(tx_hash.hex(), tx_index))

                try:
                    tx_reference_docs = TxReference_db_switch.objects(tx_hash=tx_hash, tx_index=tx_index)
                except (CursorNotFound):
                    disconnect(alias)
                    raise DatabaseQueryFailed("Database query failed for tx reference {}:{} with address: {}".format(tx_hash.hex(), tx_index, address.hex()))

                if len(tx_reference_docs) == 1:
                    tx_reference_doc = tx_reference_docs[0]
                    if log_handler is not None:
                        log_handler("[-] Tx reference {}:{} is already added to database".format(tx_hash.hex(), tx_index))
                    tx_reference_doc.delete()
                    if log_handler is not None:
                        log_handler("[-] Tx reference {}:{} deleted".format(tx_hash.hex(), tx_index))
                else:
                    if log_handler is not None:
                        log_handler("[-] Tx reference {}:{} is not in database".format(tx_hash.hex(), tx_index))
Ejemplo n.º 6
0
def get_block_doc_from_block_height(block_height):
    try:
        block_docs = Block_db.objects(block_height__gte=block_height)
        return block_docs
    except (CursorNotFound):
        raise DatabaseQueryFailed(
            "Database query failed for from block height: {}".format(
                block_height))
Ejemplo n.º 7
0
def get_block_doc_block_hash(block_hash):
    try:
        block_doc = Block_db.objects(block_hash=block_hash)[0]
        return block_doc
    except (CursorNotFound, IndexError):
        raise DatabaseQueryFailed(
            "Database query failed for block hash: {}".format(
                block_hash.hex()))
Ejemplo n.º 8
0
def delete_all_block_db():
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                Block_db_switch.objects().all().delete()
    except (CursorNotFound):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for delete all blocks")
Ejemplo n.º 9
0
def get_tx_reference(tx_hash, tx_index):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(TxReference_db, alias) as TxReference_db_switch:
                tx_reference_doc = TxReference_db_switch.objects(tx_hash=tx_hash, tx_index=tx_index)
                return tx_reference_doc
    except (CursorNotFound):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for tx reference: {}:{}".format(tx_hash.hex(), tx_index))
Ejemplo n.º 10
0
def get_latest_blocks_db():
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                blocks = Block_db_switch.objects.order_by("-block_height")[:10]
                return blocks
    except (CursorNotFound, IndexError):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for get latest blocks")
Ejemplo n.º 11
0
def get_block_doc_block_height(block_height):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                block_doc = Block_db_switch.objects(block_height=block_height)[0]
                return block_doc
    except (CursorNotFound, IndexError):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for block height: {}".format(block_height))
Ejemplo n.º 12
0
def get_block_doc_from_block_height(block_height):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                block_docs = Block_db_switch.objects(block_height__gte=block_height)
                return block_docs
    except (CursorNotFound):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for from block height: {}".format(block_height))
Ejemplo n.º 13
0
def get_block_doc_tx_hash(tx_hash):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                block_doc = Block_db_switch.objects(txs__tx_hash=tx_hash)[0]
                return block_doc
    except (CursorNotFound, IndexError):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for block at tx hash: {}".format(tx_hash.hex()))
Ejemplo n.º 14
0
def check_tx_reference_db():
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(TxReference_db, alias) as TxReference_db_switch:
                tx_reference_docs = TxReference_db_switch.objects()
                return tx_reference_docs
    except (CursorNotFound, IndexError):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for address: {}".format(address.hex()))
Ejemplo n.º 15
0
def process_block(working_block):
    try:
        logging.info("[-] Validating block hash: {}".format(working_block.hash().hex()))
        block_doc = get_block_doc(working_block)
        return working_block.txs, block_doc
    except AssertionError as assertion_error:
        raise ProcessBlockFailed(assertion_error)
    except DatabaseQueryFailed as database_query_error:
        raise DatabaseQueryFailed(database_query_error)
Ejemplo n.º 16
0
def get_tx_reference(tx_hash, tx_index):
    try:
        tx_reference_doc = TxReference_db.objects(tx_hash=tx_hash,
                                                  tx_index=tx_index)
        return tx_reference_doc
    except (CursorNotFound):
        raise DatabaseQueryFailed(
            "Database query failed for tx reference: {}:{}".format(
                tx_hash.hex(), tx_index))
Ejemplo n.º 17
0
def get_block_doc_tx_in_hash_prev_index(tx_hash, tx_index):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                #block_doc = Block_db_switch.objects(txs__tx_ins__match={"prev_tx": tx_hash, "prev_index": tx_index})
                block_doc = Block_db.objects(Q(txs__tx_hash__ne=tx_hash) & Q(txs__tx_ins__match={"prev_tx": tx_hash, "prev_index": tx_index}))
                return block_doc
    except (CursorNotFound):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for block at tx hash: {}".format(tx_hash.hex()))
Ejemplo n.º 18
0
def get_tx_db(tx_hash):
    try:
        with Mongo_Wrapper() as (alias, db_client):
            with switch_db(Block_db, alias) as Block_db_switch:
                txs = Block_db_switch.objects(txs__tx_hash=tx_hash).only("txs")[0].txs
                tx = list(filter(lambda tx: tx.tx_hash == tx_hash, txs))[0]
                return tx
    except (CursorNotFound, IndexError):
        disconnect(alias)
        raise DatabaseQueryFailed("Database query failed for tx_hash: {}".format(tx_hash.hex()))
Ejemplo n.º 19
0
def get_block_doc_tx_in_hash_prev_index(tx_hash, tx_index):
    try:
        block_doc = Block_db.objects(txs__tx_ins__match={
            "prev_tx": tx_hash,
            "prev_index": tx_index
        })
        return block_doc
    except (CursorNotFound):
        raise DatabaseQueryFailed(
            "Database query failed for block at tx hash: {}".format(
                tx_hash.hex()))
Ejemplo n.º 20
0
def get_tx_db(tx_hash):
    try:
        block = Block_db.objects(txs__tx_hash=tx_hash)[0]
        txs = block.txs
        tx = list(filter(lambda tx: tx.tx_hash == tx_hash, txs))[0]
        tx = TxDetail(block_hash=block.block_hash,
                      block_height=block.block_height,
                      timestamp=block.timestamp,
                      segwit=tx.segwit,
                      tx_hash=tx.tx_hash,
                      version=tx.version,
                      tx_ins=tx.tx_ins,
                      tx_outs=tx.tx_outs,
                      locktime=tx.locktime,
                      size=tx.size)
        return tx
    except (CursorNotFound, IndexError):
        raise DatabaseQueryFailed(
            "Database query failed for tx_hash: {}".format(tx_hash.hex()))
Ejemplo n.º 21
0
def del_block(block_hash, log_handler=None):
    with Mongo_Wrapper() as (alias, db_client):
        with switch_db(Block_db, alias) as Block_db_switch:
            try:
                blocks_to_delete = Block_db_switch.objects(block_hash=block_hash)
            except (CursorNotFound):
                disconnect(alias)
                raise DatabaseQueryFailed("Database query failed for get block hashes")

            if log_handler is not None:
                log_handler("[-] Attempting to delete block with hash: {} from database".format(block_hash.hex()))
            if len(blocks_to_delete) == 1:
                if log_handler is not None:
                    log_handler("[-] Block is already added to database. Deleting")
                block_to_delete = blocks_to_delete[0]
                block_to_delete.delete()
                if log_handler is not None:
                    log_handler("[-] Block deleted")
            else:
                if log_handler is not None:
                    log_handler("[-] Block is not in database.")
    def get_update_attribute_stream(self, event_id, ids):
        logging.info(
            "[-] Incoming RPC request - get_attribute_stream: {} items".format(
                len(ids)))
        for id in ids:
            if event_id in operation_events.keys(
            ) and operation_events[event_id].is_set():
                try:
                    result = process_value(id)
                    if result:
                        if result[0] == "block_height":
                            block_height = result[1]
                            block_doc = get_block_doc_block_height(
                                block_height)
                            block_doc = json.loads(block_doc.to_json())
                            result = {"type": "block", "data": block_doc}
                            result = sanitize(result)
                            logging.info(
                                "[-] Successful Response: Incoming RPC request - get_attribute_stream: {} items"
                                .format(len(ids)))
                            logging.debug(result)
                            yield json.dumps(result)
                            continue
                        if result[0] == "hash":
                            hash = result[1]
                            try:
                                tx_reference_docs = get_tx_reference_address_db(
                                    hash)
                                tx_references = [(doc.tx_hash, doc.tx_index)
                                                 for doc in tx_reference_docs]
                                utxos = [{
                                    "tx_hash": ref[0].hex(),
                                    "tx_index": ref[1]
                                } for ref in tx_references]
                                tx_docs = [
                                    get_tx_db(doc[0]) for doc in tx_references
                                ]

                                total_balance = 0
                                for i in range(0, len(tx_references)):
                                    tx_hash = tx_references[i][0]
                                    tx_index = tx_references[i][1]
                                    tx = tx_docs[i]
                                    if tx.tx_hash == tx_hash:
                                        total_balance += tx.tx_outs[
                                            tx_index].amount

                                tx_docs = [
                                    json.loads(doc.to_json())
                                    for doc in tx_docs
                                ]
                                result = {
                                    "type": "address",
                                    "data": {
                                        "address_b58":
                                        id,
                                        "address_raw":
                                        tx_reference_docs[0]["address"].hex(),
                                        "total_balance":
                                        total_balance,
                                        "utxos":
                                        utxos,
                                        "txs":
                                        tx_docs
                                    }
                                }
                                result = sanitize(result)
                                logging.info(
                                    "[-] Successful Response: Incoming RPC request - get_attribute_stream: {} items"
                                    .format(len(ids)))
                                logging.debug(result)
                                yield json.dumps(result)
                                continue
                            except (DatabaseQueryFailed,
                                    IndexError) as fetch_address_err:
                                err = [str(fetch_address_err)]
                                try:
                                    tx_doc = get_tx_db(hash)
                                    tx_doc = json.loads(tx_doc.to_json())
                                    result = {"type": "tx", "data": tx_doc}
                                    result = sanitize(result)
                                    logging.info(
                                        "[-] Successful Response: Incoming RPC request - get_attribute_stream: {} items"
                                        .format(len(ids)))
                                    logging.debug(result)
                                    yield json.dumps(result)
                                    continue
                                except (DatabaseQueryFailed,
                                        IndexError) as fetch_tx_err:
                                    err.append(str(fetch_tx_err))
                                    try:
                                        block_doc = get_block_doc_block_hash(
                                            hash)
                                        block_doc = json.loads(
                                            block_doc.to_json())
                                        result = {
                                            "type": "block",
                                            "data": block_doc
                                        }
                                        result = sanitize(result)
                                        logging.info(
                                            "[-] Successful Response: Incoming RPC request - get_attribute_stream: {} items"
                                            .format(len(ids)))
                                        logging.debug(result)
                                        yield json.dumps(result)
                                        continue
                                    except (DatabaseQueryFailed,
                                            IndexError) as fetch_block_err:
                                        err.append(str(fetch_block_err))
                                        raise DatabaseQueryFailed(err)
                    result = {
                        "type": "error",
                        "data": "passed value format is not correct"
                    }
                    logging.info(result)
                    yield json.dumps(result)
                    continue
                except DatabaseQueryFailed as err:
                    result = {"type": "error", "data": err.args[0]}
                    logging.info(result)
                    yield json.dumps(result)
                    continue
            else:
                break
    def get_new_attribute(self, id):
        logging.info("[-] Incoming RPC request - get_attribute: {}".format(id))
        try:
            result = process_value(id)
            if result:
                clear_events()
                current_event = Event()
                current_event.set()
                current_event_id = str(uuid.uuid4())
                operation_events[current_event_id] = current_event

                if result[0] == "block_height":
                    block_height = result[1]
                    block_doc = get_block_doc_block_height(block_height)
                    block_doc = json.loads(block_doc.to_json())
                    result = {
                        "event_id": current_event_id,
                        "type": "block",
                        "data": block_doc
                    }
                    result = sanitize(result)
                    logging.info(
                        "[-] Successful Response: Incoming RPC request - get_attribute: {}"
                        .format(id))
                    logging.debug(result)
                    return json.dumps(result)
                if result[0] == "hash":
                    hash = result[1]
                    try:
                        tx_reference_docs = get_tx_reference_address_db(hash)
                        tx_references = [(doc.tx_hash, doc.tx_index)
                                         for doc in tx_reference_docs]
                        utxos = [{
                            "tx_hash": ref[0].hex(),
                            "tx_index": ref[1]
                        } for ref in tx_references]
                        tx_docs = [get_tx_db(doc[0]) for doc in tx_references]

                        total_balance = 0
                        for i in range(0, len(tx_references)):
                            tx_hash = tx_references[i][0]
                            tx_index = tx_references[i][1]
                            tx = tx_docs[i]
                            if tx.tx_hash == tx_hash:
                                total_balance += tx.tx_outs[tx_index].amount

                        tx_docs = [
                            json.loads(doc.to_json()) for doc in tx_docs
                        ]
                        result = {
                            "event_id": current_event_id,
                            "type": "address",
                            "data": {
                                "address_b58":
                                id,
                                "address_raw":
                                tx_reference_docs[0]["address"].hex(),
                                "total_balance":
                                total_balance,
                                "utxos":
                                utxos,
                                "txs":
                                tx_docs
                            }
                        }
                        result = sanitize(result)
                        logging.info(
                            "[-] Successful Response: Incoming RPC request - get_attribute: {}"
                            .format(id))
                        logging.debug(result)
                        return json.dumps(result)
                    except (DatabaseQueryFailed,
                            IndexError) as fetch_address_err:
                        err = [str(fetch_address_err)]
                        try:
                            tx_doc = get_tx_db(hash)
                            tx_doc = json.loads(tx_doc.to_json())
                            result = {
                                "event_id": current_event_id,
                                "type": "tx",
                                "data": tx_doc
                            }
                            result = sanitize(result)
                            logging.info(
                                "[-] Successful Response: Incoming RPC request - get_attribute: {}"
                                .format(id))
                            logging.debug(result)
                            return json.dumps(result)
                        except (DatabaseQueryFailed,
                                IndexError) as fetch_tx_err:
                            err.append(str(fetch_tx_err))
                            try:
                                block_doc = get_block_doc_block_hash(hash)
                                block_doc = json.loads(block_doc.to_json())
                                result = {
                                    "event_id": current_event_id,
                                    "type": "block",
                                    "data": block_doc
                                }
                                result = sanitize(result)
                                logging.info(
                                    "[-] Successful Response: Incoming RPC request - get_attribute: {}"
                                    .format(id))
                                logging.debug(result)
                                return json.dumps(result)
                            except (DatabaseQueryFailed,
                                    IndexError) as fetch_block_err:
                                err.append(str(fetch_block_err))
                                raise DatabaseQueryFailed(err)
            result = {
                "type": "error",
                "data": "passed value format is not correct"
            }
            logging.info(result)
            return json.dumps(result)
        except DatabaseQueryFailed as err:
            result = {"type": "error", "data": err.args[0]}
            logging.info(result)
            return json.dumps(result)
Ejemplo n.º 24
0
def get_latest_block_db():
    try:
        block = Block_db.objects.order_by("-block_height").first()
        return block
    except (CursorNotFound):
        raise DatabaseQueryFailed("Database query failed for get latest block")