Skip to content

Commit a919b2e

Browse files
droserasproutm-kus
andauthored
Partial indexing, integration tests (#18)
* REST-only mode, integration test for HEN * Less logs * Fix Quipuswap demo once again, add test * Refactoring * tzcolors test Co-authored-by: Michael Zaikin <[email protected]>
1 parent 626a080 commit a919b2e

File tree

16 files changed

+386
-79
lines changed

16 files changed

+386
-79
lines changed

src/demo_hic_et_nunc/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package: demo_hic_et_nunc
33

44
database:
55
kind: sqlite
6-
path: db.sqlite3
6+
path: hic_et_nunc.sqlite3
77

88
contracts:
99
HEN_objkts:

src/demo_quipuswap/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package: demo_quipuswap
33

44
database:
55
kind: sqlite
6-
path: db.sqlite3
6+
path: quipuswap.sqlite3
77

88
contracts:
99
kusd_dex_mainnet:

src/demo_quipuswap/hasura_metadata.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
"role": "user",
7373
"permission": {
7474
"columns": [
75-
"tez_qty",
7675
"id",
76+
"tez_qty",
7777
"token_qty"
7878
],
7979
"filter": {},
@@ -107,13 +107,13 @@
107107
"role": "user",
108108
"permission": {
109109
"columns": [
110-
"slippage",
111-
"quantity",
110+
"side",
112111
"level",
112+
"slippage",
113113
"price",
114-
"side",
114+
"id",
115115
"timestamp",
116-
"id"
116+
"quantity"
117117
],
118118
"filter": {},
119119
"allow_aggregations": true

src/dipdup/cli.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from dipdup.config import DipDupConfig, IndexTemplateConfig, LoggingConfig, OperationIndexConfig, TzktDatasourceConfig
2121
from dipdup.datasources.tzkt.datasource import TzktDatasource
2222
from dipdup.models import IndexType, State
23+
from dipdup.utils import tortoise_wrapper
2324

2425
_logger = logging.getLogger(__name__)
2526

@@ -69,15 +70,10 @@ async def cli(ctx, config: str, logging_config: str):
6970
async def run(ctx) -> None:
7071
config: DipDupConfig = ctx.obj.config
7172

72-
try:
73+
url = config.database.connection_string
74+
models = f'{config.package}.models'
75+
async with tortoise_wrapper(url, models):
7376
_logger.info('Initializing database')
74-
await Tortoise.init(
75-
db_url=config.database.connection_string,
76-
modules={
77-
'models': [f'{config.package}.models'],
78-
'int_models': ['dipdup.models'],
79-
},
80-
)
8177

8278
connection_name, connection = next(iter(Tortoise._connections.items()))
8379
schema_sql = get_schema_sql(connection, False)
@@ -118,9 +114,6 @@ async def run(ctx) -> None:
118114
datasource_run_tasks = [asyncio.create_task(d.start()) for d in datasources.values()]
119115
await asyncio.gather(*datasource_run_tasks)
120116

121-
finally:
122-
await Tortoise.close_connections()
123-
124117

125118
@cli.command(help='Initialize new dipdap')
126119
@click.pass_context

src/dipdup/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ async def initialize(self) -> None:
432432
index_name=index_name,
433433
index_type=IndexType.operation,
434434
hash=index_hash,
435+
level=index_config.first_block - 1,
435436
)
436437
await state.save()
437438

src/dipdup/configs/debug.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
SignalRCoreClient:
1414
formatter: brief
1515
dipdup.datasources.tzkt.datasource:
16-
level: INFO
16+
level: DEBUG
1717
dipdup.datasources.tzkt.cache:
18-
level: INFO
18+
level: DEBUG
1919
aiosqlite:
20-
level: INFO
20+
level: DEBUG
2121
db_client:
22-
level: INFO
22+
level: DEBUG
23+
dipdup.models:
24+
level: DEBUG
2325
root:
2426
level: DEBUG
2527
handlers:

src/dipdup/configs/warning.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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: WARNING
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: INFO
17+
dipdup.datasources.tzkt.cache:
18+
level: INFO
19+
aiosqlite:
20+
level: INFO
21+
db_client:
22+
level: INFO
23+
root:
24+
level: WARNING
25+
handlers:
26+
- console

src/dipdup/datasources/tzkt/datasource.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,24 @@ def _get_client(self) -> BaseHubConnection:
109109

110110
async def start(self):
111111
self._logger.info('Starting datasource')
112+
rest_only = False
112113
for operation_index_config in self._operation_index_configs.values():
113-
await self.add_subscription(operation_index_config.contract)
114114

115+
if operation_index_config.last_block:
116+
await self.fetch_operations(operation_index_config.last_block, initial=True)
117+
rest_only = True
118+
continue
119+
120+
await self.add_subscription(operation_index_config.contract)
115121
latest_block = await self.get_latest_block()
116122
current_level = latest_block['level']
117123
state_level = operation_index_config.state.level
118124
if current_level != state_level:
119125
await self.fetch_operations(current_level, initial=True)
120126

121-
self._logger.info('Starting websocket client')
122-
await self._get_client().start()
127+
if not rest_only:
128+
self._logger.info('Starting websocket client')
129+
await self._get_client().start()
123130

124131
async def stop(self):
125132
...
@@ -193,7 +200,7 @@ async def _process_operations(address, operations):
193200
for index_config in self._operation_index_configs.values():
194201

195202
sync_event = self._sync_events[index_config.state.index_name]
196-
level = index_config.state.level or 0
203+
level = index_config.state.level
197204

198205
operations = []
199206
offset = 0

src/dipdup/models.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,23 @@ def get_merged_storage(self, storage_type: Type[StorageType]) -> StorageType:
8181
if self.storage is None:
8282
raise Exception('`storage` field missing')
8383

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

100-
_logger.debug('After: %s', storage)
100+
_logger.debug('After: %s', storage)
101101

102102
return storage_type.parse_obj(storage)
103103

src/dipdup/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from contextlib import asynccontextmanager
2+
from typing import Optional
3+
4+
from tortoise import Tortoise
5+
6+
7+
@asynccontextmanager
8+
async def tortoise_wrapper(url: str, models: Optional[str] = None):
9+
try:
10+
modules = {'int_models': ['dipdup.models']}
11+
if models:
12+
modules['models'] = [models]
13+
await Tortoise.init(
14+
db_url=url,
15+
modules=modules, # type: ignore
16+
)
17+
yield
18+
finally:
19+
await Tortoise.close_connections()

0 commit comments

Comments
 (0)