Skip to content
Closed
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
37 changes: 35 additions & 2 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 All @@ -41,7 +42,7 @@
TransactionStorage,
)
from hathor.util import Random, Reactor, get_environment_info
from hathor.verification.verification_service import VerificationService
from hathor.verification.verification_service import VerificationService, VertexVerifiers
from hathor.wallet import BaseWallet, Wallet

logger = get_logger()
Expand Down Expand Up @@ -102,6 +103,9 @@ 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._verification_service: Optional[VerificationService] = None

self._rocksdb_path: Optional[str] = None
Expand Down Expand Up @@ -161,6 +165,7 @@ def build(self) -> BuildArtifacts:
feature_service = self._get_or_create_feature_service(tx_storage)
bit_signaling_service = self._get_or_create_bit_signaling_service(tx_storage)
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 @@ -181,9 +186,11 @@ def build(self) -> BuildArtifacts:

manager = HathorManager(
reactor,
settings=settings,
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 @@ -431,10 +438,26 @@ def _get_or_create_bit_signaling_service(self, tx_storage: TransactionStorage) -

def _get_or_create_verification_service(self) -> VerificationService:
if self._verification_service is None:
self._verification_service = VerificationService()
verifiers = self._get_or_create_vertex_verifiers()
self._verification_service = VerificationService(verifiers=verifiers)

return self._verification_service

def _get_or_create_vertex_verifiers(self) -> VertexVerifiers:
if self._vertex_verifiers is None:
settings = self._get_or_create_settings()
daa = self._get_or_create_daa()
self._vertex_verifiers = VertexVerifiers.create(settings=settings, daa=daa)

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 @@ -532,6 +555,16 @@ def set_verification_service(self, verification_service: VerificationService) ->
self._verification_service = verification_service
return self

def set_vertex_verifiers(self, vertex_verifiers: VertexVerifiers) -> 'Builder':
self.check_if_can_modify()
self._vertex_verifiers = vertex_verifiers
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: 8 additions & 2 deletions 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 All @@ -35,7 +36,7 @@
from hathor.pubsub import PubSubManager
from hathor.stratum import StratumFactory
from hathor.util import Random, Reactor
from hathor.verification.verification_service import VerificationService
from hathor.verification.verification_service import VerificationService, VertexVerifiers
from hathor.wallet import BaseWallet, HDWallet, Wallet

logger = get_logger()
Expand Down Expand Up @@ -203,7 +204,10 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
not_support_features=self._args.signal_not_support
)

verification_service = VerificationService()
daa = DifficultyAdjustmentAlgorithm(settings=settings)

vertex_verifiers = VertexVerifiers.create(settings=settings, daa=daa)
verification_service = VerificationService(verifiers=vertex_verifiers)

p2p_manager = ConnectionsManager(
reactor,
Expand All @@ -220,10 +224,12 @@ def create_manager(self, reactor: Reactor) -> HathorManager:

self.manager = HathorManager(
reactor,
settings=settings,
network=network,
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
9 changes: 8 additions & 1 deletion hathor/cli/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

import requests

from hathor.conf.get_settings import get_settings
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.verification.block_verifier import BlockVerifier

_SLEEP_ON_ERROR_SECONDS = 5
_MAX_CONN_RETRIES = math.inf

Expand Down Expand Up @@ -134,7 +138,10 @@ def execute(args: Namespace) -> None:
block.nonce, block.weight))

try:
block.verify_without_storage()
settings = get_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()))
else:
Expand Down
Loading