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