Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from structlog import get_logger

from hathor.checkpoint import Checkpoint
from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.conf.settings import HathorSettings as HathorSettingsType
from hathor.consensus import ConsensusAlgorithm
from hathor.event import EventManager
Expand Down Expand Up @@ -259,7 +259,7 @@ def set_peer_id(self, peer_id: PeerId) -> 'Builder':

def _get_or_create_settings(self) -> HathorSettingsType:
if self._settings is None:
self._settings = HathorSettings()
self._settings = get_settings()
return self._settings

def _get_reactor(self) -> Reactor:
Expand Down
5 changes: 2 additions & 3 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def check_or_raise(self, condition: bool, message: str) -> None:

def create_manager(self, reactor: Reactor) -> HathorManager:
import hathor
from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings_source
from hathor.conf.get_settings import get_settings, get_settings_source
from hathor.daa import TestMode, _set_test_mode
from hathor.event.storage import EventMemoryStorage, EventRocksDBStorage, EventStorage
from hathor.event.websocket.factory import EventWebsocketFactory
Expand All @@ -73,7 +72,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
)
from hathor.util import get_environment_info

settings = HathorSettings()
settings = get_settings()

# only used for logging its location
settings_source = get_settings_source()
Expand Down
4 changes: 2 additions & 2 deletions hathor/builder/resources_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create_prometheus(self) -> PrometheusMetricsExporter:
return prometheus

def create_resources(self) -> server.Site:
from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.debug_resources import (
DebugCrashResource,
DebugLogResource,
Expand Down Expand Up @@ -141,7 +141,7 @@ def create_resources(self) -> server.Site:
)
from hathor.websocket import HathorAdminWebsocketFactory, WebsocketStatsResource

settings = HathorSettings()
settings = get_settings()
cpu = get_cpu_profiler()

# TODO get this from a file. How should we do with the factory?
Expand Down
8 changes: 4 additions & 4 deletions hathor/cli/db_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def register_signal_handlers(self) -> None:

@classmethod
def create_parser(cls) -> ArgumentParser:
from hathor.conf import HathorSettings
settings = HathorSettings()
from hathor.conf.get_settings import get_settings
settings = get_settings()

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

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

for tx in self._iter_tx:
Expand Down
4 changes: 2 additions & 2 deletions hathor/cli/events_simulator/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def simulate_single_chain_one_block(simulator: 'Simulator', manager: 'HathorMana

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

settings = HathorSettings()
settings = get_settings()
assert manager.wallet is not None
address = manager.wallet.get_unused_address(mark_as_used=False)

Expand Down
4 changes: 2 additions & 2 deletions hathor/cli/nginx_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ def generate_nginx_config(openapi: dict[str, Any], *, out_file: TextIO, rate_k:
"""
from datetime import datetime

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings

settings = HathorSettings()
settings = get_settings()
api_prefix = settings.API_VERSION_PREFIX

locations: dict[str, dict[str, Any]] = {}
Expand Down
8 changes: 4 additions & 4 deletions hathor/cli/run_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ def prepare(self, *, register_resources: bool = True) -> None:
assert self.manager.stratum_factory is not None
self.reactor.listenTCP(self._args.stratum, self.manager.stratum_factory)

from hathor.conf import HathorSettings
settings = HathorSettings()
from hathor.conf.get_settings import get_settings
settings = get_settings()

if register_resources:
resources_builder = ResourcesBuilder(
Expand Down Expand Up @@ -204,8 +204,8 @@ def start_sentry_if_possible(self) -> None:
sys.exit(-3)

import hathor
from hathor.conf import HathorSettings
settings = HathorSettings()
from hathor.conf.get_settings import get_settings
settings = get_settings()
sentry_sdk.init(
dsn=self._args.sentry_dsn,
release=hathor.__version__,
Expand Down
14 changes: 7 additions & 7 deletions hathor/consensus/block_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from structlog import get_logger

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.profiler import get_cpu_profiler
from hathor.transaction import BaseTransaction, Block, Transaction, sum_weights
from hathor.util import classproperty, not_none
Expand All @@ -26,7 +26,6 @@
from hathor.consensus.context import ConsensusAlgorithmContext

logger = get_logger()
settings = HathorSettings()
cpu = get_cpu_profiler()

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

def __init__(self, context: 'ConsensusAlgorithmContext') -> None:
self._settings = get_settings()
self.context = context

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

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

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

if score >= best_score + settings.WEIGHT_TOL:
if score >= best_score + self._settings.WEIGHT_TOL:
# We have a new winner candidate.
self.update_score_and_mark_as_the_best_chain_if_possible(block)
# As `update_score_and_mark_as_the_best_chain_if_possible` may affect `voided_by`,
Expand Down Expand Up @@ -294,10 +294,10 @@ def update_score_and_mark_as_the_best_chain_if_possible(self, block: Block) -> N
best_heads: list[Block]
for head in heads:
head_meta = head.get_metadata(force_reload=True)
if head_meta.score <= best_score - settings.WEIGHT_TOL:
if head_meta.score <= best_score - self._settings.WEIGHT_TOL:
continue

if head_meta.score >= best_score + settings.WEIGHT_TOL:
if head_meta.score >= best_score + self._settings.WEIGHT_TOL:
best_heads = [head]
best_score = head_meta.score
else:
Expand Down
12 changes: 6 additions & 6 deletions hathor/consensus/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from structlog import get_logger

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.consensus.block_consensus import BlockConsensusAlgorithmFactory
from hathor.consensus.context import ConsensusAlgorithmContext
from hathor.consensus.transaction_consensus import TransactionConsensusAlgorithmFactory
Expand All @@ -24,7 +24,6 @@
from hathor.util import not_none

logger = get_logger()
settings = HathorSettings()
cpu = get_cpu_profiler()

_base_transaction_log = logger.new()
Expand Down Expand Up @@ -57,6 +56,7 @@ class ConsensusAlgorithm:
"""

def __init__(self, soft_voided_tx_ids: set[bytes], pubsub: PubSubManager) -> None:
self._settings = get_settings()
self.log = logger.new()
self._pubsub = pubsub
self.soft_voided_tx_ids = frozenset(soft_voided_tx_ids)
Expand All @@ -76,7 +76,7 @@ def update(self, base: BaseTransaction) -> None:
try:
self._unsafe_update(base)
except Exception:
meta.add_voided_by(settings.CONSENSUS_FAIL_ID)
meta.add_voided_by(self._settings.CONSENSUS_FAIL_ID)
assert base.storage is not None
base.storage.save_transaction(base, only_metadata=True)
raise
Expand All @@ -87,7 +87,7 @@ def _unsafe_update(self, base: BaseTransaction) -> None:

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

# this context instance will live only while this update is running
Expand Down Expand Up @@ -152,9 +152,9 @@ def filter_out_soft_voided_entries(self, tx: BaseTransaction, voided_by: set[byt
return voided_by
ret = set()
for h in voided_by:
if h == settings.SOFT_VOIDED_ID:
if h == self._settings.SOFT_VOIDED_ID:
continue
if h == settings.CONSENSUS_FAIL_ID:
if h == self._settings.CONSENSUS_FAIL_ID:
continue
if h == tx.hash:
continue
Expand Down
2 changes: 0 additions & 2 deletions hathor/consensus/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from structlog import get_logger

from hathor.conf import HathorSettings
from hathor.profiler import get_cpu_profiler
from hathor.pubsub import PubSubManager
from hathor.transaction import BaseTransaction, Block
Expand All @@ -27,7 +26,6 @@
from hathor.consensus.transaction_consensus import TransactionConsensusAlgorithm

logger = get_logger()
settings = HathorSettings()
cpu = get_cpu_profiler()

_base_transaction_log = logger.new()
Expand Down
18 changes: 9 additions & 9 deletions hathor/consensus/transaction_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from structlog import get_logger

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.profiler import get_cpu_profiler
from hathor.transaction import BaseTransaction, Block, Transaction, TxInput, sum_weights
from hathor.util import classproperty
Expand All @@ -25,7 +25,6 @@
from hathor.consensus.context import ConsensusAlgorithmContext

logger = get_logger()
settings = HathorSettings()
cpu = get_cpu_profiler()

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

def __init__(self, context: 'ConsensusAlgorithmContext') -> None:
self._settings = get_settings()
self.context = context

@classproperty
Expand Down Expand Up @@ -180,7 +180,7 @@ def update_voided_info(self, tx: Transaction) -> None:
parent_meta = parent.get_metadata()
if parent_meta.voided_by:
voided_by.update(self.context.consensus.filter_out_soft_voided_entries(parent, parent_meta.voided_by))
assert settings.SOFT_VOIDED_ID not in voided_by
assert self._settings.SOFT_VOIDED_ID not in voided_by
assert not (self.context.consensus.soft_voided_tx_ids & voided_by)

# Union of voided_by of inputs
Expand All @@ -189,13 +189,13 @@ def update_voided_info(self, tx: Transaction) -> None:
spent_meta = spent_tx.get_metadata()
if spent_meta.voided_by:
voided_by.update(spent_meta.voided_by)
voided_by.discard(settings.SOFT_VOIDED_ID)
assert settings.SOFT_VOIDED_ID not in voided_by
voided_by.discard(self._settings.SOFT_VOIDED_ID)
assert self._settings.SOFT_VOIDED_ID not in voided_by

# Update accumulated weight of the transactions voiding us.
assert tx.hash not in voided_by
for h in voided_by:
if h == settings.SOFT_VOIDED_ID:
if h == self._settings.SOFT_VOIDED_ID:
continue
tx2 = tx.storage.get_transaction(h)
tx2_meta = tx2.get_metadata()
Expand All @@ -207,7 +207,7 @@ def update_voided_info(self, tx: Transaction) -> None:
assert not meta.voided_by or meta.voided_by == {tx.hash}
assert meta.accumulated_weight == tx.weight
if tx.hash in self.context.consensus.soft_voided_tx_ids:
voided_by.add(settings.SOFT_VOIDED_ID)
voided_by.add(self._settings.SOFT_VOIDED_ID)
voided_by.add(tx.hash)
if meta.conflict_with:
voided_by.add(tx.hash)
Expand All @@ -221,7 +221,7 @@ def update_voided_info(self, tx: Transaction) -> None:

# Check conflicts of the transactions voiding us.
for h in voided_by:
if h == settings.SOFT_VOIDED_ID:
if h == self._settings.SOFT_VOIDED_ID:
continue
if h == tx.hash:
continue
Expand Down Expand Up @@ -300,7 +300,7 @@ def check_conflicts(self, tx: Transaction) -> None:
candidate.update_accumulated_weight(stop_value=meta.accumulated_weight)
tx_meta = candidate.get_metadata()
d = tx_meta.accumulated_weight - meta.accumulated_weight
if abs(d) < settings.WEIGHT_TOL:
if abs(d) < self._settings.WEIGHT_TOL:
tie_list.append(candidate)
elif d > 0:
is_highest = False
Expand Down
Loading