Skip to content

Commit ab9219e

Browse files
committed
refactor(settings): change daa to use injected settings
1 parent d8e40db commit ab9219e

23 files changed

+248
-242
lines changed

hathor/builder/builder.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from hathor.conf.get_settings import get_settings
2222
from hathor.conf.settings import HathorSettings as HathorSettingsType
2323
from hathor.consensus import ConsensusAlgorithm
24+
from hathor.daa import DifficultyAdjustmentAlgorithm
2425
from hathor.event import EventManager
2526
from hathor.event.storage import EventMemoryStorage, EventRocksDBStorage, EventStorage
2627
from hathor.event.websocket import EventWebsocketFactory
@@ -102,6 +103,8 @@ def __init__(self) -> None:
102103
self._feature_service: Optional[FeatureService] = None
103104
self._bit_signaling_service: Optional[BitSignalingService] = None
104105

106+
self._daa: Optional[DifficultyAdjustmentAlgorithm] = None
107+
105108
self._vertex_verifiers: Optional[VertexVerifiers] = None
106109
self._verification_service: Optional[VerificationService] = None
107110

@@ -162,6 +165,7 @@ def build(self) -> BuildArtifacts:
162165
feature_service = self._get_or_create_feature_service(tx_storage)
163166
bit_signaling_service = self._get_or_create_bit_signaling_service(tx_storage)
164167
verification_service = self._get_or_create_verification_service()
168+
daa = self._get_or_create_daa()
165169

166170
if self._enable_address_index:
167171
indexes.enable_address_index(pubsub)
@@ -186,6 +190,7 @@ def build(self) -> BuildArtifacts:
186190
network=self._network,
187191
pubsub=pubsub,
188192
consensus_algorithm=consensus_algorithm,
193+
daa=daa,
189194
peer_id=peer_id,
190195
tx_storage=tx_storage,
191196
p2p_manager=p2p_manager,
@@ -441,10 +446,18 @@ def _get_or_create_verification_service(self) -> VerificationService:
441446
def _get_or_create_vertex_verifiers(self) -> VertexVerifiers:
442447
if self._vertex_verifiers is None:
443448
settings = self._get_or_create_settings()
444-
self._vertex_verifiers = VertexVerifiers.create(settings=settings)
449+
daa = self._get_or_create_daa()
450+
self._vertex_verifiers = VertexVerifiers.create(settings=settings, daa=daa)
445451

446452
return self._vertex_verifiers
447453

454+
def _get_or_create_daa(self) -> DifficultyAdjustmentAlgorithm:
455+
if self._daa is None:
456+
settings = self._get_or_create_settings()
457+
self._daa = DifficultyAdjustmentAlgorithm(settings=settings)
458+
459+
return self._daa
460+
448461
def use_memory(self) -> 'Builder':
449462
self.check_if_can_modify()
450463
self._storage_type = StorageType.MEMORY
@@ -547,6 +560,11 @@ def set_vertex_verifiers(self, vertex_verifiers: VertexVerifiers) -> 'Builder':
547560
self._vertex_verifiers = vertex_verifiers
548561
return self
549562

563+
def set_daa(self, daa: DifficultyAdjustmentAlgorithm) -> 'Builder':
564+
self.check_if_can_modify()
565+
self._daa = daa
566+
return self
567+
550568
def set_reactor(self, reactor: Reactor) -> 'Builder':
551569
self.check_if_can_modify()
552570
self._reactor = reactor

hathor/builder/cli_builder.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from hathor.cli.run_node import RunNodeArgs
2525
from hathor.consensus import ConsensusAlgorithm
26+
from hathor.daa import DifficultyAdjustmentAlgorithm
2627
from hathor.event import EventManager
2728
from hathor.exception import BuilderError
2829
from hathor.feature_activation.bit_signaling_service import BitSignalingService
@@ -203,7 +204,9 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
203204
not_support_features=self._args.signal_not_support
204205
)
205206

206-
vertex_verifiers = VertexVerifiers.create(settings=settings)
207+
daa = DifficultyAdjustmentAlgorithm(settings=settings)
208+
209+
vertex_verifiers = VertexVerifiers.create(settings=settings, daa=daa)
207210
verification_service = VerificationService(verifiers=vertex_verifiers)
208211

209212
p2p_manager = ConnectionsManager(
@@ -226,6 +229,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
226229
hostname=hostname,
227230
pubsub=pubsub,
228231
consensus_algorithm=consensus_algorithm,
232+
daa=daa,
229233
peer_id=peer_id,
230234
tx_storage=tx_storage,
231235
p2p_manager=p2p_manager,

hathor/cli/events_simulator/scenario.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def simulate_single_chain_one_block(simulator: 'Simulator', manager: 'HathorMana
5050

5151

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

@@ -62,13 +61,13 @@ def simulate_single_chain_blocks_and_transactions(simulator: 'Simulator', manage
6261
simulator.run(60)
6362

6463
tx = gen_new_tx(manager, address, 1000)
65-
tx.weight = daa.minimum_tx_weight(tx)
64+
tx.weight = manager.daa.minimum_tx_weight(tx)
6665
tx.update_hash()
6766
assert manager.propagate_tx(tx, fails_silently=False)
6867
simulator.run(60)
6968

7069
tx = gen_new_tx(manager, address, 2000)
71-
tx.weight = daa.minimum_tx_weight(tx)
70+
tx.weight = manager.daa.minimum_tx_weight(tx)
7271
tx.update_hash()
7372
assert manager.propagate_tx(tx, fails_silently=False)
7473
simulator.run(60)

hathor/cli/mining.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import requests
2626

2727
from hathor.conf.get_settings import get_settings
28+
from hathor.daa import DifficultyAdjustmentAlgorithm
2829
from hathor.verification.block_verifier import BlockVerifier
2930

3031
_SLEEP_ON_ERROR_SECONDS = 5
@@ -138,7 +139,8 @@ def execute(args: Namespace) -> None:
138139

139140
try:
140141
settings = get_settings()
141-
verifier = BlockVerifier(settings=settings)
142+
daa = DifficultyAdjustmentAlgorithm(settings=settings)
143+
verifier = BlockVerifier(settings=settings, daa=daa)
142144
verifier.verify_without_storage(block)
143145
except HathorError:
144146
print('[{}] ERROR: Block has not been pushed because it is not valid.'.format(datetime.datetime.now()))

0 commit comments

Comments
 (0)