Skip to content

Commit

Permalink
fix for pruning deleting all blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zelje committed Apr 24, 2024
1 parent 6a8114b commit 476ab16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
7 changes: 7 additions & 0 deletions doge_indexer/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def extract_initial_block_height(self) -> int:
"""
latest_block = DogeBlock.objects.order_by("block_number").last()
if latest_block is None:
height = self._get_current_block_height(self.toplevel_worker)

safety_factor = 1.5

blocks_since_pruning = int(config.PRUNE_KEEP_DAYS * 24 * 60 * safety_factor)
if config.INITIAL_BLOCK_HEIGHT < height - blocks_since_pruning:
return height - blocks_since_pruning
return config.INITIAL_BLOCK_HEIGHT

if latest_block.block_number < config.INITIAL_BLOCK_HEIGHT:
Expand Down
52 changes: 28 additions & 24 deletions doge_indexer/management/commands/block_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,33 @@ def handle(self, *args, **options):

cutoff = now_ts - config.PRUNE_KEEP_DAYS * 24 * 60 * 60

logger.info("Pruning at: %s", now_ts, " all transactions and block before cutoff: %s", cutoff)

with transaction.atomic():
# objects with fk to transaction first
TransactionInput.objects.filter(transaction_link__timestamp__lt=cutoff).delete()
TransactionInputCoinbase.objects.filter(transaction_link__timestamp__lt=cutoff).delete()
TransactionOutput.objects.filter(transaction_link__timestamp__lt=cutoff).delete()

# then others
DogeBlock.objects.filter(timestamp__lt=cutoff).delete()
DogeTransaction.objects.filter(timestamp__lt=cutoff).delete()

bottom_block = DogeBlock.objects.order_by("block_number").first()
bottom_block_transaction = DogeTransaction.objects.order_by("block_number").first()

if bottom_block is not None and bottom_block_transaction is not None:
if bottom_block.block_number != bottom_block_transaction.block_number:
raise Exception("Bottom block and bottom transaction block number mismatch while pruning")

prune_state.latest_indexed_tail_height = bottom_block.block_number
prune_state.timestamp = now_ts
prune_state.save()

logger.info("Sleeping for %s sec", config.PRUNE_INTERVAL_SECONDS)
logger.info(f"Pruning at: {now_ts} all transactions and block before cutoff: {cutoff}")

latest_block = DogeBlock.objects.order_by("block_number").last()
if latest_block is None or latest_block.timestamp <= cutoff:
logger.info("Not pruning when the latest block height is older than PRUNE_KEEP_DAYS (%s days)", config.PRUNE_KEEP_DAYS)
else:
with transaction.atomic():
# objects with fk to transaction first
TransactionInput.objects.filter(transaction_link__timestamp__lt=cutoff).delete()
TransactionInputCoinbase.objects.filter(transaction_link__timestamp__lt=cutoff).delete()
TransactionOutput.objects.filter(transaction_link__timestamp__lt=cutoff).delete()

# then others
DogeBlock.objects.filter(timestamp__lt=cutoff).delete()
DogeTransaction.objects.filter(timestamp__lt=cutoff).delete()

bottom_block = DogeBlock.objects.order_by("block_number").first()
bottom_block_transaction = DogeTransaction.objects.order_by("block_number").first()

if bottom_block is not None and bottom_block_transaction is not None:
if bottom_block.block_number != bottom_block_transaction.block_number:
raise Exception("Bottom block and bottom transaction block number mismatch while pruning")

prune_state.latest_indexed_tail_height = bottom_block.block_number
prune_state.timestamp = now_ts
prune_state.save()

logger.info("Prune finished, sleeping for %s sec", config.PRUNE_INTERVAL_SECONDS)

time.sleep(config.PRUNE_INTERVAL_SECONDS)

0 comments on commit 476ab16

Please sign in to comment.