Skip to content

Commit 5c3ab80

Browse files
Fix merging bigmapdiffs (#13)
1 parent ca160e5 commit 5c3ab80

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed
-26.8 KB
Binary file not shown.

poetry.lock

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ asyncpg = "^0.22.0"
2828
datamodel-code-generator = "^0.10.0"
2929
"ruamel.yaml" = "^0.17.2"
3030
tortoise-orm = "^0.17.1"
31-
websockets = "^8.1"
3231
pydantic = "^1.8.1"
33-
aiosignalrcore = {path = "aiosignalrcore-0.0.0-py3-none-any.whl"}
32+
aiosignalrcore = "^0.9.2"
3433

3534
[tool.poetry.dev-dependencies]
3635
black = "^20.8b1"

src/dipdup/configs/debug.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: 1
2+
disable_existing_loggers: false
3+
formatters:
4+
brief:
5+
format: "%(levelname)-8s %(name)-35s %(message)s"
6+
handlers:
7+
console:
8+
level: DEBUG
9+
formatter: brief
10+
class: logging.StreamHandler
11+
stream : ext://sys.stdout
12+
loggers:
13+
SignalRCoreClient:
14+
formatter: brief
15+
dipdup.datasources.tzkt.datasource:
16+
level: DEBUG
17+
dipdup.datasources.tzkt.cache:
18+
level: DEBUG
19+
root:
20+
level: DEBUG
21+
handlers:
22+
- console

src/dipdup/models.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import logging
2-
from contextlib import suppress
32
from copy import deepcopy
43
from datetime import datetime
54
from enum import Enum
6-
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, get_args, get_origin
5+
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar
76

87
from pydantic import BaseModel
98
from pydantic.dataclasses import dataclass
@@ -35,7 +34,7 @@ class Meta:
3534

3635
@dataclass
3736
class OperationData:
38-
# FIXME:
37+
# FIXME: Bug in TzKT, shouldn't be optional
3938
type: Optional[str]
4039
id: int
4140
level: int
@@ -67,7 +66,7 @@ class OperationData:
6766

6867
def _merge_bigmapdiffs(self, storage_dict: Dict[str, Any], bigmap_name: str) -> None:
6968
if self.bigmaps is None:
70-
raise Exception('`bigaps` field missing')
69+
raise Exception('`bigmaps` field missing')
7170
bigmapdiffs = [bm for bm in self.bigmaps if bm['path'] == bigmap_name]
7271
for diff in bigmapdiffs:
7372
if diff['action'] in ('add_key', 'update_key'):
@@ -80,20 +79,23 @@ def _merge_bigmapdiffs(self, storage_dict: Dict[str, Any], bigmap_name: str) ->
8079
def get_merged_storage(self, storage_type: Type[StorageType]) -> StorageType:
8180
if self.storage is None:
8281
raise Exception('`storage` field missing')
83-
if self.bigmaps is None:
84-
return storage_type.parse_obj(self.storage)
8582

8683
storage = deepcopy(self.storage)
84+
_logger.debug('Merging storage')
85+
_logger.debug('Before: %s', storage)
8786
for key, field in storage_type.__fields__.items():
88-
if field.type_ != int and isinstance(storage[key], int):
89-
with suppress(AttributeError):
90-
if 'key' in field.type_.__fields__:
91-
storage[key] = []
92-
else:
93-
storage[key] = {}
94-
95-
self._merge_bigmapdiffs(storage, key)
96-
87+
# NOTE: TzKT could return bigmaps as object or as array of key-value objects. We need to guess this from storage.
88+
# TODO: This code should be a part of datasource module.
89+
if field.type_ not in (int, bool) and isinstance(storage[key], int):
90+
if hasattr(field.type_, '__fields__') and 'key' in field.type_.__fields__:
91+
storage[key] = []
92+
else:
93+
storage[key] = {}
94+
95+
if self.bigmaps is not None:
96+
self._merge_bigmapdiffs(storage, key)
97+
98+
_logger.debug('After: %s', storage)
9799
return storage_type.parse_obj(storage)
98100

99101

tests/test_dipdup/test_datasources/test_tzkt/test_datasource.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ async def test_on_operation_match(self):
323323

324324
callback_mock = AsyncMock()
325325
storage_type_mock = MagicMock()
326+
storage_type_mock.__fields__ = MagicMock()
326327

327328
self.index_config.handlers[0].callback_fn = callback_mock
328329
self.index_config.handlers[0].pattern[0].storage_type_cls = storage_type_mock

0 commit comments

Comments
 (0)