Skip to content

Commit

Permalink
refactor: move get settings [part 1]
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Sep 11, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d56924e commit ac0a152
Showing 18 changed files with 92 additions and 109 deletions.
4 changes: 4 additions & 0 deletions hathor/conf/get_settings.py
Original file line number Diff line number Diff line change
@@ -33,6 +33,10 @@ class _SettingsMetadata(NamedTuple):
_settings_singleton: Optional[_SettingsMetadata] = None


def get_settings() -> Settings:
return HathorSettings()


def HathorSettings() -> Settings:
"""
Returns the configuration named tuple.
8 changes: 4 additions & 4 deletions hathor/p2p/sync_v1/agent.py
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
from twisted.internet.interfaces import IConsumer, IDelayedCall, IPushProducer
from zope.interface import implementer

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.p2p.messages import GetNextPayload, GetTipsPayload, NextPayload, ProtocolMessages, TipsPayload
from hathor.p2p.sync_agent import SyncAgent
from hathor.p2p.sync_v1.downloader import Downloader
@@ -34,7 +34,6 @@
from hathor.util import Reactor, json_dumps, json_loads
from hathor.utils.zope import asserted_cast

settings = HathorSettings()
logger = get_logger()

if TYPE_CHECKING:
@@ -191,6 +190,7 @@ def __init__(self, protocol: 'HathorProtocol', downloader: Downloader, reactor:
:param reactor: Reactor to schedule later calls. (default=twisted.internet.reactor)
:type reactor: Reactor
"""
self._settings = get_settings()
self.protocol = protocol
self.manager = protocol.node
self.downloader = downloader
@@ -228,7 +228,7 @@ def __init__(self, protocol: 'HathorProtocol', downloader: Downloader, reactor:

# Maximum difference between our latest timestamp and synced timestamp to consider
# that the peer is synced (in seconds).
self.sync_threshold: int = settings.P2P_SYNC_THRESHOLD
self.sync_threshold: int = self._settings.P2P_SYNC_THRESHOLD

# Indicate whether the sync manager has been started.
self._started: bool = False
@@ -624,7 +624,7 @@ def send_tips(self, timestamp: Optional[int] = None, include_hashes: bool = Fals
"""Try to send a TIPS message. If rate limit has been reached, it schedules to send it later."""
if not self.global_rate_limiter.add_hit(self.GlobalRateLimiter.SEND_TIPS):
self.log.debug('send_tips throttled')
if len(self._send_tips_call_later) >= settings.MAX_GET_TIPS_DELAYED_CALLS:
if len(self._send_tips_call_later) >= self._settings.MAX_GET_TIPS_DELAYED_CALLS:
self.protocol.send_error_and_close_connection(
'Too many GET_TIPS message'
)
10 changes: 5 additions & 5 deletions hathor/p2p/sync_v1/downloader.py
Original file line number Diff line number Diff line change
@@ -20,11 +20,9 @@
from twisted.internet import defer
from twisted.internet.defer import Deferred

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.transaction.storage.exceptions import TransactionDoesNotExist

settings = HathorSettings()

if TYPE_CHECKING:
from hathor.manager import HathorManager
from hathor.p2p.sync_v1.agent import NodeSyncTimestamp
@@ -53,6 +51,7 @@ class TxDetails:
requested_index: int

def __init__(self, tx_id: bytes, deferred: Deferred, connections: list['NodeSyncTimestamp']):
self._settings = get_settings()
self.log = logger.new()
self.tx_id = tx_id
self.deferred = deferred
@@ -109,7 +108,7 @@ def get_connection(self) -> Optional['NodeSyncTimestamp']:
self.retry_count += 1

# only try next peer if we reach max retries on the current one
if self.retry_count >= settings.GET_DATA_RETRIES:
if self.retry_count >= self._settings.GET_DATA_RETRIES:
self.requested_index += 1
self.retry_count = 0

@@ -146,6 +145,7 @@ class Downloader:
window_size: int

def __init__(self, manager: 'HathorManager', window_size: int = 100):
self._settings = get_settings()
self.log = logger.new()
self.manager = manager

@@ -227,7 +227,7 @@ def add_get_downloading_deferred(self, tx_id: bytes, details: TxDetails, connect
# Adding timeout to callback
fn_timeout = partial(self.on_deferred_timeout, tx_id=tx_id)
details.downloading_deferred.addTimeout(
settings.GET_DATA_TIMEOUT,
self._settings.GET_DATA_TIMEOUT,
connection.reactor,
onTimeoutCancel=fn_timeout
)
8 changes: 4 additions & 4 deletions hathor/p2p/sync_v2/agent.py
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.internet.task import LoopingCall

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.p2p.messages import ProtocolMessages
from hathor.p2p.sync_agent import SyncAgent
from hathor.p2p.sync_v2.mempool import SyncMempoolManager
@@ -39,7 +39,6 @@
if TYPE_CHECKING:
from hathor.p2p.protocol import HathorProtocol

settings = HathorSettings()
logger = get_logger()

MAX_GET_TRANSACTIONS_BFS_LEN: int = 8
@@ -66,6 +65,7 @@ def __init__(self, protocol: 'HathorProtocol', reactor: Optional[Reactor] = None
:param reactor: Reactor to schedule later calls. (default=twisted.internet.reactor)
:type reactor: Reactor
"""
self._settings = get_settings()
self.protocol = protocol
self.manager = protocol.node
self.tx_storage = protocol.node.tx_storage
@@ -85,7 +85,7 @@ def __init__(self, protocol: 'HathorProtocol', reactor: Optional[Reactor] = None

# Extra
self._blk_size = 0
self._blk_end_hash = settings.GENESIS_BLOCK_HASH
self._blk_end_hash = self._settings.GENESIS_BLOCK_HASH
self._blk_max_quantity = 0

# indicates whether we're receiving a stream from the peer
@@ -319,7 +319,7 @@ def run_sync_transactions(self) -> None:
# Start with the last received block and find the best block full validated in its chain
block = self._last_received_block
if block is None:
block = cast(Block, self.tx_storage.get_genesis(settings.GENESIS_BLOCK_HASH))
block = cast(Block, self.tx_storage.get_genesis(self._settings.GENESIS_BLOCK_HASH))
else:
with self.tx_storage.allow_partially_validated_context():
while not block.get_metadata().validation.is_valid():
5 changes: 2 additions & 3 deletions hathor/p2p/utils.py
Original file line number Diff line number Diff line change
@@ -28,13 +28,11 @@
from twisted.internet.defer import inlineCallbacks
from twisted.internet.interfaces import IAddress

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.indexes.height_index import HeightInfo
from hathor.p2p.peer_discovery import DNSPeerDiscovery
from hathor.transaction.genesis import GENESIS_HASH

settings = HathorSettings()


def discover_hostname() -> Optional[str]:
""" Try to discover your hostname. It is a synchonous operation and
@@ -83,6 +81,7 @@ def get_genesis_short_hash() -> str:
def get_settings_hello_dict() -> dict[str, Any]:
""" Return a dict of settings values that must be validated in the hello state
"""
settings = get_settings()
settings_dict = {}
for key in settings.P2P_SETTINGS_HASH_FIELDS:
value = getattr(settings, key)
7 changes: 3 additions & 4 deletions hathor/prometheus.py
Original file line number Diff line number Diff line change
@@ -18,14 +18,12 @@
from prometheus_client import CollectorRegistry, Gauge, write_to_textfile
from twisted.internet.task import LoopingCall

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.util import reactor

if TYPE_CHECKING:
from hathor.metrics import Metrics

settings = HathorSettings()

# Define prometheus metrics and it's explanation
METRIC_INFO = {
'transactions': 'Number of transactions',
@@ -79,6 +77,7 @@ def __init__(self, metrics: 'Metrics', path: str, filename: str = 'hathor.prom',
:param filename: Name of the prometheus file (must end in .prom)
:type filename: str
"""
self._settings = get_settings()
self.metrics = metrics
self.metrics_prefix = metrics_prefix

@@ -99,7 +98,7 @@ def __init__(self, metrics: 'Metrics', path: str, filename: str = 'hathor.prom',
self.running: bool = False

# Interval in which the write data method will be called (in seconds)
self.call_interval: int = settings.PROMETHEUS_WRITE_INTERVAL
self.call_interval: int = self._settings.PROMETHEUS_WRITE_INTERVAL

# A timer to periodically write data to prometheus
self._lc_write_data = LoopingCall(self._write_data)
3 changes: 0 additions & 3 deletions hathor/simulator/fake_connection.py
Original file line number Diff line number Diff line change
@@ -20,13 +20,10 @@
from twisted.internet.address import HostnameAddress
from twisted.internet.testing import StringTransport

from hathor.conf import HathorSettings

if TYPE_CHECKING:
from hathor.manager import HathorManager
from hathor.p2p.peer_id import PeerId

settings = HathorSettings()
logger = get_logger()


9 changes: 4 additions & 5 deletions hathor/simulator/miner/geometric_miner.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
import math
from typing import TYPE_CHECKING, Optional

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.manager import HathorEvents
from hathor.simulator.miner.abstract_miner import AbstractMiner
from hathor.util import Random
@@ -25,8 +25,6 @@
from hathor.pubsub import EventArguments
from hathor.transaction import Block

settings = HathorSettings()


class GeometricMiner(AbstractMiner):
""" Simulate block mining with actually solving the block. It is supposed to be used
@@ -46,6 +44,7 @@ def __init__(
blocks than values provided, 0 is used.
"""
super().__init__(manager, rng)
self._settings = get_settings()

self._hashpower = hashpower
self._signal_bits = signal_bits or []
@@ -107,9 +106,9 @@ def _schedule_next_block(self):
else:
dt = 60

if dt > settings.WEIGHT_DECAY_ACTIVATE_DISTANCE:
if dt > self._settings.WEIGHT_DECAY_ACTIVATE_DISTANCE:
self._block = None
dt = settings.WEIGHT_DECAY_ACTIVATE_DISTANCE
dt = self._settings.WEIGHT_DECAY_ACTIVATE_DISTANCE

if self._delayed_call and self._delayed_call.active():
self._delayed_call.cancel()
4 changes: 2 additions & 2 deletions hathor/simulator/simulator.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
from structlog import get_logger

from hathor.builder import BuildArtifacts, Builder
from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.daa import TestMode, _set_test_mode
from hathor.manager import HathorManager
from hathor.p2p.peer_id import PeerId
@@ -116,7 +116,7 @@ def __init__(self, seed: Optional[int] = None):
seed = secrets.randbits(64)
self.seed = seed
self.rng = Random(self.seed)
self.settings = HathorSettings()
self.settings = get_settings()
self._network = 'testnet'
self._clock = MemoryReactorHeapClock()
self._peers: OrderedDict[str, HathorManager] = OrderedDict()
6 changes: 3 additions & 3 deletions hathor/simulator/tx_generator.py
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
from structlog import get_logger

from hathor import daa
from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.transaction.exceptions import RewardLocked
from hathor.util import Random
from hathor.wallet.exceptions import InsufficientFunds
@@ -28,7 +28,6 @@
from hathor.manager import HathorManager
from hathor.transaction import Transaction

settings = HathorSettings()
logger = get_logger()


@@ -45,6 +44,7 @@ def __init__(self, manager: 'HathorManager', rng: Random, *,
:param: rate: Number of transactions per second
:param: hashpower: Number of hashes per second
"""
self._settings = get_settings()
self.manager = manager

# List of addresses to send tokens. If this list is empty, tokens will be sent to an address
@@ -101,7 +101,7 @@ def schedule_next_transaction(self):
def new_tx_step1(self):
""" Generate a new transaction and schedule the mining part of the transaction.
"""
balance = self.manager.wallet.balance[settings.HATHOR_TOKEN_UID]
balance = self.manager.wallet.balance[self._settings.HATHOR_TOKEN_UID]
if balance.available == 0 and self.ignore_no_funds:
self.delayedcall = self.clock.callLater(0, self.schedule_next_transaction)
return
13 changes: 7 additions & 6 deletions hathor/stratum/stratum.py
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
from twisted.protocols.basic import LineReceiver
from twisted.python.failure import Failure

from hathor.conf import HathorSettings
from hathor.conf.get_settings import get_settings
from hathor.crypto.util import decode_address
from hathor.exception import InvalidNewTransaction
from hathor.p2p.utils import format_address
@@ -49,7 +49,6 @@
from hathor.manager import HathorManager # noqa: F401

logger = get_logger()
settings = HathorSettings()


def valid_uuid(uuid: Any) -> bool:
@@ -364,6 +363,7 @@ class StratumProtocol(JSONRPC):

def __init__(self, factory: 'StratumFactory', manager: 'HathorManager', address: IAddress,
id_generator: Optional[Callable[[], Iterator[Union[str, int]]]] = lambda: count()):
self._settings = get_settings()
self.log = logger.new(address=address)
self.factory = factory
self.manager = manager
@@ -374,7 +374,7 @@ def __init__(self, factory: 'StratumFactory', manager: 'HathorManager', address:
self.miner_id = None
self.miner_address = None
self.job_ids = []
self.mine_txs = settings.STRATUM_MINE_TXS_DEFAULT
self.mine_txs = self._settings.STRATUM_MINE_TXS_DEFAULT
self.estimated_hash_rate = 0.0
self.completed_jobs = 0
self.connection_start_time = 0
@@ -665,7 +665,8 @@ def create_job_tx(self, jobid: UUID) -> BaseTransaction:
assert self.miner_id is not None

# Only get first 32 bytes of peer_id because block data is limited to 100 bytes
data = '{}-{}-{}'.format(peer_id[:32], self.miner_id.hex, jobid.hex).encode()[:settings.BLOCK_DATA_MAX_SIZE]
data = '{}-{}-{}'.format(peer_id[:32], self.miner_id.hex, jobid.hex).encode()
data = data[:self._settings.BLOCK_DATA_MAX_SIZE]
block = self.manager.generate_mining_block(data=data, address=self.miner_address,
merge_mined=self.merged_mining)
self.log.debug('prepared block for mining', block=block)
@@ -687,7 +688,7 @@ def calculate_share_weight(self) -> float:
:rtype: float
"""
if len(self.job_ids) <= 1:
return settings.MIN_BLOCK_WEIGHT
return self._settings.MIN_BLOCK_WEIGHT

mn = self.jobs[self.job_ids[0]].tx.timestamp
mx = self.jobs[self.job_ids[-1]].tx.timestamp
@@ -701,7 +702,7 @@ def calculate_share_weight(self) -> float:
hash_rate = acc_weight - log(dt, 2)
self.estimated_hash_rate = hash_rate
share_weight = hash_rate + log(self.AVERAGE_JOB_TIME, 2)
share_weight = max(share_weight, settings.MIN_SHARE_WEIGHT)
share_weight = max(share_weight, self._settings.MIN_SHARE_WEIGHT)
return share_weight

def get_stats(self) -> MinerStatistics:
4 changes: 0 additions & 4 deletions hathor/transaction/__init__.py
Original file line number Diff line number Diff line change
@@ -14,8 +14,6 @@

from hathor.transaction.aux_pow import BitcoinAuxPow
from hathor.transaction.base_transaction import (
MAX_NUM_INPUTS,
MAX_NUM_OUTPUTS,
MAX_OUTPUT_VALUE,
BaseTransaction,
TxInput,
@@ -38,8 +36,6 @@
'TxInput',
'TxOutput',
'TxVersion',
'MAX_NUM_INPUTS',
'MAX_NUM_OUTPUTS',
'MAX_OUTPUT_VALUE',
'sum_weights',
]
Loading

0 comments on commit ac0a152

Please sign in to comment.