-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(indexer): logging last successful block check instead of relying…
… on last stake event (#763) * feat(indexer): logging last successful block check instead of relying on last stake event * chore(indexer): removing old debug code * adding clarifying comment
- Loading branch information
1 parent
1e525f7
commit d3d3ef8
Showing
7 changed files
with
942 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Generated by Django 4.2.6 on 2025-01-08 21:51 | ||
|
||
from decimal import Decimal | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def initialize_last_blocks(apps, schema_editor): | ||
""" | ||
Initialize LastBlock entries based on the latest blocks from StakeEvent | ||
and legacy GTCStakeEvent tables | ||
""" | ||
LastBlock = apps.get_model("stake", "LastBlock") | ||
StakeEvent = apps.get_model("stake", "StakeEvent") | ||
db_alias = schema_editor.connection.alias | ||
|
||
# Get all unique chain IDs from StakeEvent | ||
chain_ids = ( | ||
StakeEvent.objects.using(db_alias).values_list("chain", flat=True).distinct() | ||
) | ||
|
||
# Get latest blocks for each chain from StakeEvent | ||
for chain_id in chain_ids: | ||
latest_block = ( | ||
StakeEvent.objects.using(db_alias) | ||
.filter(chain=chain_id) | ||
.order_by("-block_number") | ||
.values_list("block_number", flat=True) | ||
.first() | ||
) | ||
|
||
LastBlock.objects.using(db_alias).create( | ||
chain=chain_id, block_number=latest_block or Decimal("0") | ||
) | ||
|
||
# Handle legacy chain using GTCStakeEvent model | ||
GTCStakeEvent = apps.get_model("registry", "GTCStakeEvent") | ||
legacy_block = ( | ||
GTCStakeEvent.objects.using(db_alias) | ||
.order_by("-block_number") | ||
.values_list("block_number", flat=True) | ||
.first() | ||
) | ||
|
||
LastBlock.objects.using(db_alias).create( | ||
chain=0, # Legacy contract on Ethereum | ||
block_number=Decimal(str(legacy_block or 0)), | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("stake", "0006_reindexrequest_alter_stakeevent_block_number_and_more"), | ||
("registry", "0026_gtcstakeevent"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="LastBlock", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"chain", | ||
models.IntegerField( | ||
db_index=True, | ||
help_text="Decimal chain ID. Ethereum: 1, Optimism: 10, Arbitrum: 42161, Legacy: 0", | ||
unique=True, | ||
), | ||
), | ||
( | ||
"block_number", | ||
models.DecimalField(db_index=True, decimal_places=0, max_digits=78), | ||
), | ||
], | ||
), | ||
migrations.RunPython( | ||
initialize_last_blocks, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.