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)
def fix_previous_block(block, chain): logger.info('fixing previous block for {}'.format(block)) prev_hash = get_block_hash(block.height - 1, schema_name=chain) if not prev_hash: return try: prev_block = Block.objects.get(hash=prev_hash) except Block.DoesNotExist: prev_block = Block(hash=prev_hash) try: prev_height_block = Block.objects.get(height=block.height - 1) except Block.DoesNotExist: prev_height_block = prev_block if prev_block != prev_height_block: # the block with the previous height doesn't match the hash from this block # likely to be an orphan so remove it prev_height_block.delete() block.previous_block = prev_block block.save() prev_block.height = block.height - 1 prev_block.next_block = block prev_block.save()