Skip to content

Commit 5808c9a

Browse files
Fix entering sync state (#142)
* Fix entering sync state * Fix
1 parent d178d74 commit 5808c9a

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

src/dipdup/datasources/tzkt/datasource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ async def _on_head_message(self, message: List[Dict[str, Any]]) -> None:
779779
async def set_head_from_http(self) -> None:
780780
"""Set block from `get_head_block` HTTP method for indexes to use the same level during initial sync"""
781781
if self._head:
782-
self._logger.warning('Attempt to set head twice')
783-
return
782+
raise RuntimeError('Head is already set')
783+
784784
block = await self.get_head_block()
785785
self._head, created = await Head.get_or_create(
786786
name=self._http._url,

src/dipdup/index.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
from dipdup.context import DipDupContext
2222
from dipdup.datasources.tzkt.datasource import BigMapFetcher, OperationFetcher, TzktDatasource
2323
from dipdup.exceptions import ConfigInitializationException, InvalidDataError, ReindexingReason
24-
from dipdup.models import BigMapData, BigMapDiff, Head, HeadBlockData
25-
from dipdup.models import Index as IndexState
26-
from dipdup.models import IndexStatus, OperationData, Origination, Transaction
24+
from dipdup.models import BigMapData, BigMapDiff, HeadBlockData, IndexStatus, OperationData, Origination, Transaction
2725
from dipdup.utils import FormattedLogger
2826
from dipdup.utils.database import in_global_transaction
2927

@@ -52,30 +50,27 @@ def state(self) -> models.Index:
5250
raise RuntimeError('Index state is not initialized')
5351
return self._state
5452

55-
async def initialize_state(self, state: Optional[IndexState] = None) -> None:
56-
if not self._state:
57-
if not state:
58-
state, _ = await models.Index.get_or_create(
59-
name=self._config.name,
60-
type=self._config.kind,
61-
defaults=dict(
62-
level=self._config.first_level,
63-
config_hash=self._config.hash(),
64-
template=self._config.parent.name if self._config.parent else None,
65-
template_values=self._config.template_values,
66-
),
67-
)
68-
69-
self._state = state
53+
async def initialize_state(self) -> None:
54+
if self._state:
55+
raise RuntimeError('Index state is already initialized')
56+
57+
self._state, _ = await models.Index.get_or_create(
58+
name=self._config.name,
59+
type=self._config.kind,
60+
defaults=dict(
61+
level=self._config.first_level,
62+
config_hash=self._config.hash(),
63+
template=self._config.parent.name if self._config.parent else None,
64+
template_values=self._config.template_values,
65+
),
66+
)
7067

7168
# NOTE: No need to check hashes of indexes which are not synchronized.
7269
head = await self.state.head
73-
if not head or self.state.level != head.level:
74-
return
75-
76-
block = await self._datasource.get_block(self.state.level)
77-
if head.hash != block.hash:
78-
await self._ctx.reindex(ReindexingReason.BLOCK_HASH_MISMATCH)
70+
if head and self.state.status == IndexStatus.REALTIME:
71+
block = await self._datasource.get_block(head.level)
72+
if head.hash != block.hash:
73+
await self._ctx.reindex(ReindexingReason.BLOCK_HASH_MISMATCH)
7974

8075
async def process(self) -> None:
8176
# NOTE: `--oneshot` flag implied
@@ -113,12 +108,10 @@ async def _process_queue(self) -> None:
113108
async def _enter_sync_state(self, last_level: int) -> Optional[int]:
114109
if self.state.status == IndexStatus.ONESHOT:
115110
return None
116-
# FIXME: I'm not sure if this is a good way to check if index is in sync
117-
# TODO: Move to model class
118-
elif self.state.status == IndexStatus.REALTIME and isinstance(self.state.head, Head):
119-
self.state.head.level
120-
else:
121-
first_level = self.state.level
111+
112+
head = await self.state.head
113+
# FIXME: Use Head when postponed WS init will be reversed
114+
first_level = self.state.level
122115

123116
if first_level == last_level:
124117
return None

src/dipdup/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ async def update_status(
317317

318318
if head:
319319
self.head = head
320-
self.head_id = head.pk if head else None
321320

322321
self.status = status
323322
await self.save()

0 commit comments

Comments
 (0)