Skip to content

Commit

Permalink
feat(indexer): logging last successful block check instead of relying…
Browse files Browse the repository at this point in the history
… 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
lucianHymer authored Jan 13, 2025
1 parent 1e525f7 commit d3d3ef8
Show file tree
Hide file tree
Showing 7 changed files with 942 additions and 605 deletions.
11 changes: 10 additions & 1 deletion api/stake/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from scorer.scorer_admin import ScorerModelAdmin
from stake.models import Stake, StakeEvent, ReindexRequest
from stake.models import LastBlock, ReindexRequest, Stake, StakeEvent


@admin.register(Stake)
Expand Down Expand Up @@ -68,3 +69,11 @@ class ReindexRequestAdmin(admin.ModelAdmin):
]

readonly_fields = ("pending",)


@admin.register(LastBlock)
class LastBlockAdmin(admin.ModelAdmin):
list_display = [
"chain",
"block_number",
]
87 changes: 87 additions & 0 deletions api/stake/migrations/0007_lastblock.py
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
),
]
16 changes: 16 additions & 0 deletions api/stake/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,19 @@ class Meta:
condition=models.Q(pending=True),
),
]


class LastBlock(models.Model):
chain = models.IntegerField(
null=False,
blank=False,
db_index=True,
unique=True,
# The legacy indexer is using totally different rust code from the new indexer, so
# instead of passing its chain ID, it's just passing a hardcoded "0"
help_text="Decimal chain ID. Ethereum: 1, Optimism: 10, Arbitrum: 42161, Legacy: 0",
)

block_number = models.DecimalField(
decimal_places=0, null=False, blank=False, max_digits=78, db_index=True
)
Loading

0 comments on commit d3d3ef8

Please sign in to comment.