Skip to content

Commit da50f53

Browse files
committed
refactor(verification): move verification methods
1 parent 96b93db commit da50f53

26 files changed

+918
-693
lines changed

hathor/builder/builder.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
TransactionStorage,
4242
)
4343
from hathor.util import Random, Reactor, get_environment_info
44-
from hathor.verification.verification_service import VerificationService
44+
from hathor.verification.verification_service import VerificationService, VertexVerifiers
4545
from hathor.wallet import BaseWallet, Wallet
4646

4747
logger = get_logger()
@@ -102,6 +102,7 @@ def __init__(self) -> None:
102102
self._feature_service: Optional[FeatureService] = None
103103
self._bit_signaling_service: Optional[BitSignalingService] = None
104104

105+
self._vertex_verifiers: Optional[VertexVerifiers] = None
105106
self._verification_service: Optional[VerificationService] = None
106107

107108
self._rocksdb_path: Optional[str] = None
@@ -432,10 +433,18 @@ def _get_or_create_bit_signaling_service(self, tx_storage: TransactionStorage) -
432433

433434
def _get_or_create_verification_service(self) -> VerificationService:
434435
if self._verification_service is None:
435-
self._verification_service = VerificationService()
436+
verifiers = self._get_or_create_vertex_verifiers()
437+
self._verification_service = VerificationService(verifiers=verifiers)
436438

437439
return self._verification_service
438440

441+
def _get_or_create_vertex_verifiers(self) -> VertexVerifiers:
442+
if self._vertex_verifiers is None:
443+
settings = self._get_or_create_settings()
444+
self._vertex_verifiers = VertexVerifiers.create(settings=settings)
445+
446+
return self._vertex_verifiers
447+
439448
def use_memory(self) -> 'Builder':
440449
self.check_if_can_modify()
441450
self._storage_type = StorageType.MEMORY
@@ -533,6 +542,11 @@ def set_verification_service(self, verification_service: VerificationService) ->
533542
self._verification_service = verification_service
534543
return self
535544

545+
def set_vertex_verifiers(self, vertex_verifiers: VertexVerifiers) -> 'Builder':
546+
self.check_if_can_modify()
547+
self._vertex_verifiers = vertex_verifiers
548+
return self
549+
536550
def set_reactor(self, reactor: Reactor) -> 'Builder':
537551
self.check_if_can_modify()
538552
self._reactor = reactor

hathor/builder/cli_builder.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from hathor.pubsub import PubSubManager
3636
from hathor.stratum import StratumFactory
3737
from hathor.util import Random, Reactor
38-
from hathor.verification.verification_service import VerificationService
38+
from hathor.verification.verification_service import VerificationService, VertexVerifiers
3939
from hathor.wallet import BaseWallet, HDWallet, Wallet
4040

4141
logger = get_logger()
@@ -202,7 +202,8 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
202202
not_support_features=self._args.signal_not_support
203203
)
204204

205-
verification_service = VerificationService()
205+
vertex_verifiers = VertexVerifiers.create(settings=settings)
206+
verification_service = VerificationService(verifiers=vertex_verifiers)
206207

207208
p2p_manager = ConnectionsManager(
208209
reactor,

hathor/cli/mining.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import requests
2626

27+
from hathor.conf.get_settings import get_settings
28+
from hathor.verification.block_verifier import BlockVerifier
29+
2730
_SLEEP_ON_ERROR_SECONDS = 5
2831
_MAX_CONN_RETRIES = math.inf
2932

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

136139
try:
137-
block.verify_without_storage()
140+
settings = get_settings()
141+
verifier = BlockVerifier(settings=settings)
142+
verifier.verify_without_storage(block)
138143
except HathorError:
139144
print('[{}] ERROR: Block has not been pushed because it is not valid.'.format(datetime.datetime.now()))
140145
else:

hathor/simulator/simulator.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
from hathor.simulator.clock import HeapClock, MemoryReactorHeapClock
2929
from hathor.simulator.miner.geometric_miner import GeometricMiner
3030
from hathor.simulator.tx_generator import RandomTransactionGenerator
31+
from hathor.simulator.verification import (
32+
SimulatorBlockVerifier,
33+
SimulatorMergeMinedBlockVerifier,
34+
SimulatorTokenCreationTransactionVerifier,
35+
SimulatorTransactionVerifier,
36+
)
3137
from hathor.util import Random
38+
from hathor.verification.verification_service import VertexVerifiers
3239
from hathor.wallet import HDWallet
3340

3441
if TYPE_CHECKING:
@@ -52,25 +59,17 @@ def _apply_patches(cls):
5259
5360
Patches:
5461
55-
- disable pow verification
5662
- disable Transaction.resolve method
5763
- set DAA test-mode to DISABLED (will actually run the pow function, that won't actually verify the pow)
5864
- override AVG_TIME_BETWEEN_BLOCKS to 64
5965
"""
6066
from hathor.transaction import BaseTransaction
6167

62-
def verify_pow(self: BaseTransaction, *args: Any, **kwargs: Any) -> None:
63-
assert self.hash is not None
64-
logger.new().debug('Skipping BaseTransaction.verify_pow() for simulator')
65-
6668
def resolve(self: BaseTransaction, update_time: bool = True) -> bool:
6769
self.update_hash()
6870
logger.new().debug('Skipping BaseTransaction.resolve() for simulator')
6971
return True
7072

71-
cls._original_verify_pow = BaseTransaction.verify_pow
72-
BaseTransaction.verify_pow = verify_pow
73-
7473
cls._original_resolve = BaseTransaction.resolve
7574
BaseTransaction.resolve = resolve
7675

@@ -85,7 +84,6 @@ def _remove_patches(cls):
8584
""" Remove the patches previously applied.
8685
"""
8786
from hathor.transaction import BaseTransaction
88-
BaseTransaction.verify_pow = cls._original_verify_pow
8987
BaseTransaction.resolve = cls._original_resolve
9088

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

171+
vertex_verifiers = VertexVerifiers(
172+
block=SimulatorBlockVerifier(settings=self.settings),
173+
merge_mined_block=SimulatorMergeMinedBlockVerifier(settings=self.settings),
174+
tx=SimulatorTransactionVerifier(settings=self.settings),
175+
token_creation_tx=SimulatorTokenCreationTransactionVerifier(settings=self.settings),
176+
)
177+
173178
artifacts = builder \
174179
.set_reactor(self._clock) \
175180
.set_rng(Random(self.rng.getrandbits(64))) \
176181
.set_wallet(wallet) \
182+
.set_vertex_verifiers(vertex_verifiers) \
177183
.build()
178184

179185
artifacts.manager.start()

hathor/simulator/verification.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2023 Hathor Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Optional
16+
17+
from structlog import get_logger
18+
19+
from hathor.transaction import BaseTransaction
20+
from hathor.verification.block_verifier import BlockVerifier
21+
from hathor.verification.merge_mined_block_verifier import MergeMinedBlockVerifier
22+
from hathor.verification.token_creation_transaction_verifier import TokenCreationTransactionVerifier
23+
from hathor.verification.transaction_verifier import TransactionVerifier
24+
25+
logger = get_logger()
26+
27+
28+
def verify_pow(vertex: BaseTransaction) -> None:
29+
assert vertex.hash is not None
30+
logger.new().debug('Skipping BaseTransaction.verify_pow() for simulator')
31+
32+
33+
class SimulatorBlockVerifier(BlockVerifier):
34+
@classmethod
35+
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
36+
verify_pow(vertex)
37+
38+
39+
class SimulatorMergeMinedBlockVerifier(MergeMinedBlockVerifier):
40+
@classmethod
41+
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
42+
verify_pow(vertex)
43+
44+
45+
class SimulatorTransactionVerifier(TransactionVerifier):
46+
@classmethod
47+
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
48+
verify_pow(vertex)
49+
50+
51+
class SimulatorTokenCreationTransactionVerifier(TokenCreationTransactionVerifier):
52+
@classmethod
53+
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
54+
verify_pow(vertex)

hathor/stratum/stratum.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from hathor.transaction import BaseTransaction, BitcoinAuxPow, Block, MergeMinedBlock, Transaction, sum_weights
4242
from hathor.transaction.exceptions import PowError, ScriptError, TxValidationError
4343
from hathor.util import Reactor, json_dumpb, json_loadb, reactor
44+
from hathor.verification.vertex_verifier import VertexVerifier
4445
from hathor.wallet.exceptions import InvalidAddress
4546

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

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

544545
try:
545-
tx.verify_pow()
546+
VertexVerifier.verify_pow(tx)
546547
except PowError:
547548
# Transaction pow was not enough, but the share was succesfully submited
548549
self.log.info('high hash, keep mining', tx=tx)

0 commit comments

Comments
 (0)