Exemplo n.º 1
0
def fix_next_block(block, chain):
    logger.info('fixing next block for {}'.format(block))
    # it's likely that this block is an orphan so we should remove it and rescan
    this_height = block.height
    this_hash = get_block_hash(this_height, schema_name=chain)

    if not this_hash:
        logger.warning(
            'could not get hash for block at height {}'.format(this_height))
        return

    if this_hash != block.hash:
        block = Block.objects.create(hash=this_hash, height=this_height)

    next_hash = get_block_hash(this_height + 1, schema_name=chain)

    if not next_hash:
        logger.warning(
            'could not get next hash for height {}'.format(this_height + 1))
        return

    try:
        next_block = Block.objects.get(hash=next_hash)
    except Block.DoesNotExist:
        next_block = Block(hash=next_hash)

    block.next_block = next_block
    block.save()

    next_block.height = this_height + 1
    next_block.previous_block = block
    next_block.save(validate=False)