Skip to content

Commit 22cc07e

Browse files
committed
refactor(storage): change storages to use injected settings
1 parent f95d189 commit 22cc07e

File tree

8 files changed

+50
-19
lines changed

8 files changed

+50
-19
lines changed

hathor/builder/builder.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ def _get_or_create_indexes_manager(self) -> IndexesManager:
435435

436436
def _get_or_create_tx_storage(self) -> TransactionStorage:
437437
indexes = self._get_or_create_indexes_manager()
438+
settings = self._get_or_create_settings()
438439

439440
if self._tx_storage is not None:
440441
# If a tx storage is provided, set the indexes manager to it.
@@ -446,11 +447,11 @@ def _get_or_create_tx_storage(self) -> TransactionStorage:
446447
store_indexes = None
447448

448449
if self._storage_type == StorageType.MEMORY:
449-
self._tx_storage = TransactionMemoryStorage(indexes=store_indexes)
450+
self._tx_storage = TransactionMemoryStorage(indexes=store_indexes, settings=settings)
450451

451452
elif self._storage_type == StorageType.ROCKSDB:
452453
rocksdb_storage = self._get_or_create_rocksdb_storage()
453-
self._tx_storage = TransactionRocksDBStorage(rocksdb_storage, indexes=store_indexes)
454+
self._tx_storage = TransactionRocksDBStorage(rocksdb_storage, indexes=store_indexes, settings=settings)
454455

455456
else:
456457
raise NotImplementedError

hathor/builder/cli_builder.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
127127
self.check_or_raise(not self._args.data, '--data should not be used with --memory-storage')
128128
# if using MemoryStorage, no need to have cache
129129
indexes = MemoryIndexesManager()
130-
tx_storage = TransactionMemoryStorage(indexes)
130+
tx_storage = TransactionMemoryStorage(indexes, settings=settings)
131131
event_storage = EventMemoryStorage()
132132
self.check_or_raise(not self._args.x_rocksdb_indexes, 'RocksDB indexes require RocksDB data')
133133
self.log.info('with storage', storage_class=type(tx_storage).__name__)
@@ -150,14 +150,14 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
150150
# We should only pass indexes if cache is disabled. Otherwise,
151151
# only TransactionCacheStorage should have indexes.
152152
kwargs['indexes'] = indexes
153-
tx_storage = TransactionRocksDBStorage(self.rocksdb_storage, **kwargs)
153+
tx_storage = TransactionRocksDBStorage(self.rocksdb_storage, settings=settings, **kwargs)
154154
event_storage = EventRocksDBStorage(self.rocksdb_storage)
155155
feature_storage = FeatureActivationStorage(settings=settings, rocksdb_storage=self.rocksdb_storage)
156156

157157
self.log.info('with storage', storage_class=type(tx_storage).__name__, path=self._args.data)
158158
if self._args.cache:
159159
self.check_or_raise(not self._args.memory_storage, '--cache should not be used with --memory-storage')
160-
tx_storage = TransactionCacheStorage(tx_storage, reactor, indexes=indexes)
160+
tx_storage = TransactionCacheStorage(tx_storage, reactor, indexes=indexes, settings=settings)
161161
if self._args.cache_size:
162162
tx_storage.capacity = self._args.cache_size
163163
if self._args.cache_interval:

hathor/transaction/storage/cache_storage.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from twisted.internet import threads
1919

20+
from hathor.conf.settings import HathorSettings
2021
from hathor.indexes import IndexesManager
2122
from hathor.reactor import ReactorProtocol as Reactor
2223
from hathor.transaction import BaseTransaction
@@ -32,8 +33,17 @@ class TransactionCacheStorage(BaseTransactionStorage):
3233
cache: OrderedDict[bytes, BaseTransaction]
3334
dirty_txs: set[bytes]
3435

35-
def __init__(self, store: 'BaseTransactionStorage', reactor: Reactor, interval: int = 5,
36-
capacity: int = 10000, *, indexes: Optional[IndexesManager], _clone_if_needed: bool = False):
36+
def __init__(
37+
self,
38+
store: 'BaseTransactionStorage',
39+
reactor: Reactor,
40+
interval: int = 5,
41+
capacity: int = 10000,
42+
*,
43+
settings: HathorSettings,
44+
indexes: Optional[IndexesManager],
45+
_clone_if_needed: bool = False,
46+
) -> None:
3747
"""
3848
:param store: a subclass of BaseTransactionStorage
3949
:type store: :py:class:`hathor.transaction.storage.BaseTransactionStorage`
@@ -68,7 +78,7 @@ def __init__(self, store: 'BaseTransactionStorage', reactor: Reactor, interval:
6878

6979
# we need to use only one weakref dict, so we must first initialize super, and then
7080
# attribute the same weakref for both.
71-
super().__init__(indexes=indexes)
81+
super().__init__(indexes=indexes, settings=settings)
7282
self._tx_weakref = store._tx_weakref
7383
# XXX: just to make sure this isn't being used anywhere, setters/getters should be used instead
7484
del self._allow_scope

hathor/transaction/storage/memory_storage.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from typing import Any, Iterator, Optional, TypeVar
1616

17+
from hathor.conf.settings import HathorSettings
1718
from hathor.indexes import IndexesManager
1819
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
1920
from hathor.transaction.storage.migrations import MigrationState
@@ -25,7 +26,13 @@
2526

2627

2728
class TransactionMemoryStorage(BaseTransactionStorage):
28-
def __init__(self, indexes: Optional[IndexesManager] = None, *, _clone_if_needed: bool = False) -> None:
29+
def __init__(
30+
self,
31+
indexes: Optional[IndexesManager] = None,
32+
*,
33+
settings: HathorSettings,
34+
_clone_if_needed: bool = False,
35+
) -> None:
2936
"""
3037
:param _clone_if_needed: *private parameter*, defaults to True, controls whether to clone
3138
transaction/blocks/metadata when returning those objects.
@@ -36,7 +43,7 @@ def __init__(self, indexes: Optional[IndexesManager] = None, *, _clone_if_needed
3643
# Store custom key/value attributes
3744
self.attributes: dict[str, Any] = {}
3845
self._clone_if_needed = _clone_if_needed
39-
super().__init__(indexes=indexes)
46+
super().__init__(indexes=indexes, settings=settings)
4047

4148
def _check_and_set_network(self) -> None:
4249
# XXX: does not apply to memory storage, can safely be ignored

hathor/transaction/storage/rocksdb_storage.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from structlog import get_logger
1818

19+
from hathor.conf.settings import HathorSettings
1920
from hathor.indexes import IndexesManager
2021
from hathor.storage import RocksDBStorage
2122
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
@@ -43,15 +44,21 @@ class TransactionRocksDBStorage(BaseTransactionStorage):
4344
It uses Protobuf serialization internally.
4445
"""
4546

46-
def __init__(self, rocksdb_storage: RocksDBStorage, indexes: Optional[IndexesManager] = None):
47+
def __init__(
48+
self,
49+
rocksdb_storage: RocksDBStorage,
50+
indexes: Optional[IndexesManager] = None,
51+
*,
52+
settings: HathorSettings,
53+
) -> None:
4754
self._cf_tx = rocksdb_storage.get_or_create_column_family(_CF_NAME_TX)
4855
self._cf_meta = rocksdb_storage.get_or_create_column_family(_CF_NAME_META)
4956
self._cf_attr = rocksdb_storage.get_or_create_column_family(_CF_NAME_ATTR)
5057
self._cf_migrations = rocksdb_storage.get_or_create_column_family(_CF_NAME_MIGRATIONS)
5158

5259
self._rocksdb_storage = rocksdb_storage
5360
self._db = rocksdb_storage.get_db()
54-
super().__init__(indexes=indexes)
61+
super().__init__(indexes=indexes, settings=settings)
5562

5663
def _load_from_bytes(self, tx_data: bytes, meta_data: bytes) -> 'BaseTransaction':
5764
from hathor.transaction.base_transaction import tx_or_block_from_bytes

hathor/transaction/storage/transaction_storage.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from intervaltree.interval import Interval
2424
from structlog import get_logger
2525

26-
from hathor.conf.get_settings import get_global_settings
26+
from hathor.conf.settings import HathorSettings
2727
from hathor.execution_manager import ExecutionManager
2828
from hathor.indexes import IndexesManager
2929
from hathor.indexes.height_index import HeightInfo
@@ -104,8 +104,8 @@ class TransactionStorage(ABC):
104104

105105
_migrations: list[BaseMigration]
106106

107-
def __init__(self) -> None:
108-
self._settings = get_global_settings()
107+
def __init__(self, *, settings: HathorSettings) -> None:
108+
self._settings = settings
109109
# Weakref is used to guarantee that there is only one instance of each transaction in memory.
110110
self._tx_weakref: WeakValueDictionary[bytes, BaseTransaction] = WeakValueDictionary()
111111
self._tx_weakref_disabled: bool = False
@@ -1165,8 +1165,14 @@ def get_block(self, block_id: VertexId) -> Block:
11651165
class BaseTransactionStorage(TransactionStorage):
11661166
indexes: Optional[IndexesManager]
11671167

1168-
def __init__(self, indexes: Optional[IndexesManager] = None, pubsub: Optional[Any] = None) -> None:
1169-
super().__init__()
1168+
def __init__(
1169+
self,
1170+
indexes: Optional[IndexesManager] = None,
1171+
pubsub: Optional[Any] = None,
1172+
*,
1173+
settings: HathorSettings,
1174+
) -> None:
1175+
super().__init__(settings=settings)
11701176

11711177
# Pubsub is used to publish tx voided and winner but it's optional
11721178
self.pubsub = pubsub

tests/consensus/test_consensus.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BaseConsensusTestCase(unittest.TestCase):
1313

1414
def setUp(self) -> None:
1515
super().setUp()
16-
self.tx_storage = TransactionMemoryStorage()
16+
self.tx_storage = TransactionMemoryStorage(settings=self._settings)
1717
self.genesis = self.tx_storage.get_all_genesis()
1818
self.genesis_blocks = [tx for tx in self.genesis if tx.is_block]
1919
self.genesis_txs = [tx for tx in self.genesis if not tx.is_block]

tests/tx/test_genesis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def setUp(self) -> None:
3434
self._daa = DifficultyAdjustmentAlgorithm(settings=self._settings)
3535
verifiers = VertexVerifiers.create_defaults(settings=self._settings, daa=self._daa, feature_service=Mock())
3636
self._verification_service = VerificationService(settings=self._settings, verifiers=verifiers)
37-
self.storage = TransactionMemoryStorage()
37+
self.storage = TransactionMemoryStorage(settings=settings)
3838

3939
def test_pow(self):
4040
verifier = VertexVerifier(settings=self._settings)

0 commit comments

Comments
 (0)