@@ -51,14 +51,12 @@ class IndexesManager(ABC):
51
51
log = get_logger ()
52
52
53
53
info : InfoIndex
54
- all_tips : TipsIndex
55
- block_tips : TipsIndex
56
- tx_tips : TipsIndex
57
-
54
+ all_tips : Optional [TipsIndex ]
55
+ block_tips : Optional [TipsIndex ]
56
+ tx_tips : Optional [TipsIndex ]
58
57
sorted_all : TimestampIndex
59
58
sorted_blocks : TimestampIndex
60
59
sorted_txs : TimestampIndex
61
-
62
60
height : HeightIndex
63
61
mempool_tips : Optional [MempoolTipsIndex ]
64
62
addresses : Optional [AddressIndex ]
@@ -94,6 +92,11 @@ def iter_all_indexes(self) -> Iterator[BaseIndex]:
94
92
self .utxo ,
95
93
])
96
94
95
+ @abstractmethod
96
+ def enable_tips_indexes (self ) -> None :
97
+ """Enable tips indexs. It does nothing if it has already been enabled."""
98
+ raise NotImplementedError
99
+
97
100
@abstractmethod
98
101
def enable_address_index (self , pubsub : 'PubSubManager' ) -> None :
99
102
"""Enable address index. It does nothing if it has already been enabled."""
@@ -198,18 +201,21 @@ def add_tx(self, tx: BaseTransaction) -> bool:
198
201
199
202
# These two calls return False when a transaction changes from
200
203
# voided to executed and vice-versa.
201
- r1 = self .all_tips .add_tx (tx )
202
- r2 = self .sorted_all .add_tx (tx )
203
- assert r1 == r2
204
+ r1 = self .sorted_all .add_tx (tx )
205
+ if self .all_tips is not None :
206
+ r2 = self .all_tips .add_tx (tx )
207
+ assert r1 == r2
204
208
205
209
if tx .is_block :
206
- r3 = self .block_tips .add_tx (tx )
207
- r4 = self .sorted_blocks .add_tx (tx )
208
- assert r3 == r4
210
+ r3 = self .sorted_blocks .add_tx (tx )
211
+ if self .block_tips is not None :
212
+ r4 = self .block_tips .add_tx (tx )
213
+ assert r3 == r4
209
214
else :
210
- r3 = self .tx_tips .add_tx (tx )
211
- r4 = self .sorted_txs .add_tx (tx )
212
- assert r3 == r4
215
+ r3 = self .sorted_txs .add_tx (tx )
216
+ if self .tx_tips is not None :
217
+ r4 = self .tx_tips .add_tx (tx )
218
+ assert r3 == r4
213
219
214
220
if self .addresses :
215
221
self .addresses .add_tx (tx )
@@ -235,7 +241,8 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:
235
241
# We delete from indexes in two cases: (i) mark tx as voided, and (ii) remove tx.
236
242
# We only remove tx from all_tips and sorted_all when it is removed from the storage.
237
243
# For clarity, when a tx is marked as voided, it is not removed from all_tips and sorted_all.
238
- self .all_tips .del_tx (tx , relax_assert = relax_assert )
244
+ if self .all_tips is not None :
245
+ self .all_tips .del_tx (tx , relax_assert = relax_assert )
239
246
self .sorted_all .del_tx (tx )
240
247
if self .addresses :
241
248
self .addresses .remove_tx (tx )
@@ -249,11 +256,13 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:
249
256
self .mempool_tips .update (tx , remove = True )
250
257
251
258
if tx .is_block :
252
- self .block_tips .del_tx (tx , relax_assert = relax_assert )
253
259
self .sorted_blocks .del_tx (tx )
260
+ if self .block_tips is not None :
261
+ self .block_tips .del_tx (tx , relax_assert = relax_assert )
254
262
else :
255
- self .tx_tips .del_tx (tx , relax_assert = relax_assert )
256
263
self .sorted_txs .del_tx (tx )
264
+ if self .tx_tips is not None :
265
+ self .tx_tips .del_tx (tx , relax_assert = relax_assert )
257
266
258
267
if self .tokens :
259
268
self .tokens .del_tx (tx )
@@ -264,12 +273,11 @@ def __init__(self) -> None:
264
273
from hathor .indexes .memory_height_index import MemoryHeightIndex
265
274
from hathor .indexes .memory_info_index import MemoryInfoIndex
266
275
from hathor .indexes .memory_timestamp_index import MemoryTimestampIndex
267
- from hathor .indexes .memory_tips_index import MemoryTipsIndex
268
276
269
277
self .info = MemoryInfoIndex ()
270
- self .all_tips = MemoryTipsIndex ( scope_type = TipsScopeType . ALL )
271
- self .block_tips = MemoryTipsIndex ( scope_type = TipsScopeType . BLOCKS )
272
- self .tx_tips = MemoryTipsIndex ( scope_type = TipsScopeType . TXS )
278
+ self .all_tips = None
279
+ self .block_tips = None
280
+ self .tx_tips = None
273
281
274
282
self .sorted_all = MemoryTimestampIndex (scope_type = TimestampScopeType .ALL )
275
283
self .sorted_blocks = MemoryTimestampIndex (scope_type = TimestampScopeType .BLOCKS )
@@ -284,6 +292,15 @@ def __init__(self) -> None:
284
292
# XXX: this has to be at the end of __init__, after everything has been initialized
285
293
self .__init_checks__ ()
286
294
295
+ def enable_tips_indexes (self ) -> None :
296
+ from hathor .indexes .memory_tips_index import MemoryTipsIndex
297
+ if self .all_tips is None :
298
+ self .all_tips = MemoryTipsIndex (scope_type = TipsScopeType .ALL )
299
+ if self .block_tips is None :
300
+ self .block_tips = MemoryTipsIndex (scope_type = TipsScopeType .BLOCKS )
301
+ if self .tx_tips is None :
302
+ self .tx_tips = MemoryTipsIndex (scope_type = TipsScopeType .TXS )
303
+
287
304
def enable_address_index (self , pubsub : 'PubSubManager' ) -> None :
288
305
from hathor .indexes .memory_address_index import MemoryAddressIndex
289
306
if self .addresses is None :
@@ -307,7 +324,6 @@ def enable_mempool_index(self) -> None:
307
324
308
325
class RocksDBIndexesManager (IndexesManager ):
309
326
def __init__ (self , rocksdb_storage : 'RocksDBStorage' ) -> None :
310
- from hathor .indexes .partial_rocksdb_tips_index import PartialRocksDBTipsIndex
311
327
from hathor .indexes .rocksdb_height_index import RocksDBHeightIndex
312
328
from hathor .indexes .rocksdb_info_index import RocksDBInfoIndex
313
329
from hathor .indexes .rocksdb_timestamp_index import RocksDBTimestampIndex
@@ -316,9 +332,9 @@ def __init__(self, rocksdb_storage: 'RocksDBStorage') -> None:
316
332
317
333
self .info = RocksDBInfoIndex (self ._db )
318
334
self .height = RocksDBHeightIndex (self ._db )
319
- self .all_tips = PartialRocksDBTipsIndex ( self . _db , scope_type = TipsScopeType . ALL )
320
- self .block_tips = PartialRocksDBTipsIndex ( self . _db , scope_type = TipsScopeType . BLOCKS )
321
- self .tx_tips = PartialRocksDBTipsIndex ( self . _db , scope_type = TipsScopeType . TXS )
335
+ self .all_tips = None
336
+ self .block_tips = None
337
+ self .tx_tips = None
322
338
323
339
self .sorted_all = RocksDBTimestampIndex (self ._db , scope_type = TimestampScopeType .ALL )
324
340
self .sorted_blocks = RocksDBTimestampIndex (self ._db , scope_type = TimestampScopeType .BLOCKS )
@@ -332,6 +348,15 @@ def __init__(self, rocksdb_storage: 'RocksDBStorage') -> None:
332
348
# XXX: this has to be at the end of __init__, after everything has been initialized
333
349
self .__init_checks__ ()
334
350
351
+ def enable_tips_indexes (self ) -> None :
352
+ from hathor .indexes .partial_rocksdb_tips_index import PartialRocksDBTipsIndex
353
+ if self .all_tips is None :
354
+ self .all_tips = PartialRocksDBTipsIndex (self ._db , scope_type = TipsScopeType .ALL )
355
+ if self .block_tips is None :
356
+ self .block_tips = PartialRocksDBTipsIndex (self ._db , scope_type = TipsScopeType .BLOCKS )
357
+ if self .tx_tips is None :
358
+ self .tx_tips = PartialRocksDBTipsIndex (self ._db , scope_type = TipsScopeType .TXS )
359
+
335
360
def enable_address_index (self , pubsub : 'PubSubManager' ) -> None :
336
361
from hathor .indexes .rocksdb_address_index import RocksDBAddressIndex
337
362
if self .addresses is None :
0 commit comments