def masternode_place_image_data_in_chunkstorage(regticket,
                                                regticket_image_data):
    """
    Place image to the chunkstorage. Initially artwork is placed to the so called temporary storage
    (which exist only on the given masternode and is not distributed to other).
    After activation ticket will be created (by the wallet) and parsed by current masternode  -
    masternode will move artwork chunks to regular chunkstorage and start promote chunks to
    other masternodes, so artwork will be stored distributedly.
    """
    # reconstruct image with seeds from regticket.
    # then and only then chunk set will be identical to what wallet generated (and which hashes
    # are written to the regticket.lubyhashes).
    imagedata = ImageData(
        dictionary={
            "image":
            regticket_image_data,
            "lubychunks":
            ImageData.generate_luby_chunks(regticket_image_data,
                                           seeds=regticket.lubyseeds),
            "thumbnail":
            ImageData.generate_thumbnail(regticket_image_data),
        })
    artwork_hash = imagedata.get_artwork_hash()
    # store thumbnail
    get_chunkmanager().store_chunk_in_temp_storage(
        bytes_to_chunkid(regticket.thumbnailhash), imagedata.thumbnail)
    Chunk.create_from_hash(chunkhash=regticket.thumbnailhash,
                           artwork_hash=regticket.thumbnailhash,
                           stored=True)

    # store chunks
    for chunkhash, chunkdata in zip(imagedata.get_luby_hashes(),
                                    imagedata.lubychunks):
        chunkhash_int = bytes_to_chunkid(chunkhash)
        get_chunkmanager().store_chunk_in_temp_storage(chunkhash_int,
                                                       chunkdata)
        mn_ticket_logger.debug(
            'Adding chunk id to DB: {}'.format(chunkhash_int))
        # keep track of chunks in the local SQLite database.
        # we should be ably to find all chunks by artwork hash as well.

        # save chunk in database and mark `Stored` = True as we've already stored it in the storage (at least temp).
        Chunk.create_from_hash(chunkhash=chunkhash,
                               artwork_hash=artwork_hash,
                               stored=True)
Esempio n. 2
0
def get_and_proccess_new_activation_tickets():
    """
    As as input we have list of new activation ticket.
    Outputs of this task are:
     - Store appropriate registration tickets in local DB, marking them confirmed
     - Store chunks from these registration tickets in local DB, marking them confirmed.
     (which will be used by another chunk-processing tasks).
    """
    # FIXME: use `height` param when it will be implemented on cNode
    act_tickets = get_blockchain_connection().list_tickets(
        'act')  # list if dicts with actticket data
    act_tickets_txids = [ticket['txid'] for ticket in act_tickets]
    if act_tickets_txids is None:
        return

    for txid in filter(lambda x: len(x) == TXID_LENGTH, act_tickets_txids):
        if ActivationTicket.select().where(
                ActivationTicket.txid == txid).count() != 0:
            continue

        tasks_logger.info('New activation ticket found: {}'.format(txid))

        try:
            act_ticket = get_blockchain_connection().get_ticket(txid)
        except JSONRPCException as e:
            tasks_logger.exception(
                'Exception while fetching actticket: {}'.format(str(e)))
            # to avoid processing invalid txid multiple times - write in to the DB with height=-1
            ActivationTicket.create(txid=txid, height=-1)
            continue
        # fetch regticket from activation ticket
        # store regticket in local DB if not exist
        # get list of chunk ids, add to local DB (Chunk table)
        regticket_data = get_blockchain_connection().get_ticket(
            act_ticket['ticket']['reg_txid'])
        regticket = get_registration_ticket_object_from_data(regticket_data)
        chunk_hashes = regticket.lubyhashes  # this is list of bytes objects

        # add thumbnail chunk
        try:
            tasks_logger.info(
                'Creating Chunk record for thumbnail, hash {}'.format(
                    regticket.thumbnailhash))
            chunk = Chunk.create_from_hash(
                chunkhash=regticket.thumbnailhash,
                artwork_hash=regticket.thumbnailhash)
        except IntegrityError:  # if Chunk with such chunkhash already exist
            tasks_logger.error('Error: thumbnail chunk already exists')
            chunk = Chunk.get_by_hash(chunkhash=regticket.thumbnailhash)
        chunk.confirmed = True
        chunk.save()

        chunks_created, chunks_updated = 0, 0
        tasks_logger.info('Creating chunks record for artwork chunks...')
        for chunkhash in chunk_hashes:
            # if chunk exists - mark it as confirmed
            # else - create it. And mark as confirmed. More frequently we'll have no such chunk.
            try:
                chunk = Chunk.create_from_hash(
                    chunkhash=chunkhash, artwork_hash=regticket.imagedata_hash)
                chunks_created += 1
            except IntegrityError:  # if Chunk with such chunkhash already exist
                chunk = Chunk.get_by_hash(chunkhash=chunkhash)
                chunks_updated += 1
            chunk.confirmed = True
            chunk.save()
        tasks_logger.info('...Complete! Created {}, updated {} chunks'.format(
            chunks_created, chunks_updated))

        # write processed act ticket to DB
        tasks_logger.info(
            'Activation ticket processed, writing to the DB. Height: {}'.
            format(regticket_data['height']))
        ActivationTicket.create(txid=txid, height=act_ticket['height'])