Skip to content

Commit 37a7b99

Browse files
Fix matching in OperationCache (#56)
1 parent f420a93 commit 37a7b99

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/dipdup/datasources/tzkt/cache.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
OperationHandlerTransactionPatternConfig,
1414
OperationIndexConfig,
1515
)
16+
from dipdup.exceptions import ConfigurationError
1617
from dipdup.models import BigMapData, OperationData
1718

1819
OperationGroup = namedtuple('OperationGroup', ('hash', 'counter'))
@@ -24,15 +25,18 @@ def __init__(self) -> None:
2425
super().__init__()
2526
self._logger = logging.getLogger(__name__)
2627
self._level: Optional[int] = None
27-
self._indexes: Dict[str, OperationIndexConfig] = {}
28+
self._indexes: List[OperationIndexConfig] = []
29+
self._addresses: List[str] = []
2830
self._operations: Dict[OperationGroup, List[OperationData]] = {}
2931

3032
async def add_index(self, index_config: OperationIndexConfig) -> None:
31-
self._logger.debug('Adding index %s to cache', index_config)
3233
for contract in index_config.contract_configs:
33-
if contract.address in self._indexes:
34-
raise RuntimeError(f'Address `{contract.address}` used in multiple indexes')
35-
self._indexes[contract.address] = index_config
34+
if contract.address in self._addresses:
35+
raise ConfigurationError(f'Address `{contract.address}` used in multiple indexes')
36+
self._addresses.append(contract.address)
37+
38+
self._logger.debug('Adding index %s to cache', index_config)
39+
self._indexes.append(index_config)
3640

3741
async def add(self, operation: OperationData):
3842
self._logger.debug('Adding operation %s to cache (%s, %s)', operation.id, operation.hash, operation.counter)
@@ -91,7 +95,7 @@ async def on_match(
9195
self._logger.debug('Matching %s', key)
9296
matched = False
9397

94-
for index_config in self._indexes.values():
98+
for index_config in self._indexes:
9599
for handler_config in index_config.handlers:
96100
operation_idx = 0
97101
pattern_idx = 0

src/dipdup/datasources/tzkt/datasource.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ async def run(self):
110110
return
111111

112112

113+
def dedup_operations(operations: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
114+
return sorted(
115+
list(({op['id']: op for op in operations}).values()),
116+
key=lambda op: op['id'],
117+
)
118+
119+
113120
class OperationFetcher:
114121
def __init__(
115122
self,
@@ -250,8 +257,7 @@ async def fetch_operations_by_level(self):
250257
while self._head <= head:
251258
if self._head in self._operations:
252259
operations = self._operations.pop(self._head)
253-
operations = sorted(list(({op['id']: op for op in operations}).values()), key=lambda op: op['id'])
254-
yield self._head, operations
260+
yield self._head, dedup_operations(operations)
255261
self._head += 1
256262

257263
if all(list(self._fetched.values())):

tests/test_dipdup/test_datasources/test_tzkt/test_datasource.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
OperationIndexConfig,
1818
OperationType,
1919
)
20-
from dipdup.datasources.tzkt.datasource import TzktDatasource
20+
from dipdup.datasources.tzkt.datasource import TzktDatasource, dedup_operations
2121
from dipdup.models import IndexType, OperationData, OperationHandlerContext, State, TransactionContext
2222
from dipdup.utils import tortoise_wrapper
2323

@@ -195,3 +195,20 @@ async def test_on_operation_match_with_storage(self):
195195
callback_mock.await_args[0][1].storage.proposals['e710c1a066bbbf73692168e783607996785260cec4d60930579827298493b8b9'],
196196
Proposals,
197197
)
198+
199+
async def test_dedup_operations(self) -> None:
200+
operations = [
201+
{'id': 5},
202+
{'id': 3},
203+
{'id': 3},
204+
{'id': 1},
205+
]
206+
operations = dedup_operations(operations)
207+
self.assertEqual(
208+
[
209+
{'id': 1},
210+
{'id': 3},
211+
{'id': 5},
212+
],
213+
operations,
214+
)

0 commit comments

Comments
 (0)