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
18 changes: 16 additions & 2 deletions hathor/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 +102,7 @@ def __init__(self) -> None:
self._feature_service: Optional[FeatureService] = None
self._bit_signaling_service: Optional[BitSignalingService] = None

self._vertex_verifiers: Optional[VertexVerifiers] = None
self._verification_service: Optional[VerificationService] = None

self._rocksdb_path: Optional[str] = None
Expand Down Expand Up @@ -432,10 +433,18 @@ 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()
self._vertex_verifiers = VertexVerifiers.create(settings=settings)

return self._vertex_verifiers

def use_memory(self) -> 'Builder':
self.check_if_can_modify()
self._storage_type = StorageType.MEMORY
Expand Down Expand Up @@ -533,6 +542,11 @@ 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_reactor(self, reactor: Reactor) -> 'Builder':
self.check_if_can_modify()
self._reactor = reactor
Expand Down
5 changes: 3 additions & 2 deletions hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -202,7 +202,8 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
not_support_features=self._args.signal_not_support
)

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

p2p_manager = ConnectionsManager(
reactor,
Expand Down
7 changes: 6 additions & 1 deletion hathor/cli/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import requests

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

_SLEEP_ON_ERROR_SECONDS = 5
_MAX_CONN_RETRIES = math.inf

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

try:
block.verify_without_storage()
settings = get_settings()
verifier = BlockVerifier(settings=settings)
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
24 changes: 15 additions & 9 deletions hathor/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
from hathor.simulator.clock import HeapClock, MemoryReactorHeapClock
from hathor.simulator.miner.geometric_miner import GeometricMiner
from hathor.simulator.tx_generator import RandomTransactionGenerator
from hathor.simulator.verification import (
SimulatorBlockVerifier,
SimulatorMergeMinedBlockVerifier,
SimulatorTokenCreationTransactionVerifier,
SimulatorTransactionVerifier,
)
from hathor.util import Random
from hathor.verification.verification_service import VertexVerifiers
from hathor.wallet import HDWallet

if TYPE_CHECKING:
Expand All @@ -52,25 +59,17 @@ def _apply_patches(cls):

Patches:

- disable pow verification
- disable Transaction.resolve method
- set DAA test-mode to DISABLED (will actually run the pow function, that won't actually verify the pow)
- override AVG_TIME_BETWEEN_BLOCKS to 64
"""
from hathor.transaction import BaseTransaction

def verify_pow(self: BaseTransaction, *args: Any, **kwargs: Any) -> None:
assert self.hash is not None
logger.new().debug('Skipping BaseTransaction.verify_pow() for simulator')

def resolve(self: BaseTransaction, update_time: bool = True) -> bool:
self.update_hash()
logger.new().debug('Skipping BaseTransaction.resolve() for simulator')
return True

cls._original_verify_pow = BaseTransaction.verify_pow
BaseTransaction.verify_pow = verify_pow

cls._original_resolve = BaseTransaction.resolve
BaseTransaction.resolve = resolve

Expand All @@ -85,7 +84,6 @@ def _remove_patches(cls):
""" Remove the patches previously applied.
"""
from hathor.transaction import BaseTransaction
BaseTransaction.verify_pow = cls._original_verify_pow
BaseTransaction.resolve = cls._original_resolve

from hathor import daa
Expand Down Expand Up @@ -170,10 +168,18 @@ def create_artifacts(self, builder: Optional[Builder] = None) -> BuildArtifacts:
wallet = HDWallet(gap_limit=2)
wallet._manually_initialize()

vertex_verifiers = VertexVerifiers(
block=SimulatorBlockVerifier(settings=self.settings),
merge_mined_block=SimulatorMergeMinedBlockVerifier(settings=self.settings),
tx=SimulatorTransactionVerifier(settings=self.settings),
token_creation_tx=SimulatorTokenCreationTransactionVerifier(settings=self.settings),
)

artifacts = builder \
.set_reactor(self._clock) \
.set_rng(Random(self.rng.getrandbits(64))) \
.set_wallet(wallet) \
.set_vertex_verifiers(vertex_verifiers) \
.build()

artifacts.manager.start()
Expand Down
54 changes: 54 additions & 0 deletions hathor/simulator/verification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2023 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional

from structlog import get_logger

from hathor.transaction import BaseTransaction
from hathor.verification.block_verifier import BlockVerifier
from hathor.verification.merge_mined_block_verifier import MergeMinedBlockVerifier
from hathor.verification.token_creation_transaction_verifier import TokenCreationTransactionVerifier
from hathor.verification.transaction_verifier import TransactionVerifier

logger = get_logger()


def verify_pow(vertex: BaseTransaction) -> None:
assert vertex.hash is not None
logger.new().debug('Skipping BaseTransaction.verify_pow() for simulator')


class SimulatorBlockVerifier(BlockVerifier):
@classmethod
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
verify_pow(vertex)


class SimulatorMergeMinedBlockVerifier(MergeMinedBlockVerifier):
@classmethod
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
verify_pow(vertex)


class SimulatorTransactionVerifier(TransactionVerifier):
@classmethod
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
verify_pow(vertex)


class SimulatorTokenCreationTransactionVerifier(TokenCreationTransactionVerifier):
@classmethod
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
verify_pow(vertex)
5 changes: 3 additions & 2 deletions hathor/stratum/stratum.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from hathor.transaction import BaseTransaction, BitcoinAuxPow, Block, MergeMinedBlock, Transaction, sum_weights
from hathor.transaction.exceptions import PowError, ScriptError, TxValidationError
from hathor.util import Reactor, json_dumpb, json_loadb, reactor
from hathor.verification.vertex_verifier import VertexVerifier
from hathor.wallet.exceptions import InvalidAddress

if TYPE_CHECKING:
Expand Down Expand Up @@ -526,7 +527,7 @@ def handle_submit(self, params: dict, msgid: Optional[str]) -> None:
self.log.debug('share received', block=tx, block_base=block_base.hex(), block_base_hash=block_base_hash.hex())

try:
tx.verify_pow(job.weight)
VertexVerifier.verify_pow(tx, override_weight=job.weight)
except PowError:
self.log.error('bad share, discard', job_weight=job.weight, tx=tx)
return self.send_error(INVALID_SOLUTION, msgid, {
Expand All @@ -542,7 +543,7 @@ def handle_submit(self, params: dict, msgid: Optional[str]) -> None:
self.manager.reactor.callLater(0, self.job_request)

try:
tx.verify_pow()
VertexVerifier.verify_pow(tx)
except PowError:
# Transaction pow was not enough, but the share was succesfully submited
self.log.info('high hash, keep mining', tx=tx)
Expand Down
Loading