|
21 | 21 | from dipdup.context import DipDupContext |
22 | 22 | from dipdup.datasources.tzkt.datasource import BigMapFetcher, OperationFetcher, TzktDatasource |
23 | 23 | 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 |
27 | 25 | from dipdup.utils import FormattedLogger |
28 | 26 | from dipdup.utils.database import in_global_transaction |
29 | 27 |
|
@@ -52,30 +50,27 @@ def state(self) -> models.Index: |
52 | 50 | raise RuntimeError('Index state is not initialized') |
53 | 51 | return self._state |
54 | 52 |
|
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 | + ) |
70 | 67 |
|
71 | 68 | # NOTE: No need to check hashes of indexes which are not synchronized. |
72 | 69 | 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) |
79 | 74 |
|
80 | 75 | async def process(self) -> None: |
81 | 76 | # NOTE: `--oneshot` flag implied |
@@ -113,12 +108,10 @@ async def _process_queue(self) -> None: |
113 | 108 | async def _enter_sync_state(self, last_level: int) -> Optional[int]: |
114 | 109 | if self.state.status == IndexStatus.ONESHOT: |
115 | 110 | 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 |
122 | 115 |
|
123 | 116 | if first_level == last_level: |
124 | 117 | return None |
|
0 commit comments