@@ -52,14 +52,12 @@ class IndexesManager(ABC):
52
52
log = get_logger ()
53
53
54
54
info : InfoIndex
55
- all_tips : TipsIndex
56
- block_tips : TipsIndex
57
- tx_tips : TipsIndex
58
-
55
+ all_tips : Optional [TipsIndex ]
56
+ block_tips : Optional [TipsIndex ]
57
+ tx_tips : Optional [TipsIndex ]
59
58
sorted_all : TimestampIndex
60
59
sorted_blocks : TimestampIndex
61
60
sorted_txs : TimestampIndex
62
-
63
61
height : HeightIndex
64
62
deps : Optional [DepsIndex ]
65
63
mempool_tips : Optional [MempoolTipsIndex ]
@@ -97,6 +95,11 @@ def iter_all_indexes(self) -> Iterator[BaseIndex]:
97
95
self .utxo ,
98
96
])
99
97
98
+ @abstractmethod
99
+ def enable_tips_indexes (self ) -> None :
100
+ """Enable tips indexs. It does nothing if it has already been enabled."""
101
+ raise NotImplementedError
102
+
100
103
@abstractmethod
101
104
def enable_address_index (self , pubsub : 'PubSubManager' ) -> None :
102
105
"""Enable address index. It does nothing if it has already been enabled."""
@@ -208,18 +211,21 @@ def add_tx(self, tx: BaseTransaction) -> bool:
208
211
209
212
# These two calls return False when a transaction changes from
210
213
# voided to executed and vice-versa.
211
- r1 = self .all_tips .add_tx (tx )
212
- r2 = self .sorted_all .add_tx (tx )
213
- assert r1 == r2
214
+ r1 = self .sorted_all .add_tx (tx )
215
+ if self .all_tips is not None :
216
+ r2 = self .all_tips .add_tx (tx )
217
+ assert r1 == r2
214
218
215
219
if tx .is_block :
216
- r3 = self .block_tips .add_tx (tx )
217
- r4 = self .sorted_blocks .add_tx (tx )
218
- assert r3 == r4
220
+ r3 = self .sorted_blocks .add_tx (tx )
221
+ if self .block_tips is not None :
222
+ r4 = self .block_tips .add_tx (tx )
223
+ assert r3 == r4
219
224
else :
220
- r3 = self .tx_tips .add_tx (tx )
221
- r4 = self .sorted_txs .add_tx (tx )
222
- assert r3 == r4
225
+ r3 = self .sorted_txs .add_tx (tx )
226
+ if self .tx_tips is not None :
227
+ r4 = self .tx_tips .add_tx (tx )
228
+ assert r3 == r4
223
229
224
230
if self .addresses :
225
231
self .addresses .add_tx (tx )
@@ -249,7 +255,8 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:
249
255
# We delete from indexes in two cases: (i) mark tx as voided, and (ii) remove tx.
250
256
# We only remove tx from all_tips and sorted_all when it is removed from the storage.
251
257
# For clarity, when a tx is marked as voided, it is not removed from all_tips and sorted_all.
252
- self .all_tips .del_tx (tx , relax_assert = relax_assert )
258
+ if self .all_tips is not None :
259
+ self .all_tips .del_tx (tx , relax_assert = relax_assert )
253
260
self .sorted_all .del_tx (tx )
254
261
if self .addresses :
255
262
self .addresses .remove_tx (tx )
@@ -263,11 +270,13 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:
263
270
self .mempool_tips .update (tx , remove = True )
264
271
265
272
if tx .is_block :
266
- self .block_tips .del_tx (tx , relax_assert = relax_assert )
267
273
self .sorted_blocks .del_tx (tx )
274
+ if self .block_tips is not None :
275
+ self .block_tips .del_tx (tx , relax_assert = relax_assert )
268
276
else :
269
- self .tx_tips .del_tx (tx , relax_assert = relax_assert )
270
277
self .sorted_txs .del_tx (tx )
278
+ if self .tx_tips is not None :
279
+ self .tx_tips .del_tx (tx , relax_assert = relax_assert )
271
280
272
281
if self .tokens :
273
282
self .tokens .del_tx (tx )
@@ -282,12 +291,11 @@ def __init__(self) -> None:
282
291
from hathor .indexes .memory_height_index import MemoryHeightIndex
283
292
from hathor .indexes .memory_info_index import MemoryInfoIndex
284
293
from hathor .indexes .memory_timestamp_index import MemoryTimestampIndex
285
- from hathor .indexes .memory_tips_index import MemoryTipsIndex
286
294
287
295
self .info = MemoryInfoIndex ()
288
- self .all_tips = MemoryTipsIndex ( scope_type = TipsScopeType . ALL )
289
- self .block_tips = MemoryTipsIndex ( scope_type = TipsScopeType . BLOCKS )
290
- self .tx_tips = MemoryTipsIndex ( scope_type = TipsScopeType . TXS )
296
+ self .all_tips = None
297
+ self .block_tips = None
298
+ self .tx_tips = None
291
299
292
300
self .sorted_all = MemoryTimestampIndex (scope_type = TimestampScopeType .ALL )
293
301
self .sorted_blocks = MemoryTimestampIndex (scope_type = TimestampScopeType .BLOCKS )
@@ -303,6 +311,15 @@ def __init__(self) -> None:
303
311
# XXX: this has to be at the end of __init__, after everything has been initialized
304
312
self .__init_checks__ ()
305
313
314
+ def enable_tips_indexes (self ) -> None :
315
+ from hathor .indexes .memory_tips_index import MemoryTipsIndex
316
+ if self .all_tips is None :
317
+ self .all_tips = MemoryTipsIndex (scope_type = TipsScopeType .ALL )
318
+ if self .block_tips is None :
319
+ self .block_tips = MemoryTipsIndex (scope_type = TipsScopeType .BLOCKS )
320
+ if self .tx_tips is None :
321
+ self .tx_tips = MemoryTipsIndex (scope_type = TipsScopeType .TXS )
322
+
306
323
def enable_address_index (self , pubsub : 'PubSubManager' ) -> None :
307
324
from hathor .indexes .memory_address_index import MemoryAddressIndex
308
325
if self .addresses is None :
@@ -331,7 +348,6 @@ def enable_deps_index(self) -> None:
331
348
332
349
class RocksDBIndexesManager (IndexesManager ):
333
350
def __init__ (self , rocksdb_storage : 'RocksDBStorage' ) -> None :
334
- from hathor .indexes .partial_rocksdb_tips_index import PartialRocksDBTipsIndex
335
351
from hathor .indexes .rocksdb_height_index import RocksDBHeightIndex
336
352
from hathor .indexes .rocksdb_info_index import RocksDBInfoIndex
337
353
from hathor .indexes .rocksdb_timestamp_index import RocksDBTimestampIndex
@@ -340,9 +356,9 @@ def __init__(self, rocksdb_storage: 'RocksDBStorage') -> None:
340
356
341
357
self .info = RocksDBInfoIndex (self ._db )
342
358
self .height = RocksDBHeightIndex (self ._db )
343
- self .all_tips = PartialRocksDBTipsIndex ( self . _db , scope_type = TipsScopeType . ALL )
344
- self .block_tips = PartialRocksDBTipsIndex ( self . _db , scope_type = TipsScopeType . BLOCKS )
345
- self .tx_tips = PartialRocksDBTipsIndex ( self . _db , scope_type = TipsScopeType . TXS )
359
+ self .all_tips = None
360
+ self .block_tips = None
361
+ self .tx_tips = None
346
362
347
363
self .sorted_all = RocksDBTimestampIndex (self ._db , scope_type = TimestampScopeType .ALL )
348
364
self .sorted_blocks = RocksDBTimestampIndex (self ._db , scope_type = TimestampScopeType .BLOCKS )
@@ -357,6 +373,15 @@ def __init__(self, rocksdb_storage: 'RocksDBStorage') -> None:
357
373
# XXX: this has to be at the end of __init__, after everything has been initialized
358
374
self .__init_checks__ ()
359
375
376
+ def enable_tips_indexes (self ) -> None :
377
+ from hathor .indexes .partial_rocksdb_tips_index import PartialRocksDBTipsIndex
378
+ if self .all_tips is None :
379
+ self .all_tips = PartialRocksDBTipsIndex (self ._db , scope_type = TipsScopeType .ALL )
380
+ if self .block_tips is None :
381
+ self .block_tips = PartialRocksDBTipsIndex (self ._db , scope_type = TipsScopeType .BLOCKS )
382
+ if self .tx_tips is None :
383
+ self .tx_tips = PartialRocksDBTipsIndex (self ._db , scope_type = TipsScopeType .TXS )
384
+
360
385
def enable_address_index (self , pubsub : 'PubSubManager' ) -> None :
361
386
from hathor .indexes .rocksdb_address_index import RocksDBAddressIndex
362
387
if self .addresses is None :
0 commit comments