Skip to content

Commit

Permalink
refactor(settings): change daa to use injected settings
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Nov 3, 2023
1 parent 98121c4 commit d2f2993
Show file tree
Hide file tree
Showing 26 changed files with 285 additions and 254 deletions.
38 changes: 30 additions & 8 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# limitations under the License.

from enum import Enum
from typing import Any, Callable, NamedTuple, Optional
from typing import Any, Callable, NamedTuple, Optional, TypeAlias

from structlog import get_logger

from hathor.checkpoint import Checkpoint
from hathor.conf.get_settings import get_settings
from hathor.conf.settings import HathorSettings as HathorSettingsType
from hathor.consensus import ConsensusAlgorithm
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.event import EventManager
from hathor.event.storage import EventMemoryStorage, EventRocksDBStorage, EventStorage
from hathor.event.websocket import EventWebsocketFactory
Expand Down Expand Up @@ -70,6 +71,12 @@ class BuildArtifacts(NamedTuple):
stratum_factory: Optional[StratumFactory]


_VertexVerifiersBuilder: TypeAlias = Callable[
[HathorSettingsType, DifficultyAdjustmentAlgorithm, FeatureService],
VertexVerifiers
]


class Builder:
"""Builder builds the core objects to run a full node.
Expand Down Expand Up @@ -103,8 +110,10 @@ def __init__(self) -> None:
self._feature_service: Optional[FeatureService] = None
self._bit_signaling_service: Optional[BitSignalingService] = None

self._daa: Optional[DifficultyAdjustmentAlgorithm] = None

self._vertex_verifiers: Optional[VertexVerifiers] = None
self._vertex_verifiers_builder: Callable[[HathorSettingsType, FeatureService], VertexVerifiers] | None = None
self._vertex_verifiers_builder: _VertexVerifiersBuilder | None = None
self._verification_service: Optional[VerificationService] = None

self._rocksdb_path: Optional[str] = None
Expand Down Expand Up @@ -164,6 +173,7 @@ def build(self) -> BuildArtifacts:
feature_service = self._get_or_create_feature_service()
bit_signaling_service = self._get_or_create_bit_signaling_service()
verification_service = self._get_or_create_verification_service()
daa = self._get_or_create_daa()

if self._enable_address_index:
indexes.enable_address_index(pubsub)
Expand All @@ -188,6 +198,7 @@ def build(self) -> BuildArtifacts:
network=self._network,
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
daa=daa,
peer_id=peer_id,
tx_storage=tx_storage,
p2p_manager=p2p_manager,
Expand Down Expand Up @@ -459,17 +470,26 @@ def _get_or_create_vertex_verifiers(self) -> VertexVerifiers:
if self._vertex_verifiers is None:
settings = self._get_or_create_settings()
feature_service = self._get_or_create_feature_service()
daa = self._get_or_create_daa()

if self._vertex_verifiers_builder:
self._vertex_verifiers = self._vertex_verifiers_builder(settings, feature_service)
self._vertex_verifiers = self._vertex_verifiers_builder(settings, daa, feature_service)
else:
self._vertex_verifiers = VertexVerifiers.create_defaults(
settings=settings,
feature_service=feature_service
daa=daa,
feature_service=feature_service,
)

return self._vertex_verifiers

def _get_or_create_daa(self) -> DifficultyAdjustmentAlgorithm:
if self._daa is None:
settings = self._get_or_create_settings()
self._daa = DifficultyAdjustmentAlgorithm(settings=settings)

return self._daa

def use_memory(self) -> 'Builder':
self.check_if_can_modify()
self._storage_type = StorageType.MEMORY
Expand Down Expand Up @@ -572,14 +592,16 @@ def set_vertex_verifiers(self, vertex_verifiers: VertexVerifiers) -> 'Builder':
self._vertex_verifiers = vertex_verifiers
return self

def set_vertex_verifiers_builder(
self,
builder: Callable[[HathorSettingsType, FeatureService], VertexVerifiers]
) -> 'Builder':
def set_vertex_verifiers_builder(self, builder: _VertexVerifiersBuilder) -> 'Builder':
self.check_if_can_modify()
self._vertex_verifiers_builder = builder
return self

def set_daa(self, daa: DifficultyAdjustmentAlgorithm) -> 'Builder':
self.check_if_can_modify()
self._daa = daa
return self

def set_reactor(self, reactor: Reactor) -> 'Builder':
self.check_if_can_modify()
self._reactor = reactor
Expand Down
10 changes: 9 additions & 1 deletion hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from hathor.cli.run_node import RunNodeArgs
from hathor.consensus import ConsensusAlgorithm
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.event import EventManager
from hathor.exception import BuilderError
from hathor.feature_activation.bit_signaling_service import BitSignalingService
Expand Down Expand Up @@ -207,7 +208,13 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
not_support_features=self._args.signal_not_support
)

vertex_verifiers = VertexVerifiers.create_defaults(settings=settings, feature_service=self.feature_service)
daa = DifficultyAdjustmentAlgorithm(settings=settings)

vertex_verifiers = VertexVerifiers.create_defaults(
settings=settings,
daa=daa,
feature_service=self.feature_service
)
verification_service = VerificationService(verifiers=vertex_verifiers)

p2p_manager = ConnectionsManager(
Expand All @@ -230,6 +237,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
hostname=hostname,
pubsub=pubsub,
consensus_algorithm=consensus_algorithm,
daa=daa,
peer_id=peer_id,
tx_storage=tx_storage,
p2p_manager=p2p_manager,
Expand Down
5 changes: 2 additions & 3 deletions hathor/cli/events_simulator/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ 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.get_settings import get_settings
from tests.utils import add_new_blocks, gen_new_tx

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

tx = gen_new_tx(manager, address, 1000)
tx.weight = daa.minimum_tx_weight(tx)
tx.weight = manager.daa.minimum_tx_weight(tx)
tx.update_hash()
assert manager.propagate_tx(tx, fails_silently=False)
simulator.run(60)

tx = gen_new_tx(manager, address, 2000)
tx.weight = daa.minimum_tx_weight(tx)
tx.weight = manager.daa.minimum_tx_weight(tx)
tx.update_hash()
assert manager.propagate_tx(tx, fails_silently=False)
simulator.run(60)
Expand Down
4 changes: 3 additions & 1 deletion hathor/cli/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ def execute(args: Namespace) -> None:
block.nonce, block.weight))

try:
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.verification.block_verifier import BlockVerifier
settings = get_settings()
verifier = BlockVerifier(settings=settings)
daa = DifficultyAdjustmentAlgorithm(settings=settings)
verifier = BlockVerifier(settings=settings, daa=daa)
verifier.verify_without_storage(block)
except HathorError:
print('[{}] ERROR: Block has not been pushed because it is not valid.'.format(datetime.datetime.now()))
Expand Down
Loading

0 comments on commit d2f2993

Please sign in to comment.