Skip to content

Commit 87ae52e

Browse files
authored
refactor: move get settings [part 2] (#769)
1 parent dbeaa9e commit 87ae52e

21 files changed

+84
-89
lines changed

hathor/builder/builder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from structlog import get_logger
1919

2020
from hathor.checkpoint import Checkpoint
21-
from hathor.conf import HathorSettings
21+
from hathor.conf.get_settings import get_settings
2222
from hathor.conf.settings import HathorSettings as HathorSettingsType
2323
from hathor.consensus import ConsensusAlgorithm
2424
from hathor.event import EventManager
@@ -259,7 +259,7 @@ def set_peer_id(self, peer_id: PeerId) -> 'Builder':
259259

260260
def _get_or_create_settings(self) -> HathorSettingsType:
261261
if self._settings is None:
262-
self._settings = HathorSettings()
262+
self._settings = get_settings()
263263
return self._settings
264264

265265
def _get_reactor(self) -> Reactor:

hathor/builder/cli_builder.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def check_or_raise(self, condition: bool, message: str) -> None:
5656

5757
def create_manager(self, reactor: Reactor) -> HathorManager:
5858
import hathor
59-
from hathor.conf import HathorSettings
60-
from hathor.conf.get_settings import get_settings_source
59+
from hathor.conf.get_settings import get_settings, get_settings_source
6160
from hathor.daa import TestMode, _set_test_mode
6261
from hathor.event.storage import EventMemoryStorage, EventRocksDBStorage, EventStorage
6362
from hathor.event.websocket.factory import EventWebsocketFactory
@@ -73,7 +72,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
7372
)
7473
from hathor.util import get_environment_info
7574

76-
settings = HathorSettings()
75+
settings = get_settings()
7776

7877
# only used for logging its location
7978
settings_source = get_settings_source()

hathor/builder/resources_builder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def create_prometheus(self) -> PrometheusMetricsExporter:
7777
return prometheus
7878

7979
def create_resources(self) -> server.Site:
80-
from hathor.conf import HathorSettings
80+
from hathor.conf.get_settings import get_settings
8181
from hathor.debug_resources import (
8282
DebugCrashResource,
8383
DebugLogResource,
@@ -141,7 +141,7 @@ def create_resources(self) -> server.Site:
141141
)
142142
from hathor.websocket import HathorAdminWebsocketFactory, WebsocketStatsResource
143143

144-
settings = HathorSettings()
144+
settings = get_settings()
145145
cpu = get_cpu_profiler()
146146

147147
# TODO get this from a file. How should we do with the factory?

hathor/cli/db_export.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def register_signal_handlers(self) -> None:
3434

3535
@classmethod
3636
def create_parser(cls) -> ArgumentParser:
37-
from hathor.conf import HathorSettings
38-
settings = HathorSettings()
37+
from hathor.conf.get_settings import get_settings
38+
settings = get_settings()
3939

4040
def max_height(arg: str) -> Optional[int]:
4141
if arg.lower() == 'checkpoint':
@@ -80,8 +80,8 @@ def prepare(self, *, register_resources: bool = True) -> None:
8080
self.skip_voided = self._args.export_skip_voided
8181

8282
def iter_tx(self) -> Iterator['BaseTransaction']:
83-
from hathor.conf import HathorSettings
84-
settings = HathorSettings()
83+
from hathor.conf.get_settings import get_settings
84+
settings = get_settings()
8585
soft_voided_ids = set(settings.SOFT_VOIDED_TX_IDS)
8686

8787
for tx in self._iter_tx:

hathor/cli/events_simulator/scenario.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ def simulate_single_chain_one_block(simulator: 'Simulator', manager: 'HathorMana
5151

5252
def simulate_single_chain_blocks_and_transactions(simulator: 'Simulator', manager: 'HathorManager') -> None:
5353
from hathor import daa
54-
from hathor.conf import HathorSettings
54+
from hathor.conf.get_settings import get_settings
5555
from tests.utils import add_new_blocks, gen_new_tx
5656

57-
settings = HathorSettings()
57+
settings = get_settings()
5858
assert manager.wallet is not None
5959
address = manager.wallet.get_unused_address(mark_as_used=False)
6060

hathor/cli/nginx_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ def generate_nginx_config(openapi: dict[str, Any], *, out_file: TextIO, rate_k:
115115
"""
116116
from datetime import datetime
117117

118-
from hathor.conf import HathorSettings
118+
from hathor.conf.get_settings import get_settings
119119

120-
settings = HathorSettings()
120+
settings = get_settings()
121121
api_prefix = settings.API_VERSION_PREFIX
122122

123123
locations: dict[str, dict[str, Any]] = {}

hathor/cli/run_node.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ def prepare(self, *, register_resources: bool = True) -> None:
159159
assert self.manager.stratum_factory is not None
160160
self.reactor.listenTCP(self._args.stratum, self.manager.stratum_factory)
161161

162-
from hathor.conf import HathorSettings
163-
settings = HathorSettings()
162+
from hathor.conf.get_settings import get_settings
163+
settings = get_settings()
164164

165165
if register_resources:
166166
resources_builder = ResourcesBuilder(
@@ -204,8 +204,8 @@ def start_sentry_if_possible(self) -> None:
204204
sys.exit(-3)
205205

206206
import hathor
207-
from hathor.conf import HathorSettings
208-
settings = HathorSettings()
207+
from hathor.conf.get_settings import get_settings
208+
settings = get_settings()
209209
sentry_sdk.init(
210210
dsn=self._args.sentry_dsn,
211211
release=hathor.__version__,

hathor/consensus/block_consensus.py

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

1818
from structlog import get_logger
1919

20-
from hathor.conf import HathorSettings
20+
from hathor.conf.get_settings import get_settings
2121
from hathor.profiler import get_cpu_profiler
2222
from hathor.transaction import BaseTransaction, Block, Transaction, sum_weights
2323
from hathor.util import classproperty, not_none
@@ -26,7 +26,6 @@
2626
from hathor.consensus.context import ConsensusAlgorithmContext
2727

2828
logger = get_logger()
29-
settings = HathorSettings()
3029
cpu = get_cpu_profiler()
3130

3231
_base_transaction_log = logger.new()
@@ -36,6 +35,7 @@ class BlockConsensusAlgorithm:
3635
"""Implement the consensus algorithm for blocks."""
3736

3837
def __init__(self, context: 'ConsensusAlgorithmContext') -> None:
38+
self._settings = get_settings()
3939
self.context = context
4040

4141
@classproperty
@@ -149,7 +149,7 @@ def update_voided_info(self, block: Block) -> None:
149149
storage.indexes.height.add_new(block.get_height(), block.hash, block.timestamp)
150150
storage.update_best_block_tips_cache([block.hash])
151151
# The following assert must be true, but it is commented out for performance reasons.
152-
if settings.SLOW_ASSERTS:
152+
if self._settings.SLOW_ASSERTS:
153153
assert len(storage.get_best_block_tips(skip_cache=True)) == 1
154154
else:
155155
# Resolve all other cases, but (i).
@@ -179,7 +179,7 @@ def update_voided_info(self, block: Block) -> None:
179179
score = self.calculate_score(block)
180180

181181
# Finally, check who the winner is.
182-
if score <= best_score - settings.WEIGHT_TOL:
182+
if score <= best_score - self._settings.WEIGHT_TOL:
183183
# Just update voided_by from parents.
184184
self.update_voided_by_from_parents(block)
185185

@@ -200,7 +200,7 @@ def update_voided_info(self, block: Block) -> None:
200200
common_block = self._find_first_parent_in_best_chain(block)
201201
self.add_voided_by_to_multiple_chains(block, heads, common_block)
202202

203-
if score >= best_score + settings.WEIGHT_TOL:
203+
if score >= best_score + self._settings.WEIGHT_TOL:
204204
# We have a new winner candidate.
205205
self.update_score_and_mark_as_the_best_chain_if_possible(block)
206206
# As `update_score_and_mark_as_the_best_chain_if_possible` may affect `voided_by`,
@@ -294,10 +294,10 @@ def update_score_and_mark_as_the_best_chain_if_possible(self, block: Block) -> N
294294
best_heads: list[Block]
295295
for head in heads:
296296
head_meta = head.get_metadata(force_reload=True)
297-
if head_meta.score <= best_score - settings.WEIGHT_TOL:
297+
if head_meta.score <= best_score - self._settings.WEIGHT_TOL:
298298
continue
299299

300-
if head_meta.score >= best_score + settings.WEIGHT_TOL:
300+
if head_meta.score >= best_score + self._settings.WEIGHT_TOL:
301301
best_heads = [head]
302302
best_score = head_meta.score
303303
else:

hathor/consensus/consensus.py

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

1515
from structlog import get_logger
1616

17-
from hathor.conf import HathorSettings
17+
from hathor.conf.get_settings import get_settings
1818
from hathor.consensus.block_consensus import BlockConsensusAlgorithmFactory
1919
from hathor.consensus.context import ConsensusAlgorithmContext
2020
from hathor.consensus.transaction_consensus import TransactionConsensusAlgorithmFactory
@@ -24,7 +24,6 @@
2424
from hathor.util import not_none
2525

2626
logger = get_logger()
27-
settings = HathorSettings()
2827
cpu = get_cpu_profiler()
2928

3029
_base_transaction_log = logger.new()
@@ -57,6 +56,7 @@ class ConsensusAlgorithm:
5756
"""
5857

5958
def __init__(self, soft_voided_tx_ids: set[bytes], pubsub: PubSubManager) -> None:
59+
self._settings = get_settings()
6060
self.log = logger.new()
6161
self._pubsub = pubsub
6262
self.soft_voided_tx_ids = frozenset(soft_voided_tx_ids)
@@ -76,7 +76,7 @@ def update(self, base: BaseTransaction) -> None:
7676
try:
7777
self._unsafe_update(base)
7878
except Exception:
79-
meta.add_voided_by(settings.CONSENSUS_FAIL_ID)
79+
meta.add_voided_by(self._settings.CONSENSUS_FAIL_ID)
8080
assert base.storage is not None
8181
base.storage.save_transaction(base, only_metadata=True)
8282
raise
@@ -87,7 +87,7 @@ def _unsafe_update(self, base: BaseTransaction) -> None:
8787

8888
# XXX: first make sure we can run the consensus update on this tx:
8989
meta = base.get_metadata()
90-
assert meta.voided_by is None or (settings.PARTIALLY_VALIDATED_ID not in meta.voided_by)
90+
assert meta.voided_by is None or (self._settings.PARTIALLY_VALIDATED_ID not in meta.voided_by)
9191
assert meta.validation.is_fully_connected()
9292

9393
# this context instance will live only while this update is running
@@ -152,9 +152,9 @@ def filter_out_soft_voided_entries(self, tx: BaseTransaction, voided_by: set[byt
152152
return voided_by
153153
ret = set()
154154
for h in voided_by:
155-
if h == settings.SOFT_VOIDED_ID:
155+
if h == self._settings.SOFT_VOIDED_ID:
156156
continue
157-
if h == settings.CONSENSUS_FAIL_ID:
157+
if h == self._settings.CONSENSUS_FAIL_ID:
158158
continue
159159
if h == tx.hash:
160160
continue

hathor/consensus/context.py

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

1717
from structlog import get_logger
1818

19-
from hathor.conf import HathorSettings
2019
from hathor.profiler import get_cpu_profiler
2120
from hathor.pubsub import PubSubManager
2221
from hathor.transaction import BaseTransaction, Block
@@ -27,7 +26,6 @@
2726
from hathor.consensus.transaction_consensus import TransactionConsensusAlgorithm
2827

2928
logger = get_logger()
30-
settings = HathorSettings()
3129
cpu = get_cpu_profiler()
3230

3331
_base_transaction_log = logger.new()

hathor/consensus/transaction_consensus.py

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

1717
from structlog import get_logger
1818

19-
from hathor.conf import HathorSettings
19+
from hathor.conf.get_settings import get_settings
2020
from hathor.profiler import get_cpu_profiler
2121
from hathor.transaction import BaseTransaction, Block, Transaction, TxInput, sum_weights
2222
from hathor.util import classproperty
@@ -25,7 +25,6 @@
2525
from hathor.consensus.context import ConsensusAlgorithmContext
2626

2727
logger = get_logger()
28-
settings = HathorSettings()
2928
cpu = get_cpu_profiler()
3029

3130
_base_transaction_log = logger.new()
@@ -35,6 +34,7 @@ class TransactionConsensusAlgorithm:
3534
"""Implement the consensus algorithm for transactions."""
3635

3736
def __init__(self, context: 'ConsensusAlgorithmContext') -> None:
37+
self._settings = get_settings()
3838
self.context = context
3939

4040
@classproperty
@@ -180,7 +180,7 @@ def update_voided_info(self, tx: Transaction) -> None:
180180
parent_meta = parent.get_metadata()
181181
if parent_meta.voided_by:
182182
voided_by.update(self.context.consensus.filter_out_soft_voided_entries(parent, parent_meta.voided_by))
183-
assert settings.SOFT_VOIDED_ID not in voided_by
183+
assert self._settings.SOFT_VOIDED_ID not in voided_by
184184
assert not (self.context.consensus.soft_voided_tx_ids & voided_by)
185185

186186
# Union of voided_by of inputs
@@ -189,13 +189,13 @@ def update_voided_info(self, tx: Transaction) -> None:
189189
spent_meta = spent_tx.get_metadata()
190190
if spent_meta.voided_by:
191191
voided_by.update(spent_meta.voided_by)
192-
voided_by.discard(settings.SOFT_VOIDED_ID)
193-
assert settings.SOFT_VOIDED_ID not in voided_by
192+
voided_by.discard(self._settings.SOFT_VOIDED_ID)
193+
assert self._settings.SOFT_VOIDED_ID not in voided_by
194194

195195
# Update accumulated weight of the transactions voiding us.
196196
assert tx.hash not in voided_by
197197
for h in voided_by:
198-
if h == settings.SOFT_VOIDED_ID:
198+
if h == self._settings.SOFT_VOIDED_ID:
199199
continue
200200
tx2 = tx.storage.get_transaction(h)
201201
tx2_meta = tx2.get_metadata()
@@ -207,7 +207,7 @@ def update_voided_info(self, tx: Transaction) -> None:
207207
assert not meta.voided_by or meta.voided_by == {tx.hash}
208208
assert meta.accumulated_weight == tx.weight
209209
if tx.hash in self.context.consensus.soft_voided_tx_ids:
210-
voided_by.add(settings.SOFT_VOIDED_ID)
210+
voided_by.add(self._settings.SOFT_VOIDED_ID)
211211
voided_by.add(tx.hash)
212212
if meta.conflict_with:
213213
voided_by.add(tx.hash)
@@ -221,7 +221,7 @@ def update_voided_info(self, tx: Transaction) -> None:
221221

222222
# Check conflicts of the transactions voiding us.
223223
for h in voided_by:
224-
if h == settings.SOFT_VOIDED_ID:
224+
if h == self._settings.SOFT_VOIDED_ID:
225225
continue
226226
if h == tx.hash:
227227
continue
@@ -300,7 +300,7 @@ def check_conflicts(self, tx: Transaction) -> None:
300300
candidate.update_accumulated_weight(stop_value=meta.accumulated_weight)
301301
tx_meta = candidate.get_metadata()
302302
d = tx_meta.accumulated_weight - meta.accumulated_weight
303-
if abs(d) < settings.WEIGHT_TOL:
303+
if abs(d) < self._settings.WEIGHT_TOL:
304304
tie_list.append(candidate)
305305
elif d > 0:
306306
is_highest = False

0 commit comments

Comments
 (0)